企微短剧业务系统

ActiveFansStat.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Log;
  4. use App\Models\ActiveFansData;
  5. use App\Models\DjOrder;
  6. use App\Models\OfficialAccount;
  7. use App\Models\TencentAdDailyReport;
  8. use Illuminate\Console\Command;
  9. class ActiveFansStat extends Command
  10. {
  11. protected $signature = 'ActiveFansStat {type?} {ref_date?}'; // type 1:按年抓取(date示例:2021) 2:按指定日期(date示例:2021-01-01)3:抓取昨日数据
  12. protected $description = '用户激活数据统计';
  13. protected $type;
  14. protected $dateTime;
  15. protected $activeDate = false;
  16. protected $limit = 50;
  17. protected $startId = 0;
  18. protected $appIdList = [];
  19. public function handle()
  20. {
  21. \DB::connection()->disableQueryLog();
  22. $this->info(date('m-d H:i:s') . ' 开始整理');
  23. $this->type = $this->argument('type') ? $this->argument('type') : 2;
  24. $this->dateTime = $this->argument('ref_date') ? $this->argument('ref_date') : null;
  25. # 获取需要统计的公众账号列表
  26. do{
  27. $this->info('起始ID:'.$this->startId);
  28. $this->appIdList = [];
  29. $accountList = OfficialAccount::selectRaw('id, mp_app_id as app_id')->where('enable', 1)->where('id', '>', $this->startId)
  30. ->limit($this->limit)->orderBy('id')
  31. ->get();
  32. if(empty($accountList))
  33. return false;
  34. $count = $accountList->count();
  35. $this->appIdList = $accountList->pluck('app_id');
  36. $this->startId = $accountList->max('id');
  37. $this->info('本次获取到的公众账号个数:'.$count);
  38. $this->getActiveDate();
  39. } while ($count == $this->limit);
  40. $this->info(date('m-d H:i:s') . ' 整理结束');
  41. return true;
  42. }
  43. /**
  44. * 获取粉丝激活时间
  45. * */
  46. private function getActiveDate()
  47. {
  48. try {
  49. if($this->type==1) {
  50. for ($month = 1; $month <= 12; $month++) {
  51. $days = getDaysByMonth($this->dateTime, $month);
  52. foreach ($days as $day) {
  53. if($day > date("Y-m-d"))
  54. continue 2;
  55. $this->activeDate = $day;
  56. $this->stat();
  57. }
  58. }
  59. } elseif ($this->type==2) {
  60. if($this->activeDate === false) {
  61. if(is_null($this->dateTime)) {
  62. $this->activeDate = date('Y-m-d');
  63. } else {
  64. $this->activeDate = $this->dateTime;
  65. }
  66. }
  67. $this->stat();
  68. } elseif($this->type==3) {
  69. $this->activeDate = date('Y-m-d', strtotime('-1 days'));
  70. $this->stat();
  71. }
  72. } catch(\Exception $e) {
  73. Log::logError('统计日期处理发生异常', [
  74. 'msg' => $e->getMessage(),
  75. 'line' => $e->getLine()
  76. ], 'ActiveFansStat');
  77. }
  78. return true;
  79. }
  80. /**
  81. * 废弃
  82. * */
  83. private function stat()
  84. {
  85. $days = ActiveFansData::ACTIVATE_FANS_RECORD_DAYS;
  86. $this->info('统计日期:'.$this->activeDate);
  87. foreach ($this->appIdList as $appId) {
  88. # 获取该账号在近{$days}天的投放金额
  89. $paidData = TencentAdDailyReport::select(['ref_date', 'paid', 'app_id'])->where('app_id', $appId)
  90. ->where('ref_date', '>=', date("Y-m-d", strtotime('-'.$days. ' days', strtotime($this->activeDate))))
  91. ->where('ref_date', '<=', $this->activeDate)
  92. ->where('enable', 1)
  93. ->get();
  94. for($i=0; $i<$days; $i++) {
  95. $expenseDate = date("Y-m-d", strtotime('-'.$i. ' days', strtotime($this->activeDate)));
  96. $dayPaidInfo = $paidData->where('ref_date', $expenseDate)->first();
  97. $dayPaid = isset($dayPaidInfo->paid) ? $dayPaidInfo->paid : 0;
  98. // $this->info('投放日期:'.$expenseDate);
  99. $chargeData = DjOrder::getActiveFansData($appId, $this->activeDate, $expenseDate);
  100. $activeFans = $chargeData->where('paid_at', '>=', strtotime($this->activeDate . ' 00:00:00') * 1000)
  101. ->where('paid_at', '<=', strtotime($this->activeDate . ' 23:59:59') * 1000)
  102. ->count();
  103. # 获取统计日期对应的新用户充值金额 充值次数
  104. $payInfo = DjOrder::where('bind_app_id', $appId)
  105. ->where('order_pay_time', '>=', strtotime($this->activeDate.' 00:00:00') * 1000)
  106. ->where('order_pay_time', '<=', strtotime($this->activeDate.' 23:59:59') * 1000)
  107. ->where('mp_user_register_time', '>=', strtotime($expenseDate.' 00:00:00') * 1000)
  108. ->where('mp_user_register_time', '<=', strtotime($expenseDate.' 23:59:59') * 1000)
  109. ->where('pay_status', 1)
  110. ->where('is_ad_user', 1)
  111. ->selectRaw('sum(pay_money) as pay_money, count(1) as pay_count')
  112. ->first();
  113. $payMoney = $payInfo->pay_money ?? 0;
  114. $payCount = $payInfo->pay_count ?? 0;
  115. $payMoneyTotal = DjOrder::where('bind_app_id', $appId)
  116. ->where('order_pay_time', '>=', strtotime($expenseDate.' 00:00:00') * 1000)
  117. ->where('order_pay_time', '<=', strtotime($this->activeDate.' 23:59:59') * 1000)
  118. ->where('mp_user_register_time', '>=', strtotime($expenseDate.' 00:00:00') * 1000)
  119. ->where('mp_user_register_time', '<=', strtotime($expenseDate.' 23:59:59') * 1000)
  120. ->where('pay_status', 1)
  121. ->where('is_ad_user', 1)
  122. ->sum('pay_money');
  123. $insertData = [
  124. 'paid' => $dayPaid,
  125. 'active_fans' => $activeFans,
  126. 'pay_money' => intval($payMoney / 100),
  127. 'pay_money_total' => intval($payMoneyTotal / 100),
  128. 'pay_count' => $payCount
  129. ];
  130. ActiveFansData::updateOrCreate([
  131. 'app_id' => $appId, 'expense_date' => $expenseDate, 'ref_date' => $this->activeDate
  132. ], $insertData);
  133. }
  134. }
  135. }
  136. }