AgentAttrsJob.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 AgentAttrsJob 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. protected $date = '';
  29. protected $channelUid = null;
  30. /**
  31. * Create a new job instance.
  32. *
  33. * @return void
  34. */
  35. public function __construct(string $date, $channelUid = null)
  36. {
  37. $this->date = $date;
  38. $this->channelUid = $channelUid;
  39. }
  40. /**
  41. * Execute the job.
  42. *
  43. * @return void
  44. */
  45. public function handle()
  46. {
  47. $date = Carbon::parse($this->date)->toDateString();
  48. if (is_null($this->channelUid)) {
  49. // 统计全部.
  50. $result = self::runJob($date);
  51. } else {
  52. // 统计渠道
  53. $result = self::runJob($date, $this->channelUid);
  54. }
  55. }
  56. /**
  57. * 执行统计.
  58. * @param string $date 日期
  59. * @param null $channelUid 渠道UID
  60. * @return bool
  61. */
  62. protected function runJob($date, $channelUid = null)
  63. {
  64. $this->getRepo()->rootJob($date, $channelUid);
  65. // Todo.
  66. self::notify();
  67. return true;
  68. }
  69. /**
  70. * 发送通知, 重新读取数据然后拼接数据,发送邮箱或者tg.
  71. */
  72. private function notify()
  73. {
  74. // Todo 发送通知, 重新读取数据然后拼接数据,发送邮箱或者tg.
  75. }
  76. /**
  77. * 获取属性统计仓库.
  78. * @return StatisticsAgentAttrsRepository
  79. */
  80. private function getRepo()
  81. {
  82. if (!$this->repo instanceof StatisticsAgentAttrsRepository) {
  83. $this->repo = new StatisticsAgentAttrsRepository();
  84. }
  85. return $this->repo;
  86. }
  87. }