setName('middle_open_box') ->setDescription('the middle_open_box command'); } protected function execute(Input $input, Output $output) { Db::startTrans(); try { // 添加用户数据 // $this->addUser(); // 自动预约福袋 // $this->buyBoxBefore(); // $list = Db::name('box_mid_handle')->where('id', '>', 0)->select(); // foreach ($list as $key=>$val){ // $output->writeln('key'.$key); // if (!Db::name('shop_goods')->where('goods_id', $val['goods_id'])->find()){ // sr_throw('error:'.$val['goods_id']); // } // } // 后台匹配开奖 // $this->systemHandleBox($input, $output); // 匹配开奖 $this->recordBoxAppointToMiddle($input, $output); Db::commit(); }catch (\Exception $e){ Db::rollback(); $output->writeln('error:'.$e->getMessage()); } return 22; } protected function recordBoxAppointToMiddle($input, $output){ // 总共用户的盒子累计 计算 $list1 = Db::name('box_record')->where('status', 1)->group('uid')->field('uid')->select(); foreach ($list1 as $key=>$val){ $total = Db::name('box_record')->where('status', 1)->where('uid', $val['uid'])->sum('num'); $box10 = Db::name('box_record')->where('status', 1)->where('uid', $val['uid'])->sum('box10'); $box20 = Db::name('box_record')->where('status', 1)->where('uid', $val['uid'])->sum('box20'); $box30 = Db::name('box_record')->where('status', 1)->where('uid', $val['uid'])->sum('box30'); $box40 = Db::name('box_record')->where('status', 1)->where('uid', $val['uid'])->sum('box40'); if ($total> ($box10+$box20+$box30+$box40)){ $null_count = $total- ($box10+$box20+$box30+$box40); // 如果有空盒 Db::name('user')->where('id', $val['uid'])->inc('total_null_box', $null_count)->update(); } // 增加其他盒子个数 Db::name('user')->where('id', $val['uid'])->inc('box10', $box10)->inc('box20', $box20)->inc('box30', $box30)->inc('box40', $box40)->update(); } $list = Db::name('box_record')->where('status', 1)->select()->toArray(); $mid_arrs = []; $r_ids = []; $cur_time = sr_getcurtime(time()); $model = new ShopGoodsModel(); $goods_arr20 = $model ->where('box_type', 20) ->where('goods_type', 2) ->where('can_open', 0) ->field('goods_id,box_type,goods_img,price,goods_name') ->page(1, 20) ->select(); foreach ($list as $key=>$val){ $total_goodsbox = $val['box10'] + $val['box20'] + $val['box30'] + $val['box40']; $r_goodsids = ''; // 如果有空盒 if ($val['num'] > $total_goodsbox){ for ($i=0;$i < $val['num']-$total_goodsbox;$i++){ $mid_arrs[] = [ 'h_sn'=>createdHandleOrderSn(), 'uid'=>$val['uid'], 'rid'=>$val['id'], 'goods_id'=>0, 'create_time'=>$cur_time, 'box_type'=>0, 'goods_price'=>0, 'pay_type'=>$val['pay_type'] ]; } } // if (1 == 1){ // sr_throw(); // } // 其他盒子 $box_arrs = [10, 20, 30, 40]; foreach ($box_arrs as $key1=>$val1){ $key_string = strval('box'.$val1); $box_goodsnum = $val[$key_string]; $goods_arr = $model ->where('box_type', $val1) ->where('goods_type', 2) ->where('can_open', 1) ->field('goods_id,box_type,goods_img,price,goods_name') ->page(1, 20) ->select(); // sr_throw(json_encode($goods_arr)); if ($box_goodsnum > 0){ // 如果后期要调整 取值 直接 排序 goods_arr for ($i = 0; $i < $box_goodsnum;$i++){ $rand_num = mt_rand(0, count($goods_arr)-1); $goods = $goods_arr[$rand_num]; if ($val1 == 20){ // 如果有指定 稀有 if ($i == 0 && $val['hs_xy']==1){ $rand_num = mt_rand(0, count($goods_arr20)-1); $goods = $goods_arr20[$rand_num]; } } $mid_arrs[] = [ 'h_sn'=>createdHandleOrderSn(), 'uid'=>$val['uid'], 'rid'=>$val['id'], 'goods_id'=>$goods['goods_id'], 'create_time'=>$cur_time, 'box_type'=>$val1, 'goods_price'=>$goods['price'], 'pay_type'=>$val['pay_type'] ]; $r_goodsids .= ((empty($r_goodsids)?'':',').$goods['goods_id']); } } } Db::name('box_record')->where('id', $val['id'])->save([ 'status'=>2, 'goods_ids'=>$r_goodsids ]); } $model = new BoxMidHandleModel(); $model->insertAll($mid_arrs); } public function systemHandleBox(Input $input, Output $output){ } public function addUser(){ $users = $this->randomPhone(100); foreach ($users as $key=>$val){ if (!Db::name('user')->where('mobile', $val)->find()){ $services = new UserServices(); $services->userRegister([ 'user_name'=>'db-'.$val, 'invite_code'=>'03284278', 'code'=>'111111', 'password'=>"TDFyZ3n\/5GksjKtjhj2G1w==", 'mobile'=>$val ] ); } } } public function buyBoxBefore(){ return ; $users = Db::name('user')->where('money', '>', 0)->paginate(50)->toArray(); $model = new BoxModel(); foreach ($users['data'] as $key=>$val){ $count = rand(0, 10); $model->beforeBuyBox($val['id'], ['box_id'=>23422, 'buy_count'=>$count, 'pay_type'=>rand(1,2)]); } } //随机生成n条手机号 function randomPhone($n = 100) { $tel_arr = array( '130', '131', '132', '133', '134', '135', '136', '137', '138', '139', '144', '147', '150', '151', '152', '153', '155', '156', '157', '158', '159', '176', '177', '178', '180', '181', '182', '183', '184', '185', '186', '187', '188', '189', ); for ($i = 0; $i < $n; $i++) { $tmp[] = $tel_arr[array_rand($tel_arr)] . mt_rand(1000, 9999) . mt_rand(1000, 9999); } return array_unique($tmp); } }