PromotionDayCharge.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. use Illuminate\Support\Facades\Redis;
  7. class PromotionDayCharge extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'Statistic:PromotionDayCharge {--date= : 统计日期}';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = '推广充值日统计';
  21. /**
  22. * Execute the console command.
  23. */
  24. public function handle(): void
  25. {
  26. $date = $this->option('date') ?? date('Y-m-d', strtotime('yesterday'));
  27. $optimizerDayData = [];
  28. DB::table('orders')
  29. ->whereBetween('created_at', [
  30. $date, $date. ' 23:59:59'
  31. ])->where('promotion_id', '<>', 0)
  32. ->distinct()
  33. ->select('promotion_id')
  34. ->orderBy('promotion_id')
  35. ->chunk(100, function ($items) use ($date, &$optimizerDayData){
  36. $promotionData = [];
  37. $now = date('Y-m-d H:i:s');
  38. $promotions = DB::table('promotions')
  39. ->leftJoin('users', 'users.id', '=' , 'promotions.uid')
  40. ->whereIn('promotions.id', $items->pluck('promotion_id'))
  41. ->select('promotions.id', 'promotions.uid', 'promotions.miniprogram_id', 'users.pid as puid')
  42. ->get()->keyBy('id');
  43. foreach ($items as $item) {
  44. $promotion = $promotions->get($item->promotion_id);
  45. if(!$promotion) {
  46. myLog('PromotionDayCharge')->error('订单中有的推广id,但是推广信息表中没有', ['promotion_id' => $item->promotion_id]);
  47. continue;
  48. }
  49. $promotionDayCharge = $this->promotionDayCharge($item->promotion_id, $date);
  50. $newUserCharge = $this->newUserCharge($item->promotion_id, $date);
  51. $chargeInfo = $this->getPromotionData($promotionDayCharge, $newUserCharge);
  52. $chargeInfo['new_user_num'] = $this->getNewUserNum($item->promotion_id, $date);
  53. $promotionData[] = array_merge( $chargeInfo, [
  54. 'promotion_id' => $item->promotion_id, 'day_at' => $date,
  55. 'created_at' => $now, 'updated_at' => $now,
  56. 'user_id' => $promotion->uid,'puser_id' => $promotion->puid,
  57. 'miniprogram_id' => $promotion->miniprogram_id,
  58. ]);
  59. $key = $promotion->uid . '-'. $promotion->miniprogram_id;
  60. foreach ($chargeInfo as $k => $v) {
  61. $optimizerDayData[$key][$k] = $optimizerDayData[$key][$k] ?? 0 + $v;
  62. $optimizerDayData[$key]['user_id'] = $promotion->uid;
  63. $optimizerDayData[$key]['puser_id'] = $promotion->puid;
  64. $optimizerDayData[$key]['miniprogram_id'] = $promotion->miniprogram_id;
  65. $optimizerDayData[$key]['day_at'] = $date;
  66. $optimizerDayData[$key]['created_at'] = $now;
  67. $optimizerDayData[$key]['updated_at'] = $now;
  68. }
  69. }
  70. DB::table('tj_promotion_day_charge')
  71. ->insert($promotionData);
  72. });
  73. foreach (collect($optimizerDayData)->chunk(100) as $items) {
  74. DB::table('tj_optimizer_day_charge')
  75. ->insert($items->values()->toArray());
  76. }
  77. DB::table('promotions')
  78. ->leftJoin('users', 'users.id', '=' , 'promotions.uid')
  79. ->select('promotions.id', 'promotions.uid', 'promotions.miniprogram_id', 'users.pid as puid')
  80. ->orderBy('promotions.id')
  81. ->chunk(100, function ($items) use ($date){
  82. $promotionIds = $items->pluck('id');
  83. $now = date('Y-m-d H:i:s');
  84. $alreadyExistsPromotionIds = DB::table('tj_promotion_day_charge')
  85. ->where('day_at', $date)
  86. ->whereIn('promotion_id', $promotionIds)
  87. ->select('promotion_id')
  88. ->get()->pluck('promotion_id');
  89. foreach ($items as $item) {
  90. if($alreadyExistsPromotionIds->contains($item->id)) {
  91. continue;
  92. }
  93. DB::table('tj_promotion_day_charge')
  94. ->insert([
  95. 'promotion_id' => $item->id, 'day_at' => $date,
  96. 'created_at' => $now, 'updated_at' => $now,
  97. 'user_id' => $item->uid, 'puser_id' => $item->puid,
  98. 'miniprogram_id' => $item->miniprogram_id
  99. ]);
  100. }
  101. });
  102. DB::table('users')
  103. ->join('user_has_roles', 'users.id', 'user_has_roles.user_id')
  104. ->where(['user_has_roles.role_id' => 2, 'users.deleted_at' => 0])
  105. ->distinct()
  106. ->select('users.id', 'users.pid')
  107. ->orderBy('users.id')
  108. ->chunk(50, function ($items) use($date) {
  109. $now = date('Y-m-d H:i:s');
  110. foreach ($items as $item) {
  111. $miniprogramIds = DB::table('user_has_miniprograms')
  112. ->where('uid', $item->id)
  113. ->where('is_enabled', 1)
  114. ->select('miniprogram_id')
  115. ->get()->pluck('miniprogram_id');
  116. foreach ($miniprogramIds as $miniprogramId) {
  117. if(DB::table('tj_optimizer_day_charge')
  118. ->where([
  119. 'day_at' => $date,
  120. 'user_id' => $item->id, 'miniprogram_id' => $miniprogramId,
  121. ])->exists()) {
  122. continue;
  123. }
  124. DB::table('tj_optimizer_day_charge')
  125. ->insert([
  126. 'day_at' => $date, 'user_id' => $item->id, 'miniprogram_id' => $miniprogramId,
  127. 'puser_id' => $item->pid, 'created_at' => $now, 'updated_at' => $now
  128. ]);
  129. }
  130. }
  131. });
  132. }
  133. /**
  134. * 某个推广链接在某天的新用户数量
  135. * @param $promotionId 推广链接id
  136. * @param $date 日期
  137. * @return mixed
  138. */
  139. private function getNewUserNum($promotionId, $date) {
  140. return intval(Redis::hget(sprintf('promotion:newUserCount:%s', $date), $promotionId));
  141. }
  142. public function getPromotionData($promotionDayCharge, $newUserCharge) {
  143. return [
  144. 'pay_money' => $promotionDayCharge['pay_money'] ?? 0,
  145. 'pay_count' => $promotionDayCharge['pay_count'] ?? 0,
  146. 'common_pay_money' => $promotionDayCharge['common_pay_money'] ?? 0,
  147. 'common_pay_uv' => $promotionDayCharge['common_pay_uv'] ?? 0,
  148. 'common_pay_count' => $promotionDayCharge['common_pay_count'] ?? 0,
  149. 'common_unpay_count' => $promotionDayCharge['common_unpay_count'] ?? 0,
  150. 'vip_pay_money' => $promotionDayCharge['vip_pay_money'] ?? 0,
  151. 'vip_pay_uv' => $promotionDayCharge['vip_pay_uv'] ?? 0,
  152. 'vip_pay_count' => $promotionDayCharge['vip_pay_count'] ?? 0,
  153. 'vip_unpay_count' => $promotionDayCharge['vip_unpay_count'] ?? 0,
  154. 'new_user_pay_money' => $newUserCharge['new_user_pay_money'] ?? 0,
  155. 'new_user_common_pay_money' => $newUserCharge['new_user_common_pay_money'] ?? 0,
  156. 'new_user_vip_pay_money' => $newUserCharge['new_user_vip_pay_money'] ?? 0,
  157. 'new_user_pay_uv' => $newUserCharge['new_user_pay_uv'] ?? 0,
  158. 'new_user_vip_pay_uv' => $newUserCharge['new_user_vip_pay_uv'] ?? 0,
  159. 'new_user_common_pay_uv' =>$newUserCharge['new_user_common_pay_uv'] ?? 0,
  160. ];
  161. }
  162. public function newUserCharge($promotionId, $date) {
  163. $info = DB::table('orders')
  164. ->where('promotion_id', $promotionId)
  165. ->whereBetween('ranse_created_at', [$date, $date. ' 23:59:59'])
  166. ->whereBetween('created_at', [$date, $date. ' 23:59:59'])
  167. ->select(
  168. // 总支付金额
  169. DB::raw("sum(if(status <> 'unpaid', price, 0)) as pay_money"),
  170. // 普通支付金额
  171. DB::raw("sum(if(status <> 'unpaid' and order_type in ('coin', 'first_coin'), price, 0)) as common_pay_money"),
  172. // 累计充值人数
  173. DB::raw("count(distinct if(status <> 'unpaid', concat(uid, ranse_created_at), null)) as pay_uv"),
  174. // 普通支付人数
  175. DB::raw("count(distinct if(order_type in ('coin', 'first_coin') and status <> 'unpaid', concat(uid, ranse_created_at), null)) as common_pay_uv"),
  176. // vip支付人数
  177. DB::raw("count(distinct if(order_type not in ('coin', 'first_coin') and status <> 'unpaid', concat(uid, ranse_created_at), null)) as vip_pay_uv"),
  178. )->first();
  179. if($info) {
  180. return [
  181. // 新增用户支付总额
  182. 'new_user_pay_money' => $info->pay_money,
  183. // 新增用户普通支付总额
  184. 'new_user_common_pay_money' => $info->common_pay_money,
  185. // 新增用户会员支付总额
  186. 'new_user_vip_pay_money' => $info->pay_money - $info->common_pay_money,
  187. // 新增用户在{$date}充值人数
  188. 'new_user_pay_uv' => $info->pay_uv,
  189. // 新增用户在{$date}vip充值支付人数
  190. 'new_user_vip_pay_uv' => $info->vip_pay_uv,
  191. // 新增用户在{$date}普通充值支付人数
  192. 'new_user_common_pay_uv' => $info->vip_pay_uv,
  193. ];
  194. } else {
  195. return null;
  196. }
  197. }
  198. public function promotionDayCharge($promotionId, $date) {
  199. $info = DB::table('orders')
  200. ->where('promotion_id', $promotionId)
  201. ->whereBetween('created_at', [$date, $date. ' 23:59:59'])
  202. ->where('ranse_created_at', '>', '2000-01-01')
  203. ->select(
  204. // 未支付金额
  205. DB::raw("sum(if(status = 'unpaid', price, 0)) as unpay_money"),
  206. // 未支付笔数
  207. DB::raw("sum(if(status = 'unpaid', 1, 0)) as unpay_count"),
  208. // 总金额
  209. DB::raw("sum(price) as total_money"),
  210. // 总笔数
  211. DB::raw("count(id) as total_count"),
  212. // 普通未支付笔数
  213. DB::raw("sum(if(status='unpaid' and order_type in ('coin', 'first_coin'), 1, 0)) as common_unpay_count"),
  214. // 普通总笔数
  215. DB::raw("sum(if(order_type in ('coin', 'first_coin'), 1, 0)) as common_count"),
  216. // 普通支付金额
  217. DB::raw("sum(if(order_type in ('coin', 'first_coin') and status <> 'unpaid', price, 0)) as common_pay_money"),
  218. // NOTE!!!,uid, ranse_created_at 唯一确定一个用户
  219. // 累计充值人数
  220. DB::raw("count(distinct if(status <> 'unpaid', concat(uid, ranse_created_at), null)) as pay_uv"),
  221. // 普通支付人数
  222. DB::raw("count(distinct if(order_type in ('coin', 'first_coin') and status <> 'unpaid', concat(uid, ranse_created_at), null)) as common_pay_uv"),
  223. // vip支付人数
  224. DB::raw("count(distinct if(order_type not in ('coin', 'first_coin') and status <> 'unpaid', concat(uid, ranse_created_at), null)) as vip_pay_uv"),
  225. // vip未支付笔数
  226. DB::raw("sum(if(order_type not in ('coin', 'first_coin') and status = 'unpaid', 1, 0)) as vip_unpay_count"),
  227. )->first();
  228. if($info) {
  229. return [
  230. // 支付金额
  231. 'pay_money' => bcsub($info->total_money, $info->unpay_money, 2),
  232. // 支付笔数
  233. 'pay_count' => $info->total_count - $info->unpay_count,
  234. /**
  235. * -----普通充值--------
  236. */
  237. // 支付金额
  238. 'common_pay_money' => $info->common_pay_money,
  239. // 支付人数
  240. 'common_pay_uv' => $info->common_pay_uv,
  241. // 支付笔数
  242. 'common_pay_count' => $info->common_count - $info->common_unpay_count,
  243. // 未支付笔数
  244. 'common_unpay_count' => $info->common_unpay_count,
  245. /**
  246. * ----会员充值------
  247. */
  248. // 支付金额
  249. 'vip_pay_money' => bcsub(bcsub($info->total_money, $info->unpay_money, 2), $info->common_pay_money, 2),
  250. // 支付人数
  251. 'vip_pay_uv' => $info->vip_pay_uv,
  252. // 支付笔数
  253. 'vip_pay_count' => $info->total_count - $info->unpay_count - ($info->common_count - $info->common_unpay_count),
  254. // 未支付笔数
  255. 'vip_unpay_count' => $info->vip_unpay_count,
  256. ];
  257. } else {
  258. return null;
  259. }
  260. }
  261. }