package com.xiaobodata.biz.modules.indicatrix.util;
import cn.hutool.core.util.IdUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.sun.istack.Nullable;
import com.xiaobodata.biz.common.system.api.ISysBaseAPI;
import com.xiaobodata.biz.common.system.vo.DictModel;
import com.xiaobodata.biz.common.system.vo.LoginUser;
import com.xiaobodata.biz.modules.indicatrix.exception.XiaoBoDataException;
import com.xiaobodata.biz.modules.system.service.impl.SysBaseApiImpl;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@Component
public class CheckUtils {
private static ISysBaseAPI sysBaseAPI;
@Autowired
public void setSysBaseAPI(ISysBaseAPI sysBaseAPI) {
CheckUtils.sysBaseAPI = sysBaseAPI;
}
public static void checkCollections(@Nullable String collectionName, Collection<?> collection){
if(CollectionUtils.isEmpty(collection)){
throw new XiaoBoDataException(collectionName + "为空");
}
}
public static void checkNotNull(@Nullable String name,Object object){
if(object == null){
throw new XiaoBoDataException(name + "为空");
}
}
public static void checkString(@Nullable String paramName, String param){
if(StringUtils.isEmpty(param)){
throw new XiaoBoDataException(paramName + "参数为空");
}
}
public static void checkLength(String paramName,String param,int limit){
checkString(paramName,param);
if(param.length() > limit){
throw new XiaoBoDataException(paramName + "长度超出最大长度" + limit + " ,请重新输入");
}
}
public static void checkLengthNotCheckIsNull(String paramName,String param,int limit){
if(param==null)
return ;
if(param.length() > limit){
throw new XiaoBoDataException(paramName + "长度超出最大长度" + limit + " ,请重新输入");
}
}
public static void isChineseAndEnglishAndFigures(String paramName,String content,Integer lengthStart,Integer lengthEnd){
if(!Pattern.matches("^[\\u4E00-\\u9FA5A-Za-z0-9]{"+lengthStart+","+lengthEnd+"}+$", content)){
throw new XiaoBoDataException(paramName+" 仅支持"+lengthEnd+"位中英文、数字");
}
}
public static void isChineseAndEnglishAndFiguresAndUnderline(String paramName,String content,Integer lengthStart,Integer lengthEnd){
if (!Pattern.matches("^[\\u4E00-\\u9FA5_A-Za-z0-9]{"+lengthStart+","+lengthEnd+"}+$", content)) {
throw new XiaoBoDataException(paramName+" 仅支持"+lengthEnd+"位中英文、数字和_");
}
}
public static void isChineseAndEnglishAndFiguresAndHaveBracket(String paramName,String content,Integer lengthStart,Integer lengthEnd){
if (!Pattern.matches("^[\\u4E00-\\u9FA5()()A-Za-z0-9]{"+lengthStart+","+lengthEnd+"}+$", content)) {
throw new XiaoBoDataException(paramName+" 仅支持"+lengthEnd+"位中英文、数字和{}");
}
}
public static void isChineseAndEnglishAndFiguresAndComma(String paramName,String content,Integer lengthStart,Integer lengthEnd){
if (!Pattern.matches("^[\\u4E00-\\u9FA5,,A-Za-z0-9]{"+lengthStart+","+lengthEnd+"}+$", content)) {
throw new XiaoBoDataException(paramName+" 仅支持"+lengthEnd+"位中英文、数字和,");
}
}
public static LoginUser getCurrentUser(){
return (LoginUser) SecurityUtils.getSubject().getPrincipal();
}
public static String getId(){
return IdUtil.createSnowflake(0L,0L).nextIdStr();
}
public static void CheckDictItem(String paramName,String code,String param){
List<DictModel> dictItems = sysBaseAPI.getDictItems(code);
if(!dictItems.isEmpty()){
Set<String> collect = dictItems.stream().map(DictModel::getValue).collect(Collectors.toSet());
if(!collect.contains(param)){
throw new XiaoBoDataException("请选择正确的"+paramName);
}
}
}
}