Explorar o código

wesmiler 报恩寺项目提交

wesmiler %!s(int64=4) %!d(string=hai) anos
pai
achega
bc9dfdfad3

+ 6 - 1
app/Http/Controllers/Api/TaskController.php

@@ -2,6 +2,9 @@
 
 namespace App\Http\Controllers\Api;
 
+use App\Services\MemberService;
+use App\Services\OrdersService;
+
 /**
  * 计划任务调度控制器
  * @author wesmiler
@@ -20,6 +23,8 @@ class TaskController extends BaseController
     public function __construct()
     {
         parent::__construct();
+        $this->orderService = new OrdersService();
+        $this->memberService = new MemberService();
     }
 
 
@@ -29,7 +34,7 @@ class TaskController extends BaseController
      * @return array
      */
     public function catchOrder(){
-
+        $this->orderService->orderCancel();
         return message(MESSAGE_OK, true);
     }
 

+ 93 - 0
app/Services/MemberService.php

@@ -945,4 +945,97 @@ class MemberService extends BaseService
         ];
     }
 
+    /**
+     * VIP 用户每月奖励
+     * @return array
+     */
+    public function vipAward(){
+        $cacheKey = "caches:vip:awards:logs:".date('YmdHi');
+        $num = ConfigService::make()->getConfigByCode('vip_award_catch_num');
+        $num = $num>10? $num : 200;
+
+        // 有效会员
+        $users = $this->model::where(['mark'=>1,'status'=>1])
+            ->where('vip_expire','>=', time())
+            ->where('give_expire','<', strtotime('Y-m-01'))
+            ->select(['id','nickname','coupon','give_time','vip_expire'])
+            ->limit($num)
+            ->get();
+        $giveNum = ConfigService::make()->getConfigByCode('vip_month_give_coupon');
+        $giveNum = $giveNum? $giveNum : 0;
+        RedisService::set($cacheKey.':data', ['users'=> $users,'give'=>$giveNum], 86400);
+        if($users && $giveNum>0){
+            $success = [];
+            $fail = [];
+            foreach ($users as $v){
+                // 处理用户
+                $result = $this->vipAwardCatch($v,$giveNum);
+                if($result['success'] == true){
+                    $success[] = [
+                        'id'=>$v['id'],
+                        'result'=>  $result,
+                    ];
+                }else{
+                    $fail[] = [
+                        'id'=>$v['id'],
+                        'result'=>  $result,
+                    ];
+                }
+            }
+        }
+
+        if($success || $fail){
+            $msg = 'VIP每月奖励处理完成:成功'.count($success).'个,失败'.count($fail).'个';
+            RedisService::set($cacheKey.':result',['success'=> $success,'fail'=> $fail,'result'=> $msg], 7200);
+            return message($msg, true, ['success'=> $success,'fail'=> $fail]);
+        }else{
+            return message('VIP每月奖励处理失败或暂无用户需处理', false);
+        }
+    }
+
+    /**
+     * VIP奖励处理
+     * @param $userInfo 用户信息,至少包含 id、coupon字段
+     * @param $giveNum
+     * @return array
+     */
+    public function vipAwardCatch($userInfo,$giveNum){
+        $userId = $userInfo->id;
+        $cacheKey = "caches:vip:awards:users:u_{$userId}";
+
+        \DB::beginTransaction();
+        // 扣除账户
+        if(!MemberModel::where(['id'=> $userId,'mark'=> 1])->increment('coupon', $giveNum)){
+            \DB::rollBack();
+            RedisService::set($cacheKey.':error', ['info'=>$userInfo,'give'=> $giveNum, 'error'=> '更新账户失败'], 7200);
+            return message('更新会员券账户失败',false);
+        }
+
+        // 明细
+        $data = [
+            'user_id'=> $userId,
+            'type'=> 3,
+            'coin_type'=> 1,
+            'pay_type'=> 1,
+            'money'=> $giveNum,
+            'change_type'=> 1,
+            'balance'=> $userInfo->coupon,
+            'create_time'=> time(),
+            'update_time'=> time(),
+            'remark'=> 'VIP会员每月赠送奖励',
+            'status'=> 1
+        ];
+
+        if(!TradeModel::insertGetId($data)){
+            \DB::rollBack();
+            RedisService::set($cacheKey.':error', ['info'=>$userInfo,'data'=> $data,'give'=> $giveNum, 'error'=> '交易明细处理失败'], 7200);
+            return message( "交易处理失败,请刷新重试", false);
+        }
+
+        // 更新处理时间
+        $this->model::where(['id'=> $userId,'mark'=>1])->update(['give_expire'=> time()]);
+        \DB::commit();
+
+        return message( "VIP用户[ID:{$userId}]每月奖励处理成功", true);
+    }
 }

+ 19 - 0
app/Services/OrdersService.php

@@ -509,5 +509,24 @@ class OrdersService extends BaseService
         }
     }
 
+    /**
+     * 自动取消订单
+     */
+    public function orderCancel(){
+        // 屏蔽
+        $this->model::where(['mark'=>1,'status'=> 5])
+            ->where('update_time','<=',time() - 24*3600)
+            ->update(['mark'=>0,'update_time'=> time()]);
+
+        // 删除
+        $this->model::where(['mark'=>0,'status'=> 5])
+            ->where('update_time','<=',time() - 24*3600)
+            ->delete();
+
+        // 自动取消
+        $this->model::where(['mark'=>1,'status'=> 1])
+            ->where('update_time','<=',time() - 3600)
+            ->update(['status'=>5,'update_time'=> time()]);
+    }
 
 }