| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- <?php
- declare (strict_types = 1);
- namespace app\api\command;
- use AlibabaCloud\SDK\OSS\OSS\SelectObjectRequest\body\selectRequest;
- use app\api\services\ThirdPayServices;
- use app\api\services\UserServices;
- use app\common\model\BoxMidHandleModel;
- use app\common\model\BoxModel;
- use app\common\model\CouponAreaModel;
- use app\common\model\CouponPlanLogModel;
- use app\common\model\CouponPlanModel;
- use app\common\model\ShopGoodsModel;
- use app\common\model\UserModel;
- use PhpOffice\PhpSpreadsheet\IOFactory;
- use think\cache\driver\Redis;
- use think\console\Command;
- use think\console\Input;
- use think\console\Output;
- use think\db\Where;
- use think\Exception;
- use think\facade\Db;
- class MiddleOpenBox extends Command
- {
- protected function configure()
- {
- // 后台确定开奖
- $this->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);
- }
- }
|