PC-202304251453\Administrator 6 сар өмнө
parent
commit
e47251854a

+ 41 - 0
app/process/BaseTotal.php

@@ -0,0 +1,41 @@
+<?php
+
+namespace app\process;
+use app\v1\logic\tool\BaseTotalDayLogic;
+use app\v1\logic\tool\BaseTotalHourLogic;
+use Workerman\Crontab\Crontab;
+
+/**
+ * 基础数据统计
+ */
+class BaseTotal
+{
+    public function onWorkerStart(): void
+    {
+        // 每5分钟执行一次
+        new Crontab('0 */5 * * * *', function() {
+            (new BaseTotalDayLogic())->run();
+        });
+
+        // 每5分钟执行一次
+        new Crontab('0 */5 * * * *', function() {
+            (new BaseTotalHourLogic())->run();
+        });
+    }
+
+    // {"type":"day","sdate":"2025-06-25","edate":"2025-06-26"}
+    public function run($args)
+    {
+        $params = $args ? json_decode($args, true) : [];
+
+        if(!empty($params["type"]) && $params["type"] == "day"){
+            return (new BaseTotalDayLogic())->run();
+        }
+
+        if(!empty($params["type"]) && $params["type"] == "hour"){
+            return (new BaseTotalHourLogic())->run();
+        }
+
+        return "无执行内容";
+    }
+}

+ 4 - 1
app/process/CreateTables.php

@@ -4,6 +4,9 @@ namespace app\process;
 use support\think\Db;
 use Workerman\Crontab\Crontab;
 
+/**
+ * 建表
+ */
 class CreateTables
 {
     public function onWorkerStart(): void
@@ -37,7 +40,7 @@ class CreateTables
                 $sql = str_replace('{{YEAR}}', $year, $sql);
                 $sql = str_replace('{{MONTH}}', $month, $sql);
                 // echo "创建表:".$sql."\n";
-                Db::connect("report_data")->execute($sql);
+                Db::connect("db_data_report")->execute($sql);
             }catch (\Exception $e){
                 echo $e->getMessage();
             }

+ 184 - 0
app/v1/logic/tool/BaseTotalDayLogic.php

