zhaoyang il y a 1 an
Parent
commit
838e10c3d8

+ 69 - 0
app/Console/Commands/Statistic/CompanyDayCharge.php

@@ -0,0 +1,69 @@
+<?php
+
+namespace App\Console\Commands\Statistic;
+
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\DB;
+
+class CompanyDayCharge extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'Statistic:CompanyDayCharge {--date= : 统计日期} {--company_uids= : 公司管理员ids}';
+
+    /**
+     * 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'));
+        $company_uids = $this->option('company_uids');
+        if($company_uids) {
+            $companyUids = explode(',', $company_uids);
+        } else {
+            $companyUids = DB::table('user_has_roles')
+                ->where(['role_id' => 1])
+                ->select('user_id')
+                ->get()->pluck('user_id');
+        }
+
+        $now = date('Y-m-d H:i:s');
+        foreach ($companyUids as $companyUid) {
+            $info = DB::table('orders')
+                ->whereBetween('created_at', [$date, $date. ' 23:59:59'])
+                ->where('puser_id', $companyUid)
+                ->select(
+                    DB::raw("sum(if(status = 'unpaid', 0, price)) as pay_money"),
+                    DB::raw("sum(if(status <> 'unpaid' and order_type in ('coin', 'first_coin'), 1, 0)) as common_pay_count"),
+                    DB::raw("sum(if(status = 'unpaid' and order_type in ('coin', 'first_coin'), 1, 0)) as common_unpay_count"),
+                    DB::raw("sum(if(status <> 'unpaid' and order_type not in ('coin', 'first_coin'), 1, 0)) as vip_pay_count"),
+                    DB::raw("sum(if(status = 'unpaid' and order_type not in ('coin', 'first_coin'), 1, 0)) as vip_unpay_count"),
+                )->first();
+            if($info) {
+                DB::table('tj_company_day_charge')
+                    ->insert([
+                        'day_at' => $date,
+                        'pay_money' => $info->pay_money,
+                        'common_pay_count' => $info->common_pay_count,
+                        'common_unpay_count' => $info->common_unpay_count,
+                        'vip_pay_count' => $info->vip_pay_count,
+                        'vip_unpay_count' => $info->vip_unpay_count,
+                        'company_uid' => $companyUid,
+                        'created_at' => $now,
+                        'updated_at' => $now,
+                    ]);
+            }
+        }
+
+    }
+}

+ 74 - 0
app/Console/Commands/Statistic/CompanyMonthCharge.php

@@ -0,0 +1,74 @@
+<?php
+
+namespace App\Console\Commands\Statistic;
+
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\DB;
+
+class CompanyMonthCharge extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'Statistic:CompanyMonthCharge {--month= : 统计月份} {--company_uids= : 公司管理员ids}';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '投放公司充值月统计';
+
+    /**
+     * Execute the console command.
+     */
+    public function handle(): void
+    {
+        $month = $this->option('month') ?? date('Y-m', strtotime('last month'));
+        $monthStart = $month. '-01';
+        $montEnd = date_sub(date_add(date_create($monthStart),
+            date_interval_create_from_date_string('1 month')),
+            date_interval_create_from_date_string('1 day'))->format('Y-m-d');
+
+        $company_uids = $this->option('company_uids');
+        if($company_uids) {
+            $companyUids = explode(',', $company_uids);
+        } else {
+            $companyUids = DB::table('user_has_roles')
+                ->where(['role_id' => 1])
+                ->select('user_id')
+                ->get()->pluck('user_id');
+        }
+
+        $now = date('Y-m-d H:i:s');
+        foreach ($companyUids as $companyUid) {
+            $info = DB::table('tj_company_day_charge')
+                ->whereBetween('day_at', [$monthStart, $montEnd])
+                ->where(['company_uid' => $companyUid])
+                ->select(
+                    DB::raw("sum(pay_money) as pay_money"),
+                    DB::raw("sum(common_pay_count) as common_pay_count"),
+                    DB::raw("sum(common_unpay_count) as common_unpay_count"),
+                    DB::raw("sum(vip_unpay_count) as vip_unpay_count"),
+                    DB::raw("sum(vip_pay_count) as vip_pay_count"),
+                )->first();
+            $now = date('Y-m-d H:i:s');
+            if($info) {
+                DB::table('tj_company_month_charge')
+                    ->insert([
+                        'month_at' => $month,
+                        'pay_money' => $info->pay_money,
+                        'common_pay_count' => $info->common_pay_count,
+                        'common_unpay_count' => $info->common_unpay_count,
+                        'vip_pay_count' => $info->vip_pay_count,
+                        'vip_unpay_count' => $info->vip_unpay_count,
+                        'company_uid' => $companyUid,
+                        'created_at' => $now,
+                        'updated_at' => $now,
+                    ]);
+            }
+        }
+    }
+}

