BoxModel.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. // 修复 by wes 2023-02-21
  11. $boxCount = isset($params['buy_count'])? intval($params['buy_count']) : 0;
  12. $boxId = isset($params['box_id'])? intval($params['box_id']) : 0;
  13. if ($boxCount<=0 || $boxId<=0 || !isset($params['pay_type'])){
  14. sr_throw('参数错误');
  15. }
  16. if (!in_array($params['pay_type'], [1, 2])){
  17. sr_throw('支付类型错误');
  18. }
  19. /**
  20. $boxId = isset($params['box_id'])? $params['box_id'] : 0;
  21. $userId = isset($user_info['id'])? $user_info['id'] : 0;
  22. $cacheKey = "caches:boxApply:box_{$boxId}:u_{$userId}";
  23. $redis = new Redis();
  24. if($redis->get($cacheKey)){
  25. sr_throw('请不要频繁操作');
  26. }
  27. **/
  28. $box_info = Db::name('box')->where('id', $params['box_id'])->find();
  29. if (!$box_info){
  30. sr_throw('参数错误');
  31. }
  32. //$redis->set($cacheKey, json_encode(['uid'=> $userId,'box'=> $box_info], 256), rand(1,2));
  33. //if ($box_info['open_status']!=1 && $user_info['id'] != 5344517){
  34. if ($box_info['open_status']!=1){
  35. sr_throw('预约未开启');
  36. }
  37. if ($params['buy_count'] > 20){
  38. sr_throw('最多可预约20个');
  39. }
  40. if ($params['buy_count'] < 5){
  41. $count = Db::name('box_record')->where('uid', $uid)->where('box_id', $params['box_id'])->where('status', 1)->where('num', '<', 5)->count('id');
  42. if ( $count >= 4){
  43. sr_throw('每一场数量小于5最多只能预约4次');
  44. }
  45. }
  46. if (Db::name('box_record')->where('uid', $uid)->where('status', 1)->sum('num') > 100){
  47. sr_throw('每一场最多可预约100次');
  48. }
  49. $times = explode('|', $box_info['time_set']);
  50. $cur_timeday = sr_getcurtime(time(), 'Y-m-d');
  51. $time = time();
  52. $lun = 1;
  53. $day_before = 0;
  54. $day_end = 0;
  55. $can_buy = false;
  56. foreach ($times as $key=>$val){
  57. $timesarr = explode('-', $val);
  58. $begin_time = strtotime($cur_timeday.' '.$timesarr[0]);
  59. $end_time=strtotime($cur_timeday.' '.$timesarr[1]);
  60. if ($time > $begin_time && $time < $end_time){
  61. $can_buy = true;
  62. $lun = $key+1;
  63. }
  64. }
  65. //if (!$can_buy && $user_info['id'] != 5344517){
  66. if (!$can_buy){
  67. sr_throw('预约失败,还未开始预约、或预约已结束');
  68. }
  69. $total_pay = $params['buy_count'] * env('boxsetting.one_box_price');
  70. $user_model = new UserModel();
  71. // $userinfo = $user_model->where('id', $uid)->find();
  72. // 修改 by wes at 2023-02-19 16点
  73. $user_info = $user_model->where(['id'=> $uid])->field('id,score,money,has_fd')->find();
  74. // end
  75. if ($params['pay_type'] == 1){
  76. if ($user_info['score'] < $total_pay){
  77. sr_throw('积分不足,预约失败');
  78. }
  79. edit_user_score(1, $uid, $total_pay);
  80. }
  81. if ($params['pay_type'] == 2){
  82. if ($user_info['money'] < $total_pay){
  83. sr_throw('余额不足,预约失败');
  84. }
  85. edit_user_money(1, $uid, $total_pay);
  86. }
  87. Db::name('box_record')->insert([
  88. 'box_id'=>$params['box_id'],
  89. 'num'=>$params['buy_count'],
  90. 'lun_count'=>$lun,
  91. 'uid'=>$uid,
  92. 'create_time'=>sr_getcurtime($time),
  93. 'box_img'=>$box_info['box_img'],
  94. 'box_title'=>$box_info['box_title'],
  95. 'qi_count'=>$box_info['qi_count'],
  96. 'pay_type'=>$params['pay_type']
  97. ]);
  98. // 设置用户已经预约福袋
  99. Db::name('user')->where('id', $uid)->inc('total_appoint_count', $params['buy_count'])->update();
  100. if ($user_info['has_fd']==0){
  101. Db::name('user')->where('id', $uid)->save([
  102. 'has_fd'=>1
  103. ]);
  104. }
  105. }
  106. }