MiddleOpenBox.php 8.0 KB

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