MiddleOpenBox.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\api\command;
  4. use AlibabaCloud\SDK\OSS\OSS\SelectObjectRequest\body\selectRequest;
  5. use app\api\services\ThirdPayServices;
  6. use app\api\services\UserServices;
  7. use app\common\model\BoxMidHandleModel;
  8. use app\common\model\BoxModel;
  9. use app\common\model\CouponAreaModel;
  10. use app\common\model\CouponPlanLogModel;
  11. use app\common\model\CouponPlanModel;
  12. use app\common\model\ShopGoodsModel;
  13. use app\common\model\UserModel;
  14. use PhpOffice\PhpSpreadsheet\IOFactory;
  15. use think\console\Command;
  16. use think\console\Input;
  17. use think\console\Output;
  18. use think\db\Where;
  19. use think\Exception;
  20. use think\facade\Db;
  21. class MiddleOpenBox extends Command
  22. {
  23. protected function configure()
  24. {
  25. // 后台确定开奖
  26. $this->setName('middle_open_box')
  27. ->setDescription('the middle_open_box command');
  28. }
  29. protected function execute(Input $input, Output $output)
  30. {
  31. Db::startTrans();
  32. try {
  33. // 添加用户数据
  34. // $this->addUser();
  35. // 自动预约福袋
  36. // $this->buyBoxBefore();
  37. // $list = Db::name('box_mid_handle')->where('id', '>', 0)->select();
  38. // foreach ($list as $key=>$val){
  39. // $output->writeln('key'.$key);
  40. // if (!Db::name('shop_goods')->where('goods_id', $val['goods_id'])->find()){
  41. // sr_throw('error:'.$val['goods_id']);
  42. // }
  43. // }
  44. // 后台匹配开奖
  45. // $this->systemHandleBox($input, $output);
  46. // 匹配开奖
  47. $this->recordBoxAppointToMiddle($input, $output);
  48. Db::commit();
  49. }catch (\Exception $e){
  50. Db::rollback();
  51. $output->writeln('error:'.$e->getMessage());
  52. }
  53. return 22;
  54. }
  55. protected function recordBoxAppointToMiddle($input, $output){
  56. // 总共用户的盒子累计 计算
  57. $list1 = Db::name('box_record')->where('status', 1)->group('uid')->field('uid')->select();
  58. foreach ($list1 as $key=>$val){
  59. $total = Db::name('box_record')->where('status', 1)->where('uid', $val['uid'])->sum('num');
  60. $box10 = Db::name('box_record')->where('status', 1)->where('uid', $val['uid'])->sum('box10');
  61. $box20 = Db::name('box_record')->where('status', 1)->where('uid', $val['uid'])->sum('box20');
  62. $box30 = Db::name('box_record')->where('status', 1)->where('uid', $val['uid'])->sum('box30');
  63. $box40 = Db::name('box_record')->where('status', 1)->where('uid', $val['uid'])->sum('box40');
  64. if ($total> ($box10+$box20+$box30+$box40)){
  65. $null_count = $total- ($box10+$box20+$box30+$box40);
  66. // 如果有空盒
  67. Db::name('user')->where('id', $val['uid'])->inc('total_null_box', $null_count)->update();
  68. }
  69. // 增加其他盒子个数
  70. Db::name('user')->where('id', $val['uid'])->inc('box10', $box10)->inc('box20', $box20)->inc('box30', $box30)->inc('box40', $box40)->update();
  71. }
  72. $list = Db::name('box_record')->where('status', 1)->select()->toArray();
  73. $mid_arrs = [];
  74. $r_ids = [];
  75. $cur_time = sr_getcurtime(time());
  76. $model = new ShopGoodsModel();
  77. $goods_arr20 = $model
  78. ->where('box_type', 20)
  79. ->where('goods_type', 2)
  80. ->where('can_open', 0)
  81. ->field('goods_id,box_type,goods_img,price,goods_name')
  82. ->page(1, 20)
  83. ->select();
  84. foreach ($list as $key=>$val){
  85. $total_goodsbox = $val['box10'] + $val['box20'] + $val['box30'] + $val['box40'];
  86. $r_goodsids = '';
  87. // 如果有空盒
  88. if ($val['num'] > $total_goodsbox){
  89. for ($i=0;$i < $val['num']-$total_goodsbox;$i++){
  90. $mid_arrs[] = [
  91. 'h_sn'=>createdHandleOrderSn(),
  92. 'uid'=>$val['uid'],
  93. 'rid'=>$val['id'],
  94. 'goods_id'=>0,
  95. 'create_time'=>$cur_time,
  96. 'box_type'=>0,
  97. 'goods_price'=>0,
  98. 'pay_type'=>$val['pay_type']
  99. ];
  100. }
  101. }
  102. // if (1 == 1){
  103. // sr_throw();
  104. // }
  105. // 其他盒子
  106. $box_arrs = [10, 20, 30, 40];
  107. foreach ($box_arrs as $key1=>$val1){
  108. $key_string = strval('box'.$val1);
  109. $box_goodsnum = $val[$key_string];
  110. $goods_arr = $model
  111. ->where('box_type', $val1)
  112. ->where('goods_type', 2)
  113. ->where('can_open', 1)
  114. ->field('goods_id,box_type,goods_img,price,goods_name')
  115. ->page(1, 20)
  116. ->select();
  117. // sr_throw(json_encode($goods_arr));
  118. if ($box_goodsnum > 0){
  119. // 如果后期要调整 取值 直接 排序 goods_arr
  120. for ($i = 0; $i < $box_goodsnum;$i++){
  121. $rand_num = mt_rand(0, count($goods_arr)-1);
  122. $goods = $goods_arr[$rand_num];
  123. if ($val1 == 20){
  124. // 如果有指定 稀有
  125. if ($i == 0 && $val['hs_xy']==1){
  126. $rand_num = mt_rand(0, count($goods_arr20)-1);
  127. $goods = $goods_arr20[$rand_num];
  128. }
  129. }
  130. $mid_arrs[] = [
  131. 'h_sn'=>createdHandleOrderSn(),
  132. 'uid'=>$val['uid'],
  133. 'rid'=>$val['id'],
  134. 'goods_id'=>$goods['goods_id'],
  135. 'create_time'=>$cur_time,
  136. 'box_type'=>$val1,
  137. 'goods_price'=>$goods['price'],
  138. 'pay_type'=>$val['pay_type']
  139. ];
  140. $r_goodsids .= ((empty($r_goodsids)?'':',').$goods['goods_id']);
  141. }
  142. }
  143. }
  144. Db::name('box_record')->where('id', $val['id'])->save([
  145. 'status'=>2,
  146. 'goods_ids'=>$r_goodsids
  147. ]);
  148. }
  149. $model = new BoxMidHandleModel();
  150. $model->insertAll($mid_arrs);
  151. }
  152. public function systemHandleBox(Input $input, Output $output){
  153. }
  154. public function addUser(){
  155. $users = $this->randomPhone(100);
  156. foreach ($users as $key=>$val){
  157. if (!Db::name('user')->where('mobile', $val)->find()){
  158. $services = new UserServices();
  159. $services->userRegister([
  160. 'user_name'=>'db-'.$val,
  161. 'invite_code'=>'03284278',
  162. 'code'=>'111111',
  163. 'password'=>"TDFyZ3n\/5GksjKtjhj2G1w==",
  164. 'mobile'=>$val
  165. ]
  166. );
  167. }
  168. }
  169. }
  170. public function buyBoxBefore(){
  171. return ;
  172. $users = Db::name('user')->where('money', '>', 0)->paginate(50)->toArray();
  173. $model = new BoxModel();
  174. foreach ($users['data'] as $key=>$val){
  175. $count = rand(0, 10);
  176. $model->beforeBuyBox($val['id'], ['box_id'=>23422, 'buy_count'=>$count, 'pay_type'=>rand(1,2)]);
  177. }
  178. }
  179. //随机生成n条手机号
  180. function randomPhone($n = 100)
  181. {
  182. $tel_arr = array(
  183. '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',
  184. );
  185. for ($i = 0; $i < $n; $i++) {
  186. $tmp[] = $tel_arr[array_rand($tel_arr)] . mt_rand(1000, 9999) . mt_rand(1000, 9999);
  187. }
  188. return array_unique($tmp);
  189. }
  190. }