| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
- namespace app\common\model;
- use app\api\services\UserServices;
- use app\common\model\TimeModel;
- //use app\model\RedPoolModel;
- //use app\model\RedStorePoolModel;
- use app\common\model\UserDataModel;
- use services\CacheServices;
- use think\Exception;
- use think\facade\Db;
- use think\Model;
- class BoxMidHandleModel extends Model
- {
- protected $name = "box_mid_handle";
- public function user ()
- {
- return $this->belongsTo('app\common\model\UserModel', 'uid', 'id');
- }
- public function box ()
- {
- return $this->belongsTo('app\common\model\BoxModel', 'box_id', 'id');
- }
- public function openBoxOnline($uid, $params, $user_info){
- // 11:30 12:00
- // 15:30 16:00
- // 20:30 21:00
- $cur_time = time();
- // $arr = ['12:00-12:00', '15:30-16:00', '20:30-21:00'];
- $box_info = Db::name('box')->where('status', 1)->find();
- if (!$box_info){
- sr_throw('还未开启');
- }
- // 开奖的半个小时
- $open_time = 30 * 60;
- $timesymdtring = sr_getcurtime(time(), 'Y-m-d ');
- $is_midhanle = false;
- $is_handle = false;
- $arr = explode('|', $box_info['time_set']);
- foreach ($arr as $key=>$val){
- $times = explode('-', $val);
- if ($cur_time>strtotime($timesymdtring.$times[0]) && $cur_time < strtotime($timesymdtring.$times[1])){
- // 正在预约中
- $is_midhanle = true;
- }
- if ($cur_time>strtotime($timesymdtring.$times[1]) && $cur_time < strtotime($timesymdtring.$times[1])+$open_time){
- // 正在预约中
- $is_handle = true;
- }
- }
- if ($is_midhanle){
- sr_throw('正在预约中');
- }
- if ($is_handle){
- sr_throw('福袋正在发放中');
- }
- // 如果有预约记录 但是没有匹配
- if (Db::name('box_record')->where('uid', $uid)->where('status', 1)->find()){
- sr_throw('您预约的福袋正在发放中,请稍后在试');
- }
- $list = self::alias('r')
- ->where('r.uid', $uid)
- ->where('r.status', 1)
- // ->where('r.box_type', '>', 0)
- ->leftJoin('shop_goods g', 'g.goods_id = r.goods_id')
- ->field('r.*,g.goods_name,g.goods_sn,g.goods_img,g.price,g.box_type,g.spec_name')
- ->order('g.box_type desc')
- ->select();
- $return_data = [];
- $total_count = 0;
- $total_count_null = 0;
- // 如有有预约记录并且预约记录没有拆开,状态是已匹配
- $has_appoint = Db::name('box_record')->where('uid', $uid)->where('status', 2)->find();
- if ($has_appoint){
- $total_count = self::where('uid', $uid)->where('status', 1)->count();
- $total_count_null = self::where('uid', $uid)->where('status', 1)->where('box_type', 0)->count();
- self::where('uid', $uid)->where('status', 1)->where('box_type', 0)->save([
- 'status'=>2
- ]);
- $ids = [];
- $data = [];
- $r_ids = array();
- foreach ($list as $key=>$val){
- if ($val['box_type'] > 0){
- $goods_info = Db::name('shop_goods')->where('goods_id', $val['goods_id'])->find();
- $data[] = [
- 'status'=>1,
- 'h_sn'=>createdHandleOrderSn(),
- 'uid'=>$val['uid'],
- 'rid'=>$val['id'],
- 'goods_id'=>$val['goods_id'],
- 'create_time'=>sr_getcurtime(time()),
- 'box_type'=>$goods_info['box_type'],
- 'goods_price'=>$val['goods_price']
- ];
- $return_data[] = $val;
- }else{
- // // 空盒 原路返回
- if ($val['pay_type'] == 1){
- edit_user_score(2, $val['uid'], env('boxsetting.ONE_BOX_PRICE'), $val['id']);
- }
- if ($val['pay_type'] == 2){
- edit_user_money(3, $val['uid'], env('boxsetting.ONE_BOX_PRICE'), $val['id']);
- }
- }
- $ids[] = $val['id'];
- $r_ids[$val['rid']] = 1;
- }
- self::where('id', 'in', $ids)->save([
- 'status'=>2
- ]);
- $model = new BoxHandleModel();
- $model->insertAll($data);
- $count = count($return_data);
- if ( $count > 0){
- Db::name('user')->where('id', 'in', $user_info['path'])->inc('total_team_box', $count)->update();
- Db::name('user')->where('id', 'in', $user_info['path'])->inc('today_team_box', $count)->update();
- 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();
- }
- // 预约记录变成已拆开
- Db::name('box_record')->whereIn('id', array_keys($r_ids))->where('status', 2)->save([
- 'status'=>3
- ]);
- }else{
- sr_throw('暂无预约');
- }
- // return ['list'=>$return_data, 'total_count'=>$total_count, 'total_count_box'=>$total_count-$total_count_null];
- }
- }
|