BoxMidHandleModel.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace app\common\model;
  3. use app\api\services\UserServices;
  4. use app\common\model\TimeModel;
  5. //use app\model\RedPoolModel;
  6. //use app\model\RedStorePoolModel;
  7. use app\common\model\UserDataModel;
  8. use services\CacheServices;
  9. use think\Exception;
  10. use think\facade\Db;
  11. use think\Model;
  12. class BoxMidHandleModel extends Model
  13. {
  14. protected $name = "box_mid_handle";
  15. public function user ()
  16. {
  17. return $this->belongsTo('app\common\model\UserModel', 'uid', 'id');
  18. }
  19. public function box ()
  20. {
  21. return $this->belongsTo('app\common\model\BoxModel', 'box_id', 'id');
  22. }
  23. public function openBoxOnline($uid, $params, $user_info){
  24. // 11:30 12:00
  25. // 15:30 16:00
  26. // 20:30 21:00
  27. $cur_time = time();
  28. // $arr = ['12:00-12:00', '15:30-16:00', '20:30-21:00'];
  29. $box_info = Db::name('box')->where('status', 1)->find();
  30. if (!$box_info){
  31. sr_throw('还未开启');
  32. }
  33. // 开奖的半个小时
  34. $open_time = 30 * 60;
  35. $timesymdtring = sr_getcurtime(time(), 'Y-m-d ');
  36. $is_midhanle = false;
  37. $is_handle = false;
  38. $arr = explode('|', $box_info['time_set']);
  39. foreach ($arr as $key=>$val){
  40. $times = explode('-', $val);
  41. if ($cur_time>strtotime($timesymdtring.$times[0]) && $cur_time < strtotime($timesymdtring.$times[1])){
  42. // 正在预约中
  43. $is_midhanle = true;
  44. }
  45. if ($cur_time>strtotime($timesymdtring.$times[1]) && $cur_time < strtotime($timesymdtring.$times[1])+$open_time){
  46. // 正在预约中
  47. $is_handle = true;
  48. }
  49. }
  50. if ($is_midhanle){
  51. sr_throw('正在预约中');
  52. }
  53. if ($is_handle){
  54. sr_throw('福袋正在发放中');
  55. }
  56. // 如果有预约记录 但是没有匹配
  57. if (Db::name('box_record')->where('uid', $uid)->where('status', 1)->find()){
  58. sr_throw('您预约的福袋正在发放中,请稍后在试');
  59. }
  60. $list = self::alias('r')
  61. ->where('r.uid', $uid)
  62. ->where('r.status', 1)
  63. // ->where('r.box_type', '>', 0)
  64. ->leftJoin('shop_goods g', 'g.goods_id = r.goods_id')
  65. ->field('r.*,g.goods_name,g.goods_sn,g.goods_img,g.price,g.box_type,g.spec_name')
  66. ->order('g.box_type desc')
  67. ->select();
  68. $return_data = [];
  69. $total_count = 0;
  70. $total_count_null = 0;
  71. // 如有有预约记录并且预约记录没有拆开,状态是已匹配
  72. $has_appoint = Db::name('box_record')->where('uid', $uid)->where('status', 2)->find();
  73. if ($has_appoint){
  74. $total_count = self::where('uid', $uid)->where('status', 1)->count();
  75. $total_count_null = self::where('uid', $uid)->where('status', 1)->where('box_type', 0)->count();
  76. self::where('uid', $uid)->where('status', 1)->where('box_type', 0)->save([
  77. 'status'=>2
  78. ]);
  79. $ids = [];
  80. $data = [];
  81. $r_ids = array();
  82. foreach ($list as $key=>$val){
  83. if ($val['box_type'] > 0){
  84. $goods_info = Db::name('shop_goods')->where('goods_id', $val['goods_id'])->find();
  85. $data[] = [
  86. 'status'=>1,
  87. 'h_sn'=>createdHandleOrderSn(),
  88. 'uid'=>$val['uid'],
  89. 'rid'=>$val['id'],
  90. 'goods_id'=>$val['goods_id'],
  91. 'create_time'=>sr_getcurtime(time()),
  92. 'box_type'=>$goods_info['box_type'],
  93. 'goods_price'=>$val['goods_price']
  94. ];
  95. $return_data[] = $val;
  96. }else{
  97. // // 空盒 原路返回
  98. if ($val['pay_type'] == 1){
  99. edit_user_score(2, $val['uid'], env('boxsetting.ONE_BOX_PRICE'), $val['id']);
  100. }
  101. if ($val['pay_type'] == 2){
  102. edit_user_money(3, $val['uid'], env('boxsetting.ONE_BOX_PRICE'), $val['id']);
  103. }
  104. }
  105. $ids[] = $val['id'];
  106. $r_ids[$val['rid']] = 1;
  107. }
  108. self::where('id', 'in', $ids)->save([
  109. 'status'=>2
  110. ]);
  111. $model = new BoxHandleModel();
  112. $model->insertAll($data);
  113. $count = count($return_data);
  114. if ( $count > 0){
  115. Db::name('user')->where('id', 'in', $user_info['path'])->inc('total_team_box', $count)->update();
  116. Db::name('user')->where('id', 'in', $user_info['path'])->inc('today_team_box', $count)->update();
  117. Db::name('user')->where('id', $uid)->inc('today_box', $count)->inc('today_team_box', $count)->inc('total_box', $count)->inc('total_team_box', $count)->update();
  118. }
  119. // 预约记录变成已拆开
  120. Db::name('box_record')->whereIn('id', array_keys($r_ids))->where('status', 2)->save([
  121. 'status'=>3
  122. ]);
  123. }else{
  124. sr_throw('暂无预约');
  125. }
  126. // return ['list'=>$return_data, 'total_count'=>$total_count, 'total_count_box'=>$total_count-$total_count_null];
  127. }
  128. }