PromotionDayCharge.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace App\Console\Commands\Statistic;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Support\Arr;
  5. use Illuminate\Support\Facades\DB;
  6. class PromotionDayCharge extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'Statistic:PromotionDayCharge {--date= : 统计日期}';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = '推广充值日统计';
  20. /**
  21. * Execute the console command.
  22. */
  23. public function handle(): void
  24. {
  25. $date = $this->option('date') ?? date('Y-m-d', strtotime('yesterday'));
  26. $optimizerDayData = [];
  27. DB::table('orders')
  28. ->whereBetween('created_at', [
  29. $date, $date. ' 23:59:59'
  30. ])->where('promotion_id', '<>', 0)
  31. ->distinct()
  32. ->select('promotion_id')
  33. ->orderBy('promotion_id')
  34. ->chunk(100, function ($items) use ($date, &$optimizerDayData){
  35. $promotionData = [];
  36. $now = date('Y-m-d H:i:s');
  37. $promotions = DB::table('promotions')->whereIn('id', $items->pluck('promotion_id'))
  38. ->select('id', 'uid', 'miniprogram_id')
  39. ->get()->keyBy('id');
  40. foreach ($items as $item) {
  41. $promotionDayCharge = $this->promotionDayCharge($item->promotion_id, $date);
  42. $newUserCharge = $this->newUserCharge($item->promotion_id, $date);
  43. $chargeInfo = $this->getPromotionData($promotionDayCharge, $newUserCharge);
  44. $promotionData[] = array_merge( $chargeInfo, [
  45. 'promotion_id' => $item->promotion_id, 'date' => $date,
  46. 'created_at' => $now, 'updated_at' => $now,
  47. ]);
  48. $promotion = $promotions->get($item->promotion_id);
  49. if(!$promotion) {
  50. myLog('PromotionDayCharge')->error('订单中有的推广id,但是推广信息表中没有', ['promotion_id' => $item->promotion_id]);
  51. continue;
  52. }
  53. $key = $promotion->uid . '-'. $promotion->miniprogram_id;
  54. foreach ($chargeInfo as $k => $v) {
  55. $optimizerDayData[$key][$k] = $optimizerDayData[$key][$k] ?? 0 + $v;
  56. $optimizerDayData[$key]['user_id'] = $promotion->uid;
  57. $optimizerDayData[$key]['miniprogram_id'] = $promotion->miniprogram_id;
  58. $optimizerDayData[$key]['date'] = $date;
  59. $optimizerDayData[$key]['created_at'] = $now;
  60. $optimizerDayData[$key]['updated_at'] = $now;
  61. }
  62. }
  63. // DB::table('tj_promotion_day_charge')
  64. // ->insert($promotionData);
  65. dump($promotionData);
  66. });
  67. foreach (collect($optimizerDayData)->chunk(100) as $items) {
  68. // DB::table('tj_optimizer_day_charge')
  69. // ->insert($items->values()->toArray());
  70. dump($items->values());
  71. }
  72. }
  73. public function getPromotionData($promotionDayCharge, $newUserCharge, ) {
  74. return [
  75. 'pay_money' => $promotionDayCharge['pay_money'] ?? 0,
  76. 'pay_count' => $promotionDayCharge['pay_count'] ?? 0,
  77. 'new_user_pay_money' => $newUserCharge['new_user_pay_money'] ?? 0,
  78. 'new_user_common_pay_money' => $newUserCharge['new_user_common_pay_money'] ?? 0,
  79. 'new_user_vip_pay_money' => $newUserCharge['new_user_vip_pay_money'] ?? 0,
  80. 'common_pay_money' => $promotionDayCharge['common_pay_money'] ?? 0,
  81. 'common_pay_uv' => $promotionDayCharge['common_pay_uv'] ?? 0,
  82. 'common_pay_count' => $promotionDayCharge['common_pay_count'] ?? 0,
  83. 'common_unpay_count' => $promotionDayCharge['common_unpay_count'] ?? 0,
  84. 'vip_pay_money' => $promotionDayCharge['vip_pay_money'] ?? 0,
  85. 'vip_pay_uv' => $promotionDayCharge['vip_pay_uv'] ?? 0,
  86. 'vip_pay_count' => $promotionDayCharge['vip_pay_count'] ?? 0,
  87. 'vip_unpay_count' => $promotionDayCharge['vip_unpay_count'] ?? 0,
  88. ];
  89. }
  90. public function newUserCharge($promotionId, $date) {
  91. $info = DB::table('orders')
  92. ->where('promotion_id', $promotionId)
  93. ->whereBetween('ranse_created_at', [$date, $date. ' 23:59:59'])
  94. ->select(
  95. // 总支付金额
  96. DB::raw("sum(if(status <> 'unpaid', price, 0)) as pay_money"),
  97. // 普通支付金额
  98. DB::raw("sum(if(status <> 'unpaid' and order_type in ('coin', 'first_coin'), price, 0)) as common_pay_money"),
  99. )->first();
  100. if($info) {
  101. return [
  102. // 新用户支付总额
  103. 'new_user_pay_money' => $info->pay_money,
  104. // 新用户普通支付总额
  105. 'new_user_common_pay_money' => $info->common_pay_money,
  106. // 新用户会员支付总额
  107. 'new_user_vip_pay_money' => $info->pay_money - $info->common_pay_money
  108. ];
  109. } else {
  110. return null;
  111. }
  112. }
  113. public function promotionDayCharge($promotionId, $date) {
  114. $info = DB::table('orders')
  115. ->where('promotion_id', $promotionId)
  116. ->whereBetween('created_at', [$date, $date. ' 23:59:59'])
  117. ->select(
  118. // 未支付金额
  119. DB::raw("sum(if(status = 'unpaid', price, 0)) as unpay_money"),
  120. // 未支付笔数
  121. DB::raw("sum(if(status = 'unpaid', 1, 0)) as unpay_count"),
  122. // 总金额
  123. DB::raw("sum(price) as total_money"),
  124. // 总笔数
  125. DB::raw("count(id) as total_count"),
  126. // 普通未支付笔数
  127. DB::raw("sum(if(status='unpaid' and order_type in ('coin', 'first_coin'), 1, 0)) as common_unpay_count"),
  128. // 普通总笔数
  129. DB::raw("sum(if(order_type in ('coin', 'first_coin'), 1, 0)) as common_count"),
  130. // 普通支付金额
  131. DB::raw("sum(if(order_type in ('coin', 'first_coin') and status <> 'unpaid', price, 0)) as common_pay_money"),
  132. // 普通支付人数
  133. DB::raw("count(distinct if(order_type in ('coin', 'first_coin') and status <> 'unpaid', uid, null)) as common_pay_uv"),
  134. // vip支付人数
  135. DB::raw("count(distinct if(order_type not in ('coin', 'first_coin') and status <> 'unpaid', uid, null)) as vip_pay_uv"),
  136. // vip未支付笔数
  137. DB::raw("sum(if(order_type not in ('coin', 'first_coin') and status = 'unpaid', 1, 0)) as vip_unpay_count"),
  138. )->first();
  139. if($info) {
  140. return [
  141. // 支付金额
  142. 'pay_money' => bcsub($info->total_money, $info->unpay_money, 2),
  143. // 支付笔数
  144. 'pay_count' => $info->total_count - $info->unpay_count,
  145. /**
  146. * -----普通充值--------
  147. */
  148. // 支付金额
  149. 'common_pay_money' => $info->common_pay_money,
  150. // 支付人数
  151. 'common_pay_uv' => $info->common_pay_uv,
  152. // 支付笔数
  153. 'common_pay_count' => $info->common_count - $info->common_unpay_count,
  154. // 未支付笔数
  155. 'common_unpay_count' => $info->common_unpay_count,
  156. /**
  157. * ----会员充值------
  158. */
  159. // 支付金额
  160. 'vip_pay_money' => bcsub(bcsub($info->total_money, $info->unpay_money, 2), $info->common_pay_money, 2),
  161. // 支付人数
  162. 'vip_pay_uv' => $info->vip_pay_uv,
  163. // 支付笔数
  164. 'vip_pay_count' => $info->total_count - $info->unpay_count - ($info->common_count - $info->common_unpay_count),
  165. // 未支付笔数
  166. 'vip_unpay_count' => $info->vip_unpay_count,
  167. ];
  168. } else {
  169. return null;
  170. }
  171. }
  172. }