BoxModel.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace app\common\model;
  3. use app\common\model\TimeModel;
  4. use think\facade\Db;
  5. use think\Model;
  6. class BoxModel extends Model
  7. {
  8. protected $name = "box";
  9. public function beforeBuyBox($uid, $params, $user_info){
  10. if (!isset($params['buy_count']) || !isset($params['box_id']) || !isset($params['pay_type'])){
  11. sr_throw('参数错误');
  12. }
  13. if (!in_array($params['pay_type'], [1, 2])){
  14. sr_throw('支付类型错误');
  15. }
  16. $box_info = Db::name('box')->where('id', $params['box_id'])->find();
  17. if (!$box_info){
  18. sr_throw('参数错误');
  19. }
  20. if ($params['buy_count'] > 20){
  21. sr_throw('最多可预约20个');
  22. }
  23. if ($params['buy_count'] < 5){
  24. $count = Db::name('box_record')->where('uid', $uid)->where('box_id', $params['box_id'])->where('status', 1)->where('num', '<', 5)->count('id');
  25. if ( $count >= 4){
  26. sr_throw('每一场数量小于5最多只能预约4次');
  27. }
  28. }
  29. if (Db::name('box_record')->where('uid', $uid)->where('status', 1)->sum('num') > 100){
  30. sr_throw('每一场最多可预约100次');
  31. }
  32. $times = explode('|', $box_info['time_set']);
  33. $cur_timeday = sr_getcurtime(time(), 'Y-m-d');
  34. $time = time();
  35. $lun = 1;
  36. $day_before = 0;
  37. $day_end = 0;
  38. $can_buy = false;
  39. foreach ($times as $key=>$val){
  40. $timesarr = explode('-', $val);
  41. $begin_time = strtotime($cur_timeday.' '.$timesarr[0]);
  42. $end_time=strtotime($cur_timeday.' '.$timesarr[1]);
  43. if ($time > $begin_time && $time < $end_time){
  44. $can_buy = true;
  45. $lun = $key+1;
  46. }
  47. }
  48. if (!$can_buy){
  49. sr_throw('预约失败,还未开始预约、或预约已结束');
  50. }
  51. $total_pay = $params['buy_count'] * env('boxsetting.one_box_price');
  52. $user_model = new UserModel();
  53. $user_info = $user_model->where(['id'=> $uid])->field('id,score,money,has_fd')->find();
  54. if ($params['pay_type'] == 1){
  55. if ($user_info['score'] < $total_pay){
  56. sr_throw('积分不足,预约失败');
  57. }
  58. edit_user_score(1, $uid, $total_pay);
  59. }
  60. if ($params['pay_type'] == 2){
  61. if ($user_info['money'] < $total_pay){
  62. sr_throw('余额不足,预约失败');
  63. }
  64. edit_user_money(1, $uid, $total_pay);
  65. }
  66. Db::name('box_record')->insert([
  67. 'box_id'=>$params['box_id'],
  68. 'num'=>$params['buy_count'],
  69. 'lun_count'=>$lun,
  70. 'uid'=>$uid,
  71. 'create_time'=>sr_getcurtime($time),
  72. 'box_img'=>$box_info['box_img'],
  73. 'box_title'=>$box_info['box_title'],
  74. 'qi_count'=>$box_info['qi_count'],
  75. 'pay_type'=>$params['pay_type']
  76. ]);
  77. // 设置用户已经预约福袋
  78. Db::name('user')->where('id', $uid)->inc('total_appoint_count', $params['buy_count'])->update();
  79. if ($user_info['has_fd']==0){
  80. Db::name('user')->where('id', $uid)->save([
  81. 'has_fd'=>1
  82. ]);
  83. }
  84. }
  85. }