@@ -0,0 +1,184 @@
+<?php
+
+namespace app\v1\logic\tool;
+
+
+use support\think\Db;
+
+class BaseTotalDayLogic
+{
+    protected string $table = "base_total_day";
+    protected string $group = 'game_id, agent_id, site_id';
+
+    protected array $data;
+
+    protected string $date;
+    public function run($params=[])
+    {
+        $sDate = $params['sdate'] ?? date('Y-m-d');
+        $eDate = $params['edate'] ?? date('Y-m-d');
+
+        for ($date = $sDate; $date <= $eDate; $date = date('Y-m-d', strtotime($date . '+1 day'))){
+            $this->date = $date;
+            $this->table = $this->table . "_" . date('Y', strtotime($this->date));
+
+            try {
+                $this->initStart();
+            }catch (\Exception $e){
+                return json_encode(["status"=>"error", "msg"=>$e->getMessage()], 256);
+            }
+        }
+
+        return json_encode(["status"=>"success", "msg"=>""], 256);
+    }
+
+    protected function initStart(): void
+    {
+        // 重置数据
+        $this->data = [];
+
+        $this->regData();
+
+        $this->loginData();
+
+        $this->oldLoginData();
+
+        $this->payRegData();
+
+        $this->payTotalData();
+
+        $this->roleCreateData();
+
+        $this->siteAuth();
+
+        $this->replaceData();
+    }
+
+    protected function regData()
+    {
+        $regTotalTb = "basic_reg_total_" . date('Y', strtotime($this->date));
+        $filed = "{$this->group}, sum(reg_count) as reg_total, sum(login_count) as reg_login_total";
+        $where = ['tdate' => $this->date];
+        $result = Db::connect('db_data_report')->table($regTotalTb)->field($filed)->where($where)->group($this->group)->select();
+        $this->pushData($result);
+    }
+
+    protected function loginData()
+    {
+        $loginGameTb = "basic_login_total_game_" . date('Y', strtotime($this->date));
+        $filed = "{$this->group},sum(login_count) as login_total";
+        $where = ['tdate' => $this->date];
+        $result = Db::connect('db_data_report')->table($loginGameTb)->field($filed)->where($where)->group($this->group)->select();
+        $this->pushData($result);
+    }
+
+    protected function oldLoginData()
+    {
+        $loginGameTb = "basic_login_total_game_" . date('Y', strtotime($this->date));
+        $filed = "{$this->group},sum(login_count) as old_login_total";
+        $where = [
+            'tdate' => $this->date,
+            ['active', '>', 0]
+        ];
+        $result = Db::connect('db_data_report')->table($loginGameTb)->field($filed)->where($where)->group('agent_id,site_id,game_id')->select();
+        $this->pushData($result);
+    }
+
+    // 付费统计 - 当天注册
+    protected function payRegData()
+    {
+        $tb = "sdk_order_success";
+
+        $sDate = "{$this->date} 00:00:00";
+        $eDate = "{$this->date} 23:59:59";
+
+        $filed = "{$this->group}, sum(money) as reg_pay_total, sum(paid_amount) as reg_pay_amount, count(distinct user_name) as reg_pay_num";
+
+        $where = [
+            ['reg_date', 'between', [$sDate, $eDate]],
+            ['pay_date', 'between', [$sDate, $eDate]],
+        ];
+
+        $result = Db::connect('db_game_log')->table($tb)->field($filed)->where($where)->group($this->group)->select();
+        $this->pushData($result);
+    }
+
+    // 付费统计 - 总
+    protected function payTotalData(): void
+    {
+        $tb = "sdk_order_success";
+
+        $sDate = "{$this->date} 00:00:00";
+        $eDate = "{$this->date} 23:59:59";
+
+        $filed = "{$this->group}, sum(money) as pay_total, sum(paid_amount) as pay_amount, count(distinct user_name) as pay_num";
+
+        $where = [
+            ['pay_date', 'between', [$sDate, $eDate]],
+        ];
+
+        $result = Db::connect('db_game_log')->table($tb)->field($filed)->where($where)->group($this->group)->select();
+
+        $this->pushData($result);
+    }
+
+    /**
+     * role_create_user 创建角色用户数
+     */
+    protected function roleCreateData(): void
+    {
+        $sTime = $this->date . " 00:00:00";
+        $eTime = $this->date . " 23:59:59";
+
+        $filed = "{$this->group}, COUNT(DISTINCT uid) as role_create_user";
+        $where = [
+            ['create_time', 'between', [$sTime, $eTime]],
+        ];
+
+        // 安卓
+        $result = Db::connect('db_game_log')->table("role_data_and")->field($filed)->where($where)->group($this->group)->select();
+        $this->pushData($result);
+
+        // iOS
+        $result = Db::connect('db_game_log')->table("role_data_ios")->field($filed)->where($where)->group($this->group)->select();
+        $this->pushData($result);
+    }
+
+    protected function siteAuth(): void
+    {
+        $agentList = Db::connect('db_advert')->table("agent")->column("id, media_id, auth_id");
+        $agentListMap = array_column($agentList, null, "id");
+
+        if($this->data){
+            foreach ($this->data as &$item){
+                $item['auth_id'] = $agentListMap[$item['agent_id']]['auth_id'] ?? 0;    // 负责人
+                $item['media_id'] = $agentListMap[$item['agent_id']]['media_id'] ?? 0;  // 媒体
+            }
+        }
+    }
+
+    protected function replaceData(): bool
+    {
+        // 先删除,再写入
+        Db::connect('db_data_report')->table($this->table)->where(['tdate' => $this->date])->delete();
+
+        return Db::connect('db_data_report')->table($this->table)->insertAll($this->data) !== false;
+    }
+
+    protected function pushData($rows): void
+    {
+        if ($rows) {
+            foreach ($rows as $row) {
+                $groupArr = explode(',', $this->group);
+                $uniqueKey = "";
+                foreach ($groupArr as $groupKey){
+                    $uniqueKey .= $row[$groupKey] . "-";
+                }
+
+                foreach ($row as $k => $v) {
+                    $this->data[$uniqueKey][$k] = !empty($this->data[$uniqueKey][$k]) ? $this->data[$uniqueKey][$k] + $v :$v;
+                }
+            }
+        }
+    }
+}

