fix(): 修改代码规范

1. 增加枚举工具类
2. 部门添加为mybatis-plus方式进行添加
3. 部门删除改为mybatis-plus方式进行删除
boot3.0
dongzeliang 2025-01-16 23:32:49 +08:00
parent 989cef6f65
commit 347a329b0a
10 changed files with 295 additions and 59 deletions

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -113,4 +113,5 @@ public class SysDept extends BaseEntity {
public String getEmail () { public String getEmail () {
return email; return email;
} }
} }

View File

@ -9,7 +9,9 @@ import com.muyu.common.log.enums.BusinessType;
import com.muyu.common.security.annotation.RequiresPermissions; import com.muyu.common.security.annotation.RequiresPermissions;
import com.muyu.common.security.utils.SecurityUtils; import com.muyu.common.security.utils.SecurityUtils;
import com.muyu.common.system.domain.SysDept; 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.model.SysDeptPageQueryModel;
import com.muyu.system.domain.rep.SysDeptAddReq;
import com.muyu.system.domain.rep.SysDeptListReq; import com.muyu.system.domain.rep.SysDeptListReq;
import com.muyu.system.service.SysDeptService; import com.muyu.system.service.SysDeptService;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
@ -70,12 +72,12 @@ public class SysDeptController extends BaseController {
@RequiresPermissions("system:dept:add") @RequiresPermissions("system:dept:add")
@Log(title = "部门管理", businessType = BusinessType.INSERT) @Log(title = "部门管理", businessType = BusinessType.INSERT)
@PostMapping @PostMapping
public Result add (@Validated @RequestBody SysDept dept) { public Result<String> add (@Validated @RequestBody SysDeptAddReq sysDeptAddReq) {
if (!deptService.checkDeptNameUnique(dept)) { if (!deptService.checkDeptNameUnique(CheckDeptNameUniqueModel.of(sysDeptAddReq.getDeptName()))) {
return error("新增部门'" + dept.getDeptName() + "'失败,部门名称已存在"); return error("新增部门'" + sysDeptAddReq.getDeptName() + "'失败,部门名称已存在");
} }
dept.setCreateBy(SecurityUtils.getUsername()); deptService.insertDept(sysDeptAddReq.buildSysDept());
return toAjax(deptService.insertDept(dept)); return success();
} }
/** /**
@ -87,7 +89,9 @@ public class SysDeptController extends BaseController {
public Result edit (@Validated @RequestBody SysDept dept) { public Result edit (@Validated @RequestBody SysDept dept) {
Long deptId = dept.getDeptId(); Long deptId = dept.getDeptId();
deptService.checkDeptDataScope(deptId); deptService.checkDeptDataScope(deptId);
if (!deptService.checkDeptNameUnique(dept)) { if (!deptService.checkDeptNameUnique(
CheckDeptNameUniqueModel.of(dept.getDeptName(),deptId,dept.getParentId())
)) {
return error("修改部门'" + dept.getDeptName() + "'失败,部门名称已存在"); return error("修改部门'" + dept.getDeptName() + "'失败,部门名称已存在");
} else if (dept.getParentId().equals(deptId)) { } else if (dept.getParentId().equals(deptId)) {
return error("修改部门'" + dept.getDeptName() + "'失败,上级部门不能是自己"); return error("修改部门'" + dept.getDeptName() + "'失败,上级部门不能是自己");
@ -104,7 +108,7 @@ public class SysDeptController extends BaseController {
@RequiresPermissions("system:dept:remove") @RequiresPermissions("system:dept:remove")
@Log(title = "部门管理", businessType = BusinessType.DELETE) @Log(title = "部门管理", businessType = BusinessType.DELETE)
@DeleteMapping("/{deptId}") @DeleteMapping("/{deptId}")
public Result remove (@PathVariable("deptId") Long deptId) { public Result<String> remove (@PathVariable("deptId") Long deptId) {
if (deptService.hasChildByDeptId(deptId)) { if (deptService.hasChildByDeptId(deptId)) {
return warn("存在下级部门,不允许删除"); return warn("存在下级部门,不允许删除");
} }
@ -112,6 +116,7 @@ public class SysDeptController extends BaseController {
return warn("部门存在用户,不允许删除"); return warn("部门存在用户,不允许删除");
} }
deptService.checkDeptDataScope(deptId); deptService.checkDeptDataScope(deptId);
return toAjax(deptService.deleteDeptById(deptId)); deptService.deleteDeptById(deptId);
return success();
} }
} }

View File

@ -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 IDID
*/
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();
}
}

View File

@ -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();
}
}

View File

