peng
2026-03-18 e59a0201057ba67cad425fed804c82ff4ba0c6f1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package com.tievd.cube.modules.system.extra.fillrules;
 
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tievd.cube.commons.intf.IFillRuleHandler;
import com.tievd.cube.commons.utils.YouBianCodeUtil;
import com.tievd.cube.modules.system.entity.SysDepart;
import com.tievd.cube.modules.system.service.ISysDepartService;
import lombok.SneakyThrows;
 
import java.util.List;
 
/**
 * 机构编码生成规则
 *
 * @author xinwuy
 * @version V2.3.0
 * @since 2021-10-26
 */
public class OrgCodeRule implements IFillRuleHandler {
 
    @SneakyThrows
    @Override
    public Object execute(JSONObject params, String formData) {
        ISysDepartService sysDepartService = SpringUtil.getBean("sysDepartServiceImpl");
        LambdaQueryWrapper<SysDepart> query = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<SysDepart> query1 = new LambdaQueryWrapper<>();
        // 创建一个List集合,存储查询返回的所有SysDepart对象
        List<SysDepart> departList;
        String[] strArray = new String[2];
        //定义部门类型
        String orgType;
        // 定义新编码字符串
        String newOrgCode;
        // 定义旧编码字符串
        String oldOrgCode;
        String parentId = null;
        if (StrUtil.isNotEmpty(formData)) {
            parentId = JSONUtil.parseObj(formData).getStr("parentId");
        } else if (params.size() > 0) {
            parentId = params.getStr("parentId");
        }
        //如果是最高级,则查询出同级的org_code, 调用工具类生成编码并返回
        if (StrUtil.isEmpty(parentId)) {
            // 线判断数据库中的表是否为空,空则直接返回初始编码
            query1.eq(SysDepart::getParentId, "").or().isNull(SysDepart::getParentId);
            query1.orderByDesc(SysDepart::getOrgCode);
            departList = sysDepartService.list(query1);
            if (departList == null || departList.size() == 0) {
                strArray[0] = YouBianCodeUtil.getNextYouBianCode(null);
                strArray[1] = "1";
                return strArray;
            } else {
                SysDepart depart = departList.get(0);
                oldOrgCode = depart.getOrgCode();
                orgType = depart.getOrgType();
                newOrgCode = YouBianCodeUtil.getNextYouBianCode(oldOrgCode);
            }
        } else {//反之则查询出所有同级的部门,获取结果后有两种情况,有同级和没有同级
            // 封装查询同级的条件
            query.eq(SysDepart::getParentId, parentId);
            // 降序排序
            query.orderByDesc(SysDepart::getOrgCode);
            // 查询出同级部门的集合
            List<SysDepart> parentList = sysDepartService.list(query);
            // 查询出父级部门
            SysDepart depart = sysDepartService.getById(parentId);
            // 获取父级部门的Code
            String parentCode = depart.getOrgCode();
            // 根据父级部门类型算出当前部门的类型
            orgType = String.valueOf(Integer.parseInt(depart.getOrgType()) + 1);
            // 处理同级部门为null的情况
            if (parentList == null || parentList.size() == 0) {
                // 直接生成当前的部门编码并返回
                newOrgCode = YouBianCodeUtil.getSubYouBianCode(parentCode, null);
            } else { //处理有同级部门的情况
                // 获取同级部门的编码,利用工具类
                String subCode = parentList.get(0).getOrgCode();
                // 返回生成的当前部门编码
                newOrgCode = YouBianCodeUtil.getSubYouBianCode(parentCode, subCode);
            }
        }
        // 返回最终封装了部门编码和部门类型的数组
        strArray[0] = newOrgCode;
        strArray[1] = orgType;
        return strArray;
    }
}