liuzejian 1 год назад
Родитель
Сommit
52d2b84538
1 измененных файлов с 181 добавлено и 0 удалено
  1. 181 0
      app/Console/Commands/Statistic/PromotionDayCharge.php

+ 181 - 0
app/Console/Commands/Statistic/PromotionDayCharge.php

@@ -0,0 +1,181 @@
+<?php
+
+namespace App\Console\Commands\Statistic;
+
+use Illuminate\Console\Command;
+use Illuminate\Support\Arr;
+use Illuminate\Support\Facades\DB;
+
+class PromotionDayCharge extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'Statistic:PromotionDayCharge {--date= : 统计日期}';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '推广充值日统计';
+
+    /**
+     * Execute the console command.
+     */
+    public function handle(): void
+    {
+        $date = $this->option('date') ?? date('Y-m-d', strtotime('yesterday'));
+        $optimizerDayData = [];
+        DB::table('orders')
+            ->whereBetween('created_at', [
+                $date, $date. ' 23:59:59'
+            ])->where('promotion_id', '<>', 0)
+            ->distinct()
+            ->select('promotion_id')
+            ->orderBy('promotion_id')
+            ->chunk(100, function ($items) use ($date, &$optimizerDayData){
+                $promotionData = [];
+                $now = date('Y-m-d H:i:s');
+                $promotions = DB::table('promotions')->whereIn('id', $items->pluck('promotion_id'))
+                    ->select('id', 'uid', 'miniprogram_id')
+                    ->get()->keyBy('id');
+                foreach ($items as $item) {
+                    $promotionDayCharge = $this->promotionDayCharge($item->promotion_id, $date);
+                    $newUserCharge = $this->newUserCharge($item->promotion_id, $date);
+                    $chargeInfo = $this->getPromotionData($promotionDayCharge, $newUserCharge);
+                    $promotionData[] = array_merge( $chargeInfo, [
+                        'promotion_id' => $item->promotion_id, 'date' => $date,
+                        'created_at' => $now, 'updated_at' => $now,
+                    ]);
+                    $promotion = $promotions->get($item->promotion_id);
+                    if(!$promotion) {
+                        myLog('PromotionDayCharge')->error('订单中有的推广id,但是推广信息表中没有', ['promotion_id' => $item->promotion_id]);
+                        continue;
+                    }
+                    $key = $promotion->uid . '-'. $promotion->miniprogram_id;
+                    foreach ($chargeInfo as $k => $v) {
+                        $optimizerDayData[$key][$k] = $optimizerDayData[$key][$k] ?? 0 + $v;
+                        $optimizerDayData[$key]['user_id'] = $promotion->uid;
+                        $optimizerDayData[$key]['miniprogram_id'] = $promotion->miniprogram_id;
+                        $optimizerDayData[$key]['date'] = $date;
+                        $optimizerDayData[$key]['created_at'] = $now;
+                        $optimizerDayData[$key]['updated_at'] = $now;
+                    }
+                }
+//                DB::table('tj_promotion_day_charge')
+//                    ->insert($promotionData);
+                dump($promotionData);
+            });
+
+        foreach (collect($optimizerDayData)->chunk(100) as $items) {
+//            DB::table('tj_optimizer_day_charge')
+//                ->insert($items->values()->toArray());
+            dump($items->values());
+        }
+    }
+
+    public function getPromotionData($promotionDayCharge, $newUserCharge, ) {
+        return [
+            'pay_money' => $promotionDayCharge['pay_money'] ?? 0,
+            'pay_count' => $promotionDayCharge['pay_count'] ?? 0,
+            'new_user_pay_money' => $newUserCharge['new_user_pay_money'] ?? 0,
+            'new_user_common_pay_money' => $newUserCharge['new_user_common_pay_money'] ?? 0,
+            'new_user_vip_pay_money' => $newUserCharge['new_user_vip_pay_money'] ?? 0,
+            'common_pay_money' => $promotionDayCharge['common_pay_money'] ?? 0,
+            'common_pay_uv' => $promotionDayCharge['common_pay_uv'] ?? 0,
+            'common_pay_count' => $promotionDayCharge['common_pay_count'] ?? 0,
+            'common_unpay_count' => $promotionDayCharge['common_unpay_count'] ?? 0,
+            'vip_pay_money' => $promotionDayCharge['vip_pay_money'] ?? 0,
+            'vip_pay_uv' => $promotionDayCharge['vip_pay_uv'] ?? 0,
+            'vip_pay_count' => $promotionDayCharge['vip_pay_count'] ?? 0,
+            'vip_unpay_count' => $promotionDayCharge['vip_unpay_count'] ?? 0,
+        ];
+    }
+
+    public function newUserCharge($promotionId, $date) {
+        $info = DB::table('orders')
+            ->where('promotion_id', $promotionId)
+            ->whereBetween('ranse_created_at', [$date, $date. ' 23:59:59'])
+            ->select(
+                // 总支付金额
+                DB::raw("sum(if(status <> 'unpaid', price, 0)) as pay_money"),
+                // 普通支付金额
+                DB::raw("sum(if(status <> 'unpaid' and order_type in ('coin', 'first_coin'), price, 0)) as common_pay_money"),
+            )->first();
+        if($info) {
+            return [
+                // 新用户支付总额
+                'new_user_pay_money' => $info->pay_money,
+                // 新用户普通支付总额
+                'new_user_common_pay_money' => $info->common_pay_money,
+                // 新用户会员支付总额
+                'new_user_vip_pay_money' => $info->pay_money - $info->common_pay_money
+            ];
+        } else {
+            return null;
+        }
+    }
+
+    public function promotionDayCharge($promotionId, $date) {
+        $info = DB::table('orders')
+            ->where('promotion_id', $promotionId)
+            ->whereBetween('created_at', [$date, $date. ' 23:59:59'])
+            ->select(
+                // 未支付金额
+                DB::raw("sum(if(status = 'unpaid', price, 0)) as unpay_money"),
+                // 未支付笔数
+                DB::raw("sum(if(status = 'unpaid', 1, 0)) as unpay_count"),
+                // 总金额
+                DB::raw("sum(price) as total_money"),
+                // 总笔数
+                DB::raw("count(id) as total_count"),
+                // 普通未支付笔数
+                DB::raw("sum(if(status='unpaid' and order_type in ('coin', 'first_coin'), 1, 0)) as common_unpay_count"),
+                // 普通总笔数
+                DB::raw("sum(if(order_type in ('coin', 'first_coin'), 1,  0)) as common_count"),
+                // 普通支付金额
+                DB::raw("sum(if(order_type in ('coin', 'first_coin') and status <> 'unpaid', price, 0)) as common_pay_money"),
+                // 普通支付人数
+                DB::raw("count(distinct if(order_type in ('coin', 'first_coin') and status <> 'unpaid', uid, null)) as common_pay_uv"),
+                // vip支付人数
+                DB::raw("count(distinct if(order_type not in ('coin', 'first_coin') and status <> 'unpaid', uid, null)) as vip_pay_uv"),
+                // vip未支付笔数
+                DB::raw("sum(if(order_type not in ('coin', 'first_coin') and status = 'unpaid', 1,  0)) as vip_unpay_count"),
+            )->first();
+        if($info) {
+            return [
+                // 支付金额
+                'pay_money' => bcsub($info->total_money, $info->unpay_money, 2),
+                // 支付笔数
+                'pay_count' => $info->total_count - $info->unpay_count,
+                /**
+                 * -----普通充值--------
+                 */
+                // 支付金额
+                'common_pay_money' => $info->common_pay_money,
+                // 支付人数
+                'common_pay_uv' => $info->common_pay_uv,
+                // 支付笔数
+                'common_pay_count' => $info->common_count - $info->common_unpay_count,
+                // 未支付笔数
+                'common_unpay_count' => $info->common_unpay_count,
+                /**
+                 * ----会员充值------
+                 */
+                // 支付金额
+                'vip_pay_money' => bcsub(bcsub($info->total_money, $info->unpay_money, 2), $info->common_pay_money, 2),
+                // 支付人数
+                'vip_pay_uv' => $info->vip_pay_uv,
+                // 支付笔数
+                'vip_pay_count' => $info->total_count - $info->unpay_count - ($info->common_count - $info->common_unpay_count),
+                // 未支付笔数
+                'vip_unpay_count' => $info->vip_unpay_count,
+            ];
+        } else {
+            return null;
+        }
+    }
+}