package com.persagy.proxy.adm.utils;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.persagy.dmp.common.context.AppContext;
import com.persagy.proxy.common.entity.InstanceUrlParam;

/**
 * ADM上下文工具类
 * @author Charlie Yu
 * @date 2021-09-13
 */
public class AdmContextUtil {

    /**
     * 转换获取数据中台上下文参数
     * @return
     */
    public static InstanceUrlParam toDmpContext() {
        return InstanceUrlParam.builder().groupCode(AppContext.getContext().getGroupCode())
                .projectId(AppContext.getContext().getProjectId())
                .appId(AppContext.getContext().getAppId()).build();
    }

    /**
     * 转换获取数据中台上下文参数
     * @return
     */
    public static String generateIdStr(String prefix,Boolean addProjectFlag) {
        if (StrUtil.isNotBlank(AppContext.getContext().getProjectId())
                && addProjectFlag && AppContext.getContext().getProjectId().matches("^Pj[0-9]{10}$")){
            String projectIdSuffix = StrUtil.subSuf(AppContext.getContext().getProjectId(),2);
            return StrUtil.isNotBlank(prefix)?prefix+projectIdSuffix+ IdWorker.getIdStr():projectIdSuffix+ IdWorker.getIdStr();
        }
        return StrUtil.isNotBlank(prefix)?prefix+ IdWorker.getIdStr():IdWorker.getIdStr();
    }

}
