NewUsersChannelJob.php 5.1 KB

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