+ 184 - 0
app/v1/logic/tool/BaseTotalHourLogic.php

@@ -0,0 +1,184 @@
+<?php
+
+namespace app\v1\logic\tool;
+
+
+use support\think\Db;
+
+class BaseTotalHourLogic
+{
+    protected string $table = "base_total_day";
+    protected string $group = 'game_id, agent_id, site_id';
+
+    protected array $data;
+
+    protected string $date;
+    public function run($params=[])
+    {
+        $sDate = $params['sdate'] ?? date('Y-m-d');
+        $eDate = $params['edate'] ?? date('Y-m-d');
+
+        for ($date = $sDate; $date <= $eDate; $date = date('Y-m-d', strtotime($date . '+1 day'))){
+            $this->date = $date;
+            $this->table = $this->table . "_" . date('Y', strtotime($this->date));
+
+            try {
+                $this->initStart();
+            }catch (\Exception $e){
+                return json_encode(["status"=>"error", "msg"=>$e->getMessage()], 256);
+            }
+        }
+
+        return json_encode(["status"=>"success", "msg"=>""], 256);
+    }
+
+    protected function initStart(): void
+    {
+        // 重置数据
+        $this->data = [];
+
+        $this->regData();
+
+        $this->loginData();
+
+        $this->oldLoginData();
+
+        $this->payRegData();
+
+        $this->payTotalData();
+
+        $this->roleCreateData();
+
+        $this->siteAuth();
+
+        $this->replaceData();
+    }
+
+    protected function regData()
+    {
+        $regTotalTb = "basic_reg_total_" . date('Y', strtotime($this->date));
+        $filed = "{$this->group}, sum(reg_count) as reg_total, sum(login_count) as reg_login_total";
+        $where = ['tdate' => $this->date];
+        $result = Db::connect('db_data_report')->table($regTotalTb)->field($filed)->where($where)->group($this->group)->select();
+        $this->pushData($result);
+    }
+
+    protected function loginData()
+    {
+        $loginGameTb = "basic_login_total_game_" . date('Y', strtotime($this->date));
+        $filed = "{$this->group},sum(login_count) as login_total";
+        $where = ['tdate' => $this->date];
+        $result = Db::connect('db_data_report')->table($loginGameTb)->field($filed)->where($where)->group($this->group)->select();
+        $this->pushData($result);
+    }
+
+    protected function oldLoginData()
+    {
+        $loginGameTb = "basic_login_total_game_" . date('Y', strtotime($this->date));
+        $filed = "{$this->group},sum(login_count) as old_login_total";
+        $where = [
+            'tdate' => $this->date,
+            ['active', '>', 0]
+        ];
+        $result = Db::connect('db_data_report')->table($loginGameTb)->field($filed)->where($where)->group('agent_id,site_id,game_id')->select();
+        $this->pushData($result);
+    }
+
+    // 付费统计 - 当天注册
+    protected function payRegData()
+    {
+        $tb = "sdk_order_success";
+
+        $sDate = "{$this->date} 00:00:00";
+        $eDate = "{$this->date} 23:59:59";
+
+        $filed = "{$this->group}, sum(money) as reg_pay_total, sum(paid_amount) as reg_pay_amount, count(distinct user_name) as reg_pay_num";
+
+        $where = [
+            ['reg_date', 'between', [$sDate, $eDate]],
+            ['pay_date', 'between', [$sDate, $eDate]],
+        ];
+
+        $result = Db::connect('db_game_log')->table($tb)->field($filed)->where($where)->group($this->group)->select();
+        $this->pushData($result);
+    }
+
+    // 付费统计 - 总
+    protected function payTotalData(): void
+    {
+        $tb = "sdk_order_success";
+
+        $sDate = "{$this->date} 00:00:00";
+        $eDate = "{$this->date} 23:59:59";
+
+        $filed = "{$this->group}, sum(money) as pay_total, sum(paid_amount) as pay_amount, count(distinct user_name) as pay_num";
+
+        $where = [
+            ['pay_date', 'between', [$sDate, $eDate]],
+        ];
+
+        $result = Db::connect('db_game_log')->table($tb)->field($filed)->where($where)->group($this->group)->select();
+
+        $this->pushData($result);
+    }
+
+    /**
+     * role_create_user 创建角色用户数
+     */
+    protected function roleCreateData(): void
+    {
+        $sTime = $this->date . " 00:00:00";
+        $eTime = $this->date . " 23:59:59";
+
+        $filed = "{$this->group}, COUNT(DISTINCT uid) as role_create_user";
+        $where = [
+            ['create_time', 'between', [$sTime, $eTime]],
+        ];
+
+        // 安卓
+        $result = Db::connect('db_game_log')->table("role_data_and")->field($filed)->where($where)->group($this->group)->select();
+        $this->pushData($result);
+
+        // iOS
+        $result = Db::connect('db_game_log')->table("role_data_ios")->field($filed)->where($where)->group($this->group)->select();
+        $this->pushData($result);
+    }
+
+    protected function siteAuth(): void
+    {
+        $agentList = Db::connect('db_advert')->table("agent")->column("id, media_id, auth_id");
+        $agentListMap = array_column($agentList, null, "id");
+
+        if($this->data){
+            foreach ($this->data as &$item){
+                $item['auth_id'] = $agentListMap[$item['agent_id']]['auth_id'] ?? 0;    // 负责人
+                $item['media_id'] = $agentListMap[$item['agent_id']]['media_id'] ?? 0;  // 媒体
+            }
+        }
+    }
+
+    protected function replaceData(): bool
+    {
+        // 先删除,再写入
+        Db::connect('db_data_report')->table($this->table)->where(['tdate' => $this->date])->delete();
+
+        return Db::connect('db_data_report')->table($this->table)->insertAll($this->data) !== false;
+    }
+
+    protected function pushData($rows): void
+    {
+        if ($rows) {
+            foreach ($rows as $row) {
+                $groupArr = explode(',', $this->group);
+                $uniqueKey = "";
+                foreach ($groupArr as $groupKey){
+                    $uniqueKey .= $row[$groupKey] . "-";
+                }
+
+                foreach ($row as $k => $v) {
+                    $this->data[$uniqueKey][$k] = !empty($this->data[$uniqueKey][$k]) ? $this->data[$uniqueKey][$k] + $v :$v;
+                }
+            }
+        }
+    }
+}

