| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- namespace app\common\model;
- use app\common\model\TimeModel;
- use think\facade\Db;
- use think\Model;
- class BoxModel extends Model
- {
- protected $name = "box";
- public function beforeBuyBox($uid, $params, $user_info){
- // 修复 by wes 2023-02-21
- $boxCount = isset($params['buy_count'])? intval($params['buy_count']) : 0;
- $boxId = isset($params['box_id'])? intval($params['box_id']) : 0;
- if ($boxCount<=0 || $boxId<=0 || !isset($params['pay_type'])){
- sr_throw('参数错误');
- }
- if (!in_array($params['pay_type'], [1, 2])){
- sr_throw('支付类型错误');
- }
-
- /**
- $boxId = isset($params['box_id'])? $params['box_id'] : 0;
- $userId = isset($user_info['id'])? $user_info['id'] : 0;
- $cacheKey = "caches:boxApply:box_{$boxId}:u_{$userId}";
- $redis = new Redis();
- if($redis->get($cacheKey)){
- sr_throw('请不要频繁操作');
- }
- **/
-
- $box_info = Db::name('box')->where('id', $params['box_id'])->find();
- if (!$box_info){
- sr_throw('参数错误');
- }
-
- //$redis->set($cacheKey, json_encode(['uid'=> $userId,'box'=> $box_info], 256), rand(1,2));
- //if ($box_info['open_status']!=1 && $user_info['id'] != 5344517){
- if ($box_info['open_status']!=1){
- sr_throw('预约未开启');
- }
- if ($params['buy_count'] > 20){
- sr_throw('最多可预约20个');
- }
- if ($params['buy_count'] < 5){
- $count = Db::name('box_record')->where('uid', $uid)->where('box_id', $params['box_id'])->where('status', 1)->where('num', '<', 5)->count('id');
- if ( $count >= 4){
- sr_throw('每一场数量小于5最多只能预约4次');
- }
- }
- if (Db::name('box_record')->where('uid', $uid)->where('status', 1)->sum('num') > 100){
- sr_throw('每一场最多可预约100次');
- }
- $times = explode('|', $box_info['time_set']);
- $cur_timeday = sr_getcurtime(time(), 'Y-m-d');
- $time = time();
- $lun = 1;
- $day_before = 0;
- $day_end = 0;
- $can_buy = false;
- foreach ($times as $key=>$val){
- $timesarr = explode('-', $val);
- $begin_time = strtotime($cur_timeday.' '.$timesarr[0]);
- $end_time=strtotime($cur_timeday.' '.$timesarr[1]);
- if ($time > $begin_time && $time < $end_time){
- $can_buy = true;
- $lun = $key+1;
- }
- }
- //if (!$can_buy && $user_info['id'] != 5344517){
- if (!$can_buy){
- sr_throw('预约失败,还未开始预约、或预约已结束');
- }
- $total_pay = $params['buy_count'] * env('boxsetting.one_box_price');
- $user_model = new UserModel();
- // $userinfo = $user_model->where('id', $uid)->find();
- // 修改 by wes at 2023-02-19 16点
- $user_info = $user_model->where(['id'=> $uid])->field('id,score,money,has_fd')->find();
-
- // end
- if ($params['pay_type'] == 1){
- if ($user_info['score'] < $total_pay){
- sr_throw('积分不足,预约失败');
- }
- edit_user_score(1, $uid, $total_pay);
- }
- if ($params['pay_type'] == 2){
- if ($user_info['money'] < $total_pay){
- sr_throw('余额不足,预约失败');
- }
- edit_user_money(1, $uid, $total_pay);
- }
- Db::name('box_record')->insert([
- 'box_id'=>$params['box_id'],
- 'num'=>$params['buy_count'],
- 'lun_count'=>$lun,
- 'uid'=>$uid,
- 'create_time'=>sr_getcurtime($time),
- 'box_img'=>$box_info['box_img'],
- 'box_title'=>$box_info['box_title'],
- 'qi_count'=>$box_info['qi_count'],
- 'pay_type'=>$params['pay_type']
- ]);
- // 设置用户已经预约福袋
- Db::name('user')->where('id', $uid)->inc('total_appoint_count', $params['buy_count'])->update();
- if ($user_info['has_fd']==0){
- Db::name('user')->where('id', $uid)->save([
- 'has_fd'=>1
- ]);
- }
- }
- }
|