+ 78 - 0
app/Console/Commands/Statistic/OptimizerMonthCharge.php

@@ -0,0 +1,78 @@
+<?php
+
+namespace App\Console\Commands\Statistic;
+
+use Illuminate\Console\Command;
+use Illuminate\Support\Facades\DB;
+
+class OptimizerMonthCharge extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'Statistic:OptimizerMonthCharge {--month= : 统计月份} {--user_ids= : 投手uids}';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = '优化师充值月统计';
+
+    /**
+     * Execute the console command.
+     */
+    public function handle(): void
+    {
+        $month = $this->option('month') ?? date('Y-m', strtotime('last month'));
+        $monthStart = $month. '-01';
+        $montEnd = date_sub(date_add(date_create($monthStart),
+            date_interval_create_from_date_string('1 month')),
+            date_interval_create_from_date_string('1 day'))->format('Y-m-d');
+
+        $user_ids = $this->option('user_ids');
+        if($user_ids) {
+            $userIds = explode(',',  $user_ids);
+        } else {
+            $userIds = DB::table('user_has_roles')
+                ->where(['role_id' => 2])
+                ->select('user_id')
+                ->get()->pluck('user_id');
+        }
+
+        $now = date('Y-m-d H:i:s');
+        foreach ($userIds as $userId) {
+            $puser_id = DB::table('users')->where('id', $userId)->value('pid');
+            $result = DB::table('tj_optimizer_day_charge')
+                ->whereBetween('day_at', [$monthStart, $montEnd])
+                ->where('user_id', $userId)
+                ->select(
+                    'miniprogram_id',
+                    DB::raw("sum(pay_money) as pay_money"),
+                    DB::raw("sum(common_pay_count) as common_pay_count"),
+                    DB::raw("sum(common_unpay_count) as common_unpay_count"),
+                    DB::raw("sum(vip_unpay_count) as vip_unpay_count"),
+                    DB::raw("sum(vip_pay_count) as vip_pay_count"),
+                )->groupBy('miniprogram_id')
+                ->get();
+            $insertData = [];
+            foreach ($result as $item) {
+                $info = (array)$item;
+                $info['created_at'] = $info['updated_at'] = $now;
+                $info['month_at'] = $month;
+                $info['user_id'] = $userId;
+                $info['puser_id'] = $puser_id;
+                $insertData[] = $info;
+            }
+
+            DB::table('tj_optimizer_month_charge')
+                ->insert($insertData);
+        }
+
+
+        DB::table('tj_optimizer_month_charge')
+            ->insert($insertData);
+    }
+}

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

