draw_amount); if (empty($w) || $w->use_up == 1){ $this->error = '福利已结束'; return false; } $code_id = 0; $flag = 0; if($w->target_type == self::TARGET_TYPE_YYFU){ $code = MemberCooperationCoupon::where(['welfare_id'=>$id,'used'=>0])->lock(true)->find(); if (empty($code)){ $this->error = '福利已领完,下次早点来哦'; return false; } $code_id = $code->id; }elseif($w->target_type == self::TARGET_TYPE_HYZS){ $model = new UserCouponModel; $flag = $model->receive($w->coupon_id,false,true); //dd($flag); if ($flag === false){ $this->error = $model->getError(); return false; } } $m = new self(); $data['user_id'] =$user->user_id; $data['target_type'] =$w->target_type; $data['target_id'] =$id; $data['number'] =$w->number; $data['name'] =$w->name; $data['cost'] =$w->cost; $data['amount'] =$w->amount; $data['start_time'] =$w->start_time; $data['end_time'] =$w->end_time; $data['use_start_time'] =$w->use_start_time; $data['use_end_time'] =$w->use_end_time; $data['exchange_type'] =$w->exchange_type; $data['position'] =$w->position; $data['url'] =$w->url; $data['img'] =$w->img; $data['code_id'] =$code_id; $data['user_coupon_id'] =$flag; $data['deploy_type'] =$w->deploy_type; $data['create_time'] =time(); $data['update_time'] =time(); Db::startTrans(); try { $insertId = $m->insertGetId($data); if($w->target_type == self::TARGET_TYPE_YYFU) { $code->where('id',$code_id)->where('used',0)->update(['user_id'=>$user->user_id,'used'=>1]); } if($w->target_type == self::TARGET_TYPE_HYZS && $flag){ $model = new UserCouponModel; $flag = $model->where('user_coupon_id',$flag)->update(['member_welfare_draw_id'=>$insertId]); //dd($flag); if ($flag === false){ $this->error = $model->getError(); return false; } } $w->draw_amount = $w->draw_amount+1; log_record($w->amount.'-----'.$w->draw_amount); if ($w->amount <= $w->draw_amount){ $w->use_up = 1; } $w->save(); // 提交事务 Db::commit(); } catch (\Exception $e) { // 回滚事务 Db::rollback(); log_record($e->getMessage()); $this->error = '系统繁忙'; return false; } return true; } /** * 会员福利领取记录 * @param $userId * @return \think\Paginator * @throws \think\db\exception\DbException */ public function welfareRecords($userId){ $now = time(); return $this->where('user_id',$userId) ->field('id,user_id,target_id,target_type,name,cost,use_start_time,use_end_time,use_status,create_time,check_status,user_coupon_id') ->order('id desc') ->paginate(15)->each(function(&$item) use ($now){ $item['is_expired'] = 0; $et = 0; if ($item['target_type'] == self::TARGET_TYPE_YYFU){ $et = strtotime($item['use_end_time']); } if ($item['target_type'] == self::TARGET_TYPE_HYZS){ $coupon = MemberWelfare::detail($item['target_id']); $item['coupon'] = Coupon::where('coupon_id',$coupon['coupon_id'])->field('coupon_id,name,reduce_price,min_price,status,audit_status')->find(); $userCp = UserCouponModel::where('user_coupon_id',$item['user_coupon_id'])->field('user_coupon_id,expire_time')->find(); $et = strtotime($userCp['expire_time']); } if ($et < $now){ $item['is_expired'] = 1; } }); } public function myWelfareDetail($id){ $item = $this->with(['checkcode','welfare','user'])->where('id',$id) ->field('id,user_id,target_id,target_type,name,deploy_type,check_status,check_time,cost,exchange_type,use_start_time,use_end_time,create_time,code_id,check_status,url,user_coupon_id') ->find(); $item['exchange_type'] = json_decode($item['exchange_type']); $item['is_expired'] = 0; //$et = strtotime($item['use_end_time']); $et = 0; if ($item['target_type'] == self::TARGET_TYPE_YYFU){ $et = strtotime($item['use_end_time']); } if ($item['target_type'] == self::TARGET_TYPE_HYZS){ $userCp = UserCouponModel::where('user_coupon_id',$item['user_coupon_id']) ->field('user_coupon_id,expire_time')->find(); $et = strtotime($userCp['expire_time']); } if ($et < time()){ $item['is_expired'] = 1; } return $item; } // public function checkcode(){ // return $this->belongsTo(\app\common\model\member\MemberCooperationCoupon::class,'code_id','id') // ->field('id,coupon_id,welfare_id,code'); //} // // public function welfare() // { // return $this->belongsTo(MemberWelfare::class, 'target_id', 'id') // ->field('id,desc,shop_desc'); // } }