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]; } }