WelfareNextDay.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Modules\Welfare\Services\WelfarePriceSerivce;
  4. use Illuminate\Console\Command;
  5. use DB;
  6. class WelfareNextDay extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'welfare:nextday {--today}';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = '分销每天的抽奖次数';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return mixed
  33. */
  34. public function handle()
  35. {
  36. $today = $this->option('today');
  37. if($today){
  38. $this->start(true);
  39. }else{
  40. $this->start(false);
  41. }
  42. }
  43. private function start(bool $is_today):void{
  44. $account = $this->getOfficialAccount();
  45. if($account){
  46. foreach ($account as $v){
  47. $this->createRecord($v->channel_user_id,$v->count,$is_today);
  48. }
  49. }
  50. }
  51. /**
  52. * 获取公众号
  53. * @return mixed
  54. */
  55. private function getOfficialAccount(){
  56. $res = DB::table('official_accounts')->join('distribution_channels','official_accounts.distribution_channel_id','=','distribution_channels.id')
  57. ->groupBy('distribution_channels.channel_user_id')
  58. ->select(DB::raw('count(*) as `count`'),'distribution_channels.channel_user_id')
  59. ->where('official_accounts.is_auth',1)
  60. ->get();
  61. return $res;
  62. }
  63. /**
  64. * 计算抽奖次数
  65. * @param int $official_count
  66. * @return int
  67. */
  68. private function getCount(int $official_count):int{
  69. if($official_count <= 0){
  70. return 0;
  71. }
  72. if($official_count <= 3){
  73. return 1;
  74. }elseif ($official_count <= 9){
  75. return 2;
  76. }else{
  77. return 3;
  78. }
  79. }
  80. /**
  81. * 保存记录
  82. * @param int $channel_user_id
  83. * @param int $official_count
  84. */
  85. private function createRecord(int $channel_user_id,int $official_count,bool $is_today):void{
  86. //$count = $this->getCount($official_count);
  87. $count = 1;
  88. if($is_today){
  89. $date = date('Y-m-d');
  90. }else{
  91. $date = date('Y-m-d',time()+86400);
  92. }
  93. WelfarePriceSerivce::create([
  94. 'channel_user_id'=>$channel_user_id,
  95. 'date'=>$date,
  96. 'num'=>$count,
  97. 'left'=>$count,
  98. ]);
  99. }
  100. }