validate($this->input, [ ['type_id', 'require|integer|in:1,2,3', '类型不能为空|类型必须是整数|类型必须1,2,3'], ['page', 'require|integer|egt:1', '页数不能为空|页数必须是整数|页数必须大于1'], ['pageSize', 'require|integer|egt:10', '页码不能为空|页码必须是整数|页码必须大于10'], ['gameid', 'require|integer|gt:1', '游戏ID不能为空|游戏ID必须是整数|游戏ID必须大于1'], ]); if (true !== $checkResult) { $this->jsonResult('', 0, $checkResult); } $where = []; $type_id = $this->input['type_id']; //查询参数 $where['a.member_id'] = $this->input['userid']; $where['a.examine'] = 2; $couponMemberModel = model('CouponMember'); if($type_id == 1){ $order = "a.id desc"; }elseif($type_id == 2){ $order = "a.use_time desc"; }else{ $order = "c.end_time desc"; } $list = $couponMemberModel ->alias('a') ->join('cy_coupon c', 'a.coupon_id=c.id') ->where($where) ->where(function ($query) use ($type_id) { $end_time = strtotime(date("Y-m-d")); //1未使用 2已使用 3已失效 if ($type_id == 1) { $query->where([ 'c.state' => 1, 'a.state' => 1, 'a.is_use' => 1, ])->whereRaw(sprintf('(c.type_id =1 and c.end_time>=%d) or (c.type_id=2 and a.end_time>=%d)', $end_time, $end_time)); } else if ($type_id == 2) { $query->whereIn('a.is_use', [2, 3]); } else { $query->whereRaw(sprintf('(c.type_id =1 and c.end_time<%d) or (c.type_id=2 and a.end_time<%d) or c.state =2 or a.state = 2', $end_time, $end_time)); } }) ->field('a.*,c.name,c.money,c.min_money,c.start_time,c.end_time,a.start_time as coupon_start_time,a.end_time as coupon_end_time,c.type_id,c.game_id,c.state as coupon_state,c.content,a.use_time') ->order($order) ->paginate(['list_rows' => $this->input['pageSize'], 'page' => $this->input['page']]) ->toArray(); if ($list) { $tmpGameList = model('Common/Game')->getAllByCondition('id,name,nickname'); $gameList = array(); foreach ($tmpGameList as $game) { $gameList[$game['id']] = $game; } foreach ($list['data'] as $k => $v) { if ($v['type_id'] == 2) { $list['data'][$k]['start_time'] = date('Y-m-d H:i:s',$v['coupon_start_time']); $list['data'][$k]['end_time'] = date('Y-m-d H:i:s',$v['coupon_end_time']); }else{ $list['data'][$k]['end_time'] = date('Y-m-d H:i:s',strtotime($v['end_time'])); } if ($v['game_id']) { $game_ids = explode(',', $v['game_id']); $tmp = []; foreach ($game_ids as $kk => $vv) { $tmp[] = $gameList[$vv]['name']; } $list['data'][$k]['game_name'] = implode(' ', array_unique($tmp)); } else { $list['data'][$k]['game_name'] = '全部游戏'; } } } $list['type_id'] = $type_id; $this->jsonResult($list, 1, '获取成功'); } /** * 可领取代金券列表 */ public function getCoupon() { $checkResult = $this->validate($this->input, [ ['page', 'require|integer|egt:1', '页数不能为空|页数必须是整数|页数必须大于1'], ['pageSize', 'require|integer|egt:10', '页码不能为空|页码必须是整数|页码必须大于10'], ['gameid', 'require|integer|gt:1', '游戏ID不能为空|游戏ID必须是整数|游戏ID必须大于1'], ]); if (true !== $checkResult) { $this->jsonResult('', 0, $checkResult); } $list = model('Coupon')->alias('a') ->join(sprintf('(select * from cy_coupon_member where member_id=%d group by coupon_id ) b', $this->input['userid']), 'a.id=b.coupon_id', 'left') ->where('a.is_show', 2) ->whereRaw(sprintf('FIND_IN_SET(%d,a.game_id)',$this->input['gameid'])) ->where('a.state', 1)->whereRaw(sprintf('(a.type_id=1 and a.end_time>=%d) or a.type_id=2', strtotime(date('Y-m-d'))))->order('a.id desc') ->field('a.*,if(b.member_id>0,b.member_id,0) as member_id,b.start_time as coupon_start_time,b.end_time as coupon_end_time,b.code') ->paginate(['list_rows' => $this->input['pageSize'], 'page' => $this->input['page']]) ->toArray(); if ($list) { $tmpGameList = model('Common/Game')->getAllByCondition('id,name,nickname'); $gameList = array(); foreach ($tmpGameList as $game) { $gameList[$game['id']] = $game; } foreach ($list['data'] as $k => $v) { //1未领取 2已领取 if ($v['member_id'] > 0) { $list['data'][$k]['receive'] = 2; if ($v['type_id'] == 2) { $list['data'][$k]['start_time'] = date('Y-m-d H:i:s',$v['coupon_start_time']); $list['data'][$k]['end_time'] = date('Y-m-d H:i:s',$v['coupon_end_time']+86399); } } else { $list['data'][$k]['receive'] = 1; } if ($v['grant_num'] > 0) { $unreceivedNum = model('Common/CouponMember') ->where('coupon_id', $v['id']) ->where('member_id', 0) ->count(); if ($unreceivedNum <= 0) { $list['data'][$k]['percentage'] = 0; } else { $list['data'][$k]['percentage'] = (int)number_format(($v['grant_num'] - $v['receive_num']) / $v['grant_num'] * 100, 0, '.', ''); } } else { $list['data'][$k]['percentage'] = 0; } if ($v['game_id']) { $game_ids = explode(',', $v['game_id']); $tmp = []; foreach ($game_ids as $kk => $vv) { $tmp[] = $gameList[$vv]['nickname']; } $list['data'][$k]['game_name'] = implode(' ', array_unique($tmp)); } else { $list['data'][$k]['game_name'] = '全部游戏'; } } } $this->jsonResult($list, 1, '获取成功'); } // 代金卷领取 public function receiveCoupon() { $checkResult = $this->validate($this->input, [ ['id', 'require|integer|egt:1', '代金券ID不能为空|代金券ID必须是整数|代金券ID必须大于1'], ['gameid', 'require|integer|gt:1', '游戏ID不能为空|游戏ID必须是整数|游戏ID必须大于1'], ]); if (true !== $checkResult) { $this->jsonResult('', 0, $checkResult); } $coupon = model('Common/Coupon')->cache('v2:coupon:receiveCoupon' . $this->input['id'], 60)->where('id', $this->input['id']) ->whereRaw(sprintf('(type_id=1 and end_time>=%s) or (type_id=2) and state=1 and is_show=2', Db::quote(date('Y-m-d'))))->find(); if (!$coupon) { $this->jsonResult('', 0, '代金券不存在'); } if ($coupon['receive_num'] >= $coupon['grant_num']) { $this->jsonResult('', 0, '代金券已领完'); } if (model('common/CouponMember')->where(['member_id' => $this->input['userid'], 'coupon_id' => $this->input['id']])->find()) { $this->jsonResult('', 0, '已领取', 'json'); } model('Common/Coupon')->startTrans(); if (!$couponMember = model('common/CouponMember')->where(['coupon_id' => $this->input['id'], 'state' => 1, 'is_use' => 1, 'member_id' => 0])->find()) { model('Common/Coupon')->rollback(); $this->jsonResult('', 0, '代金券已领完'); } if (!model('Common/Coupon')->where('id', $coupon['id'])->update(['receive_num' => $coupon['receive_num'] + 1])) { model('Common/Coupon')->rollback(); $this->jsonResult('', 0, '领取失败,稍后再试 '); } if ($coupon['type_id'] == 1) { $start_time = 0; $end_time = 0; } else { $start_time = strtotime(date('Y-m-d')); $end_time = strtotime(date('Y-m-d')) + 86400 * $coupon['day'] - 1; } if (model('common/CouponMember')->where(['id' => $couponMember['id'], 'coupon_id' => $this->input['id'], 'state' => 1, 'is_use' => 1, 'member_id' => 0])->update(['member_id' => $this->input['userid'], 'update_time' => time(), 'start_time' => $start_time, 'end_time' => $end_time])) { model('Common/Coupon')->commit(); $this->jsonResult(['code'=>$couponMember['code']], 1, '领取成功'); } else { model('Common/Coupon')->rollback(); $this->jsonResult('', 0, '领取失败,稍后再试'); } } // 代金券码兑换 public function exchangeCoupon() { $checkResult = $this->validate($this->input, [ ['code', 'require', '代金券兑换码不能为空'], ['gameid', 'require|integer|gt:1', '游戏ID不能为空|游戏ID必须是整数|游戏ID必须大于1'], ]); if (true !== $checkResult) { $this->jsonResult('', 0, $checkResult); } if (!$couponMember = model('common/CouponMember')->where('code', trim($this->input['code']))->find()) { $this->jsonResult('', 0, '兑换码错误'); } if ($couponMember['member_id'] > 0) { $this->jsonResult('', 0, '该兑换码已兑换'); } $coupon = model('Common/Coupon')->cache('v2:coupon:receiveCoupon' . $couponMember['coupon_id'], 60)->where('id', $couponMember['coupon_id']) ->whereRaw(sprintf('(type_id=1 and end_time>=%s) or (type_id=2) and state=1 and is_show=2', Db::quote(date('Y-m-d'))))->find(); if (!$coupon) { $this->jsonResult('', 0, '代金券不存在'); } model('Common/Coupon')->startTrans(); if (!model('Common/Coupon')->where('id', $coupon['id'])->update(['receive_num' => $coupon['receive_num'] + 1])) { model('Common/Coupon')->rollback(); $this->jsonResult('', 0, '领取失败,稍后再试 '); } if ($coupon['type_id'] == 1) { $start_time = 0; $end_time = 0; if($coupon['end_time']>time()){ $this->jsonResult('', 0, '代金券已过期'); } } else { $start_time = strtotime(date('Y-m-d')); $end_time = strtotime(date('Y-m-d')) + 86400 * $coupon['day'] - 1; } if (model('common/CouponMember')->where(['id' => $couponMember['id'], 'coupon_id' => $coupon['id'], 'state' => 1, 'is_use' => 1, 'member_id' => 0])->update(['member_id' => $this->input['userid'], 'update_time' => time(), 'start_time' => $start_time, 'end_time' => $end_time])) { model('Common/Coupon')->commit(); $this->jsonResult('', 1, '领取成功'); } else { model('Common/Coupon')->rollback(); $this->jsonResult('', 0, '领取失败,稍后再试'); } } /** * 支付可用代金券获取 */ public function getPayCoupon() { $checkResult = $this->validate($this->input, [ ['money', 'require|integer|egt:1', '金额不能为空|金额必须是整数|金额必须大于0'], ['gameid', 'require|integer|gt:1', '游戏ID不能为空|游戏ID必须是整数|游戏ID必须大于1'], ]); if (true !== $checkResult) { $this->jsonResult('', 0, $checkResult); } $list = model('common/CouponMember')->getPayCoupon($this->input['userid'], $this->input['gameid'], $this->input['money']); $this->jsonResult(['list' => $list], 1, '获取成功'); } }