| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- <?php
- namespace app\api\controller\v1;
- use app\common\model\CouponAreaModel;
- use app\common\model\CouponPlanLogModel;
- use app\common\model\CouponPlanModel;
- use think\cache\driver\Redis;
- use think\db\Where;
- use think\facade\Db;
- use think\Request;
- use think\Response;
- class coupon
- {
- // 大区列表
- public function getCouponAreaList(Request $request)
- {
- $apikey = 'getCouponAreaList';
- // $redis = new Redis();
- // if ($redis->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]);
- }
- }
|