|
@@ -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);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|