| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- <?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){
- if (!isset($params['buy_count']) || !isset($params['box_id']) || !isset($params['pay_type'])){
- sr_throw('参数错误');
- }
- if (!in_array($params['pay_type'], [1, 2])){
- sr_throw('支付类型错误');
- }
- $box_info = Db::name('box')->where('id', $params['box_id'])->find();
- if (!$box_info){
- 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){
- sr_throw('预约失败,还未开始预约、或预约已结束');
- }
- $total_pay = $params['buy_count'] * env('boxsetting.one_box_price');
- $user_model = new UserModel();
- $user_info = $user_model->where(['id'=> $uid])->field('id,score,money,has_fd')->find();
- 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
- ]);
- }
- }
- }
|