diff --git a/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/enums/SysDelFlag.java b/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/enums/SysDelFlag.java new file mode 100644 index 0000000..2c5ff7a --- /dev/null +++ b/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/enums/SysDelFlag.java @@ -0,0 +1,30 @@ +package com.muyu.common.core.enums; + +/** + * @author dongzeliang + * @version 1.0 + * @description: 系统逻辑删除 + * @date 2025/1/16 18:48 + */ +public enum SysDelFlag { + // 存在 + EXIST("0", "存在"), + // 删除 + NOT_EXIST("2", "删除") + ; + private final String code; + private final String info; + + SysDelFlag(String code, String info) { + this.code = code; + this.info = info; + } + + public String getCode() { + return code; + } + + public String getInfo() { + return info; + } +} diff --git a/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/enums/SysNormalDisable.java b/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/enums/SysNormalDisable.java new file mode 100644 index 0000000..007cf0f --- /dev/null +++ b/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/enums/SysNormalDisable.java @@ -0,0 +1,41 @@ +package com.muyu.common.core.enums; + +import lombok.Getter; + +/** + * @author dongzeliang + * @version 1.0 + * @description: 系统开关 + * @date 2025/1/16 18:44 + */ +@Getter +public enum SysNormalDisable { + ENABLE("0", "正常"), + DISABLE("1", "停用"); + private final String code; + private final String info; + + SysNormalDisable(String code, String info) { + this.code = code; + this.info = info; + } + + /** + * 是否启用 + * @param code 编码 + * @return 启用:true 禁用:false + */ + public static boolean isEnable(String code) { + return ENABLE.getCode().equals(code); + } + + /** + * 是否禁用 + * @param code 编码 + * @return 启用:false 禁用:true + */ + public static boolean isDisable(String code) { + return !isEnable(code); + } + +} diff --git a/cloud-common/cloud-common-system/src/main/java/com/muyu/common/system/domain/SysDept.java b/cloud-common/cloud-common-system/src/main/java/com/muyu/common/system/domain/SysDept.java index c7247bd..f58b16c 100644 --- a/cloud-common/cloud-common-system/src/main/java/com/muyu/common/system/domain/SysDept.java +++ b/cloud-common/cloud-common-system/src/main/java/com/muyu/common/system/domain/SysDept.java @@ -113,4 +113,5 @@ public class SysDept extends BaseEntity { public String getEmail () { return email; } + } diff --git a/cloud-modules/cloud-modules-system/src/main/java/com/muyu/system/controller/SysDeptController.java b/cloud-modules/cloud-modules-system/src/main/java/com/muyu/system/controller/SysDeptController.java index 8bcd88c..c3a2f52 100644 --- a/cloud-modules/cloud-modules-system/src/main/java/com/muyu/system/controller/SysDeptController.java +++ b/cloud-modules/cloud-modules-system/src/main/java/com/muyu/system/controller/SysDeptController.java @@ -9,7 +9,9 @@ import com.muyu.common.log.enums.BusinessType; import com.muyu.common.security.annotation.RequiresPermissions; import com.muyu.common.security.utils.SecurityUtils; import com.muyu.common.system.domain.SysDept; +import com.muyu.system.domain.model.CheckDeptNameUniqueModel; import com.muyu.system.domain.model.SysDeptPageQueryModel; +import com.muyu.system.domain.rep.SysDeptAddReq; import com.muyu.system.domain.rep.SysDeptListReq; import com.muyu.system.service.SysDeptService; import io.swagger.v3.oas.annotations.tags.Tag; @@ -70,12 +72,12 @@ public class SysDeptController extends BaseController { @RequiresPermissions("system:dept:add") @Log(title = "部门管理", businessType = BusinessType.INSERT) @PostMapping - public Result add (@Validated @RequestBody SysDept dept) { - if (!deptService.checkDeptNameUnique(dept)) { - return error("新增部门'" + dept.getDeptName() + "'失败,部门名称已存在"); + public Result add (@Validated @RequestBody SysDeptAddReq sysDeptAddReq) { + if (!deptService.checkDeptNameUnique(CheckDeptNameUniqueModel.of(sysDeptAddReq.getDeptName()))) { + return error("新增部门'" + sysDeptAddReq.getDeptName() + "'失败,部门名称已存在"); } - dept.setCreateBy(SecurityUtils.getUsername()); - return toAjax(deptService.insertDept(dept)); + deptService.insertDept(sysDeptAddReq.buildSysDept()); + return success(); } /** @@ -87,7 +89,9 @@ public class SysDeptController extends BaseController { public Result edit (@Validated @RequestBody SysDept dept) { Long deptId = dept.getDeptId(); deptService.checkDeptDataScope(deptId); - if (!deptService.checkDeptNameUnique(dept)) { + if (!deptService.checkDeptNameUnique( + CheckDeptNameUniqueModel.of(dept.getDeptName(),deptId,dept.getParentId()) + )) { return error("修改部门'" + dept.getDeptName() + "'失败,部门名称已存在"); } else if (dept.getParentId().equals(deptId)) { return error("修改部门'" + dept.getDeptName() + "'失败,上级部门不能是自己"); @@ -104,7 +108,7 @@ public class SysDeptController extends BaseController { @RequiresPermissions("system:dept:remove") @Log(title = "部门管理", businessType = BusinessType.DELETE) @DeleteMapping("/{deptId}") - public Result remove (@PathVariable("deptId") Long deptId) { + public Result remove (@PathVariable("deptId") Long deptId) { if (deptService.hasChildByDeptId(deptId)) { return warn("存在下级部门,不允许删除"); } @@ -112,6 +116,7 @@ public class SysDeptController extends BaseController { return warn("部门存在用户,不允许删除"); } deptService.checkDeptDataScope(deptId); - return toAjax(deptService.deleteDeptById(deptId)); + deptService.deleteDeptById(deptId); + return success(); } } diff --git a/cloud-modules/cloud-modules-system/src/main/java/com/muyu/system/domain/model/CheckDeptNameUniqueModel.java b/cloud-modules/cloud-modules-system/src/main/java/com/muyu/system/domain/model/CheckDeptNameUniqueModel.java new file mode 100644 index 0000000..8df610b --- /dev/null +++ b/cloud-modules/cloud-modules-system/src/main/java/com/muyu/system/domain/model/CheckDeptNameUniqueModel.java @@ -0,0 +1,73 @@ +package com.muyu.system.domain.model; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * @author dongzeliang + * @version 1.0 + * @description: 校验部门名称唯一性模型 + * @date 2025/1/16 18:18 + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class CheckDeptNameUniqueModel { + + /** + * 部门名称 + */ + private String deptName; + + /** + * 部门ID 若传入部门ID则排除此部门 + */ + private Long deptId; + + /** + * 部门父级ID 传入父级ID则只校验子集ID + */ + private Long parentDeptId; + + /** + * 根据部门名称进行唯一性查询模型构建 + * @param deptName 部门名称 + * @return 唯一性查询模型 + */ + public static CheckDeptNameUniqueModel of(String deptName){ + return CheckDeptNameUniqueModel.builder() + .deptName(deptName) + .build(); + } + + /** + * 根据部门名称进行唯一性查询模型构建 + * @param deptName 部门名称 + * @param deptId 部门ID + * @return 唯一性查询模型 + */ + public static CheckDeptNameUniqueModel of(String deptName, Long deptId){ + return CheckDeptNameUniqueModel.builder() + .deptName(deptName) + .deptId(deptId) + .build(); + } + + /** + * 根据部门名称进行唯一性查询模型构建 + * @param deptName 部门名称 + * @param deptId 部门ID + * @param parentDeptId 部门父级ID + * @return 唯一性查询模型 + */ + public static CheckDeptNameUniqueModel of(String deptName, Long deptId, Long parentDeptId){ + return CheckDeptNameUniqueModel.builder() + .deptName(deptName) + .deptId(deptId) + .parentDeptId(parentDeptId) + .build(); + } +} diff --git a/cloud-modules/cloud-modules-system/src/main/java/com/muyu/system/domain/rep/SysDeptAddReq.java b/cloud-modules/cloud-modules-system/src/main/java/com/muyu/system/domain/rep/SysDeptAddReq.java new file mode 100644 index 0000000..23fc330 --- /dev/null +++ b/cloud-modules/cloud-modules-system/src/main/java/com/muyu/system/domain/rep/SysDeptAddReq.java @@ -0,0 +1,77 @@ +package com.muyu.system.domain.rep; + +import com.baomidou.mybatisplus.annotation.TableField; +import com.muyu.common.security.utils.SecurityUtils; +import com.muyu.common.system.domain.SysDept; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * @author dongzeliang + * @version 1.0 + * @description: 部门管理添加请求对象 + * @date 2025/1/16 18:06 + */ +@Data +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class SysDeptAddReq { + + /** + * 父部门ID + */ + private Long parentId; + + /** + * 部门名称 + */ + private String deptName; + + /** + * 显示顺序 + */ + private Integer orderNum; + + /** + * 负责人 + */ + private String leader; + + /** + * 联系电话 + */ + private String phone; + + /** + * 邮箱 + */ + private String email; + + /** + * 部门状态:0正常,1停用 + */ + private String status; + + /** + * 删除标志(0代表存在 2代表删除) + */ + private String delFlag; + + public SysDept buildSysDept(){ + return SysDept.builder() + .deptName(this.deptName) + .orderNum(this.orderNum) + .email(this.email) + .delFlag(this.delFlag) + .phone(this.phone) + .status(this.status) + .leader(this.leader) + .parentId(this.parentId) + .createBy(SecurityUtils.getUsername()) + .build(); + } + +} diff --git a/cloud-modules/cloud-modules-system/src/main/java/com/muyu/system/mapper/SysDeptMapper.java b/cloud-modules/cloud-modules-system/src/main/java/com/muyu/system/mapper/SysDeptMapper.java index 5ef9ab4..42871ec 100644 --- a/cloud-modules/cloud-modules-system/src/main/java/com/muyu/system/mapper/SysDeptMapper.java +++ b/cloud-modules/cloud-modules-system/src/main/java/com/muyu/system/mapper/SysDeptMapper.java @@ -1,5 +1,6 @@ package com.muyu.system.mapper; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.muyu.common.system.domain.SysDept; import org.apache.ibatis.annotations.Param; @@ -31,23 +32,6 @@ public interface SysDeptMapper extends BaseMapper { */ public List selectDeptListByRoleId (@Param("roleId") Long roleId, @Param("deptCheckStrictly") boolean deptCheckStrictly); - /** - * 根据部门ID查询信息 - * - * @param deptId 部门ID - * - * @return 部门信息 - */ - public SysDept selectDeptById (Long deptId); - - /** - * 根据ID查询所有子部门 - * - * @param deptId 部门ID - * - * @return 部门列表 - */ - public List selectChildrenDeptById (Long deptId); /** * 根据ID查询所有子部门(正常状态) diff --git a/cloud-modules/cloud-modules-system/src/main/java/com/muyu/system/service/SysDeptService.java b/cloud-modules/cloud-modules-system/src/main/java/com/muyu/system/service/SysDeptService.java index a252608..a9576f1 100644 --- a/cloud-modules/cloud-modules-system/src/main/java/com/muyu/system/service/SysDeptService.java +++ b/cloud-modules/cloud-modules-system/src/main/java/com/muyu/system/service/SysDeptService.java @@ -2,6 +2,7 @@ package com.muyu.system.service; import com.baomidou.mybatisplus.extension.service.IService; import com.muyu.common.system.domain.SysDept; +import com.muyu.system.domain.model.CheckDeptNameUniqueModel; import com.muyu.system.domain.model.SysDeptPageQueryModel; import com.muyu.system.domain.vo.TreeSelect; @@ -95,11 +96,10 @@ public interface SysDeptService extends IService { /** * 校验部门名称是否唯一 * - * @param dept 部门信息 - * + * @param checkDeptNameUniqueModel 部门信息 * @return 结果 */ - public boolean checkDeptNameUnique (SysDept dept); + public boolean checkDeptNameUnique (CheckDeptNameUniqueModel checkDeptNameUniqueModel); /** * 校验部门是否有数据权限 @@ -115,7 +115,7 @@ public interface SysDeptService extends IService { * * @return 结果 */ - public int insertDept (SysDept dept); + public void insertDept (SysDept dept); /** * 修改保存部门信息 @@ -133,7 +133,7 @@ public interface SysDeptService extends IService { * * @return 结果 */ - public int deleteDeptById (Long deptId); + public void deleteDeptById (Long deptId); /** * 查询部门信息 @@ -141,4 +141,13 @@ public interface SysDeptService extends IService { * @return 分页返回结果 */ List queryList(SysDeptPageQueryModel sysDeptPageQueryModel); + + /** + * 根据ID查询所有子部门 + * + * @param deptId 部门ID + * + * @return 部门列表 + */ + public List selectChildrenDeptById (Long deptId); } diff --git a/cloud-modules/cloud-modules-system/src/main/java/com/muyu/system/service/impl/SysDeptServiceImpl.java b/cloud-modules/cloud-modules-system/src/main/java/com/muyu/system/service/impl/SysDeptServiceImpl.java index 57ad332..3f4363f 100644 --- a/cloud-modules/cloud-modules-system/src/main/java/com/muyu/system/service/impl/SysDeptServiceImpl.java +++ b/cloud-modules/cloud-modules-system/src/main/java/com/muyu/system/service/impl/SysDeptServiceImpl.java @@ -1,8 +1,11 @@ package com.muyu.system.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.muyu.common.core.constant.UserConstants; +import com.muyu.common.core.enums.SysDelFlag; +import com.muyu.common.core.enums.SysNormalDisable; import com.muyu.common.core.exception.ServiceException; import com.muyu.common.core.text.Convert; import com.muyu.common.core.utils.SpringUtils; @@ -12,6 +15,7 @@ import com.muyu.common.security.utils.SecurityUtils; import com.muyu.common.system.domain.SysDept; import com.muyu.common.system.domain.SysRole; import com.muyu.common.system.domain.SysUser; +import com.muyu.system.domain.model.CheckDeptNameUniqueModel; import com.muyu.system.domain.model.SysDeptPageQueryModel; import com.muyu.system.domain.vo.TreeSelect; import com.muyu.system.mapper.SysDeptMapper; @@ -19,6 +23,7 @@ import com.muyu.system.mapper.SysRoleMapper; import com.muyu.system.service.SysDeptService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.util.Assert; import java.util.ArrayList; import java.util.List; @@ -45,6 +50,7 @@ public class SysDeptServiceImpl extends ServiceImpl LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); queryWrapper.like(StringUtils.isNotEmpty(sysDeptPageQueryModel.getDeptName()),SysDept::getDeptName, sysDeptPageQueryModel.getDeptName()); queryWrapper.eq(StringUtils.isNotEmpty(sysDeptPageQueryModel.getStatus()),SysDept::getStatus,sysDeptPageQueryModel.getStatus()); + queryWrapper.eq(SysDept::getStatus, SysDelFlag.EXIST.getCode()); return this.list(queryWrapper); } @@ -141,7 +147,7 @@ public class SysDeptServiceImpl extends ServiceImpl */ @Override public SysDept selectDeptById (Long deptId) { - return deptMapper.selectDeptById(deptId); + return this.getById(deptId); } /** @@ -185,14 +191,19 @@ public class SysDeptServiceImpl extends ServiceImpl /** * 校验部门名称是否唯一 * - * @param dept 部门信息 - * + * @param checkDeptNameUniqueModel 检查模型 * @return 结果 */ @Override - public boolean checkDeptNameUnique (SysDept dept) { - Long deptId = StringUtils.isNull(dept.getDeptId()) ? -1L : dept.getDeptId(); - SysDept info = deptMapper.checkDeptNameUnique(dept.getDeptName(), dept.getParentId()); + public boolean checkDeptNameUnique (CheckDeptNameUniqueModel checkDeptNameUniqueModel) { + Assert.notNull(checkDeptNameUniqueModel.getDeptName(), "部门名称不可为空"); + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); + queryWrapper.eq(SysDept::getDeptName, checkDeptNameUniqueModel.getDeptName()); + queryWrapper.eq(Objects.nonNull(checkDeptNameUniqueModel.getParentDeptId()), SysDept::getParentId, checkDeptNameUniqueModel.getParentDeptId()); + queryWrapper.eq(SysDept::getStatus, SysDelFlag.EXIST.getCode()); + + Long deptId = StringUtils.isNull(checkDeptNameUniqueModel.getDeptId()) ? -1L : checkDeptNameUniqueModel.getDeptId(); + SysDept info = deptMapper.checkDeptNameUnique(checkDeptNameUniqueModel.getDeptName(), checkDeptNameUniqueModel.getParentDeptId()); if (StringUtils.isNotNull(info) && info.getDeptId().longValue() != deptId.longValue()) { return UserConstants.NOT_UNIQUE; } @@ -207,9 +218,9 @@ public class SysDeptServiceImpl extends ServiceImpl @Override public void checkDeptDataScope (Long deptId) { if (!SysUser.isAdmin(SecurityUtils.getUserId())) { - List depts = SpringUtils.getAopProxy(this) + List deptList = SpringUtils.getAopProxy(this) .selectDeptList(SysDeptPageQueryModel.ofToDeptId(deptId)); - if (StringUtils.isEmpty(depts)) { + if (StringUtils.isEmpty(deptList)) { throw new ServiceException("没有权限访问部门数据!"); } } @@ -223,14 +234,14 @@ public class SysDeptServiceImpl extends ServiceImpl * @return 结果 */ @Override - public int insertDept (SysDept dept) { - SysDept info = deptMapper.selectDeptById(dept.getParentId()); + public void insertDept (SysDept dept) { + SysDept parentSysDept = this.getById(dept.getParentId()); // 如果父节点不为正常状态,则不允许新增子节点 - if (!UserConstants.DEPT_NORMAL.equals(info.getStatus())) { + if (SysNormalDisable.isDisable(dept.getStatus())) { throw new ServiceException("部门停用,不允许新增"); } - dept.setAncestors(info.getAncestors() + "," + dept.getParentId()); - return deptMapper.insertDept(dept); + dept.setAncestors(parentSysDept.getAncestors() + "," + dept.getParentId()); + this.save(dept); } /** @@ -242,8 +253,8 @@ public class SysDeptServiceImpl extends ServiceImpl */ @Override public int updateDept (SysDept dept) { - SysDept newParentDept = deptMapper.selectDeptById(dept.getParentId()); - SysDept oldDept = deptMapper.selectDeptById(dept.getDeptId()); + SysDept newParentDept = this.getById(dept.getParentId()); + SysDept oldDept = this.getById(dept.getDeptId()); if (StringUtils.isNotNull(newParentDept) && StringUtils.isNotNull(oldDept)) { String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId(); String oldAncestors = oldDept.getAncestors(); @@ -278,7 +289,7 @@ public class SysDeptServiceImpl extends ServiceImpl * @param oldAncestors 旧的父ID集合 */ public void updateDeptChildren (Long deptId, String newAncestors, String oldAncestors) { - List children = deptMapper.selectChildrenDeptById(deptId); + List children = this.selectChildrenDeptById(deptId); for (SysDept child : children) { child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors)); } @@ -295,8 +306,11 @@ public class SysDeptServiceImpl extends ServiceImpl * @return 结果 */ @Override - public int deleteDeptById (Long deptId) { - return deptMapper.deleteDeptById(deptId); + public void deleteDeptById (Long deptId) { + LambdaUpdateWrapper updateWrapper = new LambdaUpdateWrapper<>(); + updateWrapper.set(SysDept::getStatus, SysDelFlag.NOT_EXIST.getCode()); + updateWrapper.eq(SysDept::getDeptId, deptId); + this.update(updateWrapper); } @@ -334,4 +348,17 @@ public class SysDeptServiceImpl extends ServiceImpl private boolean hasChild (List list, SysDept t) { return !getChildList(list, t).isEmpty(); } + + /** + * 根据ID查询所有子部门 + * + * @param deptId 部门ID + * + * @return 部门列表 + */ + public List selectChildrenDeptById (Long deptId){ + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); + queryWrapper.apply("FIND_IN_SET('" + deptId + "', type)"); + return this.list(queryWrapper); + } } diff --git a/cloud-modules/cloud-modules-system/src/main/resources/mapper/system/SysDeptMapper.xml b/cloud-modules/cloud-modules-system/src/main/resources/mapper/system/SysDeptMapper.xml index a571d8b..d812d2f 100644 --- a/cloud-modules/cloud-modules-system/src/main/resources/mapper/system/SysDeptMapper.xml +++ b/cloud-modules/cloud-modules-system/src/main/resources/mapper/system/SysDeptMapper.xml @@ -70,11 +70,6 @@ order by d.parent_id, d.order_num - - - -