<?php
/**
 * 业务配置类
 * 用于区分不同小程序业务的数据表映射
 */
namespace app\common\config;

class BusinessConfig
{
    // 业务类型常量
    const BUSINESS_ADMIN = 'admin';      // 通用后台
    const BUSINESS_ADMINGHD = 'adminghd'; // GHD项目后台
    
    /**
     * 获取业务类型对应的数据表映射
     * @param string $businessType 业务类型
     * @return array
     */
    public static function getTableMap($businessType)
    {
        $maps = [
            self::BUSINESS_ADMIN => [
                'user' => 'wechat_user',
                'real_time_info' => 'wechat_real_time_info',
                'calculate_record' => 'wechat_calculate_record',
            ],
            self::BUSINESS_ADMINGHD => [
                'user' => 'ghd_wechat_user',
                'real_time_info' => 'wechat_real_time_info',
                'calculate_record' => 'wechat_calculate_record',
            ],
        ];
        
        return isset($maps[$businessType]) ? $maps[$businessType] : $maps[self::BUSINESS_ADMIN];
    }
    
    /**
     * 获取业务类型对应的Session标识
     * @param string $businessType 业务类型
     * @return string
     */
    public static function getSessionKey($businessType)
    {
        $keys = [
            self::BUSINESS_ADMIN => 'admin_user_id',
            self::BUSINESS_ADMINGHD => 'adminghd_user_id',
        ];
        
        return isset($keys[$businessType]) ? $keys[$businessType] : 'admin_user_id';
    }
    
    /**
     * 获取业务类型对应的登录时间Session标识
     * @param string $businessType 业务类型
     * @return string
     */
    public static function getLoginTimeKey($businessType)
    {
        $keys = [
            self::BUSINESS_ADMIN => 'admin_user_login_time',
            self::BUSINESS_ADMINGHD => 'adminghd_user_login_time',
        ];
        
        return isset($keys[$businessType]) ? $keys[$businessType] : 'admin_user_login_time';
    }
    
    /**
     * 根据当前模块名获取业务类型
     * @return string
     */
    public static function getBusinessTypeByModule()
    {
        $module = request()->module();
        return $module === 'adminghd' ? self::BUSINESS_ADMINGHD : self::BUSINESS_ADMIN;
    }
    
    /**
     * 获取数据表名
     * @param string $tableKey 表键名（user, real_time_info, calculate_record）
     * @param string|null $businessType 业务类型，为空则自动检测
     * @return string
     */
    public static function getTableName($tableKey, $businessType = null)
    {
        if ($businessType === null) {
            $businessType = self::getBusinessTypeByModule();
        }
        
        $map = self::getTableMap($businessType);
        return isset($map[$tableKey]) ? $map[$tableKey] : $tableKey;
    }
}
