NewUserStatistic.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. namespace App\Console\Commands\Trade;
  3. use App\Modules\Trade\Models\NewUserChargeStatistic;
  4. use App\Modules\Trade\Models\Order;
  5. use App\Modules\User\Models\User;
  6. use Illuminate\Console\Command;
  7. use Illuminate\Support\Collection;
  8. use Illuminate\Support\Facades\Log;
  9. use stdClass;
  10. class NewUserStatistic extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'new_user_charge_statistic {--begin_date=} {--end_date=} {--date=}';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = '新注册用户充值统计';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return mixed
  37. */
  38. public function handle()
  39. {
  40. print_r("======新注册用户充值统计 【任务执行开始】=====" . date("y-m-d H:i:s" . "\n"));
  41. Log::info("======新注册用户充值统计 【任务执行开始】=====" . date("y-m-d H:i:s" . "\n"));
  42. ini_set('memory_limit', '256M');
  43. $begin_date = $this->option('begin_date');
  44. $end_date = $this->option('end_date');
  45. if ($begin_date) {
  46. $date = $begin_date;
  47. while (strtotime($date) < strtotime($end_date)) {
  48. Log::info("======新注册用户充值统计 【任务执行中】===== 日期:" . $date . ' ' . date("y-m-d H:i:s" . "\n"));
  49. print_r("======新注册用户充值统计 【任务执行中】===== 日期:" . $date . ' ' . date("y-m-d H:i:s" . "\n"));
  50. $this->statistic($date);
  51. $date = date('Y-m-d', strtotime('+1 days', strtotime($date)));
  52. }
  53. } else {
  54. $date = $this->option('date');
  55. $date = $date ? $date : date('Y-m-d', strtotime('-1 days'));
  56. $this->statistic($date);
  57. }
  58. print_r("======新注册用户充值统计 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
  59. Log::info("======新注册用户充值统计 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
  60. }
  61. /**
  62. * 新用户数据统计
  63. * @param string $date 统计日期
  64. */
  65. public function statistic(string $date)
  66. {
  67. $statistic_info = $this->runSql($date);
  68. $collections = collect($statistic_info);
  69. $diff_days = [0, 1, 2, 3, 4, 5, 6];
  70. $days_items = $collections->whereIn('diff_day', $diff_days)->all();
  71. foreach ($days_items as $item) {
  72. $this->saveStatistic($item, $item->register_date, $item->diff_day);
  73. }
  74. $acc_days = [7, 14, 29];
  75. foreach ($acc_days as $day) {
  76. $this->saveStatisticAccumulate($collections, $day);
  77. }
  78. }
  79. public function runSql(string $date)
  80. {
  81. $end_date = date('Y-m-d', strtotime('+1 days', strtotime($date)));
  82. return Order::join('users', 'orders.uid', 'users.id')
  83. ->where('orders.created_at', '>=', $date)
  84. ->where('orders.created_at', '<', $end_date)
  85. ->where('status', 'PAID')
  86. ->selectRaw("orders.distribution_channel_id as channel_id,sum(price) as amount,
  87. timestampdiff(day,DATE_FORMAT(users.created_at, '%Y-%m-%d'),DATE_FORMAT(orders.created_at, '%Y-%m-%d')) as diff_day,
  88. DATE_FORMAT(users.created_at, '%Y-%m-%d') as register_date,
  89. count(distinct uid) as users,count(orders.id) as count")
  90. ->groupBy('channel_id', 'register_date')
  91. ->get();
  92. }
  93. /**
  94. * 保存统计数据
  95. * @param string $date 统计日期
  96. * @param Collection $collect 统计数据
  97. * @param int $day 第几天
  98. */
  99. private function saveStatistic($item, string $date, int $day)
  100. {
  101. $params = [];
  102. switch ($day) {
  103. case 0:
  104. $params = [
  105. 'first_day_count' => $item->count,
  106. 'first_day_amount' => $item->amount,
  107. 'first_day_users' => $item->users,
  108. ];
  109. break;
  110. case 1:
  111. $params = [
  112. 'second_day_count' => $item->count,
  113. 'second_day_amount' => $item->amount,
  114. 'second_day_users' => $item->users,
  115. ];
  116. break;
  117. case 2:
  118. $params = [
  119. 'third_day_count' => $item->count,
  120. 'third_day_amount' => $item->amount,
  121. 'third_day_users' => $item->users,
  122. ];
  123. break;
  124. case 3:
  125. $params = [
  126. 'forth_day_count' => $item->count,
  127. 'forth_day_amount' => $item->amount,
  128. 'forth_day_users' => $item->users,
  129. ];
  130. break;
  131. case 4:
  132. $params = [
  133. 'fifth_day_count' => $item->count,
  134. 'fifth_day_amount' => $item->amount,
  135. 'fifth_day_users' => $item->users,
  136. ];
  137. break;
  138. case 5:
  139. $params = [
  140. 'sixth_day_count' => $item->count,
  141. 'sixth_day_amount' => $item->amount,
  142. 'sixth_day_users' => $item->users,
  143. ];
  144. break;
  145. case 6:
  146. $params = [
  147. 'seventh_day_count' => $item->count,
  148. 'seventh_day_amount' => $item->amount,
  149. 'seventh_day_users' => $item->users,
  150. ];
  151. break;
  152. }
  153. NewUserChargeStatistic::updateOrCreate(
  154. [
  155. 'date' => $date,
  156. 'channel_id' => $item->channel_id,
  157. ],
  158. $params
  159. );
  160. }
  161. /**
  162. * 保存累计充值统计数据(不能重复跑)
  163. */
  164. private function saveStatisticAccumulate(Collection $collections, int $day)
  165. {
  166. switch ($day) {
  167. case 7:
  168. $items = $collections->where('diff_day', '>=', 7)->where('diff_day', '<', 30);
  169. break;
  170. case 14:
  171. case 29:
  172. $items = $collections->where('diff_day', '<=', $day);
  173. break;
  174. }
  175. switch ($day) {
  176. case 7:
  177. $columns = [
  178. 'count',
  179. 'amount',
  180. 'users',
  181. ];
  182. break;
  183. case 14:
  184. $columns = [
  185. '15_day_count',
  186. '15_day_amount',
  187. '15_day_users',
  188. ];
  189. break;
  190. case 29:
  191. $columns = [
  192. '30_day_count',
  193. '30_day_amount',
  194. '30_day_users',
  195. ];
  196. break;
  197. }
  198. $items->each(function ($item) use ($columns) {
  199. $info = NewUserChargeStatistic::where(
  200. [
  201. 'date' => $item->register_date,
  202. 'channel_id' => $item->channel_id,
  203. ]
  204. )->select(
  205. 'id',
  206. 'date',
  207. 'channel_id',
  208. $columns[0],
  209. $columns[1],
  210. $columns[2]
  211. )->first();
  212. if ($info) {
  213. $info[$columns[0]] += $item->count;
  214. $info[$columns[1]] += $item->amount;
  215. $info[$columns[2]] += $item->users;
  216. $info->save();
  217. } else {
  218. $data = [
  219. 'date' => $item->register_date,
  220. 'channel_id' => $item->channel_id,
  221. ];
  222. $data[$columns[0]] = $item->count;
  223. $data[$columns[1]] = $item->amount;
  224. $data[$columns[2]] = $item->users;
  225. NewUserChargeStatistic::create($data);
  226. }
  227. });
  228. }
  229. }