<?php
/**
 * 系统概览公共控制器
 * 提供统一的业务逻辑，供admin和adminghd模块继承使用
 */
namespace app\common\controller;

use app\common\common\BaseController;
use think\Db;

class DashboardBase extends BaseController
{
    /**
     * 获取统计数据
     */
    public function getStatistics()
    {
        try {
            // 获取表名
            $userTable = $this->getTableName('user');
            $infoTable = $this->getTableName('real_time_info');
            $recordTable = $this->getTableName('calculate_record');
            
            // 小程序注册用户数量
            $userCount = Db::name($userTable)->count();
            
            // 首页资讯数量
            $infoCount = Db::name($infoTable)->count();
            
            // 最近一次计算时间
            $lastCalculate = Db::name($recordTable)
                ->order('create_time desc')
                ->value('create_time');
            
            // 今日新增用户
            $todayUserCount = Db::name($userTable)
                ->whereTime('create_time', 'today')
                ->count();
            
            // 总计算次数
            $totalCalculateCount = Db::name($recordTable)->count();
            
            return json([
                'status' => 1,
                'msg' => '获取成功',
                'data' => [
                    'user_count' => $userCount,
                    'info_count' => $infoCount,
                    'last_calculate_time' => $lastCalculate ?: '暂无',
                    'today_user_count' => $todayUserCount,
                    'total_calculate_count' => $totalCalculateCount
                ]
            ]);
        } catch (\Exception $e) {
            return json([
                'status' => 0,
                'msg' => '获取失败：' . $e->getMessage(),
                'data' => [
                    'user_count' => 0,
                    'info_count' => 0,
                    'last_calculate_time' => '暂无',
                    'today_user_count' => 0,
                    'total_calculate_count' => 0
                ]
            ]);
        }
    }
}
