田源
2024-08-23 9e20b9fb77a41cb5b4a6eb6213fc51cab1f0bb91
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;
        }
    }
    /**
@@ -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{
@@ -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 数组对象
@@ -1929,4 +2019,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;
    }
}