| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- <?php
- // +----------------------------------------------------------------------
- // | likeadmin快速开发前后端分离管理后台(PHP版)
- // +----------------------------------------------------------------------
- // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
- // | 开源版本可自由商用,可去除界面版权logo
- // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
- // | github下载:https://github.com/likeshop-github/likeadmin
- // | 访问官网:https://www.likeadmin.cn
- // | likeadmin团队 版权所有 拥有最终解释权
- // +----------------------------------------------------------------------
- // | author: likeadminTeam
- // +----------------------------------------------------------------------
- namespace app\common\command;
- use app\common\enum\PayEnum;
- use app\common\enum\RefundEnum;
- use app\common\enum\worker\WorkerAccountLogEnum;
- use app\common\logic\RetentionMoneyLogic;
- use app\common\logic\WorkerAccountLogLogic;
- use app\common\model\equity\UserEquity;
- use app\common\model\master_worker\MasterWorkerAccountLog;
- use app\common\model\master_worker\MasterWorkerRetentionMoneyLog;
- use app\common\model\property\PropertyCommission;
- use app\common\model\property\PropertyHead;
- use app\common\model\property\PropertyOrder;
- use app\common\model\property\PropertySurplusLog;
- use app\common\model\recharge\RechargeOrder;
- use app\common\model\refund\RefundLog;
- use app\common\model\refund\RefundRecord;
- use app\common\model\works\ServiceWork;
- use app\common\service\pay\WeChatPayService;
- use think\console\Command;
- use think\console\Input;
- use think\console\Output;
- use think\facade\Db;
- use think\facade\Log;
- class QueryRefund extends Command
- {
- protected function configure()
- {
- $this->setName('query_refund')
- ->setDescription('订单退款状态处理');
- }
- protected function execute(Input $input, Output $output)
- {
- while (true) {
- try {
- // 查找退款中的退款记录(微信,支付宝支付)
- $refundRecords = (new RefundLog())->alias('l')
- ->join('refund_record r', 'r.id = l.record_id')
- ->field([
- 'l.id' => 'log_id', 'l.sn' => 'log_sn',
- 'r.id' => 'record_id', 'r.order_id', 'r.sn' => 'record_sn', 'r.order_type'
- ])
- ->where(['l.refund_status' => RefundEnum::REFUND_ING])
- ->select()->toArray();
- if (empty($refundRecords)) {
- sleep(10);
- continue;
- }
- // 分别处理各个类型订单
- $rechargeRecords = array_filter($refundRecords, function ($item) {
- return $item['order_type'] == RefundEnum::ORDER_TYPE_ORDER;
- });
- if (!empty($rechargeRecords)) {
- $this->handleRechargeOrder($rechargeRecords);
- }
- } catch (\Exception $e) {
- Log::write('订单退款状态查询失败,失败原因:' . $e->getMessage());
- sleep(5);
- continue;
- }
- sleep(1);
- }
- }
- /**
- * @notes 处理充值订单
- * @param $refundRecords
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @author 段誉
- * @date 2023/3/1 15:55
- */
- public function handleRechargeOrder($refundRecords)
- {
- $orderIds = array_unique(array_column($refundRecords, 'order_id'));
- $Orders = RechargeOrder::whereIn('id', $orderIds)->column('*', 'id');
- foreach ($refundRecords as $record) {
- if (!isset($Orders[$record['order_id']])) {
- continue;
- }
- $order = $Orders[$record['order_id']];
- if (!in_array($order['pay_way'], [PayEnum::WECHAT_PAY, PayEnum::ALI_PAY])) {
- continue;
- }
- $this->checkReFundStatus([
- 'record_id' => $record['record_id'],
- 'log_id' => $record['log_id'],
- 'log_sn' => $record['log_sn'],
- 'pay_way' => $order['pay_way'],
- 'order_terminal' => $order['order_terminal'],
- 'order_id'=>$order['id'],
- 'paid_amount'=>$order['paid_amount'],
- ]);
- }
- }
- /**
- * @notes 校验退款状态
- * @param $refundData
- * @return bool
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @author 段誉
- * @date 2023/3/1 15:54
- */
- public function checkReFundStatus($refundData)
- {
- $result = null;
- switch ($refundData['pay_way']) {
- case PayEnum::WECHAT_PAY:
- if((float)$refundData['paid_amount'] > 0){
- $result = self::checkWechatRefund($refundData['order_terminal'], $refundData['log_sn']);
- }else{
- $result = true;
- }
- break;
- }
- if (is_null($result)) {
- return false;
- }
- if (true === $result) {
- $this->updateRefundSuccess($refundData['log_id'], $refundData['record_id'],$refundData['order_id']);
- } else {
- $this->updateRefundMsg($refundData['log_id'], $result);
- }
- return true;
- }
- /**
- * @notes 查询微信支付退款状态
- * @param $orderTerminal
- * @param $refundLogSn
- * @return bool|string|null
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @author 段誉
- * @date 2023/3/1 15:47
- */
- public function checkWechatRefund($orderTerminal, $refundLogSn)
- {
- // 根据商户退款单号查询退款
- $result = (new WeChatPayService($orderTerminal))->queryRefund($refundLogSn);
- if (!empty($result['status']) && $result['status'] == 'SUCCESS') {
- return true;
- }
- if (!empty($result['code']) || !empty($result['message'])) {
- return '微信:' . $result['code'] . '-' . $result['message'];
- }
- return null;
- }
- /**
- * @notes 更新记录为成功
- * @param $logId
- * @param $recordId
- * @author 段誉
- * @date 2023/3/1 15:38
- */
- public function updateRefundSuccess($logId, $recordId,$order_id)
- {
- // 更新日志
- RefundLog::update([
- 'id' => $logId,
- 'refund_status' => RefundEnum::REFUND_SUCCESS,
- ]);
- // 更新记录
- RefundRecord::update([
- 'id' => $recordId,
- 'refund_status' => RefundEnum::REFUND_SUCCESS,
- ]);
- //更新工单退款状态以及处理工程师金额和物业金额
- $order = RechargeOrder::where('id',$order_id)->findOrEmpty();
- $work = ServiceWork::where('id',$order->work_id)->findOrEmpty();
- Log::channel('re_fund')->info('工单ID'.$order->work_id.':'.json_encode($work,JSON_UNESCAPED_UNICODE));
- if(!$work->isEmpty()){
- if(($work->work_pay_status == '1' || $work->work_pay_status == '2') and $work->work_status != '9' and $work->refund_approval != '2' and $work->work_status > '6'){
- //工程师余额变动
- $change_amount = MasterWorkerAccountLog::where(['work_sn'=>$work->work_sn,'action'=>1])->value('change_amount');
- WorkerAccountLogLogic::addAccountLog($work,$change_amount,WorkerAccountLogEnum::UM_DEC_ADMIN,WorkerAccountLogEnum::DEC);
- //工程师质保金变动 - 退质保金
- $retentionAmount = MasterWorkerRetentionMoneyLog::where(['action'=>WorkerAccountLogEnum::INC,'source'=>2,'worker_id'=>$work->master_worker_id,'work_id'=>$work->id])->value('amount');
- if($retentionAmount){
- $remark = '工单号:'.$work->work_sn.',退款金额:'.$retentionAmount.',退款原因:工单退款';
- RetentionMoneyLogic::refundRetention([
- 'work_id'=>$work->id,'worker_id'=>$work->master_worker_id,'amount'=>$retentionAmount,'remark'=>$remark,'source'=>2
- ],false);
- }
- Log::channel('re_fund')->info('工单ID'.$order->work_id.',工单退款金额:'.$change_amount);
- //物业余额变动
- $property_commission = PropertyCommission::where('work_id',$order->work_id)->findOrEmpty();
- if(!$property_commission->isEmpty()){
- $propertyHeadInfo = PropertyHead::where('id',$property_commission->property_head_id)->findOrEmpty();
- $propertyHeadInfo = $propertyHeadInfo->toArray();
- $propertyHeadId = $propertyHeadInfo['id'];
- // 出账记录 - 扣除
- PropertySurplusLog::create([
- 'in_out' => 2,
- 'property_head_id' => $propertyHeadId,
- 'amount' => $property_commission['commission_amount'],
- 'status' => 1,
- 'remark'=> '用户已退款,分成金额已退回'
- ]);
- // 更新 物业负责人余额收益
- PropertyHead::where(['id' => $propertyHeadId])->update([
- 'surplus_profit_amount' => Db::raw('surplus_profit_amount-'.$property_commission['commission_amount'])
- ]);
- }
- // 权益卡剩余次数
- if(!empty($work->user_equity_id)){
- UserEquity::where(['id'=>$work->user_equity_id])->inc('number')->save();
- }
- }else{
- Log::channel('re_fund')->info('工单ID'.$order->work_id.'数据'.json_encode($work,JSON_UNESCAPED_UNICODE));
- }
- $work->refund_approval = 2;
- $work->service_status = 5;
- $work->work_status = 9;
- $work->save();
- }
- }
- /**
- * @notes 更新退款信息
- * @param $logId
- * @param $msg
- * @author 段誉
- * @date 2023/3/1 15:47
- */
- public function updateRefundMsg($logId, $msg)
- {
- // 更新日志
- RefundLog::update([
- 'id' => $logId,
- 'refund_msg' => $msg,
- ]);
- }
- }
|