QueryRefund.php 8.3 KB

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