package com.qmth.teachcloud.exchange.common.contant;

import com.aventrix.jnanoid.jnanoid.NanoIdUtils;
import com.qmth.boot.core.uid.service.UidService;
import com.qmth.teachcloud.exchange.common.util.ServletUtil;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.Charset;

/**
 * @Description: 系统常量
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2019/10/11
 */
public class SystemConstant {

    /**
     * 广药mq请求参数
     */
    public static final String DEFAULT_DATE = "yyyyMMdd";
    public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";
    public static final String GDPU_SYS_APP_ID = "sysAppId";
    public static final String GDPU_APP_ID_PARAM = "gdpuAppId";
    public static final String GDPU_APP_SECRET_PARAM = "gdpuAppSecret";
    public static final String GDPU_SYS_APP_ID_PARAM = "gdpuSysAppId";
    public static final String GDPU_SERVICE_ADDRESS_PARAM = "gdpuServiceAddress";

    public static final String GDPU_DOCUN_ID = "docunId";
    public static final String GDPU_HEADER_APP_ID = "appid";
    public static final String GDPU_HEADER_APP_SECRET = "secret";
    public static final String GDPU_CAS_MQ_URL = "http://examsso.gdpu.edu.cn/api/open/gdpu/mq/account";

    /**
     * api前缀
     */
    public static final String PREFIX_URL_OPEN = "/open";
    public static final String PREFIX_URL_GDPU_GET_BACK_LOG = "/getbacklog";
    public static final String PREFIX_URL_GDPU_MQ = "/open/mq";

    /**
     * 系统常量
     */
    public static final String CLIENT_SYSTEM_EXPLICIT_PORT = "";
    public static final String SERVICE_KEY = "service";
    public static final String CAS_ADDRESS = "casAddress";
    public static final String SCHOOL_SERVICE_ADDRESS = "schoolServiceAddress";
    public static final String CHARSET_NAME = "UTF-8";
    public static final Charset CHARSET = Charset.forName(CHARSET_NAME);
    public static final String SUCCESS = "success";
    public static final String UPDATE_TIME = "updateTime";
    public static final String SESSION = "session:";
    public static final String USER = "account";
    public static final String FUNCTIONAL_TYPE = "functionalType";
    public static final String SCHOOL_ID = "schoolId";
    public static final String SCHOOL = "school";
    public static final String RETURN_URL = "returnUrl";
    public static final String PARAMS = "params";
    public static final String ORG = "org";
    public static final String MD5 = "MD5";
    public static final String ID = "id";
    public static final String SIGNATURE = "signature";
    public static final String GET = "get";
    public static final String GET_UNKNOWN = "?";
    public static final String GET_SYMBOL = "&";
    public static final String GET_EQUAL = "=";
    public static final String PATH_MATCH = "*";
    public static final String PATH_SUBSTR = "/";
    public static final String METHOD = "post";
    public static final String TEACH_CLOUD_PRINT = "teachcloudPrint";
    public static final String CAS_VUE_LOGIN_PATH = "/login-open";
    public static final String SCHOOL_CODE = "${schoolCode}";
    public static final String ACCESS_CONTROL_ALLOW_ORIGIN = "Access-Control-Allow-Origin";

    /**
     * 缓存配置
     */
    public static final String SCHOOL_CACHE = "school:cache";
    public static final String SCHOOL_CODE_CACHE = "school:code:cache";
    public static final String SYS_SETTING_CACHE = "sys:setting:cache";

    /**
     * 鉴权
     */
    public static final String HEADER_AUTHORIZATION = "Authorization";
    public static final String HEADER_TIME = "time";
    public static final String HEADER_PLATFORM = "platform";
    public static final String HEADER_DEVICE_ID = "deviceId";
    public static final String TOKEN = "token";
    public static final String SIGN = "sign";
    public static final String LOG_ERROR = "请求出错:{}";

    /**
     * http设置
     */
    public static final int CONNECT_TIME_OUT = 1000 * 60 * 2;//请求超时
    public static final int SOCKET_CONNECT_TIME_OUT = 1000 * 60 * 30;//读取数据超时

    /**
     * 获取数据库uuid
     *
     * @return
     */
    public static Long getDbUuid() {
        UidService uidservice = SpringContextHolder.getBean(UidService.class);
        return uidservice.getId();
    }

    /**
     * 获得基础路径
     *
     * @return
     */
    public static String getBasePath() {
        HttpServletRequest request = ServletUtil.getRequest();
        String scheme = request.getScheme();
        String serverName = request.getServerName();
        int serverPort = request.getServerPort();
        String contextPath = request.getContextPath();

        // 判断是否配置了显式端口
        boolean explicit_port = CLIENT_SYSTEM_EXPLICIT_PORT != null && !"".equals(CLIENT_SYSTEM_EXPLICIT_PORT);
        if (explicit_port) {
            try {
                serverPort = Integer.parseInt(CLIENT_SYSTEM_EXPLICIT_PORT);
            } catch (Exception e) {
                // 异常时赋值，方便双方排查问题
                serverPort = 19000;
            }
            return scheme + "://" + serverName + ":" + serverPort + contextPath + "/";
        } else {
            if ((serverPort == 80) || (serverPort == 443)) {
                return scheme + "://" + serverName + contextPath;
            } else {
                return scheme + "://" + serverName + ":" + serverPort + contextPath;
            }
        }
    }

    /**
     * 获取nanoId
     *
     * @return
     */
    public static String getNanoId() {
        return NanoIdUtils.randomNanoId();
    }
}