浏览代码

充值统计

liuzejian 2 年之前
父节点
当前提交
8151daa6bd

+ 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,
+                    ]);
+            }
+        }
+    }
+}

+ 39 - 20
app/Console/Commands/Statistic/OptimizerMonthCharge.php

@@ -12,7 +12,7 @@ class OptimizerMonthCharge extends Command
      *
      * @var string
      */
-    protected $signature = 'Statistic:OptimizerMonthCharge {--month= : 统计月份}';
+    protected $signature = 'Statistic:OptimizerMonthCharge {--month= : 统计月份} {--user_ids= : 投手uids}';
 
     /**
      * The console command description.
@@ -26,33 +26,52 @@ class OptimizerMonthCharge extends Command
      */
     public function handle(): void
     {
-        //tj_optimizer_month_charge
-        $month = $this->option('month');
+        $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');
 
-        $result = DB::table('tj_optimizer_day_charge')
-            ->whereBetween('day_at', [$monthStart, $montEnd])
-            ->select(
-                'user_id', 'miniprogram_id','puser_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(['user_id', 'puser_id','miniprogram_id'])
-            ->get();
-        $insertData = [];
+        $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 ($result as $item) {
-            $info = (array)$item;
-            $info['created_at'] = $info['updated_at'] = $now;
-            $info['month_at'] = $month;
-            $insertData[] = $info;
+        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);
     }

+ 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');
     }
 
     /**

+ 6 - 6
modules/Statistic/Http/Controllers/ChargeTJController.php

@@ -139,9 +139,9 @@ class ChargeTJController extends CatchController
     public function currentMonthCharge(Request $request) {
         $userId = $this->getLoginUserId();
         if($this->listUserRoles()->contains('company')) {
-            $info = DB::table('tj_optimizer_day_charge')
+            $info = DB::table('tj_company_day_charge')
                 ->whereBetween('day_at', [date('Y-m-01'), date('Y-m-d', strtotime('yesterday'))])
-                ->where('puser_id', $userId)
+                ->where('company_uid', $userId)
                 ->select(
                     DB::raw("sum(pay_money) as pay_money"),
                     DB::raw("sum(common_pay_count) as common_pay_count"),
@@ -172,9 +172,9 @@ class ChargeTJController extends CatchController
     public function lastMonthCharge(Request $request) {
         $userId = $this->getLoginUserId();
         if($this->listUserRoles()->contains('company')) {
-            $info = DB::table('tj_optimizer_month_charge')
+            $info = DB::table('tj_company_month_charge')
                 ->where('month_at', date('Y-m-d', strtotime(date('Y-m-01')) - 10))
-                ->where('puser_id', $userId)
+                ->where('company_uid', $userId)
                 ->select(
                     DB::raw("sum(pay_money) as pay_money"),
                     DB::raw("sum(common_pay_count) as common_pay_count"),
@@ -205,9 +205,9 @@ class ChargeTJController extends CatchController
     public function totalCharge(Request $request) {
         $userId = $this->getLoginUserId();
         if($this->listUserRoles()->contains('company')) {
-            $info = DB::table('tj_optimizer_month_charge')
+            $info = DB::table('tj_company_month_charge')
                 ->where('month_at','<=', date('Y-m-d', strtotime(date('Y-m-01')) - 10))
-                ->where('puser_id', $userId)
+                ->where('company_uid', $userId)
                 ->select(
                     DB::raw("sum(pay_money) as pay_money"),
                     DB::raw("sum(common_pay_count) as common_pay_count"),