ActiveChannelJob.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace App\Jobs\Statistics;
  3. use App\Models\Mysql\StatisticsActiveModel;
  4. use App\Models\Mysql\StatisticsPayModel;
  5. use App\Repositories\Statistics\StatisticsActiveRepository;
  6. use Carbon\Carbon;
  7. use Illuminate\Bus\Queueable;
  8. use Illuminate\Contracts\Queue\ShouldQueue;
  9. use Illuminate\Foundation\Bus\Dispatchable;
  10. use Illuminate\Queue\InteractsWithQueue;
  11. use Illuminate\Queue\SerializesModels;
  12. use App\Models\Mysql\ChannelModel;
  13. class ActiveChannelJob implements ShouldQueue
  14. {
  15. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  16. /**
  17. * 定义仓库.
  18. * @var null|StatisticsActiveRepository
  19. */
  20. protected $repo = null;
  21. /**
  22. * 日期.
  23. * @var string
  24. */
  25. protected $date = '';
  26. protected $channelUid = null;
  27. /**
  28. * Create a new job instance.
  29. *
  30. * @return void
  31. */
  32. public function __construct(string $date, $channelUid = null)
  33. {
  34. $this->date = $date;
  35. $this->channelUid = $channelUid;
  36. }
  37. /**
  38. * Execute the job.
  39. *
  40. * @return void
  41. */
  42. public function handle()
  43. {
  44. $date = Carbon::parse($this->date)->toDateString();
  45. if (is_null($this->channelUid)) {
  46. // 统计全部.
  47. $result = self::runJob($date);
  48. } else {
  49. // 统计渠道或者官方
  50. $result = self::runJob($date, $this->channelUid);
  51. }
  52. }
  53. /**
  54. * 执行统计.
  55. * @param string $date 日期
  56. * @param null $channelUid 渠道UID
  57. * @return bool
  58. */
  59. protected function runJob($date, $channelUid = null)
  60. {
  61. $date = Carbon::parse($date)->toDateString();
  62. // 统计全部.
  63. $total = $this->getRepo()->countOneDayActive($date, $channelUid);
  64. $android = $this->getRepo()->countOneDayActive($date, $channelUid, 'A');
  65. $ios = $this->getRepo()->countOneDayActive($date, $channelUid, 'I');
  66. $h5 = $this->getRepo()->countOneDayActive($date, $channelUid, 'H');
  67. $item = [
  68. 'date' => $date,
  69. 'total' => $total,
  70. 'android' => $android,
  71. 'ios' => $ios,
  72. 'h5' => $h5,
  73. ];
  74. $sqlChannelUid = '';
  75. $item['mom_app_uid'] = ''; // 预先设置母包UID
  76. $item['agent_uid'] = ''; // 预先定义代理UID
  77. if (!is_null($channelUid)) {
  78. $item['channel_uid'] = $channelUid;
  79. $sqlChannelUid = $channelUid;
  80. if (!empty($item['channel_uid'])) {
  81. $channelObj = ChannelModel::where(['uid' => $item['channel_uid']])->first();
  82. if (!empty($channelObj)) {
  83. // 补充母包UID
  84. $item['mom_app_uid'] = $channelObj->mom_app_uid;
  85. $item['agent_uid'] = $channelObj->agent_uid;
  86. }
  87. }
  88. $info = StatisticsActiveModel::whereDate('date', '=', $date)
  89. ->where('channel_uid', '=', $sqlChannelUid)
  90. ->first();
  91. } else {
  92. $info = StatisticsActiveModel::whereDate('date', '=', $date)
  93. ->whereNull('channel_uid')
  94. ->where('mom_app_uid', '=', '')
  95. ->first();
  96. }
  97. if (!empty($info)) {
  98. $info->total = $total;
  99. $info->android = $android;
  100. $info->ios = $ios;
  101. $info->h5 = $h5;
  102. $result = $info->save();
  103. } else {
  104. $result = StatisticsActiveModel::create($item);
  105. }
  106. // Todo.
  107. self::notify();
  108. return boolval($result);
  109. }
  110. /**
  111. * 发送通知, 重新读取数据然后拼接数据,发送邮箱或者tg.
  112. */
  113. private function notify()
  114. {
  115. // Todo 发送通知, 重新读取数据然后拼接数据,发送邮箱或者tg.
  116. }
  117. /**
  118. * 获取仓库.
  119. * @return StatisticsActiveRepository
  120. */
  121. private function getRepo()
  122. {
  123. if (!$this->repo instanceof StatisticsActiveRepository) {
  124. $this->repo = new StatisticsActiveRepository();
  125. }
  126. return $this->repo;
  127. }
  128. }