package com.energetic.energetic.util;
import com.energetic.energetic.pojo.DormStructure;
import com.energetic.energetic.pojo.SysUser;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.*;
@SuppressWarnings("all")
public class GenerateCalculatorUtil {
/**
* [
* {"dormParentId":2,"children":[],"dormId":2101,"dormName":"B1层","sign":0},
* {"dormParentId":2,"children":[],"dormId":2102,"dormName":"B2层","sign":0},
* {"dormParentId":2,"children":[],"dormId":2103,"dormName":"B3层","sign":0},
* {"dormParentId":2,"children":[],"dormId":2104,"dormName":"B4层","sign":0},
* {"dormParentId":2,"children":[],"dormId":2105,"dormName":"B5层","sign":0}
* ]
*/
public static List<DormStructure> getTree(int pid, List<DormStructure> list){
List<DormStructure> newTreeList = new ArrayList<>();
for (DormStructure item : list) {
if(pid==item.getDormParentId()) {
item.setChildren(getTree(item.getDormId(), list));
newTreeList.add(item);
}
}
return newTreeList;
}
/**
* 查出当前栋下面的所有的子集的dorm_id
* 1的所有的子集的集合是[1101,110,111,112,113,114,1102,201,202,203,204,205,1103,301,302,303,304,1104,402,401,403,404,405,1105,501,502,503]
* 2的所有的子集的集合是[2101,2102,2103,2104,2105]
* 3的所有的子集的集合是[3101,1415,3102,3103,3104,3201]
*/
public static List<Integer> treeAboutDormId(int pid, List<DormStructure> dormStructureList){
List<Integer> newTreeList = new ArrayList<>();//先声明一个容器
for (DormStructure item : dormStructureList) {//循环过滤出符合条件的传递过来的树的集合
if(pid==item.getDormParentId()) { //
newTreeList.add(item.getDormId()); //第一次添加符合条件的数据, 第二次添加第二层下面的符合条件的数据
newTreeList.addAll(treeAboutDormId(item.getDormId(),dormStructureList));
}
}
return newTreeList;
}
/**
* 递归生成一个1111到99999之间的无重复的随机数
*/
public static int getRandomCode(){
int count = 0;
try {
List list = new ArrayList();
int max=99999;
int min=1111;
Random random = new Random();
count = random.nextInt(max) % (max - min + 1) + min;
if(list.contains(count)){
return getRandomCode();
}
list.add(count);
} catch (Exception e) {
e.printStackTrace();
}
return count;
}
/**
* 登录成功返回客户端一个token
* @param user
* @param Agent
* @return
*/
public static String Token(SysUser user, String Agent)
{
StringBuilder builder=new StringBuilder();
builder.append("PC-");
builder.append(user.getName()+"-");
builder.append(user.getId()+"-");
builder.append(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()));
builder.append(GenerateCalculatorUtil.getMd5(Agent,6));
return builder.toString();
}
//MD5加密的工具类
public static String getMd5(String plainText,int length) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(plainText.getBytes());
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
// 32位
// return buf.toString();
// 16位
// return buf.toString().substring(0, 16);
return buf.toString().substring(0, length);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
}
今天写了几个递归工具类记录一下
最新推荐文章于 2022-11-16 15:52:50 发布

1235

被折叠的 条评论
为什么被折叠?