@@ -0,0 +1,186 @@
+<?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')
+                    ->leftJoin('users', 'users.id', '=' , 'promotions.uid')
+                    ->whereIn('promotions.id', $items->pluck('promotion_id'))
+                    ->select('promotions.id', 'promotions.uid', 'promotions.miniprogram_id', 'users.pid as puid')
+                    ->get()->keyBy('id');
+                foreach ($items as $item) {
+                    $promotion = $promotions->get($item->promotion_id);
+                    if(!$promotion) {
+                        myLog('PromotionDayCharge')->error('订单中有的推广id,但是推广信息表中没有', ['promotion_id' => $item->promotion_id]);
+                        continue;
+                    }
+                    $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, 'day_at' => $date,
+                        'created_at' => $now, 'updated_at' => $now,
+                        'user_id' => $promotion->uid,'puser_id' => $promotion->puid,
+                        'miniprogram_id' => $promotion->miniprogram_id,
+                    ]);
+
+                    $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]['puser_id'] = $promotion->puid;
+                        $optimizerDayData[$key]['miniprogram_id'] = $promotion->miniprogram_id;
+                        $optimizerDayData[$key]['day_at'] = $date;
+                        $optimizerDayData[$key]['created_at'] = $now;
+                        $optimizerDayData[$key]['updated_at'] = $now;
+                    }
+                }
+                DB::table('tj_promotion_day_charge')
+                    ->insert($promotionData);
+            });
+
+        foreach (collect($optimizerDayData)->chunk(100) as $items) {
+            DB::table('tj_optimizer_day_charge')
+                ->insert($items->values()->toArray());
+        }
+    }
+
+    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'])
+            ->whereBetween('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;
+        }
+    }
+}

+ 8 - 0
app/Console/Kernel.php

@@ -22,6 +22,14 @@ class Kernel extends ConsoleKernel
         $schedule->command('Jiesuan:CompanyChargeDayJiesuan', [
             '--date' => date('Y-m-d', strtotime('yesterday'))
         ])->dailyAt('01:00');
