SelectPayUser.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace App\Console\Commands\Temp;
  3. use Log;
  4. use Illuminate\Console\Command;
  5. use DB;
  6. use App\Modules\User\Services\ReadRecordService;
  7. use Redis;
  8. class SelectPayUser extends Command
  9. {
  10. /**
  11. * 执行命令 php artisan tmep:user_sign
  12. *
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'temp:select_user_pay';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = '查付费用户情况';
  24. /**
  25. * Execute the console command.
  26. *
  27. * @return mixed
  28. */
  29. public function handle()
  30. {
  31. print_r("======查付费用户情况 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
  32. Log::info("======查付费用户情况 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
  33. $date = date('Y-m-d');
  34. /**
  35. * update temp_pay_users,users set temp_pay_users.charge_balance = users.charge_balance where users.id = temp_pay_users.uid
  36. 清空 update temp_pay_users set bids = null,latest_read_at=null,to_do_date=null,to_do_fee=null
  37. */
  38. //2个月前付费用户id
  39. //更新付费用户实际余额
  40. print_r("=======更新付费用户实际余额 start=====");
  41. DB::update("update temp_pay_users,users set temp_pay_users.charge_balance = users.charge_balance,has_done = 0 where users.id = temp_pay_users.uid");
  42. print_r("=======更新付费用户实际余额 end=====");
  43. //查最近的阅读记录是否在1个月内没有记录
  44. //先更新阅读记录
  45. //选用户去除包年
  46. //$bids = [1697,1148,1345,1342,1340,1696,1311,1368,1475,1460,1900,1347,1014,1965,10,1005,1858,1580,1017,1550,1528,1634,1301,857,930,1625,1851,1458,1826,1021,1094,1695,1823,1792,1877,917,1903,1987,1685,1566,1406,1488,1878,1677,1852,1304,1905,1589,907,1924,901];
  47. $bids = [1697,1148,1345,1342,1340,1696,1311,1368,1475,1460,1900,1347,1014,1965,10,1005,1858,1580,1017,1550,1528,1634,1301,857,930,1625,1851,1458,1826,1021,1094,1695,1823,1792,1877,917,1903,1987,1685,1566,1406,1488,1878,1677,1852,1304,1905,1589,907,1924,901,636,613,614,640,633,632,630,631,689,1249,615,617,625,626,627,324,750,762,733,732,779,754,794,807,766,691,692,628,853,856,852,854,845,837,835,834,832,831,830,836,884,883,864,876,871,870,869,868,867,873,959,960,961,962,979,968,970,969,976,974,967];
  48. $day_max_fee = 500000;//日上限
  49. $day_user_max_fee = 1500;//单日用户最大消耗
  50. $day_user_min_fee = 200;//单日用户最小消耗
  51. $user_min_left_fee = 1;//用户剩余充值币
  52. //充值书币>1000
  53. $to_do_real_fee = 0;
  54. //uid黑名单
  55. $black_uids = DB::table('temp_pay_black_users')->pluck('uid')->all();
  56. DB::table('temp_pay_users')->where('charge_balance','>=',$user_min_left_fee)->whereNotIn('uid',$black_uids)->orderBy('done_num','asc')->chunk(1,function ($users) use ($bids,$day_user_max_fee,$day_user_min_fee,$day_max_fee,&$to_do_real_fee){
  57. foreach ($users as $user)
  58. {
  59. if($to_do_real_fee >= $day_max_fee)
  60. {
  61. dd('reach day max fee: '.$to_do_real_fee);
  62. }
  63. $read_record_info = Redis::hgetall('book_read:' . $user->uid);
  64. $last_record = isset($read_record_info['last_read']) ? explode('_',$read_record_info['last_read']) : null;
  65. $latest_read_at = $last_record ? $last_record[2] : 0;
  66. //判断最近一个月是否有阅读 有则continue 无阅读记录可能存储有误 刷过的用户不限制
  67. if(!$latest_read_at || (time() - $latest_read_at < 4*30*86400 && $user->done_num==0)) continue;
  68. shuffle($bids);//打乱bids顺序
  69. $read_bids = [];//阅读过的bid列表 todo
  70. foreach ($read_record_info as $k=>$_re)
  71. {
  72. is_numeric($k) && $read_bids[] = $k;
  73. }
  74. //查用户阅读记录里的图书是否有哪本书在书单内
  75. //如果有之前阅读中的图书则继续阅读
  76. foreach ($read_bids as $k=>$_bid)
  77. {
  78. $is_read_to_last = false;
  79. //判断是否订阅完毕 todo
  80. if(in_array($_bid,$bids) && !$is_read_to_last)
  81. {
  82. unset($read_bids[$k]);
  83. array_unshift($bids,$_bid);
  84. }
  85. }
  86. //实际刷的bid顺序为$bids
  87. $to_do_date = date('Y-m-d',time()+86400);
  88. $to_do_fee = min(mt_rand($day_user_min_fee,$day_user_max_fee),$user->charge_balance);//不超过总充值余额
  89. $latest_read_at = date('Y-m-d H:i:s',$latest_read_at);
  90. $bids = implode(',',$bids);
  91. $up_data = compact('bids','latest_read_at','to_do_date','to_do_fee');
  92. dump($up_data);
  93. if(DB::table('temp_pay_users')->where('id',$user->id)->update($up_data))
  94. {
  95. $to_do_real_fee += $to_do_fee;
  96. }
  97. }
  98. });
  99. Log::info("======查付费用户情况 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
  100. print_r("======查付费用户情况 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
  101. }
  102. }