企微短剧业务系统

OrderSummaryStat.php 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Log;
  4. use App\Models\OfficialAccount;
  5. use App\Models\OfficialWebUserActionSetId;
  6. use App\Models\OrderSummary;
  7. use App\Service\OrderSummaryService;
  8. use Illuminate\Console\Command;
  9. class OrderSummaryStat extends Command
  10. {
  11. protected $signature = 'OrderSummaryStat {type?} {ref_date?}'; // type 1:按年抓取(date示例:2021) 2:按指定日期(date示例:2021-01-01)3:抓取昨日数据
  12. protected $description = '订单每日概要数据统计';
  13. protected $type;
  14. protected $dateTime;
  15. protected $date = false;
  16. protected $limit = 50;
  17. protected $startId = 0;
  18. protected $appIdList = [];
  19. protected $adqAccountList = [];
  20. public function handle()
  21. {
  22. \DB::connection()->disableQueryLog();
  23. $time = date('m-d H:i:s');
  24. $this->info($time . ' 开始整理');
  25. $this->type = $this->argument('type') ? $this->argument('type') : 2;
  26. $this->dateTime = $this->argument('ref_date') ? $this->argument('ref_date') : null;
  27. // mp账号数据
  28. $this->mpData();
  29. // adq 账号数据
  30. $this->adqData();
  31. $this->info($time . ' 整理结束');
  32. return true;
  33. }
  34. private function mpData()
  35. {
  36. # 获取需要统计的公众账号列表
  37. do{
  38. $this->info('mp账号起始ID:'.$this->startId);
  39. $accountList = OfficialAccount::query()->selectRaw('id, mp_app_id as app_id')->where('enable', 1)
  40. ->where('id', '>', $this->startId)->limit($this->limit)->orderBy('id')->get();
  41. if(empty($accountList))
  42. break;
  43. $count = $accountList->count();
  44. $this->appIdList = $accountList->pluck('app_id');
  45. $this->startId = $accountList->max('id');
  46. $this->info('本次获取到的公众账号个数:'.$count);
  47. $this->getRefDate(1);
  48. } while ($count == $this->limit);
  49. }
  50. private function adqData()
  51. {
  52. # 获取需要统计的adq账号列表
  53. $this->startId = 0;
  54. do{
  55. $this->info('adq账号起始ID:'.$this->startId);
  56. $adqAccountList = OfficialWebUserActionSetId::query()->selectRaw('id, account_id')->where('enable', 1)
  57. ->where('id', '>', $this->startId)->limit($this->limit)->orderBy('id')->get();
  58. if(empty($adqAccountList))
  59. break;
  60. $adqCount = $adqAccountList->count();
  61. $this->adqAccountList = $adqAccountList->pluck('account_id');
  62. $this->startId = $adqAccountList->max('id');
  63. $this->info('本次获取到的adq账号个数:'.$adqCount);
  64. $this->getRefDate(2);
  65. } while ($adqCount == $this->limit);
  66. }
  67. private function getRefDate($type)
  68. {
  69. try {
  70. if($this->type==1) {
  71. for ($month = 1; $month <= 12; $month++) {
  72. $days = getDaysByMonth($this->dateTime, $month);
  73. foreach ($days as $day) {
  74. if($day > date("Y-m-d"))
  75. continue 2;
  76. $this->date = $day;
  77. if(1 == $type) {
  78. $this->mpStat();
  79. } else {
  80. $this->adqStat();
  81. }
  82. }
  83. }
  84. } elseif ($this->type==2) {
  85. if($this->date === false) {
  86. if(is_null($this->dateTime)) {
  87. $this->date = date('Y-m-d');
  88. } else {
  89. $this->date = $this->dateTime;
  90. }
  91. }
  92. if(1 == $type) {
  93. $this->mpStat();
  94. } else {
  95. $this->adqStat();
  96. }
  97. } elseif($this->type==3) {
  98. $this->date = date('Y-m-d', strtotime('-1 days'));
  99. if(1 == $type) {
  100. $this->mpStat();
  101. } else {
  102. $this->adqStat();
  103. }
  104. }
  105. } catch(\Exception $e) {
  106. Log::logError('统计日期处理发生异常', [
  107. 'msg' => $e->getMessage(),
  108. 'line' => $e->getLine()
  109. ], 'AccountGetWithOrderSummary');
  110. }
  111. return true;
  112. }
  113. private function mpStat()
  114. {
  115. $this->info($this->date);
  116. # 按平台从订单表中获取某个公众号某一天的
  117. $this->info('需要统计的账号列表为:'.json_encode($this->appIdList).'【'.$this->date.'】');
  118. $statData = OrderSummaryService::mpStatByDate($this->appIdList, $this->date);
  119. # 数据写入到dj_order_summary表
  120. foreach ($statData as $datum) {
  121. $orderSource = $datum->order_source ?? '';
  122. $appId = $datum->bind_app_id ?? '';
  123. $corpid = $datum->corpid ?? '';
  124. if(empty($orderSource) || empty($appId) || empty($corpid)) continue;
  125. $insertData = [
  126. 'order_count' => $datum->order_count ?? 0,
  127. 'pay_uv' => $datum->pay_uv ?? 0,
  128. 'pay_pv' => $datum->pay_pv ?? 0,
  129. 'pay_money' => $datum->pay_money ? $datum->pay_money / 100 : 0,
  130. ];
  131. $result = OrderSummary::query()->updateOrCreate([
  132. 'order_source' => $orderSource, 'app_id' => $appId, 'ref_date' => $this->date, 'corpid' => $corpid
  133. ] , $insertData);
  134. if(!$result) {
  135. Log::logError('订单数据统计结果更新至表失败', $datum, 'AccountGetWithOrderSummary');
  136. }
  137. }
  138. return true;
  139. }
  140. private function adqStat()
  141. {
  142. $this->info($this->date);
  143. # 按平台从订单表中获取某个公众号某一天的
  144. $this->info('需要统计的账号列表为:'.json_encode($this->adqAccountList).'【'.$this->date.'】');
  145. $statData = OrderSummaryService::adqStatByDate($this->adqAccountList, $this->date);
  146. # 数据写入到dj_order_summary表
  147. foreach ($statData as $datum) {
  148. $orderSource = $datum->order_source ?? '';
  149. $accountId = $datum->adq_account_id ?? '';
  150. $corpid = $datum->corpid ?? '';
  151. if(empty($orderSource) || empty($accountId) || empty($corpid)) continue;
  152. $insertData = [
  153. 'order_count' => $datum->order_count ?? 0,
  154. 'pay_uv' => $datum->pay_uv ?? 0,
  155. 'pay_pv' => $datum->pay_pv ?? 0,
  156. 'pay_money' => $datum->pay_money ? $datum->pay_money / 100 : 0,
  157. ];
  158. $result = OrderSummary::query()->updateOrCreate([
  159. 'order_source' => $orderSource, 'account_id' => $accountId, 'ref_date' => $this->date, 'corpid' => $corpid
  160. ] , $insertData);
  161. if(!$result) {
  162. Log::logError('订单数据统计结果更新至表失败', $datum, 'AccountGetWithOrderSummary');
  163. }
  164. }
  165. return true;
  166. }
  167. }