+ 107 - 2
config/think-orm.php

@@ -73,13 +73,118 @@ return [
                 'heartbeat_interval' => 50, // 心跳检测间隔,需要小于60秒
             ],
         ],
-        'report_data' => [
+        'db_data_report' => [
             // 数据库类型
             'type' => getenv('DB_TYPE'),
             // 服务器地址
             'hostname' => getenv('DB_HOST'),
             // 数据库名
-            'database' => getenv('DB_REPORT'),
+            'database' => getenv('DB_DATA_REPORT'),
+            // 数据库用户名
+            'username' => getenv('DB_USER'),
+            // 数据库密码
+            'password' => getenv('DB_PASSWORD'),
+            // 数据库连接端口
+            'hostport' => getenv('DB_PORT'),
+            // 数据库连接参数
+            'params' => [
+                // 连接超时3秒
+                \PDO::ATTR_TIMEOUT => 3,
+            ],
+            // 数据库编码默认采用utf8
+            'charset' => 'utf8',
+            // 数据库表前缀
+            'prefix' => getenv('DB_PREFIX'),
+            // 断线重连
+            'break_reconnect' => true,
+            // 自定义分页类
+            'bootstrap' =>  '',
+            // 连接池配置
+            'pool' => [
+                'max_connections' => 5, // 最大连接数
+                'min_connections' => 1, // 最小连接数
+                'wait_timeout' => 3,    // 从连接池获取连接等待超时时间
+                'idle_timeout' => 60,   // 连接最大空闲时间,超过该时间会被回收
+                'heartbeat_interval' => 50, // 心跳检测间隔,需要小于60秒
+            ],
+        ],
+        'db_origin' => [
+            // 数据库类型
+            'type' => getenv('DB_TYPE'),
+            // 服务器地址
+            'hostname' => getenv('DB_HOST'),
+            // 数据库名
+            'database' => getenv('DB_ORIGIN'),
+            // 数据库用户名
+            'username' => getenv('DB_USER'),
+            // 数据库密码
+            'password' => getenv('DB_PASSWORD'),
+            // 数据库连接端口
+            'hostport' => getenv('DB_PORT'),
+            // 数据库连接参数
+            'params' => [
+                // 连接超时3秒
+                \PDO::ATTR_TIMEOUT => 3,
+            ],
+            // 数据库编码默认采用utf8
+            'charset' => 'utf8',
+            // 数据库表前缀
+            'prefix' => getenv('DB_PREFIX'),
+            // 断线重连
+            'break_reconnect' => true,
+            // 自定义分页类
+            'bootstrap' =>  '',
+            // 连接池配置
+            'pool' => [
+                'max_connections' => 5, // 最大连接数
+                'min_connections' => 1, // 最小连接数
+                'wait_timeout' => 3,    // 从连接池获取连接等待超时时间
+                'idle_timeout' => 60,   // 连接最大空闲时间,超过该时间会被回收
+                'heartbeat_interval' => 50, // 心跳检测间隔,需要小于60秒
+            ],
+        ],
+        'db_game_log' => [
+            // 数据库类型
+            'type' => getenv('DB_TYPE'),
+            // 服务器地址
+            'hostname' => getenv('DB_HOST'),
+            // 数据库名
+            'database' => getenv('DB_GAME_LOG'),
+            // 数据库用户名
+            'username' => getenv('DB_USER'),
+            // 数据库密码
+            'password' => getenv('DB_PASSWORD'),
+            // 数据库连接端口
+            'hostport' => getenv('DB_PORT'),
+            // 数据库连接参数
+            'params' => [
+                // 连接超时3秒
+                \PDO::ATTR_TIMEOUT => 3,
+            ],
+            // 数据库编码默认采用utf8
+            'charset' => 'utf8',
+            // 数据库表前缀
+            'prefix' => getenv('DB_PREFIX'),
+            // 断线重连
+            'break_reconnect' => true,
+            // 自定义分页类
+            'bootstrap' =>  '',
+            // 连接池配置
+            'pool' => [
+                'max_connections' => 5, // 最大连接数
+                'min_connections' => 1, // 最小连接数
+                'wait_timeout' => 3,    // 从连接池获取连接等待超时时间
+                'idle_timeout' => 60,   // 连接最大空闲时间,超过该时间会被回收
+                'heartbeat_interval' => 50, // 心跳检测间隔,需要小于60秒
+            ],
+        ],
+        'db_advert' => [
+            // 数据库类型
+            'type' => getenv('DB_TYPE'),
+            // 服务器地址
+            'hostname' => getenv('DB_HOST'),
+            // 数据库名
+            'database' => getenv('DB_ADVERT'),
             // 数据库用户名
             'username' => getenv('DB_USER'),
             // 数据库密码