wangting
2024-12-25 9e18636bed4fc3fceec08ca25d6c7916d4d9a429
Source/plt-web/plt-web-parent/plt-web-base/src/main/java/com/vci/starter/web/util/VciBaseUtil.java
@@ -1,8 +1,12 @@
package com.vci.starter.web.util;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.vci.common.exception.VciExceptionTool;
import com.vci.corba.common.PLException;
import com.vci.starter.web.annotation.Id;
import com.vci.starter.web.annotation.VciBtmType;
import com.vci.starter.web.annotation.VciLinkType;
@@ -48,6 +52,23 @@
public class VciBaseUtil {
    /**
     * 获取异常信息,因为抛出的异常不同,获取错误信息的方式存在差异所以无法统一获取
     * 后续有其他异常需要获取,自行添加处理逻辑,
     * @param e
     * @return
     */
    public static String getExceptionMessage(Throwable e){
        String exceptionStr = VciExceptionTool.getExceptionStr(e);
        if(exceptionStr.contains("VciBaseException")){
            return e.getMessage();
        }else if(exceptionStr.contains("PLException")){
            return Arrays.stream(((PLException) e).messages).collect(Collectors.joining("\n"));
        }else {
            return e.getMessage();
        }
    }
    /**
     * 日志对象
     */
    private static Logger log = LoggerFactory.getLogger(VciBaseUtil.class);
@@ -59,6 +80,19 @@
     */
    public static String getPk() {
        return UUID.randomUUID().toString();
    }
    /**
     * 雪花ID
     * @return
     */
    public static String getSnowflakePk() {
        return String.valueOf(getSnowflakePk(1,1));
    }
    public static  Long getSnowflakePk(long workerId,long dataCenterId){
        Snowflake snowflake = IdUtil.getSnowflake(workerId,dataCenterId);
        return snowflake.nextId();
    }
    /**
@@ -253,6 +287,28 @@
    }
    /**
     * 去除最前面的spiltFilter,去除后面的spiltFilter
     * @param s 字符串
     * @param spiltFilter,分隔符
     * @return 去除末尾逗号
     */
    public static String removeComma(String s,String spiltFilter){
        if(s == null || s.trim().length() == 0) {
            return s;
        }
        else{
            if(s.startsWith(spiltFilter)) {
                s = s.substring(spiltFilter.length(), s.length());
            }
            if(s.endsWith(spiltFilter)) {
                s = s.substring(0, s.length() - spiltFilter.length());
            }
            return s;
        }
    }
    /**
     * 为sql中使用in时,提供转换,注意in里的值不能超过1000
     * @param s 字符串
     * @return 返回sql语句
@@ -438,6 +494,21 @@
            charMap.put(String.valueOf(c), (!charMap.containsKey(String.valueOf(c))? 1 : charMap.get(String.valueOf(c)) + 1));
        }
        return charMap.get(String.valueOf(findC));
    }
    /**
     * 带逗号的字符串转为list
     * @param s 字符串
     * @return 字符串列表
     */
    public static List<String> str2List(String s,String spilter){
        if (isNull(s)) {
            return null;
        } else {
            List<String> l = new ArrayList<String>();
            Collections.addAll(l,removeComma(s,spilter).split(spilter));
            return l;
        }
    }
    /**
@@ -743,7 +814,7 @@
        if(c.isArray()){
            return isBasicType(c.getComponentType());
        }
        return ClassUtil.isPrimitive(c);
        return ClassUtilForVCI.isPrimitive(c);
    }
    /**
@@ -1210,7 +1281,7 @@
     * @return 会话对象
     */
    public static SessionInfo getCurrentUserSessionInfoNotException() {
        return WebThreadLocalUtil.getCurrentUserSessionInfoInThread().get();
        return WebThreadLocalUtil.getCurrentUserSessionInfoInThread();
    }
    /**
@@ -1218,7 +1289,7 @@
     * @param sessionInfo 用户对象
     */
    public static void setCurrentUserSessionInfo(SessionInfo sessionInfo){
        WebThreadLocalUtil.getCurrentUserSessionInfoInThread().set(sessionInfo);
        WebThreadLocalUtil.setCurrentUserSessionInfoInThread(sessionInfo);
    }
    /**
@@ -1226,7 +1297,7 @@
     * @return true表示查询
     */
    public static boolean isQueryTotal(){
        String needQueryTotal = WebThreadLocalUtil.getNeedQueryTotalInThread().get();
        String needQueryTotal = WebThreadLocalUtil.getNeedQueryTotalInThread();
        if("false".equalsIgnoreCase(needQueryTotal)){
            return false;
        }else{
@@ -1251,7 +1322,7 @@
        Map<String,Object> map = new HashMap<String,Object>();
        if(o!=null) {
            String jsonString = JSONObject.toJSONStringWithDateFormat(o, VciDateUtil.DateTimeMillFormat, SerializerFeature.WriteDateUseDateFormat);
            if(org.apache.commons.lang3.StringUtils.isNotBlank(jsonString)) {
            if(StringUtils.isNotBlank(jsonString)) {
                JSONObject jsonObject = JSONObject.parseObject(jsonString);
                if(jsonObject!=null){
                    for(String key : jsonObject.keySet()){
@@ -1272,7 +1343,7 @@
        Map<String,String> map = new HashMap<String,String>();
        if(o!=null) {
            String jsonString = JSONObject.toJSONStringWithDateFormat(o, VciDateUtil.DateTimeMillFormat, SerializerFeature.WriteDateUseDateFormat);
            if(org.apache.commons.lang3.StringUtils.isNotBlank(jsonString)) {
            if(StringUtils.isNotBlank(jsonString)) {
                JSONObject jsonObject = JSONObject.parseObject(jsonString);
                if(jsonObject!=null){
                    for(String key : jsonObject.keySet()){
@@ -1299,6 +1370,25 @@
        }
    }
    /**
     * 数组转换为String
     * @param array 数组对象
     * @param spiltFiter 分隔符
     * @return 逗号链接的字符串
     */
    public static String array2String(String[] array,String spiltFiter) {
        if(null == array || array.length == 0) {
            return "";
        } else{
            String ss = "";
            for(String s : array){
                ss += s + spiltFiter;
                //1.8可以
            }
            return removeComma(ss,spiltFiter);
        }
    }
    /**
     * 数组转换为String
     * @param array 数组对象
@@ -1724,14 +1814,13 @@
        return name;
    }
    /**
     * 根据业务类型获取表格名称
     * @param btmname 业务类型,并且不能是视图
     * @return 数据库表格名称
     */
    public static String getTableName(String btmname) {
        return (VciQueryWrapperForDO.USER_TABLE_COMPATIBILITY?"platformbtm_":"vcibt_") + btmname.trim().toLowerCase();
        return (VciQueryWrapperForDO.USER_TABLE_COMPATIBILITY?"pbt_":"vcibt_") + btmname.trim().toLowerCase();
    }
    /**
@@ -1740,7 +1829,7 @@
     * @return 数据库表格名称
     */
    public static String getLinkTableName(String linkName){
        return (VciQueryWrapperForDO.USER_TABLE_COMPATIBILITY?"platformlt_":"vcilt_")+ linkName.trim().toLowerCase();
        return (VciQueryWrapperForDO.USER_TABLE_COMPATIBILITY?"plt_":"vcilt_")+ linkName.trim().toLowerCase();
    }
    /**
@@ -1788,7 +1877,7 @@
     * @param replaceMap 使用替换的数据源
     * @return 替换后的值
     */
    public static String replaceByFreeMarker(String freemarker,Map<String,String> replaceMap){
    public static String replaceByFreeMarker(String freemarker,Map<String,Object> replaceMap){
        if(StringUtils.isBlank(freemarker)){
            return "";
        }
@@ -1929,4 +2018,15 @@
        }
        return new String(c);
    }
    /**
     * 将string集合转为stirng数组
     * @param strCollection string集合
     * @return string数组
     */
    public static String[] collection2StrArr(Collection<String> strCollection){
        String[] strArr = new String[strCollection.size()];
        strCollection.toArray(strArr);
        return strArr;
    }
}