package com.persagy.dmp.rwd.basic.utils;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.persagy.dmp.basic.dto.RequestData;
import com.persagy.dmp.common.constant.CommonConstant;
import com.persagy.dmp.common.helper.SpringHelper;
import com.persagy.dmp.rwd.basic.constant.DigitalObjectType;
import com.persagy.dmp.rwd.basic.constant.DigitalObjectTypeShort;

/**
 * 数据孪生通用工具
 * @author Charlie Yu
 * @date 2021-06-25
 */
public class DigitalCommonUtils {

    /**
     * 生成对象主键
     * @param projectId 项目ID
     * @param category 对象类型
     * @return 两位prefix + 无前缀projectId + simpleUUID
     */
    public static String fetchObjectId(String projectId, String category) {
        if (DigitalObjectType.PROJECT.getIndex().equalsIgnoreCase(category)) {
            return projectId;
        }
        String prefix = getIdPrefix(category);
        if(StrUtil.isBlank(prefix)) {
            return IdUtil.simpleUUID();
        }
        return prefix + projectId.substring(2) + IdUtil.simpleUUID();
    }

    /**
     * 根据对象类型获取Id前缀
     * @param category 对象类型
     * @return 两位的前缀 - 空字符串表示无效的对象类型
     */
    private static String getIdPrefix(String category) {
        if(DigitalObjectType.PROJECT.getIndex().equalsIgnoreCase(category)) {
            return DigitalObjectTypeShort.PROJECT.getIndex();
        } else if (DigitalObjectType.BUILDING.getIndex().equalsIgnoreCase(category)) {
            return DigitalObjectTypeShort.BUILDING.getIndex();
        } else if (DigitalObjectType.FLOOR.getIndex().equalsIgnoreCase(category)) {
            return DigitalObjectTypeShort.FLOOR.getIndex();
        } else if (DigitalObjectType.SPACE.getIndex().equalsIgnoreCase(category)) {
            return DigitalObjectTypeShort.SPACE.getIndex();
        } else if (DigitalObjectType.SYSTEM.getIndex().equalsIgnoreCase(category)) {
            return DigitalObjectTypeShort.SYSTEM.getIndex();
        } else if (DigitalObjectType.EQUIPMENT.getIndex().equalsIgnoreCase(category)) {
            return DigitalObjectTypeShort.EQUIPMENT.getIndex();
        } else if (DigitalObjectType.SHAFT.getIndex().equalsIgnoreCase(category)) {
            return DigitalObjectTypeShort.SHAFT.getIndex();
        } else if (DigitalObjectType.COMPONENT.getIndex().equalsIgnoreCase(category)) {
            return DigitalObjectTypeShort.COMPONENT.getIndex();
        } else if (DigitalObjectType.VIRTUAL.getIndex().equalsIgnoreCase(category)) {
            return DigitalObjectTypeShort.VIRTUAL.getIndex();
        } else {
            return "";
        }
    }

    /***
     * Description: 获取默认的图实例id
     * @param graphCode : 图类型编码
     * @return : java.lang.String
     * @author : lijie
     * @date :2021/9/3 16:07
     * Update By lijie 2021/9/3 16:07
     */
    public static String getDefaultGraphId(String graphCode) {
        return StrUtil.format(CommonConstant.DEFAULT_GRAPH_ID_FORMAT,graphCode);
    }

    /***
     * Description: 计算分页参数
     * @param requestData : 请求参数
     * @return : void
     * @author : lijie
     * @date :2021/8/30 10:01
     * Update By lijie 2021/8/30 10:01
     */
    public static void computePageParam(RequestData requestData) {
        if (null==requestData.getPage()){
            requestData.setPage(1L);
        }
        // 默认分页条数,防止大量查询
        long defaultSize = SpringHelper.getLong(CommonConstant.QUERY_PAGE_SIZE_KEY, CommonConstant.QUERY_DEFAULT_PAGE_SIZE);
        if (null==requestData.getSize() || requestData.getSize()>defaultSize){
            requestData.setSize(defaultSize);
        }
        requestData.setSkip((requestData.getPage()-1)*requestData.getSize());
    }
    /***
     * Description: 获得分页对象
     * @param requestData : 请求参数
     * @return : void
     * @author : lijie
     * @date :2021/8/30 10:01
     * Update By lijie 2021/8/30 10:01
     */
    public static <T> Page<T> computePageObject(RequestData requestData) {
        computePageParam(requestData);
        Page<T> page = new Page<>();
        page.setSize(requestData.getSize());
        page.setCurrent(requestData.getPage());
        return page;
    }
}
