InternalApiController.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. if(!empty($params['pay_way']??'')) $params['extra']['pay_way'] = $params['pay_way'];
  93. $payNotifyLogic = PayNotifyLogic::handle('goods', $params['sn'], $params['extra']??[]);
  94. if($payNotifyLogic === true){
  95. return $this->success('内部支付完成', [], 0, 1);
  96. }
  97. throw new \Exception($payNotifyLogic,404);
  98. }catch(\Exception $e){
  99. return $this->fail($e->getMessage(),[],$e->getCode());
  100. }
  101. }
  102. public function cancelOrder()
  103. {
  104. try {
  105. $params = $this->request->param();
  106. if(!$this->checkSign()) throw new \Exception('签名错误',404);
  107. // 工单信息
  108. $service_work = ServiceWork::where('work_sn',$params['work_sn'])->findOrEmpty();
  109. if($service_work->isEmpty()) throw new \Exception('工单不存在',404);
  110. // 取消工单
  111. if($service_work->work_status < 7){
  112. //更新工单状态为已取消
  113. $service_work->service_status = 4;
  114. // 是否存在退款
  115. if($service_work->work_pay_status == 1 && (int)$params['is_refund'] === 1){ //全退不分给工程师
  116. $service_work->work_status = 7;
  117. $service_work->service_status = 5;
  118. $service_work->work_pay_status = 2;
  119. //取消订单
  120. RechargeOrder::where(['user_id'=>$service_work->user_id,'work_id'=>$service_work->id])->update([
  121. 'pay_status' => 2,
  122. ]);
  123. }else if($service_work->work_pay_status == 1 && (int)$params['is_refund'] === 0){ // 上门费分给工程师
  124. $paid_amount = RechargeOrder::where('work_id', $service_work->id)->where('payment_type', 1)->value('paid_amount');
  125. if($paid_amount > 0){
  126. // 存在上门费给工程师
  127. Log::info('内部取消并终止结束服务工单'.$service_work->id.',上门费:'.$paid_amount);
  128. WorkerAccountLogLogic::addAccountLog($service_work,$paid_amount,WorkerAccountLogEnum::UM_INC_ADMIN,WorkerAccountLogEnum::INC);
  129. }
  130. ServiceWork::where('id', $params['id'])->update([
  131. 'work_status' => 7,
  132. 'user_confirm_status' => 5,
  133. 'service_status' => 3,
  134. 'work_pay_status' => 2, // 已结算则不执行 onAfterUpdate
  135. 'settlement_amount' => $paid_amount??0,
  136. 'worker_price' => $paid_amount??0,
  137. 'remark' => ($service_work->remark?:'')." | 内部取消并终止结束服务:上门费-{$paid_amount}"
  138. ]);
  139. }else{
  140. $service_work->work_status = 9;
  141. }
  142. ServiceWorkLog::create([
  143. 'work_id' => $service_work->id,
  144. 'master_worker_id' => $service_work->master_worker_id,
  145. 'opera_log' => "工单:{$service_work->work_sn} 内部取消并终止结束服务"
  146. ]);
  147. $service_work->user_confirm_status = 5;
  148. $service_work->save();
  149. }
  150. /*else{
  151. // 若存在结算则先退回
  152. }*/
  153. return $this->success('内部取消工单完成', [], 0, 1);
  154. }catch(\Exception $e){
  155. return $this->fail($e->getMessage(),[],$e->getCode());
  156. }
  157. }
  158. /**
  159. * tmp - test
  160. * @return \think\response\Json
  161. * @author liugc <466014217@qq.com>
  162. * @date 2025/4/23 11:52
  163. */
  164. public function calculatePerformanceTmp()
  165. {
  166. try {
  167. $params = $this->request->param();
  168. $work = ServiceWork::find($params['work_id']);
  169. //dd($work->toArray());
  170. PerformanceLogic::calculatePerformanceTmp($work);
  171. return $this->success('tmp工单完成', [], 0, 1);
  172. }catch(\Exception $e){
  173. return $this->fail($e->getMessage(),[],$e->getCode());
  174. }
  175. }
  176. }