QueryRefund.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\common\command;
  15. use app\common\enum\PayEnum;
  16. use app\common\enum\RefundEnum;
  17. use app\common\enum\worker\WorkerAccountLogEnum;
  18. use app\common\logic\WorkerAccountLogLogic;
  19. use app\common\model\master_worker\MasterWorkerAccountLog;
  20. use app\common\model\property\PropertyCommission;
  21. use app\common\model\property\PropertyHead;
  22. use app\common\model\property\PropertyOrder;
  23. use app\common\model\property\PropertySurplusLog;
  24. use app\common\model\recharge\RechargeOrder;
  25. use app\common\model\refund\RefundLog;
  26. use app\common\model\refund\RefundRecord;
  27. use app\common\model\works\ServiceWork;
  28. use app\common\service\pay\WeChatPayService;
  29. use think\console\Command;
  30. use think\console\Input;
  31. use think\console\Output;
  32. use think\facade\Db;
  33. use think\facade\Log;
  34. class QueryRefund extends Command
  35. {
  36. protected function configure()
  37. {
  38. $this->setName('query_refund')
  39. ->setDescription('订单退款状态处理');
  40. }
  41. protected function execute(Input $input, Output $output)
  42. {
  43. while (true) {
  44. try {
  45. // 查找退款中的退款记录(微信,支付宝支付)
  46. $refundRecords = (new RefundLog())->alias('l')
  47. ->join('refund_record r', 'r.id = l.record_id')
  48. ->field([
  49. 'l.id' => 'log_id', 'l.sn' => 'log_sn',
  50. 'r.id' => 'record_id', 'r.order_id', 'r.sn' => 'record_sn', 'r.order_type'
  51. ])
  52. ->where(['l.refund_status' => RefundEnum::REFUND_ING])
  53. ->select()->toArray();
  54. if (empty($refundRecords)) {
  55. sleep(10);
  56. continue;
  57. }
  58. // 分别处理各个类型订单
  59. $rechargeRecords = array_filter($refundRecords, function ($item) {
  60. return $item['order_type'] == RefundEnum::ORDER_TYPE_ORDER;
  61. });
  62. if (!empty($rechargeRecords)) {
  63. $this->handleRechargeOrder($rechargeRecords);
  64. }
  65. } catch (\Exception $e) {
  66. Log::write('订单退款状态查询失败,失败原因:' . $e->getMessage());
  67. sleep(5);
  68. continue;
  69. }
  70. sleep(1);
  71. }
  72. }
  73. /**
  74. * @notes 处理充值订单
  75. * @param $refundRecords
  76. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  77. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  78. * @author 段誉
  79. * @date 2023/3/1 15:55
  80. */
  81. public function handleRechargeOrder($refundRecords)
  82. {
  83. $orderIds = array_unique(array_column($refundRecords, 'order_id'));
  84. $Orders = RechargeOrder::whereIn('id', $orderIds)->column('*', 'id');
  85. foreach ($refundRecords as $record) {
  86. if (!isset($Orders[$record['order_id']])) {
  87. continue;
  88. }
  89. $order = $Orders[$record['order_id']];
  90. if (!in_array($order['pay_way'], [PayEnum::WECHAT_PAY, PayEnum::ALI_PAY])) {
  91. continue;
  92. }
  93. $this->checkReFundStatus([
  94. 'record_id' => $record['record_id'],
  95. 'log_id' => $record['log_id'],
  96. 'log_sn' => $record['log_sn'],
  97. 'pay_way' => $order['pay_way'],
  98. 'order_terminal' => $order['order_terminal'],
  99. 'order_id'=>$order['id']
  100. ]);
  101. }
  102. }
  103. /**
  104. * @notes 校验退款状态
  105. * @param $refundData
  106. * @return bool
  107. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  108. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  109. * @author 段誉
  110. * @date 2023/3/1 15:54
  111. */
  112. public function checkReFundStatus($refundData)
  113. {
  114. $result = null;
  115. switch ($refundData['pay_way']) {
  116. case PayEnum::WECHAT_PAY:
  117. $result = self::checkWechatRefund($refundData['order_terminal'], $refundData['log_sn']);
  118. break;
  119. }
  120. if (is_null($result)) {
  121. return false;
  122. }
  123. if (true === $result) {
  124. $this->updateRefundSuccess($refundData['log_id'], $refundData['record_id'],$refundData['order_id']);
  125. } else {
  126. $this->updateRefundMsg($refundData['log_id'], $result);
  127. }
  128. return true;
  129. }
  130. /**
  131. * @notes 查询微信支付退款状态
  132. * @param $orderTerminal
  133. * @param $refundLogSn
  134. * @return bool|string|null
  135. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  136. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  137. * @author 段誉
  138. * @date 2023/3/1 15:47
  139. */
  140. public function checkWechatRefund($orderTerminal, $refundLogSn)
  141. {
  142. // 根据商户退款单号查询退款
  143. $result = (new WeChatPayService($orderTerminal))->queryRefund($refundLogSn);
  144. if (!empty($result['status']) && $result['status'] == 'SUCCESS') {
  145. return true;
  146. }
  147. if (!empty($result['code']) || !empty($result['message'])) {
  148. return '微信:' . $result['code'] . '-' . $result['message'];
  149. }
  150. return null;
  151. }
  152. /**
  153. * @notes 更新记录为成功
  154. * @param $logId
  155. * @param $recordId
  156. * @author 段誉
  157. * @date 2023/3/1 15:38
  158. */
  159. public function updateRefundSuccess($logId, $recordId,$order_id)
  160. {
  161. // 更新日志
  162. RefundLog::update([
  163. 'id' => $logId,
  164. 'refund_status' => RefundEnum::REFUND_SUCCESS,
  165. ]);
  166. // 更新记录
  167. RefundRecord::update([
  168. 'id' => $recordId,
  169. 'refund_status' => RefundEnum::REFUND_SUCCESS,
  170. ]);
  171. //更新工单退款状态以及处理工程师金额和物业金额
  172. $order = RechargeOrder::where('id',$order_id)->findOrEmpty();
  173. $work = ServiceWork::where('id',$order->work_id)->findOrEmpty();
  174. Log::write($work->toArray());
  175. if(!$work->isEmpty()){
  176. if(($work->work_pay_status == '1' || $work->work_pay_status == '2') and $work->service_status != '5' and $work->work_status != '9'){
  177. //工程师余额变动
  178. $change_amount = MasterWorkerAccountLog::where(['work_sn'=>$work->work_sn,'action'=>1])->value('change_amount');
  179. WorkerAccountLogLogic::addAccountLog($work,$change_amount,WorkerAccountLogEnum::UM_DEC_ADMIN,WorkerAccountLogEnum::DEC);
  180. //物业余额变动
  181. $property_commission = PropertyCommission::where('work_id',$order->work_id)->findOrEmpty();
  182. if(!$property_commission->isEmpty()){
  183. $propertyHeadInfo = PropertyHead::where('id',$property_commission->property_head_id)->findOrEmpty();
  184. $propertyHeadInfo = $propertyHeadInfo->toArray();
  185. $propertyHeadId = $propertyHeadInfo['id'];
  186. // 出账记录 - 扣除
  187. PropertySurplusLog::create([
  188. 'in_out' => 2,
  189. 'property_head_id' => $propertyHeadId,
  190. 'amount' => $property_commission['commission_amount'],
  191. 'status' => 1,
  192. 'remark'=> '用户已退款,分成金额已退回'
  193. ]);
  194. // 更新 物业负责人余额收益
  195. PropertyHead::where(['id' => $propertyHeadId])->update([
  196. 'surplus_profit_amount' => Db::raw('surplus_profit_amount-'.$property_commission['commission_amount'])
  197. ]);
  198. }
  199. }
  200. $work->service_status = 5;
  201. $work->work_status = 9;
  202. $work->save();
  203. }
  204. }
  205. /**
  206. * @notes 更新退款信息
  207. * @param $logId
  208. * @param $msg
  209. * @author 段誉
  210. * @date 2023/3/1 15:47
  211. */
  212. public function updateRefundMsg($logId, $msg)
  213. {
  214. // 更新日志
  215. RefundLog::update([
  216. 'id' => $logId,
  217. 'refund_msg' => $msg,
  218. ]);
  219. }
  220. }