+
+        /**
+         * 充值统计----------------------
+         */
+        $schedule->command('Statistic:CompanyDayCharge')->dailyAt('02:00');
+        $schedule->command('Statistic:CompanyMonthCharge')->monthlyOn(1, '04:00');
+        $schedule->command('Statistic:PromotionDayCharge')->dailyAt('01:00');
+        $schedule->command('Statistic:OptimizerMonthCharge')->monthlyOn(1,'03:00');
     }
 
     /**

+ 246 - 0
modules/Statistic/Http/Controllers/ChargeTJController.php

@@ -0,0 +1,246 @@
+<?php
+
+namespace Modules\Statistic\Http\Controllers;
+
+use Catch\Base\CatchController;
+use Illuminate\Foundation\Validation\ValidatesRequests;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\DB;
+use Modules\User\Http\Controllers\UserTrait;
+
+class ChargeTJController extends CatchController
+{
+    use ValidatesRequests;
+    use UserTrait;
+
+    private function _sql(Request $request) {
+        $this->validate($request, [
+            'start_at' => 'nullable|date_format:Y-m-d',
+            'end_at' => 'nullable|date_format:Y-m-d|after_or_equal:start_at',
+            'user_id' => 'nullable|integer|min:1',
+            'miniprogram_id' => 'nullable|integer|min:1'
+        ]);
+
+        $startAt = $request->input('start_at');
+        $endAt = $request->input('end_at');
+        $userId = $request->input('user_id');
+        $miniprogramId = $request->input('miniprogram_id');
+        $roles = $this->listUserRoles();
+        if($roles->contains('company')) {
+            $puserId = $this->getLoginUserId();
+        } else {
+            $puserId = null;
+            $userId = $this->getLoginUserId();
+        }
+
+        return  DB::table('tj_optimizer_day_charge')
+            ->when($startAt, function ($query, $startAt){
+                return $query->where('day_at', '>=', $startAt);
+            })->when($endAt, function ($query, $endAt){
+                return $query->where('day_at', '<=', $endAt);
+            })->when($userId, function ($query, $userId){
+                return $query->where('user_id', $userId);
+            })->when($miniprogramId, function ($query, $miniprogramId) {
+                return $query->where('miniprogram_id', $miniprogramId);
+            })->when($puserId, function ($query, $puserId){
+                return $query->where('puser_id', $puserId);
+            })->orderBy('id', 'desc');
+    }
+
+    // 充值明细
+    public function list(Request $request) {
+
+        $sql = $this->_sql($request);
+        $isExport = $request->input('is_export', false);
+
+        if($isExport) {
+            $result =  $sql->get();
+        } else {
+            $result =  $sql->paginate($request->input('limit', 15));
+        }
+
+        $users = DB::table('users')
+            ->whereIn('id', $result->pluck('user_id'))
+            ->select('id', 'username')
+            ->get()->keyBy('id');
+        $miniprograms = DB::table('miniprogram')
+            ->whereIn('id', $result->pluck('miniprogram_id'))
+            ->select('id','name')
+            ->get()->keyBy('id');
+
+        foreach ($result as $item) {
+            $item->common_pay_money_per = $item->common_pay_uv ? bcdiv($item->common_pay_money, $item->common_pay_uv ,2 ) : 0;
+            $item->vip_pay_money_per = $item->vip_pay_uv ? bcdiv($item->vip_pay_money, $item->vip_pay_uv ,2 ) : 0;
+            $item->common_pay_rate = $item->common_pay_count ? bcdiv($item->common_pay_count,100 * ($item->common_pay_count + $item->common_unpay_count) ,2 ) . '%' : 0 .'%';
+            $item->vip_pay_rate = $item->vip_pay_count ? bcdiv($item->vip_pay_count ,100 * ($item->vip_pay_count + $item->vip_unpay_count), 2 ) . '%' : 0 .'%';
+            $item->username = $users->get($item->user_id)->username ?? '';
+            $item->miniprogram_name = $miniprograms->get($item->miniprogram_id)->name ?? '';
+        }
+
+        return $result;
+    }
+
+    // 充值明细中的累计
+    public function listTotalCharge(Request $request) {
+        $sql = $this->_sql($request);
+        $info =  $sql->select(
+            DB::raw("sum(pay_money) as sum_pay_money"),
+            DB::raw("sum(pay_count) as sum_pay_count"),
+        )->first();
+        if($info) {
+            return [
+                'sum_pay_money' => $info->sum_pay_money,
+                'sum_pay_count' => $info->sum_pay_count,
+            ];
+        } else {
+            return [
+                'sum_pay_money' => 0,
+                'sum_pay_count' => 0,
+            ];
+        }
+    }
+
+    // 今日充值
+    public function todayCharge(Request $request) {
+        $date = date('Y-m-d');
+        $userId = $this->getLoginUserId();
+        if($this->listUserRoles()->contains('company')) {
+            $info = DB::table('orders')
+                ->whereBetween('created_at', [$date, $date. ' 23:59:59'])
+                ->where('puser_id', $userId)
+                ->select(
+                    DB::raw("sum(if(status = 'unpaid', 0, price)) as pay_money"),
+                    DB::raw("sum(if(status <> 'unpaid' and order_type in ('coin', 'first_coin'), 1, 0)) as common_pay_count"),
+                    DB::raw("sum(if(status = 'unpaid' and order_type in ('coin', 'first_coin'), 1, 0)) as common_unpay_count"),
+                    DB::raw("sum(if(status <> 'unpaid' and order_type not in ('coin', 'first_coin'), 1, 0)) as vip_pay_count"),
+                    DB::raw("sum(if(status = 'unpaid' and order_type not in ('coin', 'first_coin'), 1, 0)) as vip_unpay_count"),
+                )->first();
+        } else {
+            $info = DB::table('orders')
+                ->whereBetween('created_at', [$date, $date. ' 23:59:59'])
+                ->where('user_id', $userId)
+                ->where('miniprogram_id', $request->input('miniprogram_id'))
+                ->select(
+                    DB::raw("sum(if(status = 'unpaid', 0, price)) as pay_money"),
+                    DB::raw("sum(if(status <> 'unpaid' and order_type in ('coin', 'first_coin'), 1, 0)) as common_pay_count"),
+                    DB::raw("sum(if(status = 'unpaid' and order_type in ('coin', 'first_coin'), 1, 0)) as common_unpay_count"),
+                    DB::raw("sum(if(status <> 'unpaid' and order_type not in ('coin', 'first_coin'), 1, 0)) as vip_pay_count"),
+                    DB::raw("sum(if(status = 'unpaid' and order_type not in ('coin', 'first_coin'), 1, 0)) as vip_unpay_count"),
+                )->first();
+        }
+
+        if($info) {
+            $info->company_pay_rate = $info->common_pay_count ? bcdiv($info->common_pay_count,($info->common_pay_count + $info->common_unpay_count) * 100,  2) . '%' : '0%';
+            $info->vip_pay_rate = $info->vip_pay_count ? bcdiv($info->vip_pay_count, ($info->vip_pay_count + $info->vip_unpay_count) * 100, 2) . '%' : '0%';
+        }
+        return $info;
+    }
+    // 本月充值
+    public function currentMonthCharge(Request $request) {
+        $userId = $this->getLoginUserId();
+        if($this->listUserRoles()->contains('company')) {
+            $info = DB::table('tj_company_day_charge')
+                ->whereBetween('day_at', [date('Y-m-01'), date('Y-m-d', strtotime('yesterday'))])
+                ->where('company_uid', $userId)
+                ->select(
+                    DB::raw("sum(pay_money) as pay_money"),
+                    DB::raw("sum(common_pay_count) as common_pay_count"),
+                    DB::raw("sum(common_unpay_count) as common_unpay_count"),
+                    DB::raw("sum(vip_unpay_count) as vip_unpay_count"),
+                    DB::raw("sum(vip_pay_count) as vip_pay_count"),
+                )->first();
+        } else {
+            $info = DB::table('tj_optimizer_day_charge')
+                ->whereBetween('day_at', [date('Y-m-01'), date('Y-m-d', strtotime('yesterday'))])
+                ->where('user_id', $userId)
+                ->where('miniprogram_id', $request->input('miniprogram_id'))
+                ->select(
+                    DB::raw("sum(pay_money) as pay_money"),
+                    DB::raw("sum(common_pay_count) as common_pay_count"),
+                    DB::raw("sum(common_unpay_count) as common_unpay_count"),
+                    DB::raw("sum(vip_unpay_count) as vip_unpay_count"),
+                    DB::raw("sum(vip_pay_count) as vip_pay_count"),
+                )->first();
+        }
+        if($info) {
+            $info->company_pay_rate = $info->common_pay_count ? bcdiv($info->common_pay_count,($info->common_pay_count + $info->common_unpay_count) * 100,  2) . '%' : '0%';
+            $info->vip_pay_rate = $info->vip_pay_count ? bcdiv($info->vip_pay_count, ($info->vip_pay_count + $info->vip_unpay_count) * 100, 2) . '%' : '0%';
+        }
+        return $info;
+    }
+    // 上月充值
+    public function lastMonthCharge(Request $request) {
+        $userId = $this->getLoginUserId();
+        if($this->listUserRoles()->contains('company')) {
+            $info = DB::table('tj_company_month_charge')
+                ->where('month_at', date('Y-m-d', strtotime(date('Y-m-01')) - 10))
+                ->where('company_uid', $userId)
+                ->select(
+                    DB::raw("sum(pay_money) as pay_money"),
+                    DB::raw("sum(common_pay_count) as common_pay_count"),
+                    DB::raw("sum(common_unpay_count) as common_unpay_count"),
+                    DB::raw("sum(vip_unpay_count) as vip_unpay_count"),
+                    DB::raw("sum(vip_pay_count) as vip_pay_count"),
+                )->first();
+        } else {
+            $info = DB::table('tj_optimizer_month_charge')
+                ->where('month_at', date('Y-m-d', strtotime(date('Y-m-01')) - 10))
+                ->where('user_id', $userId)
+                ->where('miniprogram_id', $request->input('miniprogram_id'))
+                ->select(
+                    DB::raw("sum(pay_money) as pay_money"),
+                    DB::raw("sum(common_pay_count) as common_pay_count"),
+                    DB::raw("sum(common_unpay_count) as common_unpay_count"),
+                    DB::raw("sum(vip_unpay_count) as vip_unpay_count"),
+                    DB::raw("sum(vip_pay_count) as vip_pay_count"),
+                )->first();
+        }
+        if($info) {
+            $info->company_pay_rate = $info->common_pay_count ? bcdiv($info->common_pay_count,($info->common_pay_count + $info->common_unpay_count) * 100,  2) . '%' : '0%';
+            $info->vip_pay_rate = $info->vip_pay_count ? bcdiv($info->vip_pay_count,($info->vip_pay_count + $info->vip_unpay_count) * 100,  2) . '%' : '0%';
+        }
+        return $info;
+    }
+    // 累计充值
+    public function totalCharge(Request $request) {
+        $userId = $this->getLoginUserId();
+        if($this->listUserRoles()->contains('company')) {
+            $info = DB::table('tj_company_month_charge')
+                ->where('month_at','<=', date('Y-m-d', strtotime(date('Y-m-01')) - 10))
+                ->where('company_uid', $userId)
+                ->select(
+                    DB::raw("sum(pay_money) as pay_money"),
+                    DB::raw("sum(common_pay_count) as common_pay_count"),
+                    DB::raw("sum(common_unpay_count) as common_unpay_count"),
+                    DB::raw("sum(vip_unpay_count) as vip_unpay_count"),
+                    DB::raw("sum(vip_pay_count) as vip_pay_count"),
+                )->first();
+        } else {
+            $info = DB::table('tj_optimizer_month_charge')
+                ->where('month_at', '<=',date('Y-m-d', strtotime(date('Y-m-01')) - 10))
+                ->where('user_id', $userId)
+                ->where('miniprogram_id', $request->input('miniprogram_id'))
+                ->select(
+                    DB::raw("sum(pay_money) as pay_money"),
+                    DB::raw("sum(common_pay_count) as common_pay_count"),
+                    DB::raw("sum(common_unpay_count) as common_unpay_count"),
+                    DB::raw("sum(vip_unpay_count) as vip_unpay_count"),
+                    DB::raw("sum(vip_pay_count) as vip_pay_count"),
+                )->first();
+        }
+
+        $currentMonthInfo = $this->currentMonthCharge($request);
+
+        $result = (object)[
+            'pay_money' => $info->pay_money ?? 0 + $currentMonthInfo->pay_money ?? 0,
+            'common_pay_count' => $info->common_pay_count ?? 0 + $currentMonthInfo->common_pay_count ?? 0,
+            'common_unpay_count' => $info->common_unpay_count ?? 0 + $currentMonthInfo->common_unpay_count ?? 0,
+            'vip_unpay_count' => $info->vip_unpay_count ?? 0 + $currentMonthInfo->vip_unpay_count ?? 0,
+            'vip_pay_count' => $info->vip_pay_count ?? 0 + $currentMonthInfo->vip_pay_count ?? 0,
+        ];
+        $result->company_pay_rate = $result->common_pay_count ? bcdiv($result->common_pay_count,($result->common_pay_count + $result->common_unpay_count) * 100,  2) . '%' : '0%';
+        $result->vip_pay_rate = $result->vip_pay_count ? bcdiv($result->vip_pay_count,($result->vip_pay_count + $result->vip_unpay_count) * 100,  2) . '%' : '0%';
+
+        return $result;
+    }
+}

+ 10 - 0
modules/Statistic/routes/route.php

@@ -1,6 +1,7 @@
 <?php
 
 use Illuminate\Support\Facades\Route;
+use Modules\Statistic\Http\Controllers\ChargeTJController;
 use Modules\Statistic\Http\Controllers\UserStatisticsController;
 use Modules\Statistic\Http\Controllers\VideoStatController;
 
@@ -16,4 +17,13 @@ Route::prefix('statistic')->group(function(){
         Route::get('list',[VideoStatController::class,'index'])->withoutMiddleware(config('catch.route.middlewares'));
         Route::get('stats',[VideoStatController::class,'stats'])->withoutMiddleware(config('catch.route.middlewares'));
     });
+    // 充值统计
+    Route::prefix('charge')->group(function() {
+        Route::get('list', [ChargeTJController::class, 'list']);
+        Route::get('todayCharge', [ChargeTJController::class, 'todayCharge']);
+        Route::get('listTotalCharge', [ChargeTJController::class, 'listTotalCharge']);
+        Route::get('currentMonthCharge', [ChargeTJController::class, 'currentMonthCharge']);
+        Route::get('lastMonthCharge', [ChargeTJController::class, 'lastMonthCharge']);
+        Route::get('totalCharge', [ChargeTJController::class, 'totalCharge']);
+    });
 });

+ 0 - 1
modules/Tuiguang/Http/Controllers/PromotionController.php

@@ -55,7 +55,6 @@ class PromotionController extends CatchController
             'videos.name as video_name', 'promotions.series_sequence', 'promotions.callback_type',
                 'promotions.callback_config_id', 'promotions.video_id', 'promotions.remark', 'promotions.status',
             'promotions.first_charge_template_id', 'promotions.not_first_charge_template_id')
-
             ->paginate($request->input('limit', 15));
         $chargeTemplateIds = $result->pluck('first_charge_template_id')->merge($result->pluck('not_first_charge_template_id'))->unique();
 

+ 50 - 0
tests/Statistic/Http/Controllers/ChargeTJControllerTest.php

@@ -0,0 +1,50 @@
+<?php
+
+namespace Tests\Statistic\Http\Controllers;
+
+use Modules\Statistic\Http\Controllers\ChargeTJController;
+use PHPUnit\Framework\TestCase;
+use Tests\UsedTestCase;
+
+class ChargeTJControllerTest extends UsedTestCase
+{
+
+    public function testList()
+    {
+        $res = $this->withHeaders([
+            'Authorization' => 'Bearer '. $this->token,
+        ])->json('get','http://localhost/api/statistic/charge/list', [
+//            'user_id' => 10,
+            'limit' => 3,
+            'start_at' => '2023-06-03',
+            'end_at' => '2023-06-04',
+        ]);
+
+//        $res->dump();
+        $this->dumpJson($res);
+    }
+    public function testlistTotalCharge()
+    {
+        $res = $this->withHeaders([
+            'Authorization' => 'Bearer '. $this->token,
+        ])->json('get','http://localhost/api/statistic/charge/listTotalCharge', [
+//            'user_id' => 10,
+            'limit' => 3,
+            'start_at' => '2023-06-03',
+            'end_at' => '2023-06-04',
+        ]);
+
+//        $res->dump();
+        $this->dumpJson($res);
+    }
+    public function testtodayCharge()
+    {
+        $res = $this->withHeaders([
+            'Authorization' => 'Bearer '. $this->token,
+        ])->json('get','http://localhost/api/statistic/charge/todayCharge', [
+        ]);
+
+//        $res->dump();
+        $this->dumpJson($res);
+    }
+}

+ 3 - 3
tests/UsedTestCase.php

@@ -15,14 +15,14 @@ abstract class UsedTestCase extends BaseTestCase
         $tokenInfo = $this->post('http://localhost/api/login', [
 //            'email' => 'catch@admin.com',
             'remember' => false,
-//            'email' => 'xiaoli@qq.com',
+            'email' => 'xiaoli@qq.com',
             'password' => 'catchadmin',
-            'email' => 'aa4@test.com',
+//            'email' => 'aa4@test.com',
         ])->json();
         $this->token = $tokenInfo['data']['token'];
     }
 
     public function dumpJson($res) {
-        dump(\json_encode($res->json()));
+        dump(\json_encode($res->json(), JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE));
     }
 }