QueryRefund.php 10 KB

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