Selaa lähdekoodia

Merge branch 'wzq-tj-xyh' into test

zqwang 1 vuosi sitten
vanhempi
commit
9bd62707ac

+ 46 - 0
modules/Statistic/Http/Controllers/UserStatisticsController.php

@@ -0,0 +1,46 @@
+<?php
+/**
+ *
+ * @file:UserStatisticsController.php
+ * @Date: 2023/6/20
+ * @Time: 09:29
+ */
+
+
+namespace Modules\Statistic\Http\Controllers;
+
+use Catch\Base\CatchController;
+use Catch\Exceptions\FailedException;
+use Illuminate\Http\Request;
+
+use Modules\Statistic\Services\UserStatisticsService;
+use Modules\User\Http\Controllers\UserTrait;
+
+class UserStatisticsController extends CatchController
+{
+
+    use  UserTrait;
+    /**
+     *  获取当日数据
+     * name: todayData
+     * date 2023/06/20 09:45
+     */
+    public function todayData(Request $request)
+    {
+        $miniProgramId = $request->input('miniprogram_id',0);
+        if ($miniProgramId < 1){
+            throw new FailedException("参数错误,小程序id必传");
+        }
+        $userContext = $this->getUserContext(null);
+        $type = 1;
+        if( $userContext['loginUserRoles']->contains('company')){
+            $type = 2;
+        }else if ($userContext['loginUserRoles']->contains('optimizer')){
+            $type = 3;
+        }
+        $uid = $this->getLoginUserId();
+        return UserStatisticsService::getTodayData($uid,$miniProgramId,$type);
+    }
+
+
+}

+ 32 - 0
modules/Statistic/Installer.php

@@ -0,0 +1,32 @@
+<?php
+
+namespace Modules\Statistic;
+
+use Catch\Support\Module\Installer as ModuleInstaller;
+use Modules\Statistic\Providers\StatisticServiceProvider;
+
+class Installer extends ModuleInstaller
+{
+    protected function info(): array
+    {
+        // TODO: Implement info() method.
+        return [
+            'title' => '统计管理',
+            'name' => 'statistic',
+            'path' => 'statistic',
+            'keywords' => '统计管理',
+            'description' => '统计管理模块',
+            'provider' => StatisticServiceProvider::class
+        ];
+    }
+
+    protected function requirePackages(): void
+    {
+        // TODO: Implement requirePackages() method.
+    }
+
+    protected function removePackages(): void
+    {
+        // TODO: Implement removePackages() method.
+    }
+}

+ 20 - 0
modules/Statistic/Providers/StatisticServiceProvider.php

@@ -0,0 +1,20 @@
+<?php
+
+namespace Modules\Statistic\Providers;
+
+use Catch\CatchAdmin;
+use Catch\Providers\CatchModuleServiceProvider;
+
+class StatisticServiceProvider extends CatchModuleServiceProvider
+{
+    /**
+     * route path
+     *
+     * @return string
+     */
+    public function moduleName(): string
+    {
+        // TODO: Implement path() method.
+        return 'statistic';
+    }
+}

+ 126 - 0
modules/Statistic/Services/UserStatisticsService.php

