package org.jeecg.modules.stm.utils;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.RedisUtil;

public class UserUtil {

    /**
     * 获取用户代码
     * @return
     */
    public static String getUserCode(){
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        return sysUser.getUsername();
    }

    public static String getUserId(){
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        return sysUser.getId();
    }

    /**
     * 生成模板编码
     * @param curCode
     * @return
     */
    public static String getTplCode(String curCode){
        String strPre = StmConstans.TPL_CODE_PREFIX;
        Integer start_Code = StmConstans.TPL_START_CODE;
        if(curCode == null) {
            return strPre+start_Code.toString();
        }else{
            Integer code  = Integer.valueOf(curCode.replace(strPre,""))+1;
            return  strPre + code.toString();
        }
    }

    public static int getTplItemCode(Integer curCode){
        String strPre = StmConstans.TPL_ITEM_CODE_PREFIX;
        if(curCode == null) {
           return StmConstans.TPL_ITEM_START_CODE;
        } else {
            return curCode + 1;
        }
    }

    public static Integer getTplItemFieldCode(Integer curCode){
        String strPre = StmConstans.TPL_ITEM_FIELD_CODE_PREFIX;
        if(curCode == null) {
            return  StmConstans.TPL_ITEM_FIELD_START_CODE;
        }else {
            return curCode+1;
        }
    }

    public static int getSurveyTplCode(Integer curCode){
        String strPre = StmConstans.SURVEY_TPL_CODE_PREFIX;
        if(curCode == null) {
            return StmConstans.TPL_ITEM_START_CODE;
        } else {
            return curCode + 1;
        }
    }

    public static String getWorkFlowUserToken(RedisUtil redisUtil){
        String userName = UserUtil.getUserCode();
        String strKey = "flowToken_"+userName;
        if(redisUtil!=null&&redisUtil.get(strKey)!=null){
            return redisUtil.get(strKey).toString();
        }
       return  null;
    }
}
