LotteryCommand.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace App\Console\Commands\Welfare;
  3. use Illuminate\Console\Command;
  4. use App\Models\Welfare\TicketModel;
  5. use App\Models\Welfare\LotteryModel;
  6. class LotteryCommand extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'welfare:lottery';
  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. $drawed_at = mktime(20, 0, 0);
  37. $lottery = LotteryModel::where("drawed_at", $drawed_at)->first();
  38. if (!$lottery) {
  39. dd('活动不存在');
  40. }
  41. $tickets = TicketModel::where('lottery_id', $lottery->id)->get();
  42. $count = $tickets->count();
  43. if ($count < $lottery->real_count) {
  44. $tickets = $tickets->random($count);
  45. } else {
  46. $tickets = $tickets->random($lottery->real_count);
  47. }
  48. // 实际获奖名单
  49. foreach ($tickets as $ticket) {
  50. $ticket->update(['lucky' => 1]);
  51. }
  52. // 模拟用户获奖
  53. for ($i = $lottery->real_count; $i < $lottery->count; $i++) {
  54. TicketModel::create([
  55. 'uid' => rand(20000001, 20010000),
  56. 'type' => '领取',
  57. 'lottery_id' => $lottery->id,
  58. 'lucky' => 1,
  59. 'ticket_code' => getChars(6),
  60. ]);
  61. }
  62. }
  63. }