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 query = new LambdaQueryWrapper<>(); LambdaQueryWrapper query1 = new LambdaQueryWrapper<>(); // 创建一个List集合,存储查询返回的所有SysDepart对象 List 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 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; } }