Przeglądaj źródła

Weenier 168otc项目部署 0630

wesmiler 3 lat temu
rodzic
commit
466f8acb40

+ 3 - 3
app/Http/Controllers/Api/TaskController.php

@@ -2,8 +2,8 @@
 
 namespace App\Http\Controllers\Api;
 
-use App\Services\Api\AdvertService;
 use App\Services\Api\MemberService;
+use App\Services\Common\AdvertOrderService;
 use App\Services\Common\TradeOrderService;
 use App\Services\ConfigService;
 use App\Services\RedisService;
@@ -202,10 +202,10 @@ class TaskController extends webApp
             return showJson(1003, false);
         }
 
-        if($result = AdvertService::make()->catchInvalidOrder()){
+        if($result = AdvertOrderService::make()->catchInvalidOrder()){
             return showJson(1002, true, $result);
         }else{
-            return showJson(AdvertService::make()->getError(), false);
+            return showJson(AdvertOrderService::make()->getError(), false);
         }
     }
 

+ 0 - 137
app/Services/Api/AdvertService.php

@@ -101,142 +101,5 @@ class AdvertService extends BaseService
     }
 
 
-    /**
-     * 自动取消广告订单处理
-     * @return false
-     */
-    public function catchInvalidOrder(){
-        $cacheKey = "caches:adverts:cancels:";
-        if(RedisService::get($cacheKey.'lock')){
-            return false;
-        }
-
-        RedisService::set($cacheKey.'lock', 1, rand(3, 5));
-        $overtime = ConfigService::make()->getConfigByCode('trade_order_overtime');
-        $cancelTime = ConfigService::make()->getConfigByCode('trade_order_cancel');
-        $catchNum = ConfigService::make()->getConfigByCode('trade_order_catch_num');
-        $catchNum = $catchNum > 0 ? $catchNum : 200;
-
-        // 处理超时订单
-        if ($overtime > 0) {
-            $this->model->where(['mark' => 1])
-                ->where('status', '<=', 2)
-                ->where('create_time', '<=', time() - $overtime * 60)
-                ->update(['status' => 7, 'catch_at' => time()]);
-        }
-
-        if ($cancelTime <= 0) {
-            $this->error = '1023';
-            return false;
-        }
-
-        $fail = 0;
-        $success = 0;
-        $this->model->where(function ($query) use ($cancelTime) {
-            // 已更新为超时的订单
-            $query->where(['mark' => 1, 'status' => 7])
-                ->where('catch_at', '<=', time() - $cancelTime * 60);
-        })
-            ->orWhere(function ($query) use ($cancelTime, $overtime) {
-                $query->where('mark', '=', 1)
-                    ->where('status', '<=', 2)
-                    ->where('create_time', '<=', time() - ($cancelTime + $overtime) * 60);
-            })
-            ->select(['id', 'user_id', 'business_id','advert_id','order_no', 'type', 'num','total'])
-            ->get()
-            ->limit($catchNum)
-            ->each(function ($item, $k) use($cacheKey, &$fail, &$success){
-                // 客户卖出订单退还
-                $date = date('Y-m-d H:i:s');
-                $type = isset($item['type']) ? $item['type'] : 0;
-                if ($type == 2) {
-                    if(!$this->orderReback($item['user_id'], $item)){
-                        $fail++;
-                        RedisService::set($cacheKey."order_{$item['order_no']}:u{$item['user_id']}_fail", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
-                    }else{
-                        $success++;
-                        RedisService::set($cacheKey."order_{$item['order_no']}:u{$item['user_id']}_success", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
-                    }
-                }
 
-                else{
-                    if(!$this->orderReback($item['business_id'], $item)){
-                        $fail++;
-                        RedisService::set($cacheKey."order_{$item['order_no']}:b{$item['business_id']}_fail", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
-                    }else{
-                        $success++;
-                        RedisService::set($cacheKey."order_{$item['order_no']}:b{$item['business_id']}_success", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
-                    }
-                }
-            });
-
-        return ['success'=> $success,'fail'=> $fail];
-    }
-
-    /**
-     * 订单取消退还处理
-     * @param $userId
-     * @param $orderInfo
-     * @return bool
-     */
-    protected function orderReback($userId, $orderInfo){
-        try {
-            if($orderInfo['num']<=0){
-                return false;
-            }
-
-            $this->model->startTrans();
-            $updateData = ['status'=> 8, 'update_time'=> time(),'catch_at'=>time(),'exception_remark'=>'系统取消'];
-            if(!$this->model->where(['id'=> $orderInfo['id']])->update($updateData)){
-                $this->model->rollBack();
-                $this->error = '3043';
-                return false;
-            }
-
-            $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
-            if (empty($info)) {
-                $this->model->rollBack();
-                $this->error = '3019';
-                return false;
-            }
-
-            // 退还币给客户
-            if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
-                $this->model->rollBack();
-                $this->error = '3019';
-                return false;
-            }
-
-            // 明细处理
-            $data = [
-                'order_no' => $orderInfo['order_no'],
-                'user_id' => $userId,
-                'type' => 3,
-                'pay_type' => 1,
-                'trade_type' => 2,
-                'change_type' => 1,
-                'num' => $orderInfo['num'],
-                'total' => $orderInfo['total'],
-                'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
-                'create_time' => time(),
-                'update_time' => time(),
-                'remark' => '系统自动取消退还',
-                'status' => 1,
-                'mark' => 1,
-            ];
-
-            if (!$this->capitalModel->edit($data)) {
-                $this->error = '3014';
-                $this->model->rollBack();
-                return false;
-            }
-
-            $this->model->commit();
-            $this->error = '3044';
-            return true;
-        } catch (\Exception $exception){
-            $this->error = $exception->getMessage();
-            return false;
-        }
-    }
 }

+ 146 - 1
app/Services/Common/AdvertOrderService.php

@@ -13,8 +13,11 @@ namespace App\Services\Common;
 
 use App\Models\AdvertModel;
 use App\Models\AdvertOrderModel;
+use App\Models\MemberModel;
 use App\Services\Api\MemberPaymentService;
 use App\Services\BaseService;
+use App\Services\ConfigService;
+use App\Services\RedisService;
 
 /**
  * 用户广告订单-服务类
@@ -35,6 +38,7 @@ class AdvertOrderService extends BaseService
     {
         $this->model = new AdvertOrderModel();
         $this->advertModel = new AdvertModel();
+        $this->memberModel = new MemberModel();
     }
 
     /**
@@ -408,7 +412,148 @@ class AdvertOrderService extends BaseService
      */
     public function checkOrderNoCatch($userId, $type = 1)
     {
-        return $this->model->where(['user_id' => $userId, 'type' => $type, 'mark' => 1])->whereIn('status', [1, 2, 7])->count('id');
+        return $this->model->where(['user_id' => $userId, 'type' => $type, 'mark' => 1])
+            ->whereIn('status', [1, 2, 7])
+            ->count('id');
     }
 
+
+    /**
+     * 自动取消广告订单处理
+     * @return false
+     */
+    public function catchInvalidOrder(){
+        $cacheKey = "caches:adverts:cancels:";
+        if(RedisService::get($cacheKey.'lock')){
+            return false;
+        }
+
+        RedisService::set($cacheKey.'lock', 1, rand(3, 5));
+        $overtime = ConfigService::make()->getConfigByCode('trade_order_overtime');
+        $cancelTime = ConfigService::make()->getConfigByCode('trade_order_cancel');
+        $catchNum = ConfigService::make()->getConfigByCode('trade_order_catch_num');
+        $catchNum = $catchNum > 0 ? $catchNum : 200;
+
+        // 处理超时订单
+        if ($overtime > 0) {
+            $this->model->where(['mark' => 1])
+                ->where('status', '<=', 2)
+                ->where('create_time', '<=', time() - $overtime * 60)
+                ->update(['status' => 7, 'catch_at' => time()]);
+        }
+
+        if ($cancelTime <= 0) {
+            $this->error = '1023';
+            return false;
+        }
+
+        $fail = 0;
+        $success = 0;
+        $this->model->where(function ($query) use ($cancelTime) {
+            // 已更新为超时的订单
+            $query->where(['mark' => 1, 'status' => 7])
+                ->where('catch_at', '<=', time() - $cancelTime * 60);
+        })
+            ->orWhere(function ($query) use ($cancelTime, $overtime) {
+                $query->where('mark', '=', 1)
+                    ->where('status', '<=', 2)
+                    ->where('create_time', '<=', time() - ($cancelTime + $overtime) * 60);
+            })
+            ->select(['id', 'user_id', 'business_id','advert_id','order_no', 'type', 'num','total'])
+            ->get()
+            ->limit($catchNum)
+            ->each(function ($item, $k) use($cacheKey, &$fail, &$success){
+                // 客户卖出订单退还
+                $date = date('Y-m-d H:i:s');
+                $type = isset($item['type']) ? $item['type'] : 0;
+                if ($type == 2) {
+                    if(!$this->orderReback($item['user_id'], $item)){
+                        $fail++;
+                        RedisService::set($cacheKey."order_{$item['order_no']}:u{$item['user_id']}_fail", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
+                    }else{
+                        $success++;
+                        RedisService::set($cacheKey."order_{$item['order_no']}:u{$item['user_id']}_success", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
+                    }
+                }
+
+                else{
+                    if(!$this->orderReback($item['business_id'], $item)){
+                        $fail++;
+                        RedisService::set($cacheKey."order_{$item['order_no']}:b{$item['business_id']}_fail", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
+                    }else{
+                        $success++;
+                        RedisService::set($cacheKey."order_{$item['order_no']}:b{$item['business_id']}_success", ['order'=> $item,'msg'=> lang($this->error),'date'=> $date], 3600);
+                    }
+                }
+            });
+
+        return ['success'=> $success,'fail'=> $fail];
+    }
+
+    /**
+     * 订单取消退还处理
+     * @param $userId
+     * @param $orderInfo
+     * @return bool
+     */
+    protected function orderReback($userId, $orderInfo){
+        try {
+            if($orderInfo['num']<=0){
+                return false;
+            }
+
+            $this->model->startTrans();
+            $updateData = ['status'=> 8, 'update_time'=> time(),'catch_at'=>time(),'exception_remark'=>'系统取消'];
+            if(!$this->model->where(['id'=> $orderInfo['id']])->update($updateData)){
+                $this->model->rollBack();
+                $this->error = '3043';
+                return false;
+            }
+
+            $info = $this->memberModel->where(['id' => $userId, 'status' => 1, 'mark' => 1])->select(['id', 'username', 'usdt_num', 'user_type'])->first();
+            if (empty($info)) {
+                $this->model->rollBack();
+                $this->error = '3019';
+                return false;
+            }
+
+            // 退还币给客户
+            if (!$this->memberModel->where(['id' => $userId, 'mark' => 1])->increment('usdt_num', $orderInfo['num'])) {
+                $this->model->rollBack();
+                $this->error = '3019';
+                return false;
+            }
+
+            // 明细处理
+            $data = [
+                'order_no' => $orderInfo['order_no'],
+                'user_id' => $userId,
+                'type' => 3,
+                'pay_type' => 1,
+                'trade_type' => 2,
+                'change_type' => 1,
+                'num' => $orderInfo['num'],
+                'total' => $orderInfo['total'],
+                'balance' => floatval($info['usdt_num'] + $orderInfo['num']),
+                'create_time' => time(),
+                'update_time' => time(),
+                'remark' => '系统自动取消退还',
+                'status' => 1,
+                'mark' => 1,
+            ];
+
+            if (!$this->capitalModel->edit($data)) {
+                $this->error = '3014';
+                $this->model->rollBack();
+                return false;
+            }
+
+            $this->model->commit();
+            $this->error = '3044';
+            return true;
+        } catch (\Exception $exception){
+            $this->error = $exception->getMessage();
+            return false;
+        }
+    }
 }

+ 1 - 1
app/Services/Common/TradeOrderService.php

@@ -1522,8 +1522,8 @@ class TradeOrderService extends BaseService
                     ->where('create_time', '<=', time() - ($cancelTime + $overtime) * 60);
             })
             ->select(['id', 'user_id', 'business_id','order_no', 'type', 'num','total'])
+            ->take($catchNum)
             ->get()
-            ->limit($catchNum)
             ->each(function ($item, $k) use($cacheKey, &$fail, &$success){
                 // 客户卖出订单退还
                 $date = date('Y-m-d H:i:s');