diff --git a/bonus-api/bonus-api-system/src/main/java/com/bonus/system/api/domain/SysDept.java b/bonus-api/bonus-api-system/src/main/java/com/bonus/system/api/domain/SysDept.java index a9eba92..64a7a98 100644 --- a/bonus-api/bonus-api-system/src/main/java/com/bonus/system/api/domain/SysDept.java +++ b/bonus-api/bonus-api-system/src/main/java/com/bonus/system/api/domain/SysDept.java @@ -1,6 +1,7 @@ package com.bonus.system.api.domain; import com.bonus.common.core.web.domain.BaseEntity; +import lombok.Data; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; @@ -16,6 +17,7 @@ import java.util.List; * * @author bonus */ +@Data public class SysDept extends BaseEntity { private static final long serialVersionUID = 1L; @@ -23,12 +25,17 @@ public class SysDept extends BaseEntity { * 部门ID */ private Long deptId; + /** + * 部门等级 + */ + private Integer deptLevel; /** * 父部门ID */ private Long parentId; + /** * 祖级列表 */ @@ -396,4 +403,6 @@ public class SysDept extends BaseEntity { .append("level", getLevel()) .toString(); } + + } diff --git a/bonus-api/bonus-api-system/src/main/java/com/bonus/system/api/domain/SysUser.java b/bonus-api/bonus-api-system/src/main/java/com/bonus/system/api/domain/SysUser.java index 1e3f978..33c6fb7 100644 --- a/bonus-api/bonus-api-system/src/main/java/com/bonus/system/api/domain/SysUser.java +++ b/bonus-api/bonus-api-system/src/main/java/com/bonus/system/api/domain/SysUser.java @@ -6,6 +6,7 @@ import com.bonus.common.core.annotation.Excel.Type; import com.bonus.common.core.annotation.Excels; import com.bonus.common.core.web.domain.BaseEntity; import com.bonus.common.core.xss.Xss; +import lombok.Data; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; @@ -21,6 +22,7 @@ import java.util.List; * * @author bonus */ +@Data public class SysUser extends BaseEntity { private static final long serialVersionUID = 1L; @@ -152,6 +154,7 @@ public class SysUser extends BaseEntity { /**是否内置,0内置,1非内置*/ private String isBuiltIn = "1"; + private Long orgId; public SysUser() { @@ -387,4 +390,5 @@ public class SysUser extends BaseEntity { public void setIsBuiltIn(String isBuiltIn) { this.isBuiltIn = isBuiltIn; } + } diff --git a/bonus-modules/bonus-system/src/main/java/com/bonus/system/controller/SysNewDeptController.java b/bonus-modules/bonus-system/src/main/java/com/bonus/system/controller/SysNewDeptController.java new file mode 100644 index 0000000..aa4e541 --- /dev/null +++ b/bonus-modules/bonus-system/src/main/java/com/bonus/system/controller/SysNewDeptController.java @@ -0,0 +1,134 @@ +package com.bonus.system.controller; + +import com.bonus.common.core.web.controller.BaseController; +import com.bonus.common.core.web.domain.AjaxResult; +import com.bonus.common.core.web.page.TableDataInfo; +import com.bonus.system.domain.DeptQuery; +import com.bonus.system.domain.OrgQuery; +import com.bonus.system.domain.vo.OrgDeptTreeVo; +import com.bonus.system.service.ISysNewDeptService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +/** + * 机构部门管理控制器 + */ +@Api(tags = "机构部门管理") +@RestController +@RequestMapping("/newDept") +public class SysNewDeptController extends BaseController { + + @Autowired + private ISysNewDeptService sysNewDeptService; + + /** + * 获取机构树形列表 + */ + @ApiOperation("获取机构树形列表") + @GetMapping("/org/treeList") + public AjaxResult getOrgTreeList(OrgQuery query) { + List orgTree = sysNewDeptService.selectOrgTreeList(query); + return AjaxResult.success(orgTree); + } + + /** + * 获取机构树(用于部门管理左侧) + */ + @ApiOperation("获取机构树") + @GetMapping("/org/tree") + public AjaxResult getOrgTree() { + List orgTree = sysNewDeptService.selectOrgTree(); + return AjaxResult.success(orgTree); + } + + /** + * 根据机构获取部门列表 + */ + @ApiOperation("根据机构获取部门列表") + @GetMapping("/dept/list/{orgId}") + public TableDataInfo getDeptListByOrg(@PathVariable Long orgId, DeptQuery query) { + query.setOrgId(orgId); + List list = sysNewDeptService.selectDeptListByOrg(query); + return getDataTable(list); + } + + /** + * 新增机构 + */ + @ApiOperation("新增机构") + @PostMapping("/org") + public AjaxResult addOrg(@RequestBody OrgDeptTreeVo org) { + return toAjax(sysNewDeptService.insertOrg(org)); + } + + /** + * 修改机构 + */ + @ApiOperation("修改机构") + @PutMapping("/org") + public AjaxResult editOrg(@RequestBody OrgDeptTreeVo org) { + return toAjax(sysNewDeptService.updateOrg(org)); + } + + /** + * 删除机构 + */ + @ApiOperation("删除机构") + @DeleteMapping("/org/{orgId}") + public AjaxResult removeOrg(@PathVariable Long orgId) { + return toAjax(sysNewDeptService.deleteOrgById(orgId)); + } + + /** + * 新增部门 + */ + @ApiOperation("新增部门") + @PostMapping("/dept") + public AjaxResult addDept(@RequestBody OrgDeptTreeVo dept) { + return toAjax(sysNewDeptService.insertDept(dept)); + } + + /** + * 修改部门 + */ + @ApiOperation("修改部门") + @PutMapping("/dept") + public AjaxResult editDept(@RequestBody OrgDeptTreeVo dept) { + return toAjax(sysNewDeptService.updateDept(dept)); + } + + /** + * 删除部门 + */ + @ApiOperation("删除部门") + @DeleteMapping("/dept/{deptId}") + public AjaxResult removeDept(@PathVariable Long deptId) { + return toAjax(sysNewDeptService.deleteDeptById(deptId)); + } + + /** + * 导入机构数据 + */ + @ApiOperation("导入机构数据") + @PostMapping("/org/import") + public AjaxResult importOrg() { + // TODO: 实现导入逻辑 + return AjaxResult.success(); + } + + /** + * 导入部门数据 + */ + @ApiOperation("导入部门数据") + @PostMapping("/dept/import") + public AjaxResult importDept() { + // TODO: 实现导入逻辑 + return AjaxResult.success(); + } + + +} diff --git a/bonus-modules/bonus-system/src/main/java/com/bonus/system/domain/DeptQuery.java b/bonus-modules/bonus-system/src/main/java/com/bonus/system/domain/DeptQuery.java new file mode 100644 index 0000000..098fa8d --- /dev/null +++ b/bonus-modules/bonus-system/src/main/java/com/bonus/system/domain/DeptQuery.java @@ -0,0 +1,23 @@ +package com.bonus.system.domain; + + +import lombok.Data; + +/** + * 部门查询参数 + */ +@Data +public class DeptQuery { + + /** 部门名称 */ + private String deptName; + + /** 主管理员 */ + private String mainAdmin; + + /** 子管理员 */ + private String subAdmin; + + /** 机构ID */ + private Long orgId; +} diff --git a/bonus-modules/bonus-system/src/main/java/com/bonus/system/domain/OrgQuery.java b/bonus-modules/bonus-system/src/main/java/com/bonus/system/domain/OrgQuery.java new file mode 100644 index 0000000..8fefaf1 --- /dev/null +++ b/bonus-modules/bonus-system/src/main/java/com/bonus/system/domain/OrgQuery.java @@ -0,0 +1,20 @@ +package com.bonus.system.domain; + + +import lombok.Data; + +/** + * 机构查询参数 + */ +@Data +public class OrgQuery { + + /** 机构名称 */ + private String orgName; + + /** 主管理员 */ + private String mainAdmin; + + /** 子管理员 */ + private String subAdmin; +} diff --git a/bonus-modules/bonus-system/src/main/java/com/bonus/system/domain/SysOrg.java b/bonus-modules/bonus-system/src/main/java/com/bonus/system/domain/SysOrg.java new file mode 100644 index 0000000..bb96ef0 --- /dev/null +++ b/bonus-modules/bonus-system/src/main/java/com/bonus/system/domain/SysOrg.java @@ -0,0 +1,54 @@ +package com.bonus.system.domain; + +import lombok.Data; + +import java.util.ArrayList; +import java.util.List; + +@Data +public class SysOrg { + private static final long serialVersionUID = 1L; + + /** id */ + private Long orgId; + + /** 部门名称 */ + private String orgName; + + /** 部门层级 10/12/15 */ + private String orgPath; + + /** 父级部门 */ + private Long parentOrgId; + + /** 父级部门名称 */ + private String parentOrgName; + + /** 所属组织机构 */ + private Long deptId; + + /** 所属组织机构名称 */ + private String deptName; + + /** 是否有效 1有效 0无效 */ + private String isActive; + + /** 创建者 */ + private String createBy; + + /** 创建时间 */ + private String createTime; + + /** 更新者 */ + private String updateBy; + + /** 更新时间 */ + private String updateTime; + + /** 备注 */ + private String remark; + + + /** 子部门 */ + private List children = new ArrayList(); +} diff --git a/bonus-modules/bonus-system/src/main/java/com/bonus/system/domain/vo/OrgDeptTreeVo.java b/bonus-modules/bonus-system/src/main/java/com/bonus/system/domain/vo/OrgDeptTreeVo.java new file mode 100644 index 0000000..12e5f8c --- /dev/null +++ b/bonus-modules/bonus-system/src/main/java/com/bonus/system/domain/vo/OrgDeptTreeVo.java @@ -0,0 +1,61 @@ +package com.bonus.system.domain.vo; + +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.Data; + +import java.util.List; + +/** + * 机构部门树VO + */ +@Data +public class OrgDeptTreeVo { + /** ID */ + private Long id; + + /** 机构/部门名称 */ + private String orgName; + + /** 名称(兼容字段) */ + private String name; + + /** 父级ID */ + private Long parentId; + + /** 类型:org机构,dept部门 */ + private String type; + + /** 级别:一级机构、二级机构、三级机构 */ + private String level; + + /** 主管理员 */ + private String mainAdmin; + + /** 子管理员 */ + private String subAdmin; + + /** 人数 */ + private Integer personCount; + + /** 子节点 */ + private List children; + + /** 用户ID(部门列表用) */ + private Long userId; + + /** 部门名称(部门列表用) */ + private String deptName; + + /** 系统角色(部门列表用) */ + private String systemRole; + + /** 邮箱(部门列表用) */ + private String email; + + /** 机构ID(用于部门管理) */ + private Long orgDeptId; + + /** 机构ID(兼容字段) */ + private Long orgId; + +} diff --git a/bonus-modules/bonus-system/src/main/java/com/bonus/system/mapper/SysOrgMapper.java b/bonus-modules/bonus-system/src/main/java/com/bonus/system/mapper/SysOrgMapper.java new file mode 100644 index 0000000..d7df82c --- /dev/null +++ b/bonus-modules/bonus-system/src/main/java/com/bonus/system/mapper/SysOrgMapper.java @@ -0,0 +1,38 @@ +package com.bonus.system.mapper; + +import com.bonus.system.domain.SysOrg; +import org.apache.ibatis.annotations.Param; + +import java.util.List; + +public interface SysOrgMapper { + /** + * 查询部门列表 + */ + List selectSysOrgList(SysOrg sysOrg); + + /** + * 根据ID查询部门 + */ + SysOrg selectSysOrgById(Long orgId); + + /** + * 新增部门 + */ + int insertSysOrg(SysOrg sysOrg); + + /** + * 修改部门 + */ + int updateSysOrg(SysOrg sysOrg); + + /** + * 删除部门 + */ + int deleteSysOrgById(Long orgId); + + /** + * 根据父级ID查询子部门列表 + */ + List selectSysOrgListByParentId(@Param("parentOrgId") Long parentOrgId, @Param("deptId") Long deptId); +} diff --git a/bonus-modules/bonus-system/src/main/java/com/bonus/system/mapper/SysUserMapper.java b/bonus-modules/bonus-system/src/main/java/com/bonus/system/mapper/SysUserMapper.java index 21c8533..37cc73e 100644 --- a/bonus-modules/bonus-system/src/main/java/com/bonus/system/mapper/SysUserMapper.java +++ b/bonus-modules/bonus-system/src/main/java/com/bonus/system/mapper/SysUserMapper.java @@ -157,4 +157,6 @@ public interface SysUserMapper { int systemUpdateUser(SysUser user); List getList(SysUser user); + + int selectUserListByOrgid(SysUser userParam); } diff --git a/bonus-modules/bonus-system/src/main/java/com/bonus/system/service/ISysNewDeptService.java b/bonus-modules/bonus-system/src/main/java/com/bonus/system/service/ISysNewDeptService.java new file mode 100644 index 0000000..32e0519 --- /dev/null +++ b/bonus-modules/bonus-system/src/main/java/com/bonus/system/service/ISysNewDeptService.java @@ -0,0 +1,57 @@ +package com.bonus.system.service; + +import com.bonus.system.domain.DeptQuery; +import com.bonus.system.domain.OrgQuery; +import com.bonus.system.domain.vo.OrgDeptTreeVo; +import java.util.List; + +/** + * 机构部门管理服务接口 + */ +public interface ISysNewDeptService { + + /** + * 查询机构树形列表 + */ + List selectOrgTreeList(OrgQuery query); + + /** + * 查询机构树(用于部门管理左侧) + */ + List selectOrgTree(); + + /** + * 根据机构查询部门列表 + */ + List selectDeptListByOrg(DeptQuery query); + + /** + * 新增机构 + */ + int insertOrg(OrgDeptTreeVo org); + + /** + * 修改机构 + */ + int updateOrg(OrgDeptTreeVo org); + + /** + * 删除机构 + */ + int deleteOrgById(Long orgId); + + /** + * 新增部门 + */ + int insertDept(OrgDeptTreeVo dept); + + /** + * 修改部门 + */ + int updateDept(OrgDeptTreeVo dept); + + /** + * 删除部门 + */ + int deleteDeptById(Long deptId); +} diff --git a/bonus-modules/bonus-system/src/main/java/com/bonus/system/service/impl/SysNewDeptServiceImpl.java b/bonus-modules/bonus-system/src/main/java/com/bonus/system/service/impl/SysNewDeptServiceImpl.java new file mode 100644 index 0000000..514cdfa --- /dev/null +++ b/bonus-modules/bonus-system/src/main/java/com/bonus/system/service/impl/SysNewDeptServiceImpl.java @@ -0,0 +1,480 @@ +package com.bonus.system.service.impl; + +import com.bonus.common.core.utils.StringUtils; +import com.bonus.system.api.domain.SysDept; +import com.bonus.system.api.domain.SysUser; +import com.bonus.system.domain.DeptQuery; +import com.bonus.system.domain.OrgQuery; +import com.bonus.system.domain.SysOrg; +import com.bonus.system.domain.vo.OrgDeptTreeVo; +import com.bonus.system.mapper.SysDeptMapper; +import com.bonus.system.mapper.SysOrgMapper; +import com.bonus.system.mapper.SysUserMapper; +import com.bonus.system.service.ISysNewDeptService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +/** + * 机构部门管理服务实现 + */ +@Service +public class SysNewDeptServiceImpl implements ISysNewDeptService { + + @Autowired + private SysDeptMapper sysDeptMapper; + + @Autowired + private SysOrgMapper sysOrgMapper; + + @Autowired + private SysUserMapper sysUserMapper; + + @Override + public List selectOrgTreeList(OrgQuery query) { + // 查询所有机构(sys_dept表) + SysDept deptParam = new SysDept(); + deptParam.setDelFlag("0"); // 未删除 + deptParam.setStatus("0"); // 正常状态 + + if (StringUtils.isNotEmpty(query.getOrgName())) { + deptParam.setDeptName(query.getOrgName()); + } + + List deptList = sysDeptMapper.selectDeptList(deptParam); + + // 获取所有用户信息,用于统计人数和管理员信息 + SysUser userParam = new SysUser(); + userParam.setDelFlag("0"); + List userList = sysUserMapper.selectUserList(userParam); + + // 按机构ID(dept_id)分组用户 + Map> usersByDept = userList.stream() + .filter(user -> user.getDeptId() != null) + .collect(Collectors.groupingBy(SysUser::getDeptId)); + + // 构建机构树 + List treeList = buildOrgTree(deptList, usersByDept, 0L, query); + + // 过滤主管理员和子管理员 + if (StringUtils.isNotEmpty(query.getMainAdmin()) || StringUtils.isNotEmpty(query.getSubAdmin())) { + return filterOrgTreeByAdmin(treeList, query); + } + + return treeList; + } + + @Override + public List selectOrgTree() { + // 查询所有正常状态的机构 + SysDept deptParam = new SysDept(); + deptParam.setDelFlag("0"); + deptParam.setStatus("0"); + List deptList = sysDeptMapper.selectDeptList(deptParam); + + return buildSimpleOrgTree(deptList, 0L); + } + + @Override + public List selectDeptListByOrg(DeptQuery query) { + List result = new ArrayList<>(); + + if (query.getOrgId() == null) { + return result; + } + + // 查询指定机构下的部门(sys_org表) + SysOrg orgParam = new SysOrg(); + orgParam.setDeptId(query.getOrgId()); + if (StringUtils.isNotEmpty(query.getDeptName())) { + orgParam.setOrgName(query.getDeptName()); + } + orgParam.setIsActive("1"); // 有效部门 + + // 查询部门列表 + List orgList = sysOrgMapper.selectSysOrgList(orgParam); + + // 根据 parentId 将部门进行分组,构建树形结构 + Map deptMap = new HashMap<>(); + for (SysOrg org : orgList) { + OrgDeptTreeVo vo = new OrgDeptTreeVo(); + vo.setId(org.getOrgId()); + vo.setOrgName(org.getOrgName()); + vo.setParentId(org.getParentOrgId()); + + // 将每个部门放入 map 中,方便后续查找 + deptMap.put(org.getOrgId(), vo); + } + + // 构建树形结构 + for (OrgDeptTreeVo vo : deptMap.values()) { + if (vo.getParentId() == null || vo.getParentId() == 0L) { + // 根节点(没有父部门) + result.add(vo); + } else { + // 非根节点(有父部门) + OrgDeptTreeVo parentVo = deptMap.get(vo.getParentId()); + if (parentVo != null) { + // 如果父部门存在,将当前部门添加到父部门的 children 中 + if (parentVo.getChildren() == null) { + parentVo.setChildren(new ArrayList<>()); + } + parentVo.getChildren().add(vo); + } + } + } + + return result; + } + // 公用方法:递归调整单个 OrgDeptTreeVo 的 level 值 + public static int adjustDeptLevel(OrgDeptTreeVo orgDept) { + String level = orgDept.getLevel(); + + if (level == null || level.isEmpty()) { + return 0; // 或者返回其他默认值 + } + switch (level) { + case "一级机构": + return 1+1; // 对应一级节点 + case "二级机构": + return 2+1; // 对应二级节点 + case "三级机构": + return 3+1; // 对应三级节点 + default: + return 0+1; // 如果不匹配,则返回默认值 + } + } + + + + @Override + @Transactional + public int insertOrg(OrgDeptTreeVo org) { + SysDept sysDept = new SysDept(); + sysDept.setDeptName(org.getOrgName() != null ? org.getOrgName() : org.getName()); + sysDept.setStatus("0"); + sysDept.setDelFlag("0"); + sysDept.setLeader(org.getMainAdmin()); + //处理节点问题 + int levle = adjustDeptLevel(org); + org.setLevel(levle+""); + Long parentId = org.getParentId(); + if (parentId != null && parentId > 0L) { + SysDept parentDept = sysDeptMapper.selectDeptById(parentId); + if (parentDept != null) { + String ancestors = StringUtils.isEmpty(parentDept.getAncestors()) + ? parentDept.getDeptId().toString() + : parentDept.getAncestors() + "," + parentDept.getDeptId(); + sysDept.setAncestors(ancestors); + sysDept.setDeptLevel(levle); + sysDept.setParentId(parentId); + } + } else { + sysDept.setParentId(0L); + sysDept.setAncestors("0"); + sysDept.setDeptLevel(levle); + } + + return sysDeptMapper.insertDept(sysDept); + } + + @Override + @Transactional + public int updateOrg(OrgDeptTreeVo org) { + SysDept sysDept = sysDeptMapper.selectDeptById(org.getId()); + if (sysDept == null) { + return 0; + } + + sysDept.setDeptName(org.getOrgName() != null ? org.getOrgName() : org.getName()); + sysDept.setLeader(org.getMainAdmin()); + + return sysDeptMapper.updateDept(sysDept); + } + + @Override + @Transactional + public int deleteOrgById(Long orgId) { + // 检查是否有子机构 + SysDept deptParam = new SysDept(); + deptParam.setParentId(orgId); + deptParam.setDelFlag("0"); + List children = sysDeptMapper.selectDeptList(deptParam); + + if (children != null && !children.isEmpty()) { + throw new RuntimeException("存在子机构,无法删除"); + } + + // 检查机构下是否有用户 + SysUser userParam = new SysUser(); + userParam.setDeptId(orgId); + userParam.setDelFlag("0"); + List users = sysUserMapper.selectUserList(userParam); + + if (users != null && !users.isEmpty()) { + throw new RuntimeException("机构下存在用户,无法删除"); + } + + // 逻辑删除 + SysDept dept = sysDeptMapper.selectDeptById(orgId); + if (dept != null) { + dept.setDelFlag("2"); + return sysDeptMapper.updateDept(dept); + } + + return 0; + } + + @Override + @Transactional + public int insertDept(OrgDeptTreeVo dept) { + SysOrg sysOrg = new SysOrg(); + sysOrg.setOrgName(dept.getOrgName()); + sysOrg.setDeptId(dept.getOrgId()); // 所属机构ID + sysOrg.setIsActive("1"); + + Long parentId = dept.getParentId(); + if (parentId != null && parentId > 0L) { + sysOrg.setParentOrgId(parentId); + } + + return sysOrgMapper.insertSysOrg(sysOrg); + } + + @Override + @Transactional + public int updateDept(OrgDeptTreeVo dept) { + // 更新部门信息 + if (dept.getId() != null) { + // 如果ID是部门ID,更新部门表 + SysOrg sysOrg = sysOrgMapper.selectSysOrgById(dept.getId()); + if (sysOrg != null) { + sysOrg.setOrgName(dept.getOrgName()); + return sysOrgMapper.updateSysOrg(sysOrg); + } + } + + // 如果ID是用户ID,更新用户信息 + if (dept.getUserId() != null) { + SysUser user = sysUserMapper.selectUserById(dept.getUserId()); + if (user != null) { + if (StringUtils.isNotEmpty(dept.getName())) { + user.setNickName(dept.getName()); + } + if (StringUtils.isNotEmpty(dept.getEmail())) { + user.setPhonenumber(dept.getEmail()); + } + return sysUserMapper.updateUser(user); + } + } + + return 0; + } + + @Override + @Transactional + public int deleteDeptById(Long deptId) { + // 这里删除的是用户与部门的关联(实际上是删除用户记录或清除关联) + SysOrg sysOrg = sysOrgMapper.selectSysOrgById(deptId); + if (sysOrg != null) { + // 检查部门下是否有用户 + SysUser userParam = new SysUser(); + userParam.setOrgId(deptId); + userParam.setDelFlag("0"); + int count = sysUserMapper.selectUserListByOrgid(userParam); + if (count > 0) { + throw new RuntimeException("部门下存在用户,无法删除"); + } + return sysOrgMapper.deleteSysOrgById(deptId); + } + + // 如果找不到部门,可能是用户ID,清除用户与部门的关联 + SysUser user = sysUserMapper.selectUserById(deptId); + if (user != null && "0".equals(user.getDelFlag())) { + user.setOrgId(null); + return sysUserMapper.updateUser(user); + } + + return 0; + } + + /** + * 构建机构树 + */ + private List buildOrgTree(List deptList, + Map> usersByDept, + Long parentId, OrgQuery query) { + List treeList = new ArrayList<>(); + + for (SysDept dept : deptList) { + Long deptParentId = dept.getParentId(); + if (deptParentId != null && deptParentId.equals(parentId)) { + OrgDeptTreeVo vo = convertToOrgVo(dept); + + // 统计当前机构人数(包括子机构) + int personCount = countDeptUsers(dept, usersByDept, deptList); + vo.setPersonCount(personCount); + + // 设置管理员信息 + setAdminInfo(vo, usersByDept.get(dept.getDeptId())); + + // 设置级别 + vo.setLevel(getLevelString(dept.getDeptLevel())); + + // 递归构建子节点 + List children = buildOrgTree(deptList, usersByDept, dept.getDeptId(), query); + if (children != null && !children.isEmpty()) { + vo.setChildren(children); + } + + treeList.add(vo); + } + } + + return treeList; + } + + /** + * 构建简单机构树(用于部门管理左侧) + */ + private List buildSimpleOrgTree(List deptList, Long parentId) { + List treeList = new ArrayList<>(); + + for (SysDept dept : deptList) { + Long deptParentId = dept.getParentId(); + if (deptParentId != null && deptParentId.equals(parentId)) { + OrgDeptTreeVo vo = new OrgDeptTreeVo(); + vo.setId(dept.getDeptId()); + vo.setOrgId(dept.getDeptId()); + vo.setOrgName(dept.getDeptName()); + vo.setName(dept.getDeptName()); + vo.setParentId(dept.getParentId()); + + List children = buildSimpleOrgTree(deptList, dept.getDeptId()); + if (children != null && !children.isEmpty()) { + vo.setChildren(children); + } + + treeList.add(vo); + } + } + + return treeList; + } + + /** + * 转换机构为VO + */ + private OrgDeptTreeVo convertToOrgVo(SysDept dept) { + OrgDeptTreeVo vo = new OrgDeptTreeVo(); + vo.setId(dept.getDeptId()); + vo.setOrgId(dept.getDeptId()); + vo.setOrgName(dept.getDeptName()); + vo.setName(dept.getDeptName()); + vo.setParentId(dept.getParentId()); + vo.setType("org"); + return vo; + } + + /** + * 统计机构用户数(包括子机构) + */ + private int countDeptUsers(SysDept dept, Map> usersByDept, List allDepts) { + int count = 0; + + // 当前机构用户数 + List users = usersByDept.get(dept.getDeptId()); + if (users != null) { + count += users.size(); + } + + // 递归统计子机构用户数 + for (SysDept childDept : allDepts) { + if (dept.getDeptId().equals(childDept.getParentId())) { + count += countDeptUsers(childDept, usersByDept, allDepts); + } + } + + return count; + } + + /** + * 设置管理员信息 + */ + private void setAdminInfo(OrgDeptTreeVo vo, List users) { + if (users == null || users.isEmpty()) { + return; + } + + // 简单实现:第一个用户作为主管理员,其他作为子管理员 + if (users.size() > 0) { + vo.setMainAdmin(users.get(0).getNickName()); + + if (users.size() > 1) { + StringBuilder subAdmins = new StringBuilder(); + for (int i = 1; i < Math.min(users.size(), 5); i++) { // 最多显示4个子管理员 + if (i > 1) subAdmins.append(" "); + subAdmins.append(users.get(i).getNickName()); + } + vo.setSubAdmin(subAdmins.toString()); + } + } + } + + /** + * 获取级别字符串 + */ + private String getLevelString(Integer level) { + if (level == null) return ""; + + switch (level) { + case 1: return "一级机构"; + case 2: return "二级机构"; + case 3: return "三级机构"; + default: return level + "级机构"; + } + } + + /** + * 根据管理员条件过滤机构树 + */ + private List filterOrgTreeByAdmin(List treeList, OrgQuery query) { + List filteredList = new ArrayList<>(); + + for (OrgDeptTreeVo vo : treeList) { + boolean match = true; + + if (StringUtils.isNotEmpty(query.getMainAdmin())) { + match = vo.getMainAdmin() != null && vo.getMainAdmin().contains(query.getMainAdmin()); + } + + if (StringUtils.isNotEmpty(query.getSubAdmin())) { + match = match && vo.getSubAdmin() != null && vo.getSubAdmin().contains(query.getSubAdmin()); + } + + if (match) { + // 递归过滤子节点 + if (vo.getChildren() != null && !vo.getChildren().isEmpty()) { + List filteredChildren = filterOrgTreeByAdmin(vo.getChildren(), query); + vo.setChildren(filteredChildren); + } + filteredList.add(vo); + } else if (vo.getChildren() != null && !vo.getChildren().isEmpty()) { + // 如果当前节点不匹配,但子节点可能匹配,继续检查子节点 + List filteredChildren = filterOrgTreeByAdmin(vo.getChildren(), query); + if (!filteredChildren.isEmpty()) { + vo.setChildren(filteredChildren); + filteredList.add(vo); + } + } + } + + return filteredList; + } +} diff --git a/bonus-modules/bonus-system/src/main/resources/mapper/system/SysDeptMapper.xml b/bonus-modules/bonus-system/src/main/resources/mapper/system/SysDeptMapper.xml index 3036755..71e3bce 100644 --- a/bonus-modules/bonus-system/src/main/resources/mapper/system/SysDeptMapper.xml +++ b/bonus-modules/bonus-system/src/main/resources/mapper/system/SysDeptMapper.xml @@ -28,6 +28,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" + @@ -40,7 +41,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" d.status, d.del_flag, d.create_by, - d.create_time + d.create_time, + d.dept_level from sys_dept d @@ -64,7 +66,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" d.remark, d.logo, d.admin_user_id, - d.init_password + d.init_password, + d.dept_level from sys_dept d @@ -200,6 +203,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" menu_template_id, status, create_by, + dept_level, create_time )values( #{deptId}, @@ -221,6 +225,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" #{menuTemplateId}, #{status}, #{createBy}, + #{deptLevel}, sysdate() ) @@ -247,6 +252,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" menu_template_id = #{menuTemplateId}, status = #{status}, update_by = #{updateBy}, + del_flag = #{delFlag}, update_time = sysdate() where dept_id = #{deptId} diff --git a/bonus-modules/bonus-system/src/main/resources/mapper/system/SysOrgMapper.xml b/bonus-modules/bonus-system/src/main/resources/mapper/system/SysOrgMapper.xml new file mode 100644 index 0000000..9489ac4 --- /dev/null +++ b/bonus-modules/bonus-system/src/main/resources/mapper/system/SysOrgMapper.xml @@ -0,0 +1,88 @@ + + + + + + + + + + + + + + + select org_id, org_name, org_path, parent_org_id, dept_id, is_active + from sys_org + + + + + + + + insert into sys_org + + org_name, + org_path, + parent_org_id, + dept_id, + is_active, + + + #{orgName}, + #{orgPath}, + #{parentOrgId}, + #{deptId}, + #{isActive}, + + + + + update sys_org + + org_name = #{orgName}, + org_path = #{orgPath}, + parent_org_id = #{parentOrgId}, + dept_id = #{deptId}, + is_active = #{isActive}, + + where org_id = #{orgId} + + + + delete from sys_org where org_id = #{orgId} + + + + + + diff --git a/bonus-modules/bonus-system/src/main/resources/mapper/system/SysUserMapper.xml b/bonus-modules/bonus-system/src/main/resources/mapper/system/SysUserMapper.xml index 1ef1a08..0a604a0 100644 --- a/bonus-modules/bonus-system/src/main/resources/mapper/system/SysUserMapper.xml +++ b/bonus-modules/bonus-system/src/main/resources/mapper/system/SysUserMapper.xml @@ -290,6 +290,9 @@ GROUP BY u.user_id + insert into sys_user(