has($apikey)){ // $list = $redis->get($apikey); // return api_succ_return(['msg'=>'成功', 'data'=>$list]); // } $modelarea = new CouponAreaModel(); $list = $modelarea->where('status', '1')->field("id, name, buy_time,hidden_endtime")->order('is_today desc')->select()->toArray(); $curtime = time(); foreach ($list as $key=>&$value){ if (in_array($value['id'], [17, 18])){ $value['is_fl'] = 1; }else{ $value['is_fl'] = 2; } if ($value['buy_time']){ // 当前这个大区最大的一个id的计划的创建时间是否在增长中 $max_id = Db::name('coupon_plan')->where('area_id', $value['id'])->max('id'); $max_info = Db::name('coupon_plan')->where('id', $max_id)->find(); $cur_begin_new = false; if (sr_getcurtime(strtotime($max_info['create_time']), 'Y-m-d') == sr_getcurtime($curtime, 'Y-m-d')){ // 如果最大一个创建的是今天的话 // 就找到倒数第二个maxid $max_second_id = Db::name('coupon_plan')->where('area_id', $value['id'])->where('id', '<', $max_id)->max('id'); $max_info = Db::name('coupon_plan')->where('id', $max_second_id)->find(); $cur_begin_new = true; } $is_begin = false; if (strtotime($max_info['create_time']) + env('common.PLAN_EMIT_TIME') < time()){ $is_begin = true; } // if ($value['id'] == 14){ // return $is_begin?11:22; // } $curdata = json_decode($value['buy_time']); $day = sr_getcurtime($curdata->day, 'Y-m-d'); $today = sr_getcurtime(time(), 'Y-m-d'); if ($is_begin){ if ($day == $today){ if ($cur_begin_new){ $list[$key]['is_buyying'] = 0; }else{ $list[$key]['is_buyying'] = 1; } }else{ $list[$key]['is_buyying'] = 0; } }else{ $list[$key]['is_buyying'] = 0; } $arr_time = $curdata->time; $buy_times = []; $hidden_endtime = 1; for ($i = 0; $i < count($arr_time); $i++){ $val = $curdata->time[$i]; // $start_time = strtotime($day.' '.sr_getcurtime($val->start_time, 'H:i:s')); $start_time = $val->start_time; // $end_time = strtotime($day.' '.sr_getcurtime($val->end_time, 'H:i:s')); $end_time = $val->end_time; if ($curtime < $start_time){ if ($is_begin){ $state = 1; if ($cur_begin_new){ $state = 3; } $buy_times[] = ['start_time' => $start_time, 'state' => $state, 'end_time' => $end_time, 'time' => $start_time - $curtime, 'hidden_endtime' => $hidden_endtime]; }else{ $buy_times[] = ['start_time' => $start_time, 'state' => 4, 'end_time' => $end_time, 'time' => $start_time - $curtime, 'hidden_endtime' => $hidden_endtime]; } } if ($curtime > $start_time && $curtime < $end_time){ if ($is_begin){ $buy_times[] = ['start_time' => $start_time, 'state' => 2, 'end_time' => $end_time, 'time' => 0, 'hidden_endtime' => $hidden_endtime]; }else{ $buy_times[] = ['start_time' => $start_time, 'state' => 4, 'end_time' => $end_time, 'time' => 0, 'hidden_endtime' => $hidden_endtime]; } } if ($curtime > $end_time){ if ($is_begin){ $buy_times[] = ['start_time' => $start_time, 'state' => 3, 'end_time' => $end_time, 'time' => 0, 'hidden_endtime' => $hidden_endtime]; }else{ $buy_times[] = ['start_time' => $start_time, 'state' => 4, 'end_time' => $end_time, 'time' => 0, 'hidden_endtime' => $hidden_endtime]; } } } $list[$key]['buy_time'] = $buy_times; }else{ $list[$key]['buy_time'] = []; $list[$key]['is_buyying'] = 0; } } // $redis->set($apikey, $list); return api_succ_return(['msg'=>'成功', 'data'=>$list]); } // 计划列表 public function getCouponPlanList(Request $request) { $params = $request->param(); $modelplan = new CouponPlanModel(); $where = []; if (intval($params['state']) != 0){ $where[] = ['p.state', '=', $params['state']]; } $where[] = ['p.area_id', '=', $params['area_id']]; if (in_array($params['area_id'], [13, 14])){ $where[] = ['p.lun_num', 'not in', [1]]; } $where[] = $list = $modelplan ->alias('p') ->where($where) ->leftJoin('coupon_area a', 'a.id = p.area_id') ->field("p.*,a.coupon_pic,a.buy_time") ->withAttr('state', function ($val, $data){ if ($val == 0){ // 今天这一期是否还在增长中 $is_begin = false; if (strtotime($data['create_time']) + env('common.PLAN_EMIT_TIME') < time()){ $is_begin = true; } if (!$is_begin){ return -1; } // 如果在这一期就判定这个增长时间是否在三个数组内 $curdata = json_decode($data['buy_time']); $arr_time = $curdata->time; $valday = $curdata->day; $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y')); $endToday=mktime(24,0,0,date('m'),date('d'),date('Y')); $is_today = false; if ($beginToday<=$valday && $endToday>$valday){ $is_today = true; } if (!$is_today){ return -1; } return $val; // $can_buy = false; // $cur = time(); // for ($i = 0; $i < count($arr_time); $i++) { // $value = $curdata->time[$i]; // $start_time = $value->start_time; // $end_time = $value->end_time; // if ($cur>=$start_time && $cur<$end_time){ // $can_buy = true; // } // } // if ($can_buy){ // return $val; // } // return -1; } return $val; }) ->withAttr('target_num', function ($val, $data){ return 1; }) ->withAttr('cur_num', function ($val, $data){ return 1; }) ->order("p.id desc") ->select() ->toArray(); foreach ($list as $key=>&$val){ unset($list[$key]['buy_time']); if ($val['state'] == -1){ $list[$key]['cur_num'] = 0; } if (in_array($val['area_id'], [17, 18])){ $val['is_fl'] = 1; }else{ $val['is_fl'] = 2; } } return api_succ_return(['msg'=>'成功', 'data'=>$list]); } // 参加计划 public function addCouponPlan(Request $request) { $params = $request->param(); $model = new CouponPlanModel(); $get_time = time(); $is_coin = $request->param('is_coin', 1); Db::startTrans(); try { getActionBefore(1); $model->addPlan($request->uid, $params['money'], $params['plan_id'], ($is_coin==2?true:false), 0, $get_time); Db::commit(); }catch (\Exception $e){ Db::rollback(); if ($e->getCode() == 33333){ // 超出购买了金额 $coupon = Db::name('coupon_plan')->where('id', $params['plan_id'])->find(); Db::name('coupon_plan')->where('id', $params['plan_id'])->save(['target_num'=>$coupon['cur_num']+10]); } if ($e->getCode() == 478){ // sr_log('购买消费券接口478:'.$request->uid.'-'.$request->user['mobile'].'--'.sr_getcurtime($get_time).'---'.json_encode($request->param())); return api_succ_return(['msg'=>$e->getMessage(), 'code'=>$e->getCode()]); }else{ if (strpos($e->getMessage(), 'SQLSTATE[22003]') !== false){ return api_error_return('失败,请稍后在试'); }else{ return api_error_return($e->getMessage()); } } } return api_succ_return('购买成功'); } // 认购记录 public function couponPlanLog(Request $request){ $model = new CouponPlanLogModel(); $post = $request->post(); $where = []; $where[] = ['l.uid', '=', $request->uid]; if (!empty($post['state'])){ $where[] = ['l.state', '=', $post['state']]; } if (!empty($post['area_id'])){ $where[] = ['l.area_id', '=', $post['area_id']]; } // return $where; // $list = $modelplan // ->alias('p') // ->where($where) // ->leftJoin('coupon_area a', 'a.id = p.area_id') // ->field("p.*,a.coupon_pic") // ->select() // ->toArray(); $data = $model ->alias('l') ->field('l.*,p.plan_name,a.coupon_pic,a.short_name,p.lun_num,p.qi_num') ->where($where) ->leftJoin('coupon_plan p', 'p.id = l.plan_id') ->leftJoin('coupon_area a', 'a.id = l.area_id') ->paginate($request->data['limit'])->toArray(); return api_succ_return(['msg'=>'成功', 'data'=>$data['data']]); } // 福利消费抢购购买记录 public function getflCouponLog(Request $request){ $plan_id = $request->post('plan_id'); $plan_info = Db::name('coupon_plan')->where('id', $plan_id)->find(); if (!$plan_info){ return api_error_return('参数错误'); } if (!in_array($plan_info['area_id'], [17, 18])){ return api_error_return('错误'); } // 如果是 最大的一个福利区,并且小于当前抢购的时间返回空 $time = time(); if ($time - strtotime(sr_getcurtime($time, 'Y-m-d')) < 72000){ $max_id = Db::name('coupon_plan')->where('area_id', $plan_info['area_id'])->max('id'); if ($max_id == $plan_id){ return api_succ_return(['msg'=>'成功', 'data'=>[]]); } } $where = []; // $where[] = ['l.uid', '=', $request->uid]; $where[] = ['l.plan_id', '=', $plan_id]; // if (!empty($post['state'])){ // $where[] = ['l.state', '=', $post['state']]; // } // if (!empty($post['area_id'])){ // $where[] = ['l.area_id', '=', $post['area_id']]; // } $model = new CouponPlanLogModel(); $list = $model ->alias('l') // ->field('l.*,u.nickname,a.short_name,p.lun_num,p.qi_num') ->field('l.buy_num,l.create_time,l.id,u.nickname,p.plan_name,a.coupon_pic,a.short_name,p.lun_num,p.qi_num') ->where($where) ->leftJoin('user u', 'u.id = l.uid') ->leftJoin('coupon_plan p', 'p.id = l.plan_id') ->leftJoin('coupon_area a', 'a.id = l.area_id') ->page($request->post('page'), $request->post('limit')) ->select() ->toArray(); foreach ($list as $key=>&$val){ $val['create_time'] = sr_getcurtime(strtotime($val['create_time']), 'Y-m-d'); } return api_succ_return(['msg'=>'成功', 'data'=>$list]); } }