AgentAttrsTodayJob.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * 今日的累计.
  4. *
  5. * @author nj
  6. * @date 2021-08-02 12:21
  7. */
  8. namespace App\Jobs\Statistics;
  9. use App\Repositories\Statistics\StatisticsAgentAttrsRepository;
  10. use Carbon\Carbon;
  11. use Illuminate\Bus\Queueable;
  12. use Illuminate\Contracts\Queue\ShouldQueue;
  13. use Illuminate\Foundation\Bus\Dispatchable;
  14. use Illuminate\Queue\InteractsWithQueue;
  15. use Illuminate\Queue\SerializesModels;
  16. class AgentAttrsTodayJob implements ShouldQueue
  17. {
  18. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  19. /**
  20. * 定义仓库.
  21. * @var null|StatisticsAgentAttrsRepository
  22. */
  23. protected $repo = null;
  24. /**
  25. * 指定队列.
  26. * @var string
  27. */
  28. // public $queue = 'attrs_today';
  29. /**
  30. * 日期.
  31. * @var string
  32. */
  33. protected $date = '';
  34. protected $channelUid = null;
  35. /**
  36. * Create a new job instance.
  37. *
  38. * @return void
  39. */
  40. public function __construct(string $date, $channelUid = null)
  41. {
  42. $this->date = $date;
  43. $this->channelUid = $channelUid;
  44. $this->onQueue('attrs_today');
  45. }
  46. /**
  47. * Execute the job.
  48. *
  49. * @return void
  50. */
  51. public function handle()
  52. {
  53. $date = Carbon::parse($this->date)->toDateString();
  54. if (is_null($this->channelUid)) {
  55. // 统计全部.
  56. $result = self::runJob($date);
  57. } else {
  58. // 统计渠道
  59. $result = self::runJob($date, $this->channelUid);
  60. }
  61. }
  62. /**
  63. * 执行统计.
  64. * @param string $date 日期
  65. * @param null $channelUid 渠道UID
  66. * @return bool
  67. */
  68. protected function runJob($date, $channelUid = null)
  69. {
  70. $this->getRepo()->todayJob($date, $channelUid);
  71. // Todo.
  72. self::notify();
  73. return true;
  74. }
  75. /**
  76. * 发送通知, 重新读取数据然后拼接数据,发送邮箱或者tg.
  77. */
  78. private function notify()
  79. {
  80. // Todo 发送通知, 重新读取数据然后拼接数据,发送邮箱或者tg.
  81. }
  82. /**
  83. * 获取属性统计仓库.
  84. * @return StatisticsAgentAttrsRepository
  85. */
  86. private function getRepo()
  87. {
  88. if (!$this->repo instanceof StatisticsAgentAttrsRepository) {
  89. $this->repo = new StatisticsAgentAttrsRepository();
  90. }
  91. return $this->repo;
  92. }
  93. }