@ -1,5 +1,6 @@
package com.muyu.system.mapper; package com.muyu.system.mapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.muyu.common.system.domain.SysDept; import com.muyu.common.system.domain.SysDept;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
@ -31,23 +32,6 @@ public interface SysDeptMapper extends BaseMapper<SysDept> {
*/ */
public List<Long> selectDeptListByRoleId (@Param("roleId") Long roleId, @Param("deptCheckStrictly") boolean deptCheckStrictly); public List<Long> 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<SysDept> selectChildrenDeptById (Long deptId);
/** /**
* ID * ID

View File

@ -2,6 +2,7 @@ package com.muyu.system.service;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.muyu.common.system.domain.SysDept; 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.model.SysDeptPageQueryModel;
import com.muyu.system.domain.vo.TreeSelect; import com.muyu.system.domain.vo.TreeSelect;
@ -95,11 +96,10 @@ public interface SysDeptService extends IService<SysDept> {
/** /**
* *
* *
* @param dept * @param checkDeptNameUniqueModel
*
* @return * @return
*/ */
public boolean checkDeptNameUnique (SysDept dept); public boolean checkDeptNameUnique (CheckDeptNameUniqueModel checkDeptNameUniqueModel);
/** /**
* *
@ -115,7 +115,7 @@ public interface SysDeptService extends IService<SysDept> {
* *
* @return * @return
*/ */
public int insertDept (SysDept dept); public void insertDept (SysDept dept);
/** /**
* *
@ -133,7 +133,7 @@ public interface SysDeptService extends IService<SysDept> {
* *
* @return * @return
*/ */
public int deleteDeptById (Long deptId); public void deleteDeptById (Long deptId);
/** /**
* *
@ -141,4 +141,13 @@ public interface SysDeptService extends IService<SysDept> {
* @return * @return
*/ */
List<SysDept> queryList(SysDeptPageQueryModel sysDeptPageQueryModel); List<SysDept> queryList(SysDeptPageQueryModel sysDeptPageQueryModel);
/**
* ID
*
* @param deptId ID
*
* @return
*/
public List<SysDept> selectChildrenDeptById (Long deptId);
} }

View File

