| | |
| | | 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; |
| | |
| | | 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); |
| | |
| | | */ |
| | | 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(); |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | /** |
| | | * 去除最前面的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语句 |
| | |
| | | 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; |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | if(c.isArray()){ |
| | | return isBasicType(c.getComponentType()); |
| | | } |
| | | return ClassUtil.isPrimitive(c); |
| | | return ClassUtilForVCI.isPrimitive(c); |
| | | } |
| | | |
| | | /** |
| | |
| | | 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()){ |
| | |
| | | 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()){ |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 数组转换为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 数组对象 |
| | |
| | | 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(); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @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(); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @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 ""; |
| | | } |
| | |
| | | } |
| | | 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; |
| | | } |
| | | } |