8 changed files with 317 additions and 19 deletions
@ -0,0 +1,141 @@
|
||||
package com.biutag.lan; |
||||
|
||||
|
||||
import com.alibaba.excel.EasyExcel; |
||||
import com.biutag.lan.entity.DeptTree; |
||||
import com.biutag.lan.entity.DeptNode; |
||||
import com.biutag.lan.mapper.DeptTreeMapper; |
||||
import jakarta.annotation.Resource; |
||||
import org.junit.jupiter.api.Test; |
||||
import org.springframework.boot.test.context.SpringBootTest; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.stream.Collectors; |
||||
|
||||
@SpringBootTest |
||||
public class DeptTreeTreeBuild { |
||||
@Resource |
||||
private DeptTreeMapper deptTreeMapper; |
||||
|
||||
private List<DeptTree> processedNodes = new ArrayList<>(); |
||||
|
||||
public List<DeptNode> tree() { |
||||
List<DeptTree> list = deptTreeMapper.selectList(null); |
||||
return buildTree(list); |
||||
} |
||||
|
||||
private List<DeptNode> buildTree(List<DeptTree> list) { |
||||
List<DeptNode> threeNode = list.stream() |
||||
.filter(item -> item.getLevel() == 3) |
||||
.map(item -> new DeptNode() |
||||
.setCategory(item.getCategory()) |
||||
.setLevel(3) |
||||
.setId(item.getId()) |
||||
.setPid(item.getPid()) |
||||
.setName(item.getName()) |
||||
.setFullName(item.getFullName()) |
||||
) |
||||
.collect(Collectors.toList()); |
||||
// 二级
|
||||
List<DeptNode> secondNode = list.stream() |
||||
.filter(item -> item.getLevel() == 2) |
||||
.map(item -> new DeptNode() |
||||
.setCategory(item.getCategory()) |
||||
.setLevel(2) |
||||
.setId(item.getId()) |
||||
.setPid(item.getPid()) |
||||
.setName(item.getName()) |
||||
.setFullName(item.getFullName()) |
||||
.setChildren(threeNode.stream().filter(node -> item.getId().equals(node.getPid())).collect(Collectors.toList())) |
||||
) |
||||
.collect(Collectors.toList()); |
||||
// 跟节点
|
||||
return list.stream() |
||||
.filter(item -> item.getLevel() == 1) |
||||
.map(item -> new DeptNode() |
||||
.setCategory(item.getCategory()) |
||||
.setLevel(1) |
||||
.setId(item.getId()) |
||||
.setPid(item.getPid()) |
||||
.setName(item.getName()) |
||||
.setFullName(item.getFullName()) |
||||
.setChildren(secondNode.stream().filter(node -> item.getId().equals(node.getPid())).collect(Collectors.toList())) |
||||
) |
||||
.collect(Collectors.toList()); |
||||
} |
||||
|
||||
|
||||
public void processTree(DeptNode root) { |
||||
// 调用辅助方法进行递归遍历
|
||||
traverseTree(root, "", 0); |
||||
} |
||||
|
||||
// 递归遍历树结构的辅助方法
|
||||
private void traverseTree(DeptNode node, String path, int depth) { |
||||
if (node == null) { |
||||
return; |
||||
} |
||||
|
||||
// 构建当前节点的路径跟踪信息
|
||||
// 如果是根节点,不添加 "->"
|
||||
String currentPath = (depth == 0) ? Integer.toString(node.getId()) : path + "," + node.getId(); |
||||
|
||||
// 处理当前节点,比如创建一个CustomObject,并添加到列表中
|
||||
processedNodes.add(new DeptTree().setId(node.getId()).setPid(node.getPid()).setName(node.getName()) |
||||
.setUuid("/").setMobile("1").setSort(node.getId()).setIsStop(0).setIsDelete(0) |
||||
.setCreateTime(System.currentTimeMillis() / 1000) |
||||
.setUpdateTime(System.currentTimeMillis() / 1000) |
||||
.setDeleteTime(0L) |
||||
.setFullName(node.getFullName()).setCategory(handleCategory(node.getCategory())).setLevel(node.getLevel()) |
||||
.setPathTrace(currentPath)); |
||||
|
||||
|
||||
// 递归处理每个子节点
|
||||
for (DeptNode child : node.getChildren()) { |
||||
traverseTree(child, currentPath, depth + 1); |
||||
} |
||||
} |
||||
|
||||
private String handleCategory(String category) { |
||||
switch (category) { |
||||
case "分区县市局": |
||||
return "2"; |
||||
case "支队": |
||||
return "3"; |
||||
case "部、委、组、警校": |
||||
return "4"; |
||||
default: |
||||
return category; |
||||
} |
||||
} |
||||
|
||||
// 获取处理后的CustomObject列表
|
||||
public List<DeptTree> getProcessedNodes() { |
||||
return processedNodes; |
||||
} |
||||
|
||||
// 寻找根节点,假设根节点的pid为null
|
||||
public static DeptNode findRootNode(List<DeptNode> list) { |
||||
for (DeptNode node : list) { |
||||
if (node.getPid() == 0) { |
||||
return node; |
||||
} |
||||
} |
||||
return null; // 如果没有找到根节点,可能树的数据结构有问题。
|
||||
} |
||||
|
||||
|
||||
@Test |
||||
public void getResult() { |
||||
List<DeptNode> tree = tree(); |
||||
processTree(findRootNode(tree)); |
||||
List<DeptTree> result = getProcessedNodes(); |
||||
|
||||
String fileName = "data_dept.xlsx"; |
||||
EasyExcel.write(fileName, DeptTree.class).sheet("sheet").doWrite(result); |
||||
} |
||||
|
||||
} |
||||
@ -1,41 +1,69 @@
|
||||
package com.biutag.lan; |
||||
|
||||
|
||||
import org.junit.jupiter.api.Test; |
||||
|
||||
|
||||
import javax.crypto.Cipher; |
||||
import javax.crypto.spec.SecretKeySpec; |
||||
import java.net.URLEncoder; |
||||
import java.nio.charset.StandardCharsets; |
||||
import java.util.Base64; |
||||
|
||||
|
||||
public class SmsTest { |
||||
|
||||
private static final String ALGORITHM = "AES/ECB/pkcs5padding"; |
||||
private static final String CHARSET = "UTF8"; |
||||
private static final String SENDURL = "AES/ECB/PKCS5PADDING"; |
||||
private static final String USERID = "UTF8"; |
||||
|
||||
// AES密钥(长度应为16个字节)
|
||||
private static final String SECRET_KEY = "fzzd12345678dxpt"; |
||||
private static final String PWD = "fzzd12345678dxpt"; |
||||
|
||||
@Test |
||||
public void encrypt() throws Exception { |
||||
String input = "{\"timeStamp\":\"1709210345\",\"orderNo\":\"TEST001\",\"mobiles\":\"11111111111\",\"appendID\":\"\",\"validTime\":\"\",\"content\":\"123\",\"sendTime\":\"\"}"; |
||||
String key = "fzzd12345678dxpt"; |
||||
public void solve() throws Exception { |
||||
try { |
||||
|
||||
String jsonStr = "{\"timeStamp\":\"1709210345\",\"orderNo\":\"TEST001\",\"mobiles\":\"11111111111\",\"appendID\":\"\",\"validTime\":\"\",\"content\":\"123\",\"sendTime\":\"\"}"; |
||||
String result = "dk0vL%2FHI%2FMgeyko9w%2F2buOQSLLv%2FWd4xfDWUgVC%2FQt0tf8P%2Fr%2F7iP9srgLBRnrignGZCXWeaZ8cf%0D%0AqexHGp4GO7LTM0tEg9IwOEOJdMJIbuHaFvDY8SvORhT%2BZanP8UvHUevoO6fcNoZ0yBOHceS8zZ%2Ba%0D%0Ak8fQtrgakYQV7KszrVOz%2B9EcGPFGLfWmFCsFwK1P8hPqywvtpZprdXSj4d2NrnhWCbt%2BZVt9GX2u%0D%0AAPRkQuYQAmNFFGZM1iF0Jx2vt%2BoN"; |
||||
|
||||
// Base64编码
|
||||
String base64Data = Base64.getEncoder().encodeToString(input.getBytes(CHARSET)); |
||||
|
||||
// AES加密
|
||||
Cipher cipher = Cipher.getInstance(ALGORITHM); |
||||
SecretKeySpec secretKeySpec = new SecretKeySpec(SECRET_KEY.getBytes(CHARSET), "AES"); |
||||
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); |
||||
byte[] encryptedBytes = cipher.doFinal(base64Data.getBytes(CHARSET)); |
||||
|
||||
//System.out.println("jsonStr:"+jsonStr);
|
||||
String key = encrypt(jsonStr, PWD); |
||||
//System.out.println("key:"+key);
|
||||
key = URLEncoder.encode(key, "UTF-8"); |
||||
System.out.println("key:" + key); |
||||
System.out.println(result.equals(key)); |
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
} |
||||
|
||||
|
||||
// 将加密后的数据转换为Base64编码
|
||||
String encryptedBase64 = Base64.getEncoder().encodeToString(encryptedBytes); |
||||
|
||||
System.out.println(encryptedBase64); |
||||
System.out.println(result.equals(encryptedBase64)); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* AES加密 |
||||
* |
||||
* @param input 明文 |
||||
* @param key 秘钥 |
||||
* @return 加密后的BASE64编码的字符串 |
||||
* @throws Exception |
||||
*/ |
||||
public static String encrypt(String input, String key) throws Exception { |
||||
// 创建一个AES密钥
|
||||
SecretKeySpec skey = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES"); |
||||
|
||||
// 创建Cipher实例
|
||||
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); |
||||
cipher.init(Cipher.ENCRYPT_MODE, skey); |
||||
|
||||
// 执行加密操作
|
||||
byte[] crypted = cipher.doFinal(input.getBytes(StandardCharsets.UTF_8)); |
||||
|
||||
// 使用Base64进行编码
|
||||
return Base64.getEncoder().encodeToString(crypted); |
||||
} |
||||
|
||||
|
||||
} |
||||
|
||||
@ -0,0 +1,29 @@
|
||||
package com.biutag.lan.entity; |
||||
|
||||
import lombok.Data; |
||||
import lombok.experimental.Accessors; |
||||
|
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
|
||||
@Accessors(chain = true) |
||||
@Data |
||||
public class DeptNode { |
||||
private Integer id; |
||||
private Integer pid; |
||||
private String name; |
||||
private String fullName; |
||||
private Integer level; |
||||
|
||||
private String category; |
||||
|
||||
private List<DeptNode> children; |
||||
|
||||
public List<DeptNode> getChildren() { |
||||
if (children == null) { |
||||
return Collections.emptyList(); // 返回一个空列表而不是null
|
||||
} |
||||
return children; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,60 @@
|
||||
package com.biutag.lan.entity; |
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName; |
||||
import io.swagger.annotations.ApiModelProperty; |
||||
import lombok.Data; |
||||
import lombok.experimental.Accessors; |
||||
|
||||
@Accessors(chain = true) |
||||
@Data |
||||
@TableName("dept_copy1") |
||||
public class DeptTree { |
||||
|
||||
@ApiModelProperty("ID") |
||||
private Integer id; |
||||
|
||||
@ApiModelProperty("上级部门") |
||||
private Integer pid; |
||||
|
||||
@ApiModelProperty("部门名称") |
||||
private String name; |
||||
|
||||
|
||||
@ApiModelProperty("部门全称") |
||||
private String fullName; |
||||
|
||||
@ApiModelProperty("唯一id") |
||||
private String uuid; |
||||
|
||||
@ApiModelProperty("联系电话") |
||||
private String mobile; |
||||
|
||||
@ApiModelProperty("排序编号") |
||||
private Integer sort; |
||||
|
||||
|
||||
@ApiModelProperty("类别") |
||||
private String category; |
||||
|
||||
@ApiModelProperty("层级") |
||||
private Integer level; |
||||
|
||||
@ApiModelProperty("部门路径") |
||||
private String pathTrace; |
||||
|
||||
@ApiModelProperty("是否禁用: [0=否, 1=是]") |
||||
private Integer isStop; |
||||
|
||||
@ApiModelProperty("是否删除: [0=否, 1=是]") |
||||
private Integer isDelete; |
||||
|
||||
@ApiModelProperty("创建时间") |
||||
private Long createTime; |
||||
|
||||
@ApiModelProperty("更新时间") |
||||
private Long updateTime; |
||||
|
||||
@ApiModelProperty("删除时间") |
||||
private Long deleteTime; |
||||
|
||||
} |
||||
@ -0,0 +1,18 @@
|
||||
package com.biutag.lan.entity; |
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName; |
||||
import lombok.Data; |
||||
|
||||
@Data |
||||
@TableName("tmp_dept") |
||||
public class TmpDept { |
||||
private Integer id; |
||||
private Integer pid; |
||||
private Integer level; |
||||
private String name; |
||||
private String deptFullName; |
||||
private String deptType; |
||||
private String first; |
||||
private String second; |
||||
private String third; |
||||
} |
||||
@ -0,0 +1,10 @@
|
||||
package com.biutag.lan.mapper; |
||||
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
||||
import com.biutag.lan.entity.DeptTree; |
||||
import org.apache.ibatis.annotations.Mapper; |
||||
|
||||
@Mapper |
||||
public interface DeptTreeMapper extends BaseMapper<DeptTree> { |
||||
} |
||||
Loading…
Reference in new issue