InternalApiController.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace app\common\controller;
  3. use app\api\logic\PerformanceLogic;
  4. use app\api\logic\ServiceOrderLogic;
  5. use app\common\enum\PayEnum;
  6. use app\common\enum\worker\WorkerAccountLogEnum;
  7. use app\common\logic\PayNotifyLogic;
  8. use app\common\logic\WorkerAccountLogLogic;
  9. use app\common\model\external\ExternalPlatform;
  10. use app\common\model\recharge\RechargeOrder;
  11. use app\common\model\works\ServiceWork;
  12. use app\common\model\works\ServiceWorkLog;
  13. use think\facade\Config;
  14. use think\facade\Log;
  15. class InternalApiController extends BaseLikeAdminController
  16. {
  17. public array $notNeedLogin = ['changeAppointment','confirmServiceFinish','paymentSuccessful','cancelOrder'];
  18. private function checkSign(){
  19. $params = $this->request->param();
  20. // 验证IP
  21. $sign = ExternalPlatform::getSign(env('internal_api.api_sign_key'),$params);
  22. Log::info('内部请求参数'.json_encode([$params,$sign]));
  23. return true;
  24. /*if ($sign && $sign === $params['sign']) {
  25. return true;
  26. }
  27. return false;*/
  28. }
  29. public function changeAppointment()
  30. {
  31. try {
  32. $params = $this->request->param();
  33. if(!$this->checkSign()) throw new \Exception('签名错误',404);
  34. //sn appointment_time
  35. if(!isset($params['appointment_time']) || empty($params['appointment_time'])){
  36. throw new \Exception('预约时间不能为空',404);
  37. }
  38. if(!isset($params['sn']) || empty($params['sn'])){
  39. throw new \Exception('订单号不能为空',404);
  40. }
  41. $result = ServiceOrderLogic::approvalChangeAppointment($params);
  42. if (false === $result) {
  43. throw new \Exception(ServiceOrderLogic::getError(),404);
  44. }
  45. return $this->success('内部重新预约完成', [], 0, 1);
  46. }catch(\Exception $e){
  47. return $this->fail($e->getMessage(),[],$e->getCode());
  48. }
  49. }
  50. public function confirmServiceFinish()
  51. {
  52. try {
  53. $params = $this->request->param();
  54. if(!$this->checkSign()) throw new \Exception('签名错误',404);
  55. // 工单信息
  56. $service_work = ServiceWork::where('work_sn',$params['work_sn'])->findOrEmpty();
  57. if($service_work->isEmpty()) throw new \Exception('工单不存在',404);
  58. if($service_work->user_confirm_status < 3){
  59. //throw new \Exception('正在服务中,请稍后再试...',404);
  60. }
  61. if(in_array($service_work->service_status,[3,4,5])){
  62. return $this->success('内部确认服务完成', [], 0, 1);
  63. }
  64. $result = ServiceOrderLogic::confirmServiceFinish([
  65. 'sn' => $params['sn'],
  66. 'user_id' => $service_work->user_id,
  67. 'user_info'=>['real_name' => $service_work->real_name],
  68. ]);
  69. if (false === $result) {
  70. throw new \Exception(ServiceOrderLogic::getError(),404);
  71. }
  72. // 工程师完单的时候设置该规则关闭,以及短信通知工程师
  73. ServiceOrderLogic::orderQuantityRule($params);
  74. return $this->success('内部确认服务完成', [], 0, 1);
  75. }catch(\Exception $e){
  76. return $this->fail($e->getMessage(),[],$e->getCode());
  77. }
  78. }
  79. public function paymentSuccessful()
  80. {
  81. try {
  82. $params = $this->request->param();
  83. if(!$this->checkSign()) throw new \Exception('签名错误',404);
  84. $params['sn'] = mb_substr($params['sn'], 0, 18);
  85. $order = RechargeOrder::where(['sn' => $params['sn']])->findOrEmpty();
  86. if($order->isEmpty()) {
  87. throw new \Exception('内部订单不存在:'.$params['sn'],404);
  88. }
  89. if($order->pay_status == PayEnum::ISPAID) {
  90. return $this->success('内部支付已完成', [], 0, 1);
  91. }
  92. $payNotifyLogic = PayNotifyLogic::handle('goods', $params['sn'], $params['extra']??[]);
  93. if($payNotifyLogic === true){
  94. return $this->success('内部支付完成', [], 0, 1);
  95. }
  96. throw new \Exception($payNotifyLogic,404);
  97. }catch(\Exception $e){
  98. return $this->fail($e->getMessage(),[],$e->getCode());
  99. }
  100. }
  101. public function cancelOrder()
  102. {
  103. try {
  104. $params = $this->request->param();
  105. if(!$this->checkSign()) throw new \Exception('签名错误',404);
  106. // 工单信息
  107. $service_work = ServiceWork::where('work_sn',$params['work_sn'])->findOrEmpty();
  108. if($service_work->isEmpty()) throw new \Exception('工单不存在',404);
  109. // 取消工单
  110. if($service_work->work_status < 7){
  111. //更新工单状态为已取消
  112. $service_work->service_status = 4;
  113. // 是否存在退款
  114. if($service_work->work_pay_status == 1 && (int)$params['is_refund'] === 1){ //全退不分给工程师
  115. $service_work->work_status = 7;
  116. $service_work->service_status = 5;
  117. $service_work->work_pay_status = 2;
  118. //取消订单
  119. RechargeOrder::where(['user_id'=>$service_work->user_id,'work_id'=>$service_work->id])->update([
  120. 'pay_status' => 2,
  121. ]);
  122. }else if($service_work->work_pay_status == 1 && (int)$params['is_refund'] === 0){ // 上门费分给工程师
  123. $paid_amount = RechargeOrder::where('work_id', $service_work->id)->where('payment_type', 1)->value('paid_amount');
  124. if($paid_amount > 0){
  125. // 存在上门费给工程师
  126. Log::info('内部取消并终止结束服务工单'.$service_work->id.',上门费:'.$paid_amount);
  127. WorkerAccountLogLogic::addAccountLog($service_work,$paid_amount,WorkerAccountLogEnum::UM_INC_ADMIN,WorkerAccountLogEnum::INC);
  128. }
  129. ServiceWork::where('id', $params['id'])->update([
  130. 'work_status' => 7,
  131. 'user_confirm_status' => 5,
  132. 'service_status' => 3,
  133. 'work_pay_status' => 2, // 已结算则不执行 onAfterUpdate
  134. 'settlement_amount' => $paid_amount??0,
  135. 'worker_price' => $paid_amount??0,
  136. 'remark' => ($service_work->remark?:'')." | 内部取消并终止结束服务:上门费-{$paid_amount}"
  137. ]);
  138. }else{
  139. $service_work->work_status = 9;
  140. }
  141. ServiceWorkLog::create([
  142. 'work_id' => $service_work->id,
  143. 'master_worker_id' => $service_work->master_worker_id,
  144. 'opera_log' => "工单:{$service_work->work_sn} 内部取消并终止结束服务"
  145. ]);
  146. $service_work->user_confirm_status = 5;
  147. $service_work->save();
  148. }
  149. /*else{
  150. // 若存在结算则先退回
  151. }*/
  152. return $this->success('内部取消工单完成', [], 0, 1);
  153. }catch(\Exception $e){
  154. return $this->fail($e->getMessage(),[],$e->getCode());
  155. }
  156. }
  157. /**
  158. * tmp - test
  159. * @return \think\response\Json
  160. * @author liugc <466014217@qq.com>
  161. * @date 2025/4/23 11:52
  162. */
  163. public function calculatePerformanceTmp()
  164. {
  165. try {
  166. $params = $this->request->param();
  167. $work = ServiceWork::find($params['work_id']);
  168. //dd($work->toArray());
  169. PerformanceLogic::calculatePerformanceTmp($work);
  170. return $this->success('tmp工单完成', [], 0, 1);
  171. }catch(\Exception $e){
  172. return $this->fail($e->getMessage(),[],$e->getCode());
  173. }
  174. }
  175. }