部门管理功能开发 解决
This commit is contained in:
parent
0c5344e885
commit
730f78e940
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<OrgDeptTreeVo> orgTree = sysNewDeptService.selectOrgTreeList(query);
|
||||
return AjaxResult.success(orgTree);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取机构树(用于部门管理左侧)
|
||||
*/
|
||||
@ApiOperation("获取机构树")
|
||||
@GetMapping("/org/tree")
|
||||
public AjaxResult getOrgTree() {
|
||||
List<OrgDeptTreeVo> orgTree = sysNewDeptService.selectOrgTree();
|
||||
return AjaxResult.success(orgTree);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据机构获取部门列表
|
||||
*/
|
||||
@ApiOperation("根据机构获取部门列表")
|
||||
@GetMapping("/dept/list/{orgId}")
|
||||
public TableDataInfo getDeptListByOrg(@PathVariable Long orgId, DeptQuery query) {
|
||||
query.setOrgId(orgId);
|
||||
List<OrgDeptTreeVo> 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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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<SysOrg> children = new ArrayList<SysOrg>();
|
||||
}
|
||||
|
|
@ -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<OrgDeptTreeVo> children;
|
||||
|
||||
/** 用户ID(部门列表用) */
|
||||
private Long userId;
|
||||
|
||||
/** 部门名称(部门列表用) */
|
||||
private String deptName;
|
||||
|
||||
/** 系统角色(部门列表用) */
|
||||
private String systemRole;
|
||||
|
||||
/** 邮箱(部门列表用) */
|
||||
private String email;
|
||||
|
||||
/** 机构ID(用于部门管理) */
|
||||
private Long orgDeptId;
|
||||
|
||||
/** 机构ID(兼容字段) */
|
||||
private Long orgId;
|
||||
|
||||
}
|
||||
|
|
@ -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<SysOrg> selectSysOrgList(SysOrg sysOrg);
|
||||
|
||||
/**
|
||||
* 根据ID查询部门
|
||||
*/
|
||||
SysOrg selectSysOrgById(Long orgId);
|
||||
|
||||
/**
|
||||
* 新增部门
|
||||
*/
|
||||
int insertSysOrg(SysOrg sysOrg);
|
||||
|
||||
/**
|
||||
* 修改部门
|
||||
*/
|
||||
int updateSysOrg(SysOrg sysOrg);
|
||||
|
||||
/**
|
||||
* 删除部门
|
||||
*/
|
||||
int deleteSysOrgById(Long orgId);
|
||||
|
||||
/**
|
||||
* 根据父级ID查询子部门列表
|
||||
*/
|
||||
List<SysOrg> selectSysOrgListByParentId(@Param("parentOrgId") Long parentOrgId, @Param("deptId") Long deptId);
|
||||
}
|
||||
|
|
@ -157,4 +157,6 @@ public interface SysUserMapper {
|
|||
int systemUpdateUser(SysUser user);
|
||||
|
||||
List<SysUser> getList(SysUser user);
|
||||
|
||||
int selectUserListByOrgid(SysUser userParam);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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<OrgDeptTreeVo> selectOrgTreeList(OrgQuery query);
|
||||
|
||||
/**
|
||||
* 查询机构树(用于部门管理左侧)
|
||||
*/
|
||||
List<OrgDeptTreeVo> selectOrgTree();
|
||||
|
||||
/**
|
||||
* 根据机构查询部门列表
|
||||
*/
|
||||
List<OrgDeptTreeVo> 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);
|
||||
}
|
||||
|
|
@ -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<OrgDeptTreeVo> 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<SysDept> deptList = sysDeptMapper.selectDeptList(deptParam);
|
||||
|
||||
// 获取所有用户信息,用于统计人数和管理员信息
|
||||
SysUser userParam = new SysUser();
|
||||
userParam.setDelFlag("0");
|
||||
List<SysUser> userList = sysUserMapper.selectUserList(userParam);
|
||||
|
||||
// 按机构ID(dept_id)分组用户
|
||||
Map<Long, List<SysUser>> usersByDept = userList.stream()
|
||||
.filter(user -> user.getDeptId() != null)
|
||||
.collect(Collectors.groupingBy(SysUser::getDeptId));
|
||||
|
||||
// 构建机构树
|
||||
List<OrgDeptTreeVo> treeList = buildOrgTree(deptList, usersByDept, 0L, query);
|
||||
|
||||
// 过滤主管理员和子管理员
|
||||
if (StringUtils.isNotEmpty(query.getMainAdmin()) || StringUtils.isNotEmpty(query.getSubAdmin())) {
|
||||
return filterOrgTreeByAdmin(treeList, query);
|
||||
}
|
||||
|
||||
return treeList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<OrgDeptTreeVo> selectOrgTree() {
|
||||
// 查询所有正常状态的机构
|
||||
SysDept deptParam = new SysDept();
|
||||
deptParam.setDelFlag("0");
|
||||
deptParam.setStatus("0");
|
||||
List<SysDept> deptList = sysDeptMapper.selectDeptList(deptParam);
|
||||
|
||||
return buildSimpleOrgTree(deptList, 0L);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<OrgDeptTreeVo> selectDeptListByOrg(DeptQuery query) {
|
||||
List<OrgDeptTreeVo> 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<SysOrg> orgList = sysOrgMapper.selectSysOrgList(orgParam);
|
||||
|
||||
// 根据 parentId 将部门进行分组,构建树形结构
|
||||
Map<Long, OrgDeptTreeVo> 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<SysDept> children = sysDeptMapper.selectDeptList(deptParam);
|
||||
|
||||
if (children != null && !children.isEmpty()) {
|
||||
throw new RuntimeException("存在子机构,无法删除");
|
||||
}
|
||||
|
||||
// 检查机构下是否有用户
|
||||
SysUser userParam = new SysUser();
|
||||
userParam.setDeptId(orgId);
|
||||
userParam.setDelFlag("0");
|
||||
List<SysUser> 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<OrgDeptTreeVo> buildOrgTree(List<SysDept> deptList,
|
||||
Map<Long, List<SysUser>> usersByDept,
|
||||
Long parentId, OrgQuery query) {
|
||||
List<OrgDeptTreeVo> 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<OrgDeptTreeVo> children = buildOrgTree(deptList, usersByDept, dept.getDeptId(), query);
|
||||
if (children != null && !children.isEmpty()) {
|
||||
vo.setChildren(children);
|
||||
}
|
||||
|
||||
treeList.add(vo);
|
||||
}
|
||||
}
|
||||
|
||||
return treeList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建简单机构树(用于部门管理左侧)
|
||||
*/
|
||||
private List<OrgDeptTreeVo> buildSimpleOrgTree(List<SysDept> deptList, Long parentId) {
|
||||
List<OrgDeptTreeVo> 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<OrgDeptTreeVo> 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<Long, List<SysUser>> usersByDept, List<SysDept> allDepts) {
|
||||
int count = 0;
|
||||
|
||||
// 当前机构用户数
|
||||
List<SysUser> 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<SysUser> 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<OrgDeptTreeVo> filterOrgTreeByAdmin(List<OrgDeptTreeVo> treeList, OrgQuery query) {
|
||||
List<OrgDeptTreeVo> 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<OrgDeptTreeVo> filteredChildren = filterOrgTreeByAdmin(vo.getChildren(), query);
|
||||
vo.setChildren(filteredChildren);
|
||||
}
|
||||
filteredList.add(vo);
|
||||
} else if (vo.getChildren() != null && !vo.getChildren().isEmpty()) {
|
||||
// 如果当前节点不匹配,但子节点可能匹配,继续检查子节点
|
||||
List<OrgDeptTreeVo> filteredChildren = filterOrgTreeByAdmin(vo.getChildren(), query);
|
||||
if (!filteredChildren.isEmpty()) {
|
||||
vo.setChildren(filteredChildren);
|
||||
filteredList.add(vo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return filteredList;
|
||||
}
|
||||
}
|
||||
|
|
@ -28,6 +28,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||
<result property="logo" column="logo" />
|
||||
<result property="adminUserId" column="admin_user_id" />
|
||||
<result property="initPassword" column="init_password" />
|
||||
<result property="deptLevel" column="dept_level" />
|
||||
</resultMap>
|
||||
|
||||
<sql id="selectDeptVo">
|
||||
|
|
@ -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
|
||||
</sql>
|
||||
|
||||
|
|
@ -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
|
||||
</sql>
|
||||
|
||||
|
|
@ -200,6 +203,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||
<if test="menuTemplateId != null">menu_template_id,</if>
|
||||
<if test="status != null">status,</if>
|
||||
<if test="createBy != null and createBy != ''">create_by,</if>
|
||||
<if test="deptLevel != null and deptLevel != ''">dept_level,</if>
|
||||
create_time
|
||||
)values(
|
||||
<if test="deptId != null and deptId != 0">#{deptId},</if>
|
||||
|
|
@ -221,6 +225,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||
<if test="menuTemplateId != null">#{menuTemplateId},</if>
|
||||
<if test="status != null">#{status},</if>
|
||||
<if test="createBy != null and createBy != ''">#{createBy},</if>
|
||||
<if test="deptLevel != null and deptLevel != ''">#{deptLevel},</if>
|
||||
sysdate()
|
||||
)
|
||||
</insert>
|
||||
|
|
@ -247,6 +252,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||
<if test="menuTemplateId != null and menuTemplateId != ''">menu_template_id = #{menuTemplateId},</if>
|
||||
<if test="status != null and status != ''">status = #{status},</if>
|
||||
<if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
|
||||
<if test="delFlag != null and delFlag != ''">del_flag = #{delFlag},</if>
|
||||
update_time = sysdate()
|
||||
</set>
|
||||
where dept_id = #{deptId}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,88 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.bonus.system.mapper.SysOrgMapper">
|
||||
|
||||
<resultMap type="com.bonus.system.domain.SysOrg" id="SysOrgResult">
|
||||
<result property="orgId" column="org_id" />
|
||||
<result property="orgName" column="org_name" />
|
||||
<result property="orgPath" column="org_path" />
|
||||
<result property="parentOrgId" column="parent_org_id"/>
|
||||
<result property="deptId" column="dept_id" />
|
||||
<result property="isActive" column="is_active" />
|
||||
</resultMap>
|
||||
|
||||
<sql id="selectSysOrgVo">
|
||||
select org_id, org_name, org_path, parent_org_id, dept_id, is_active
|
||||
from sys_org
|
||||
</sql>
|
||||
|
||||
<select id="selectSysOrgList" parameterType="com.bonus.system.domain.SysOrg" resultMap="SysOrgResult">
|
||||
<include refid="selectSysOrgVo"/>
|
||||
<where>
|
||||
<if test="orgName != null and orgName != ''">
|
||||
and org_name like concat('%', #{orgName}, '%')
|
||||
</if>
|
||||
<if test="deptId != null">
|
||||
and dept_id = #{deptId}
|
||||
</if>
|
||||
<if test="isActive != null and isActive != ''">
|
||||
and is_active = #{isActive}
|
||||
</if>
|
||||
<if test="parentOrgId != null">
|
||||
and parent_org_id = #{parentOrgId}
|
||||
</if>
|
||||
</where>
|
||||
order by org_id
|
||||
</select>
|
||||
|
||||
<select id="selectSysOrgById" parameterType="Long" resultMap="SysOrgResult">
|
||||
<include refid="selectSysOrgVo"/>
|
||||
where org_id = #{orgId}
|
||||
</select>
|
||||
|
||||
<insert id="insertSysOrg" parameterType="com.bonus.system.domain.SysOrg" useGeneratedKeys="true" keyProperty="orgId">
|
||||
insert into sys_org
|
||||
<trim prefix="(" suffix=")" suffixOverrides=",">
|
||||
<if test="orgName != null and orgName != ''">org_name,</if>
|
||||
<if test="orgPath != null">org_path,</if>
|
||||
<if test="parentOrgId != null">parent_org_id,</if>
|
||||
<if test="deptId != null">dept_id,</if>
|
||||
<if test="isActive != null and isActive != ''">is_active,</if>
|
||||
</trim>
|
||||
<trim prefix="values (" suffix=")" suffixOverrides=",">
|
||||
<if test="orgName != null and orgName != ''">#{orgName},</if>
|
||||
<if test="orgPath != null">#{orgPath},</if>
|
||||
<if test="parentOrgId != null">#{parentOrgId},</if>
|
||||
<if test="deptId != null">#{deptId},</if>
|
||||
<if test="isActive != null and isActive != ''">#{isActive},</if>
|
||||
</trim>
|
||||
</insert>
|
||||
|
||||
<update id="updateSysOrg" parameterType="com.bonus.system.domain.SysOrg">
|
||||
update sys_org
|
||||
<trim prefix="SET" suffixOverrides=",">
|
||||
<if test="orgName != null and orgName != ''">org_name = #{orgName},</if>
|
||||
<if test="orgPath != null">org_path = #{orgPath},</if>
|
||||
<if test="parentOrgId != null">parent_org_id = #{parentOrgId},</if>
|
||||
<if test="deptId != null">dept_id = #{deptId},</if>
|
||||
<if test="isActive != null and isActive != ''">is_active = #{isActive},</if>
|
||||
</trim>
|
||||
where org_id = #{orgId}
|
||||
</update>
|
||||
|
||||
<delete id="deleteSysOrgById" parameterType="Long">
|
||||
delete from sys_org where org_id = #{orgId}
|
||||
</delete>
|
||||
|
||||
<select id="selectSysOrgListByParentId" resultMap="SysOrgResult">
|
||||
<include refid="selectSysOrgVo"/>
|
||||
where parent_org_id = #{parentOrgId}
|
||||
<if test="deptId != null">
|
||||
and dept_id = #{deptId}
|
||||
</if>
|
||||
</select>
|
||||
|
||||
</mapper>
|
||||
|
||||
|
|
@ -290,6 +290,9 @@
|
|||
<include refid="com.bonus.system.mapper.DataScopeMapper.dataScopeFilter"/>
|
||||
GROUP BY u.user_id
|
||||
</select>
|
||||
<select id="selectUserListByOrgid">
|
||||
select count(1) from sys_user where org_id=#{orgId} and del_flag = '0'
|
||||
</select>
|
||||
|
||||
<insert id="insertUser" parameterType="SysUser" useGeneratedKeys="true" keyProperty="userId">
|
||||
insert into sys_user(
|
||||
|
|
|
|||
Loading…
Reference in New Issue