Keine Beschreibung

DailyCostDataStat.php 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\JxCostDailyData;
  4. use App\Models\JxQtaskLiveOrderList;
  5. use App\Models\JxQtaskVideoFlowOrderList;
  6. use App\Models\JxQtaskVideoOrderList;
  7. use App\Models\JxStarLiveFlowOrderList;
  8. use App\Models\JxStarLiveOrderList;
  9. use App\Models\JxStarVideoFlowOrderList;
  10. use App\Models\JxStarVideoOrderList;
  11. use App\Models\Sys\SysCustomerAdver;
  12. use App\Support\Log;
  13. use Illuminate\Console\Command;
  14. class DailyCostDataStat extends Command
  15. {
  16. /**
  17. * The name and signature of the console command.
  18. *
  19. * @var string
  20. */
  21. protected $signature = 'DailyCostDataStat';
  22. /**
  23. * The console command description.
  24. *
  25. * @var string
  26. */
  27. protected $description = '每日消耗数据统计';
  28. protected $runDays = 95;
  29. /**
  30. * Create a new command instance.
  31. *
  32. * @return void
  33. */
  34. public function __construct()
  35. {
  36. parent::__construct();
  37. }
  38. /**
  39. * Execute the console command.
  40. *
  41. * @return int
  42. */
  43. public function handle()
  44. {
  45. $this->info(date('H:i') . ' 开始执行');
  46. $this->start();
  47. $this->info(date('H:i') . ' 结束执行');
  48. return 0;
  49. }
  50. public function start()
  51. {
  52. $stDate = date('Y-m-d', strtotime("-{$this->runDays} day"));
  53. $enDate = date('Y-m-d', strtotime("-1 day"));
  54. $starVideoCostList = self::getDailyCostData(
  55. JxStarVideoOrderList::query(), 'star_release_time', 'real_amount', $stDate, $enDate
  56. );
  57. # 星直播
  58. $starLiveCostList = self::getDailyCostData(
  59. JxStarLiveOrderList::query(), 'live_start_time', 'real_amount', $stDate, $enDate
  60. );
  61. # 星任务-视频
  62. $qtaskVideoCostList = self::getDailyCostData(
  63. JxQtaskVideoOrderList::query(), 'star_release_time', 'amount', $stDate, $enDate
  64. );
  65. # 星任务-直播
  66. $qtaskLiveCostList = self::getDailyCostData(
  67. JxQtaskLiveOrderList::query(), 'star_release_time', 'amount', $stDate, $enDate
  68. );
  69. # 流量助推-星视频
  70. $starVideoFlowCostList = self::getDailyCostData(
  71. JxStarVideoFlowOrderList::query(), 'promotion_begin_time', 'consume_amount', $stDate, $enDate
  72. );
  73. # 流量助推-星直播
  74. $starLiveFlowCostList = self::getDailyCostData(
  75. JxStarLiveFlowOrderList::query(), 'promotion_begin_time', 'consume_amount', $stDate, $enDate
  76. );
  77. # 流量助推-星任务视频
  78. $qtaskVideoFlowCostList = self::getDailyCostData(
  79. JxQtaskVideoFlowOrderList::query(), 'promotion_begin_time', 'consume_amount', $stDate, $enDate
  80. );
  81. # 处理数据
  82. $advertiserIds = [];
  83. $dailyCostData = [];
  84. array_map(function (
  85. $starVideoCost, $starLiveCost, $qtaskVideoCost, $qtaskLiveCost,
  86. $starVideoFlowCost, $starLiveFlowCost, $qtaskVideoFlowCost
  87. ) use (&$advertiserIds, &$dailyCostData) {
  88. if (!empty($starVideoCost)) $this->dealDailyCostData($starVideoCost, 'star_video_cost', $advertiserIds, $dailyCostData);
  89. if (!empty($starLiveCost)) $this->dealDailyCostData($starLiveCost, 'star_live_cost', $advertiserIds, $dailyCostData);
  90. if (!empty($qtaskVideoCost)) $this->dealDailyCostData($qtaskVideoCost, 'qtask_video_cost', $advertiserIds, $dailyCostData);
  91. if (!empty($qtaskLiveCost)) $this->dealDailyCostData($qtaskLiveCost, 'qtask_live_cost', $advertiserIds, $dailyCostData);
  92. if (!empty($starVideoFlowCost)) $this->dealDailyCostData($starVideoFlowCost, 'flow_star_video_cost', $advertiserIds, $dailyCostData);
  93. if (!empty($starLiveFlowCost)) $this->dealDailyCostData($starLiveFlowCost, 'flow_star_live_cost', $advertiserIds, $dailyCostData);
  94. if (!empty($qtaskVideoFlowCost)) $this->dealDailyCostData($qtaskVideoFlowCost, 'flow_qtask_video_cost', $advertiserIds, $dailyCostData);
  95. },
  96. $starVideoCostList, $starLiveCostList, $qtaskVideoCostList, $qtaskLiveCostList,
  97. $starVideoFlowCostList, $starLiveFlowCostList, $qtaskVideoFlowCostList
  98. );
  99. # 数据入库
  100. if (!empty($dailyCostData)) {
  101. $customerAdverMap = SysCustomerAdver::query()
  102. ->whereIn('advertiser_id', $advertiserIds)
  103. ->where('enable', 1)
  104. ->pluck('customer_id', 'advertiser_id');
  105. ksort($dailyCostData);
  106. $this->indbDailyCostDate($customerAdverMap, $dailyCostData);
  107. }
  108. }
  109. protected function getDailyCostData($query, $timeField, $sumField, $stDate, $enDate)
  110. {
  111. $list = $query
  112. ->selectRaw("advertiser_id, DATE_FORMAT({$timeField}, '%Y-%m-%d') AS day")
  113. ->selectRaw("SUM({$sumField}) AS cost")
  114. ->whereBetween($timeField, [$stDate.' 00:00:00', $enDate.' 23:59:59'])
  115. ->where('enable', 1)
  116. ->groupBy(['advertiser_id', 'day'])
  117. ->get();
  118. if ($list->isEmpty()) return [];
  119. return $list->toArray();
  120. }
  121. protected function dealDailyCostData($costData, $costField, &$advertiserIds, &$dailyCostData)
  122. {
  123. if (!in_array($costData['advertiser_id'], $advertiserIds)) $advertiserIds[] = $costData['advertiser_id'];
  124. if (!isset($dailyCostData[$costData['day']][$costData['advertiser_id']][$costField])) {
  125. $dailyCostData[$costData['day']][$costData['advertiser_id']][$costField] = $costData['cost'];
  126. } else {
  127. $dailyCostData[$costData['day']][$costData['advertiser_id']][$costField] += $costData['cost'];
  128. }
  129. }
  130. protected function indbDailyCostDate($customerAdverMap, $dailyCostData)
  131. {
  132. foreach ($dailyCostData as $day => $advData) {
  133. foreach ($advData as $advertiserId => $datum) {
  134. JxCostDailyData::query()
  135. ->updateOrInsert([
  136. 'day' => $day,
  137. 'advertiser_id' => $advertiserId
  138. ], [
  139. 'customer_id' => $customerAdverMap->get($advertiserId) ?? null,
  140. 'star_video_cost' => $datum['star_video_cost'] ?? 0,
  141. 'star_live_cost' => $datum['star_live_cost'] ?? 0,
  142. 'qtask_video_cost' => $datum['qtask_video_cost'] ?? 0,
  143. 'qtask_live_cost' => $datum['qtask_live_cost'] ?? 0,
  144. 'flow_star_video_cost' => $datum['flow_star_video_cost'] ?? 0,
  145. 'flow_star_live_cost' => $datum['flow_star_live_cost'] ?? 0,
  146. 'flow_qtask_video_cost' => $datum['flow_qtask_video_cost'] ?? 0,
  147. ]);
  148. // Log::info('每日消耗数据离线更新', ['day' => $day, 'advertiser_id' => $advertiserId], 'DailyCostDataStat');
  149. }
  150. }
  151. }
  152. }