whitefang пре 1 година
родитељ
комит
190c2daad0
1 измењених фајлова са 71 додато и 29 уклоњено
  1. 71 29
      app/common/command/QueryRefund.php

+ 71 - 29
app/common/command/QueryRefund.php

@@ -16,13 +16,21 @@ namespace app\common\command;
 
 use app\common\enum\PayEnum;
 use app\common\enum\RefundEnum;
+use app\common\enum\worker\WorkerAccountLogEnum;
+use app\common\logic\WorkerAccountLogLogic;
+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;
 
 
@@ -37,34 +45,38 @@ class QueryRefund extends Command
 
     protected function execute(Input $input, Output $output)
     {
-        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)) {
-                return false;
-            }
-
-            // 分别处理各个类型订单
-            $rechargeRecords = array_filter($refundRecords, function ($item) {
-                return $item['order_type'] == RefundEnum::ORDER_TYPE_ORDER;
-            });
-
-            if (!empty($rechargeRecords)) {
-                $this->handleRechargeOrder($rechargeRecords);
+        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;
             }
-
-            return true;
-        } catch (\Exception $e) {
-            Log::write('订单退款状态查询失败,失败原因:' . $e->getMessage());
-            return false;
+            sleep(1);
         }
     }
 
@@ -98,6 +110,7 @@ class QueryRefund extends Command
                 'log_sn' => $record['log_sn'],
                 'pay_way' => $order['pay_way'],
                 'order_terminal' => $order['order_terminal'],
+                'order_id'=>$record['order_id']
             ]);
         }
     }
@@ -126,7 +139,7 @@ class QueryRefund extends Command
         }
 
         if (true === $result) {
-            $this->updateRefundSuccess($refundData['log_id'], $refundData['record_id']);
+            $this->updateRefundSuccess($refundData['log_id'], $refundData['record_id'],$refundData['order_id']);
         } else {
             $this->updateRefundMsg($refundData['log_id'], $result);
         }
@@ -168,7 +181,7 @@ class QueryRefund extends Command
      * @author 段誉
      * @date 2023/3/1 15:38
      */
-    public function updateRefundSuccess($logId, $recordId)
+    public function updateRefundSuccess($logId, $recordId,$order_id)
     {
         // 更新日志
         RefundLog::update([
@@ -180,6 +193,35 @@ class QueryRefund extends Command
             'id' => $recordId,
             'refund_status' => RefundEnum::REFUND_SUCCESS,
         ]);
+        //更新工单退款状态以及处理师傅金额和物业金额
+        $order = RechargeOrder::where('id',$order_id)->findOrEmpty();
+        $work = ServiceWork::where('id',$order->work_id)->findOrEmpty();
+        if(!$work->isEmpty()){
+            $work->service_status = 5;
+            $work->save();
+            if($work->work_pay_status == 1 || $work->work_pay_status == 2){
+                //师傅余额变动
+                WorkerAccountLogLogic::addAccountLog($work,$order->worker_price,WorkerAccountLogEnum::UM_DEC_ADMIN,WorkerAccountLogEnum::DEC);
+                //物业余额变动
+                $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' => 3,
+                        'property_head_id' => $propertyHeadId,
+                        'amount' => $property_commission['commission_amount'],
+                        'status' => 1
+                    ]);
+                    // 更新 物业负责人余额收益
+                    PropertyHead::where(['id' => $propertyHeadId])->update([
+                        'surplus_profit_amount' => Db::raw('surplus_profit_amount-'.$property_commission['commission_amount'])
+                    ]);
+                }
+            }
+        }
     }