feat: Enhance department management with parent ID validation and department scope handling
This commit is contained in:
@@ -76,6 +76,11 @@ public class SysDeptController extends BaseController
|
|||||||
@PostMapping
|
@PostMapping
|
||||||
public AjaxResult add(@Validated @RequestBody SysDept dept)
|
public AjaxResult add(@Validated @RequestBody SysDept dept)
|
||||||
{
|
{
|
||||||
|
if (dept.getParentId() == null)
|
||||||
|
{
|
||||||
|
return error("上级部门不能为空");
|
||||||
|
}
|
||||||
|
deptService.checkDeptDataScope(dept.getParentId());
|
||||||
if (!deptService.checkDeptNameUnique(dept))
|
if (!deptService.checkDeptNameUnique(dept))
|
||||||
{
|
{
|
||||||
return error("新增部门'" + dept.getDeptName() + "'失败,部门名称已存在");
|
return error("新增部门'" + dept.getDeptName() + "'失败,部门名称已存在");
|
||||||
@@ -93,7 +98,12 @@ public class SysDeptController extends BaseController
|
|||||||
public AjaxResult edit(@Validated @RequestBody SysDept dept)
|
public AjaxResult edit(@Validated @RequestBody SysDept dept)
|
||||||
{
|
{
|
||||||
Long deptId = dept.getDeptId();
|
Long deptId = dept.getDeptId();
|
||||||
|
if (dept.getParentId() == null)
|
||||||
|
{
|
||||||
|
return error("上级部门不能为空");
|
||||||
|
}
|
||||||
deptService.checkDeptDataScope(deptId);
|
deptService.checkDeptDataScope(deptId);
|
||||||
|
deptService.checkDeptDataScope(dept.getParentId());
|
||||||
if (!deptService.checkDeptNameUnique(dept))
|
if (!deptService.checkDeptNameUnique(dept))
|
||||||
{
|
{
|
||||||
return error("修改部门'" + dept.getDeptName() + "'失败,部门名称已存在");
|
return error("修改部门'" + dept.getDeptName() + "'失败,部门名称已存在");
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ import org.springframework.web.bind.annotation.PostMapping;
|
|||||||
import org.springframework.web.bind.annotation.PutMapping;
|
import org.springframework.web.bind.annotation.PutMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
import com.ruoyi.common.annotation.Log;
|
import com.ruoyi.common.annotation.Log;
|
||||||
@@ -64,8 +65,10 @@ public class SysUserController extends BaseController
|
|||||||
*/
|
*/
|
||||||
@PreAuthorize("@ss.hasPermi('system:user:list')")
|
@PreAuthorize("@ss.hasPermi('system:user:list')")
|
||||||
@GetMapping("/list")
|
@GetMapping("/list")
|
||||||
public TableDataInfo list(SysUser user)
|
public TableDataInfo list(SysUser user,
|
||||||
|
@RequestParam(value = "deptScope", required = false, defaultValue = "SELF") String deptScope)
|
||||||
{
|
{
|
||||||
|
applyDeptScope(user, deptScope);
|
||||||
startPage();
|
startPage();
|
||||||
List<SysUser> list = userService.selectUserList(user);
|
List<SysUser> list = userService.selectUserList(user);
|
||||||
return getDataTable(list);
|
return getDataTable(list);
|
||||||
@@ -74,8 +77,10 @@ public class SysUserController extends BaseController
|
|||||||
@Log(title = "用户管理", businessType = BusinessType.EXPORT)
|
@Log(title = "用户管理", businessType = BusinessType.EXPORT)
|
||||||
@PreAuthorize("@ss.hasPermi('system:user:export')")
|
@PreAuthorize("@ss.hasPermi('system:user:export')")
|
||||||
@PostMapping("/export")
|
@PostMapping("/export")
|
||||||
public void export(HttpServletResponse response, SysUser user)
|
public void export(HttpServletResponse response, SysUser user,
|
||||||
|
@RequestParam(value = "deptScope", required = false, defaultValue = "SELF") String deptScope)
|
||||||
{
|
{
|
||||||
|
applyDeptScope(user, deptScope);
|
||||||
List<SysUser> list = userService.selectUserList(user);
|
List<SysUser> list = userService.selectUserList(user);
|
||||||
ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
|
ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
|
||||||
util.exportExcel(response, list, "用户数据");
|
util.exportExcel(response, list, "用户数据");
|
||||||
@@ -260,6 +265,20 @@ public class SysUserController extends BaseController
|
|||||||
return success(deptService.selectDeptTreeList(dept));
|
return success(deptService.selectDeptTreeList(dept));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置用户列表的部门范围。
|
||||||
|
*
|
||||||
|
* 未指定部门时使用当前登录用户所在部门,默认只查询本级部门。
|
||||||
|
*/
|
||||||
|
private void applyDeptScope(SysUser user, String deptScope)
|
||||||
|
{
|
||||||
|
if (user.getDeptId() == null || user.getDeptId() == 0)
|
||||||
|
{
|
||||||
|
user.setDeptId(getDeptId());
|
||||||
|
}
|
||||||
|
user.getParams().put("deptScope", "CHILDREN".equalsIgnoreCase(deptScope) ? "CHILDREN" : "SELF");
|
||||||
|
}
|
||||||
|
|
||||||
@ApiOperation("企业资质审核")
|
@ApiOperation("企业资质审核")
|
||||||
@PreAuthorize("@ss.hasPermi('sys:company:approval')")
|
@PreAuthorize("@ss.hasPermi('sys:company:approval')")
|
||||||
@PostMapping("/approval")
|
@PostMapping("/approval")
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
import org.apache.commons.lang3.ArrayUtils;
|
||||||
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 com.ruoyi.common.annotation.DataScope;
|
import com.ruoyi.common.annotation.DataScope;
|
||||||
@@ -211,7 +212,17 @@ public class SysDeptServiceImpl implements ISysDeptService
|
|||||||
@Override
|
@Override
|
||||||
public int insertDept(SysDept dept)
|
public int insertDept(SysDept dept)
|
||||||
{
|
{
|
||||||
|
if (StringUtils.isNull(dept.getParentId()))
|
||||||
|
{
|
||||||
|
throw new ServiceException("上级部门不能为空");
|
||||||
|
}
|
||||||
|
|
||||||
SysDept info = deptMapper.selectDeptById(dept.getParentId());
|
SysDept info = deptMapper.selectDeptById(dept.getParentId());
|
||||||
|
if (StringUtils.isNull(info) || !"0".equals(info.getDelFlag()))
|
||||||
|
{
|
||||||
|
throw new ServiceException("上级部门不存在");
|
||||||
|
}
|
||||||
|
|
||||||
// 如果父节点不为正常状态,则不允许新增子节点
|
// 如果父节点不为正常状态,则不允许新增子节点
|
||||||
if (!UserConstants.DEPT_NORMAL.equals(info.getStatus()))
|
if (!UserConstants.DEPT_NORMAL.equals(info.getStatus()))
|
||||||
{
|
{
|
||||||
@@ -230,13 +241,36 @@ public class SysDeptServiceImpl implements ISysDeptService
|
|||||||
@Override
|
@Override
|
||||||
public int updateDept(SysDept dept)
|
public int updateDept(SysDept dept)
|
||||||
{
|
{
|
||||||
|
if (StringUtils.isNull(dept.getDeptId()))
|
||||||
|
{
|
||||||
|
throw new ServiceException("部门编号不能为空");
|
||||||
|
}
|
||||||
|
if (StringUtils.isNull(dept.getParentId()))
|
||||||
|
{
|
||||||
|
throw new ServiceException("上级部门不能为空");
|
||||||
|
}
|
||||||
|
|
||||||
SysDept newParentDept = deptMapper.selectDeptById(dept.getParentId());
|
SysDept newParentDept = deptMapper.selectDeptById(dept.getParentId());
|
||||||
SysDept oldDept = deptMapper.selectDeptById(dept.getDeptId());
|
SysDept oldDept = deptMapper.selectDeptById(dept.getDeptId());
|
||||||
if (StringUtils.isNotNull(newParentDept) && StringUtils.isNotNull(oldDept))
|
if (StringUtils.isNull(oldDept))
|
||||||
|
{
|
||||||
|
throw new ServiceException("部门不存在");
|
||||||
|
}
|
||||||
|
if (StringUtils.isNull(newParentDept) || !"0".equals(newParentDept.getDelFlag()))
|
||||||
|
{
|
||||||
|
throw new ServiceException("上级部门不存在");
|
||||||
|
}
|
||||||
|
if (dept.getDeptId().equals(newParentDept.getDeptId())
|
||||||
|
|| ArrayUtils.contains(StringUtils.split(newParentDept.getAncestors(), ","), dept.getDeptId().toString()))
|
||||||
|
{
|
||||||
|
throw new ServiceException("不能选择当前部门或其下级部门作为上级部门");
|
||||||
|
}
|
||||||
|
|
||||||
|
String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId();
|
||||||
|
String oldAncestors = oldDept.getAncestors();
|
||||||
|
dept.setAncestors(newAncestors);
|
||||||
|
if (!StringUtils.equals(newAncestors, oldAncestors))
|
||||||
{
|
{
|
||||||
String newAncestors = newParentDept.getAncestors() + "," + newParentDept.getDeptId();
|
|
||||||
String oldAncestors = oldDept.getAncestors();
|
|
||||||
dept.setAncestors(newAncestors);
|
|
||||||
updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors);
|
updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors);
|
||||||
}
|
}
|
||||||
int result = deptMapper.updateDept(dept);
|
int result = deptMapper.updateDept(dept);
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="selectDeptById" parameterType="Long" resultMap="SysDeptResult">
|
<select id="selectDeptById" parameterType="Long" resultMap="SysDeptResult">
|
||||||
select d.dept_id, d.parent_id, d.ancestors, d.dept_name, d.order_num, d.leader, d.phone, d.email, d.status,
|
select d.dept_id, d.parent_id, d.ancestors, d.dept_name, d.order_num, d.leader, d.phone, d.email, d.status, d.del_flag,
|
||||||
(select dept_name from sys_dept where dept_id = d.parent_id) parent_name
|
(select dept_name from sys_dept where dept_id = d.parent_id) parent_name
|
||||||
from sys_dept d
|
from sys_dept d
|
||||||
where d.dept_id = #{deptId}
|
where d.dept_id = #{deptId}
|
||||||
@@ -156,4 +156,4 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
update sys_dept set del_flag = '2' where dept_id = #{deptId}
|
update sys_dept set del_flag = '2' where dept_id = #{deptId}
|
||||||
</delete>
|
</delete>
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
|
|||||||
@@ -83,7 +83,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
AND date_format(u.create_time,'%Y%m%d') <= date_format(#{params.endTime},'%Y%m%d')
|
AND date_format(u.create_time,'%Y%m%d') <= date_format(#{params.endTime},'%Y%m%d')
|
||||||
</if>
|
</if>
|
||||||
<if test="deptId != null and deptId != 0">
|
<if test="deptId != null and deptId != 0">
|
||||||
AND (u.dept_id = #{deptId} OR u.dept_id IN ( SELECT t.dept_id FROM sys_dept t WHERE find_in_set(#{deptId}, ancestors) ))
|
AND (
|
||||||
|
u.dept_id = #{deptId}
|
||||||
|
<if test="params.deptScope != null and params.deptScope == 'CHILDREN'">
|
||||||
|
OR u.dept_id IN ( SELECT t.dept_id FROM sys_dept t WHERE find_in_set(#{deptId}, ancestors) )
|
||||||
|
</if>
|
||||||
|
)
|
||||||
</if>
|
</if>
|
||||||
<if test="idCard != null and idCard != ''">
|
<if test="idCard != null and idCard != ''">
|
||||||
AND u.id_card = #{idCard}
|
AND u.id_card = #{idCard}
|
||||||
@@ -266,4 +271,4 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
where ur.user_id = #{userId}
|
where ur.user_id = #{userId}
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
</mapper>
|
</mapper>
|
||||||
|
|||||||
@@ -1257,7 +1257,7 @@ create table "ks_db3"."sys_dept"
|
|||||||
"dept_id" bigint not null,
|
"dept_id" bigint not null,
|
||||||
"parent_id" bigint default 0
|
"parent_id" bigint default 0
|
||||||
null,
|
null,
|
||||||
"ancestors" varchar(50) default ''
|
"ancestors" text default ''
|
||||||
null,
|
null,
|
||||||
"dept_name" varchar(30) default ''
|
"dept_name" varchar(30) default ''
|
||||||
null,
|
null,
|
||||||
@@ -2633,4 +2633,3 @@ comment on table "ks_db3"."sys_user_role" is '用户和角色关联表';
|
|||||||
comment on column "ks_db3"."sys_user_role"."role_id" is '角色id';
|
comment on column "ks_db3"."sys_user_role"."role_id" is '角色id';
|
||||||
|
|
||||||
comment on column "ks_db3"."sys_user_role"."user_id" is '用户id';
|
comment on column "ks_db3"."sys_user_role"."user_id" is '用户id';
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ drop table if exists sys_dept;
|
|||||||
create table sys_dept (
|
create table sys_dept (
|
||||||
dept_id bigint(20) not null auto_increment comment '部门id',
|
dept_id bigint(20) not null auto_increment comment '部门id',
|
||||||
parent_id bigint(20) default 0 comment '父部门id',
|
parent_id bigint(20) default 0 comment '父部门id',
|
||||||
ancestors varchar(50) default '' comment '祖级列表',
|
ancestors varchar(1000) default '' comment '祖级列表',
|
||||||
dept_name varchar(30) default '' comment '部门名称',
|
dept_name varchar(30) default '' comment '部门名称',
|
||||||
order_num int(4) default 0 comment '显示顺序',
|
order_num int(4) default 0 comment '显示顺序',
|
||||||
leader varchar(20) default null comment '负责人',
|
leader varchar(20) default null comment '负责人',
|
||||||
@@ -698,4 +698,4 @@ create table gen_table_column (
|
|||||||
update_by varchar(64) default '' comment '更新者',
|
update_by varchar(64) default '' comment '更新者',
|
||||||
update_time datetime comment '更新时间',
|
update_time datetime comment '更新时间',
|
||||||
primary key (column_id)
|
primary key (column_id)
|
||||||
) engine=innodb auto_increment=1 comment = '代码生成业务表字段';
|
) engine=innodb auto_increment=1 comment = '代码生成业务表字段';
|
||||||
|
|||||||
Reference in New Issue
Block a user