@ -1,8 +1,11 @@
package com.muyu.system.service.impl; package com.muyu.system.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; 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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.muyu.common.core.constant.UserConstants; 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.exception.ServiceException;
import com.muyu.common.core.text.Convert; import com.muyu.common.core.text.Convert;
import com.muyu.common.core.utils.SpringUtils; 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.SysDept;
import com.muyu.common.system.domain.SysRole; import com.muyu.common.system.domain.SysRole;
import com.muyu.common.system.domain.SysUser; 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.model.SysDeptPageQueryModel;
import com.muyu.system.domain.vo.TreeSelect; import com.muyu.system.domain.vo.TreeSelect;
import com.muyu.system.mapper.SysDeptMapper; import com.muyu.system.mapper.SysDeptMapper;
@ -19,6 +23,7 @@ import com.muyu.system.mapper.SysRoleMapper;
import com.muyu.system.service.SysDeptService; import com.muyu.system.service.SysDeptService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -45,6 +50,7 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept>
LambdaQueryWrapper<SysDept> queryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<SysDept> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.like(StringUtils.isNotEmpty(sysDeptPageQueryModel.getDeptName()),SysDept::getDeptName, sysDeptPageQueryModel.getDeptName()); queryWrapper.like(StringUtils.isNotEmpty(sysDeptPageQueryModel.getDeptName()),SysDept::getDeptName, sysDeptPageQueryModel.getDeptName());
queryWrapper.eq(StringUtils.isNotEmpty(sysDeptPageQueryModel.getStatus()),SysDept::getStatus,sysDeptPageQueryModel.getStatus()); queryWrapper.eq(StringUtils.isNotEmpty(sysDeptPageQueryModel.getStatus()),SysDept::getStatus,sysDeptPageQueryModel.getStatus());
queryWrapper.eq(SysDept::getStatus, SysDelFlag.EXIST.getCode());
return this.list(queryWrapper); return this.list(queryWrapper);
} }
@ -141,7 +147,7 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept>
*/ */
@Override @Override
public SysDept selectDeptById (Long deptId) { public SysDept selectDeptById (Long deptId) {
return deptMapper.selectDeptById(deptId); return this.getById(deptId);
} }
/** /**
@ -185,14 +191,19 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept>
/** /**
* *
* *
* @param dept * @param checkDeptNameUniqueModel
*
* @return * @return
*/ */
@Override @Override
public boolean checkDeptNameUnique (SysDept dept) { public boolean checkDeptNameUnique (CheckDeptNameUniqueModel checkDeptNameUniqueModel) {
Long deptId = StringUtils.isNull(dept.getDeptId()) ? -1L : dept.getDeptId(); Assert.notNull(checkDeptNameUniqueModel.getDeptName(), "部门名称不可为空");
SysDept info = deptMapper.checkDeptNameUnique(dept.getDeptName(), dept.getParentId()); LambdaQueryWrapper<SysDept> 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()) { if (StringUtils.isNotNull(info) && info.getDeptId().longValue() != deptId.longValue()) {
return UserConstants.NOT_UNIQUE; return UserConstants.NOT_UNIQUE;
} }
@ -207,9 +218,9 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept>
@Override @Override
public void checkDeptDataScope (Long deptId) { public void checkDeptDataScope (Long deptId) {
if (!SysUser.isAdmin(SecurityUtils.getUserId())) { if (!SysUser.isAdmin(SecurityUtils.getUserId())) {
List<SysDept> depts = SpringUtils.getAopProxy(this) List<SysDept> deptList = SpringUtils.getAopProxy(this)
.selectDeptList(SysDeptPageQueryModel.ofToDeptId(deptId)); .selectDeptList(SysDeptPageQueryModel.ofToDeptId(deptId));
if (StringUtils.isEmpty(depts)) { if (StringUtils.isEmpty(deptList)) {
throw new ServiceException("没有权限访问部门数据!"); throw new ServiceException("没有权限访问部门数据!");
} }
} }
@ -223,14 +234,14 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept>
* @return * @return
*/ */
@Override @Override
public int insertDept (SysDept dept) { public void insertDept (SysDept dept) {
SysDept info = deptMapper.selectDeptById(dept.getParentId()); SysDept parentSysDept = this.getById(dept.getParentId());
// 如果父节点不为正常状态,则不允许新增子节点 // 如果父节点不为正常状态,则不允许新增子节点
if (!UserConstants.DEPT_NORMAL.equals(info.getStatus())) { if (SysNormalDisable.isDisable(dept.getStatus())) {
throw new ServiceException("部门停用,不允许新增"); throw new ServiceException("部门停用,不允许新增");
} }
dept.setAncestors(info.getAncestors() + "," + dept.getParentId()); dept.setAncestors(parentSysDept.getAncestors() + "," + dept.getParentId());
return deptMapper.insertDept(dept); this.save(dept);
} }
/** /**
@ -242,8 +253,8 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept>
*/ */
@Override @Override
public int updateDept (SysDept dept) { public int updateDept (SysDept dept) {
SysDept newParentDept = deptMapper.selectDeptById(dept.getParentId()); SysDept newParentDept = this.getById(dept.getParentId());
SysDept oldDept = deptMapper.selectDeptById(dept.getDeptId()); SysDept oldDept = this.getById(dept.getDeptId());
if (StringUtils.isNotNull(newParentDept) && StringUtils.isNotNull(oldDept)) { if (StringUtils.isNotNull(newParentDept) && StringUtils.isNotNull(oldDept)) {
String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId(); String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId();
String oldAncestors = oldDept.getAncestors(); String oldAncestors = oldDept.getAncestors();
@ -278,7 +289,7 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept>
* @param oldAncestors ID * @param oldAncestors ID
*/ */
public void updateDeptChildren (Long deptId, String newAncestors, String oldAncestors) { public void updateDeptChildren (Long deptId, String newAncestors, String oldAncestors) {
List<SysDept> children = deptMapper.selectChildrenDeptById(deptId); List<SysDept> children = this.selectChildrenDeptById(deptId);
for (SysDept child : children) { for (SysDept child : children) {
child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors)); child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
} }
@ -295,8 +306,11 @@ public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept>
* @return * @return
*/ */
@Override @Override
public int deleteDeptById (Long deptId) { public void deleteDeptById (Long deptId) {
return deptMapper.deleteDeptById(deptId); LambdaUpdateWrapper<SysDept> 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<SysDeptMapper, SysDept>
private boolean hasChild (List<SysDept> list, SysDept t) { private boolean hasChild (List<SysDept> list, SysDept t) {
return !getChildList(list, t).isEmpty(); return !getChildList(list, t).isEmpty();
} }
/**
* ID
*
* @param deptId ID
*
* @return
*/
public List<SysDept> selectChildrenDeptById (Long deptId){
LambdaQueryWrapper<SysDept> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.apply("FIND_IN_SET('" + deptId + "', type)");
return this.list(queryWrapper);
}
} }

View File

@ -70,11 +70,6 @@
order by d.parent_id, d.order_num order by d.parent_id, d.order_num
</select> </select>
<select id="selectDeptById" parameterType="Long" resultMap="SysDeptResult">
<include refid="selectDeptVo"/>
where dept_id = #{deptId}
</select>
<select id="checkDeptExistUser" parameterType="Long" resultType="int"> <select id="checkDeptExistUser" parameterType="Long" resultType="int">
select count(1) select count(1)
from sys_user from sys_user
@ -90,12 +85,6 @@
limit 1 limit 1
</select> </select>
<select id="selectChildrenDeptById" parameterType="Long" resultMap="SysDeptResult">
select *
from sys_dept
where find_in_set(#{deptId}, ancestors)
</select>
<select id="selectNormalChildrenDeptById" parameterType="Long" resultType="int"> <select id="selectNormalChildrenDeptById" parameterType="Long" resultType="int">
select count(*) select count(*)
from sys_dept from sys_dept