@@ -0,0 +1,126 @@
+<?php
+/**
+ *
+ * @file:UserStatisticsService.php
+ * @Date: 2023/6/20
+ * @Time: 09:31
+ */
+
+
+namespace Modules\Statistic\Services;
+
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Redis;
+use Modules\Common\Services\BaseService;
+use Modules\User\Http\Controllers\UserTrait;
+
+class UserStatisticsService extends BaseService
+{
+
+    // 日期:小程序id
+    protected const PROMOTION_STATISTIC_RECORD_REDIS_KEY = 'statistic:miniprogram:users_recode:%s:%s';
+    protected const NEW_USER_NUM = 'new_user_num_%s'; // 当日新增用户数
+    protected const NEW_USER_RECHARGE_NUM = "new_user_recharge_num_%s"; // 当日新增户充值人数
+    protected const NEW_USER_RECHARGE_TOTAL = "new_user_recharge_total_%s"; // 当日新增户充值金额
+
+    /**
+     *
+     * name: getTodayData
+     * @param int $accountId 账号id
+     * @param int $miniProgramId 小程序id
+     * @param int $type 账号类型 1 其他 2 投放公司 3 优化师
+     * date 2023/06/20 10:20
+     */
+    public static function getTodayData($accountId, $miniProgramId, $type = 1)
+    {
+        $date = date("Y-m-d");
+        // $date = '2023-05-31';
+        $key = sprintf(self::PROMOTION_STATISTIC_RECORD_REDIS_KEY, $date, $miniProgramId);
+        $new_user_recharge_total = self::getValue($key, sprintf(self::NEW_USER_RECHARGE_TOTAL, $accountId)); // 当日新增用户充值总额
+        $new_user_recharge_num = self::getValue($key, sprintf(self::NEW_USER_RECHARGE_NUM, $accountId)); // 当日新增用户充值人数
+        $new_user_num = self::getValue($key, sprintf(self::NEW_USER_NUM, $accountId)); // 当日新增用户人数
+        [$recharge_coin_num, $recharge_vip_num] = self::getRechargeUserNum($accountId, $miniProgramId, $type);
+        $data = [
+            'key' => $key,
+            'account_id' =>  $accountId,
+            'date' => $date,
+            'new_user_recharge_total' => $new_user_recharge_total ?: 0,
+            'new_user_recharge_num' => $new_user_recharge_num ?: 0,
+            'new_user_num' => $new_user_num ?: 0,
+            'recharge_coin_num' => $recharge_coin_num ?: 0,
+            'recharge_vip_num' => $recharge_vip_num ?: 0,
+        ];
+  
+        if ($new_user_num > 0 && $new_user_recharge_num > 0) {
+            $data['recharge_rate'] = sprintf('%.2f%', ($new_user_recharge_num / $new_user_num) * 100);
+        } else {
+            $data['recharge_rate'] = 0;
+            $data['recharge_mean'] = 0;
+        }
+        if ($new_user_recharge_total > 0 || $new_user_recharge_num > 0) {
+            $data['recharge_mean'] = sprintf('%.2f', ($new_user_recharge_total / $new_user_recharge_num));
+        } else {
+            $data['recharge_mean'] = 0;
+        }
+
+        return $data;
+    }
+
+    // 获取redis值
+    protected static function getValue($key, $field)
+    {
+        return Redis::hget($key, $field);
+    }
+
+    // 保存redis的值
+    protected static function setValue($key, $field, $value)
+    {
+        Redis::hset($key, $field, $value);
+    }
+
+    /**
+     *  充值人数查询
+     * name: getRechargeUserNum
+     * @param $accountId 账号id
+     * @param  $miniProgramId 小程序id
+     * @param mixed $type 账号类型 2 投放公司 3 优化师
+     * date 2023/06/20 10:18
+     */
+    private static function getRechargeUserNum($accountId, $miniProgramId, mixed $type): array
+    {
+        $start = date("Y-m-d") . " 00:00:00";
+        $end = date("Y-m-d") . " 23:59:59";
+        // $start = "2023-05-31 00:00:00";
+        // $end = "2023-05-31 23:59:59";
+        if ($type == 2) {
+            // 投放公司
+            $recharge_coin_num = DB::table('orders')->where('status', "<>", 'UNPAID')
+                ->where('miniprogram_id', $miniProgramId)
+                ->where('puser_id', $accountId)->whereBetween("created_at", [$start, $end])
+                ->whereBetween('ranse_created_at', [$start, $end])
+                ->where('order_type', 'COIN')->groupBy('uid', 'ranse_created_at')->count();
+            $recharge_vip_num = DB::table('orders')->where('status', "<>", 'UNPAID')
+                ->where('miniprogram_id', $miniProgramId)
+                ->where('puser_id', $accountId)->whereBetween("created_at",[ $start, $end])
+                ->whereBetween('ranse_created_at', [$start, $end])
+                ->where('order_type', '<>', 'COIN')->groupBy('uid', 'ranse_created_at')->count();
+        } elseif ($type == 3) {
+            // 优化师
+            $recharge_coin_num = DB::table('orders')->where('status', "<>", 'UNPAID')
+                ->where('miniprogram_id', $miniProgramId)
+                ->where('user_id', $accountId)->whereBetween("created_at", [$start, $end])
+                ->whereBetween('ranse_created_at', [$start, $end])
+                ->where('order_type', 'COIN')->groupBy('uid', 'ranse_created_at')->count();
+            $recharge_vip_num = DB::table('orders')->where('status', "<>", 'UNPAID')
+                ->where('miniprogram_id', $miniProgramId)
+                ->where('user_id', $accountId)->whereBetween("created_at",[$start, $end])
+                ->whereBetween('ranse_created_at',[ $start, $end])
+                ->where('order_type', '<>', 'COIN')->groupBy('uid', 'ranse_created_at')->count();
+        } else {
+            // 其他暂不统计
+            $recharge_coin_num = 0;
+            $recharge_vip_num = 0;
+        }
+        return [$recharge_coin_num, $recharge_vip_num];
+    }
+}

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

@@ -0,0 +1,12 @@
+<?php
+
+use Illuminate\Support\Facades\Route;
+use Modules\Statistic\Http\Controllers\UserStatisticsController;
+
+Route::prefix('statistic')->group(function(){
+    //next
+    // 用户统计
+    Route::prefix('users')->group(function (){
+        Route::any('today',[UserStatisticsController::class,'todayData']);
+    });
+});