PromotionDayCharge.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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')
  38. ->leftJoin('users', 'users.id', '=' , 'promotions.uid')
  39. ->whereIn('promotions.id', $items->pluck('promotion_id'))
  40. ->select('promotions.id', 'promotions.uid', 'promotions.miniprogram_id', 'users.pid as puid')
  41. ->get()->keyBy('id');
  42. foreach ($items as $item) {
  43. $promotion = $promotions->get($item->promotion_id);
  44. if(!$promotion) {
  45. myLog('PromotionDayCharge')->error('订单中有的推广id,但是推广信息表中没有', ['promotion_id' => $item->promotion_id]);
  46. continue;
  47. }
  48. $promotionDayCharge = $this->promotionDayCharge($item->promotion_id, $date);
  49. $newUserCharge = $this->newUserCharge($item->promotion_id, $date);
  50. $chargeInfo = $this->getPromotionData($promotionDayCharge, $newUserCharge);
  51. $promotionData[] = array_merge( $chargeInfo, [
  52. 'promotion_id' => $item->promotion_id, 'day_at' => $date,
  53. 'created_at' => $now, 'updated_at' => $now,
  54. 'user_id' => $promotion->uid,'puser_id' => $promotion->puid,
  55. 'miniprogram_id' => $promotion->miniprogram_id,
  56. ]);
  57. $key = $promotion->uid . '-'. $promotion->miniprogram_id;
  58. foreach ($chargeInfo as $k => $v) {
  59. $optimizerDayData[$key][$k] = $optimizerDayData[$key][$k] ?? 0 + $v;
  60. $optimizerDayData[$key]['user_id'] = $promotion->uid;
  61. $optimizerDayData[$key]['puser_id'] = $promotion->puid;
  62. $optimizerDayData[$key]['miniprogram_id'] = $promotion->miniprogram_id;
  63. $optimizerDayData[$key]['day_at'] = $date;
  64. $optimizerDayData[$key]['created_at'] = $now;
  65. $optimizerDayData[$key]['updated_at'] = $now;
  66. }
  67. }
  68. DB::table('tj_promotion_day_charge')
  69. ->insert($promotionData);
  70. });
  71. foreach (collect($optimizerDayData)->chunk(100) as $items) {
  72. DB::table('tj_optimizer_day_charge')
  73. ->insert($items->values()->toArray());
  74. }
  75. }
  76. public function getPromotionData($promotionDayCharge, $newUserCharge, ) {
  77. return [
  78. 'pay_money' => $promotionDayCharge['pay_money'] ?? 0,
  79. 'pay_count' => $promotionDayCharge['pay_count'] ?? 0,
  80. 'new_user_pay_money' => $newUserCharge['new_user_pay_money'] ?? 0,
  81. 'new_user_common_pay_money' => $newUserCharge['new_user_common_pay_money'] ?? 0,
  82. 'new_user_vip_pay_money' => $newUserCharge['new_user_vip_pay_money'] ?? 0,
  83. 'common_pay_money' => $promotionDayCharge['common_pay_money'] ?? 0,
  84. 'common_pay_uv' => $promotionDayCharge['common_pay_uv'] ?? 0,
  85. 'common_pay_count' => $promotionDayCharge['common_pay_count'] ?? 0,
  86. 'common_unpay_count' => $promotionDayCharge['common_unpay_count'] ?? 0,
  87. 'vip_pay_money' => $promotionDayCharge['vip_pay_money'] ?? 0,
  88. 'vip_pay_uv' => $promotionDayCharge['vip_pay_uv'] ?? 0,
  89. 'vip_pay_count' => $promotionDayCharge['vip_pay_count'] ?? 0,
  90. 'vip_unpay_count' => $promotionDayCharge['vip_unpay_count'] ?? 0,
  91. ];
  92. }
  93. public function newUserCharge($promotionId, $date) {
  94. $info = DB::table('orders')
  95. ->where('promotion_id', $promotionId)
  96. ->whereBetween('ranse_created_at', [$date, $date. ' 23:59:59'])
  97. ->whereBetween('created_at', [$date, $date. ' 23:59:59'])
  98. ->select(
  99. // 总支付金额
  100. DB::raw("sum(if(status <> 'unpaid', price, 0)) as pay_money"),
  101. // 普通支付金额
  102. DB::raw("sum(if(status <> 'unpaid' and order_type in ('coin', 'first_coin'), price, 0)) as common_pay_money"),
  103. )->first();
  104. if($info) {
  105. return [
  106. // 新用户支付总额
  107. 'new_user_pay_money' => $info->pay_money,
  108. // 新用户普通支付总额
  109. 'new_user_common_pay_money' => $info->common_pay_money,
  110. // 新用户会员支付总额
  111. 'new_user_vip_pay_money' => $info->pay_money - $info->common_pay_money
  112. ];
  113. } else {
  114. return null;
  115. }
  116. }
  117. public function promotionDayCharge($promotionId, $date) {
  118. $info = DB::table('orders')
  119. ->where('promotion_id', $promotionId)
  120. ->whereBetween('created_at', [$date, $date. ' 23:59:59'])
  121. ->select(
  122. // 未支付金额
  123. DB::raw("sum(if(status = 'unpaid', price, 0)) as unpay_money"),
  124. // 未支付笔数
  125. DB::raw("sum(if(status = 'unpaid', 1, 0)) as unpay_count"),
  126. // 总金额
  127. DB::raw("sum(price) as total_money"),
  128. // 总笔数
  129. DB::raw("count(id) as total_count"),
  130. // 普通未支付笔数
  131. DB::raw("sum(if(status='unpaid' and order_type in ('coin', 'first_coin'), 1, 0)) as common_unpay_count"),
  132. // 普通总笔数
  133. DB::raw("sum(if(order_type in ('coin', 'first_coin'), 1, 0)) as common_count"),
  134. // 普通支付金额
  135. DB::raw("sum(if(order_type in ('coin', 'first_coin') and status <> 'unpaid', price, 0)) as common_pay_money"),
  136. // 普通支付人数
  137. DB::raw("count(distinct if(order_type in ('coin', 'first_coin') and status <> 'unpaid', uid, null)) as common_pay_uv"),
  138. // vip支付人数
  139. DB::raw("count(distinct if(order_type not in ('coin', 'first_coin') and status <> 'unpaid', uid, null)) as vip_pay_uv"),
  140. // vip未支付笔数
  141. DB::raw("sum(if(order_type not in ('coin', 'first_coin') and status = 'unpaid', 1, 0)) as vip_unpay_count"),
  142. )->first();
  143. if($info) {
  144. return [
  145. // 支付金额
  146. 'pay_money' => bcsub($info->total_money, $info->unpay_money, 2),
  147. // 支付笔数
  148. 'pay_count' => $info->total_count - $info->unpay_count,
  149. /**
  150. * -----普通充值--------
  151. */
  152. // 支付金额
  153. 'common_pay_money' => $info->common_pay_money,
  154. // 支付人数
  155. 'common_pay_uv' => $info->common_pay_uv,
  156. // 支付笔数
  157. 'common_pay_count' => $info->common_count - $info->common_unpay_count,
  158. // 未支付笔数
  159. 'common_unpay_count' => $info->common_unpay_count,
  160. /**
  161. * ----会员充值------
  162. */
  163. // 支付金额
  164. 'vip_pay_money' => bcsub(bcsub($info->total_money, $info->unpay_money, 2), $info->common_pay_money, 2),
  165. // 支付人数
  166. 'vip_pay_uv' => $info->vip_pay_uv,
  167. // 支付笔数
  168. 'vip_pay_count' => $info->total_count - $info->unpay_count - ($info->common_count - $info->common_unpay_count),
  169. // 未支付笔数
  170. 'vip_unpay_count' => $info->vip_unpay_count,
  171. ];
  172. } else {
  173. return null;
  174. }
  175. }
  176. }