获取当前用户所属公司

This commit is contained in:
liang.chao 2024-04-07 13:17:18 +08:00
parent d0efce8e97
commit c1d81433dd
5 changed files with 96 additions and 117 deletions

View File

@ -1,6 +1,7 @@
package com.bonus.sgzb.system.controller; package com.bonus.sgzb.system.controller;
import java.util.List; import java.util.List;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
@ -25,15 +26,15 @@ import com.bonus.sgzb.system.service.ISysDeptService;
/** /**
* 部门信息 * 部门信息
* *
* @author ruoyi * @author ruoyi
*/ */
/** 机构管理 /** 机构管理
* @author bns*/ * @author bns*/
@RestController @RestController
@RequestMapping("/dept") @RequestMapping("/dept")
public class SysDeptController extends BaseController public class SysDeptController extends BaseController {
{
@Autowired @Autowired
private ISysDeptService deptService; private ISysDeptService deptService;
@ -42,8 +43,7 @@ public class SysDeptController extends BaseController
*/ */
@RequiresPermissions("system:dept:list") @RequiresPermissions("system:dept:list")
@GetMapping("/list") @GetMapping("/list")
public AjaxResult list(SysDept dept) public AjaxResult list(SysDept dept) {
{
List<SysDept> depts = deptService.selectDeptList(dept); List<SysDept> depts = deptService.selectDeptList(dept);
return success(depts); return success(depts);
} }
@ -53,8 +53,7 @@ public class SysDeptController extends BaseController
*/ */
@RequiresPermissions("system:dept:list") @RequiresPermissions("system:dept:list")
@GetMapping("/list/exclude/{deptId}") @GetMapping("/list/exclude/{deptId}")
public AjaxResult excludeChild(@PathVariable(value = "deptId", required = false) Long deptId) public AjaxResult excludeChild(@PathVariable(value = "deptId", required = false) Long deptId) {
{
List<SysDept> depts = deptService.selectDeptList(new SysDept()); List<SysDept> depts = deptService.selectDeptList(new SysDept());
depts.removeIf(d -> d.getDeptId().intValue() == deptId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), deptId + "")); depts.removeIf(d -> d.getDeptId().intValue() == deptId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), deptId + ""));
return success(depts); return success(depts);
@ -65,22 +64,28 @@ public class SysDeptController extends BaseController
*/ */
@RequiresPermissions("system:dept:query") @RequiresPermissions("system:dept:query")
@GetMapping(value = "/{deptId}") @GetMapping(value = "/{deptId}")
public AjaxResult getInfo(@PathVariable Long deptId) public AjaxResult getInfo(@PathVariable Long deptId) {
{
deptService.checkDeptDataScope(deptId); deptService.checkDeptDataScope(deptId);
return success(deptService.selectDeptById(deptId)); return success(deptService.selectDeptById(deptId));
} }
/**
* 根据部门编号获取公司名称
*/
@GetMapping(value = "/getCompanyByAncestors")
public AjaxResult getCompanyByDeptId(String ancestors) {
return success(deptService.getCompanyByAncestors(ancestors));
}
/** /**
* 新增部门 * 新增部门
*/ */
@RequiresPermissions("system:dept:add") @RequiresPermissions("system:dept:add")
@Log(title = "部门管理", businessType = BusinessType.INSERT) @Log(title = "部门管理", businessType = BusinessType.INSERT)
@PostMapping @PostMapping
public AjaxResult add(@Validated @RequestBody SysDept dept) public AjaxResult add(@Validated @RequestBody SysDept dept) {
{ if (!deptService.checkDeptNameUnique(dept)) {
if (!deptService.checkDeptNameUnique(dept))
{
return error("新增部门'" + dept.getDeptName() + "'失败,部门名称已存在"); return error("新增部门'" + dept.getDeptName() + "'失败,部门名称已存在");
} }
dept.setCreateBy(SecurityUtils.getUsername()); dept.setCreateBy(SecurityUtils.getUsername());
@ -93,20 +98,14 @@ public class SysDeptController extends BaseController
@RequiresPermissions("system:dept:edit") @RequiresPermissions("system:dept:edit")
@Log(title = "部门管理", businessType = BusinessType.UPDATE) @Log(title = "部门管理", businessType = BusinessType.UPDATE)
@PutMapping @PutMapping
public AjaxResult edit(@Validated @RequestBody SysDept dept) public AjaxResult 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(dept)) {
{
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() + "'失败,上级部门不能是自己");
} } else if (StringUtils.equals(UserConstants.DEPT_DISABLE, dept.getStatus()) && deptService.selectNormalChildrenDeptById(deptId) > 0) {
else if (StringUtils.equals(UserConstants.DEPT_DISABLE, dept.getStatus()) && deptService.selectNormalChildrenDeptById(deptId) > 0)
{
return error("该部门包含未停用的子部门!"); return error("该部门包含未停用的子部门!");
} }
dept.setUpdateBy(SecurityUtils.getUsername()); dept.setUpdateBy(SecurityUtils.getUsername());
@ -119,14 +118,11 @@ 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 AjaxResult remove(@PathVariable Long deptId) public AjaxResult remove(@PathVariable Long deptId) {
{ if (deptService.hasChildByDeptId(deptId)) {
if (deptService.hasChildByDeptId(deptId))
{
return warn("存在下级部门,不允许删除"); return warn("存在下级部门,不允许删除");
} }
if (deptService.checkDeptExistUser(deptId)) if (deptService.checkDeptExistUser(deptId)) {
{
return warn("部门存在用户,不允许删除"); return warn("部门存在用户,不允许删除");
} }
deptService.checkDeptDataScope(deptId); deptService.checkDeptDataScope(deptId);

View File

@ -122,4 +122,6 @@ public interface SysDeptMapper
* @return List<SysDept> * @return List<SysDept>
*/ */
List<SysDept> selectDeptByAncestors(String[] deptIds); List<SysDept> selectDeptByAncestors(String[] deptIds);
String getCompanyByAncestors(String split);
} }

View File

@ -121,4 +121,6 @@ public interface ISysDeptService
* @return 结果 * @return 结果
*/ */
public int deleteDeptById(Long deptId); public int deleteDeptById(Long deptId);
String getCompanyByAncestors(String ancestors);
} }

View File

@ -24,12 +24,11 @@ import java.util.stream.Collectors;
/** /**
* 部门管理 服务实现 * 部门管理 服务实现
* *
* @author ruoyi * @author ruoyi
*/ */
@Service @Service
public class SysDeptServiceImpl implements ISysDeptService public class SysDeptServiceImpl implements ISysDeptService {
{
@Autowired @Autowired
private SysDeptMapper deptMapper; private SysDeptMapper deptMapper;
@ -38,52 +37,46 @@ public class SysDeptServiceImpl implements ISysDeptService
/** /**
* 查询部门管理数据 * 查询部门管理数据
* *
* @param dept 部门信息 * @param dept 部门信息
* @return 部门信息集合 * @return 部门信息集合
*/ */
@Override @Override
@DataScope(deptAlias = "d") @DataScope(deptAlias = "d")
public List<SysDept> selectDeptList(SysDept dept) public List<SysDept> selectDeptList(SysDept dept) {
{
return deptMapper.selectDeptList(dept); return deptMapper.selectDeptList(dept);
} }
/** /**
* 查询部门树结构信息 * 查询部门树结构信息
* *
* @param dept 部门信息 * @param dept 部门信息
* @return 部门树信息集合 * @return 部门树信息集合
*/ */
@Override @Override
public List<TreeSelect> selectDeptTreeList(SysDept dept) public List<TreeSelect> selectDeptTreeList(SysDept dept) {
{
List<SysDept> depts = SpringUtils.getAopProxy(this).selectDeptList(dept); List<SysDept> depts = SpringUtils.getAopProxy(this).selectDeptList(dept);
return buildDeptTreeSelect(depts); return buildDeptTreeSelect(depts);
} }
/** /**
* 构建前端所需要树结构 * 构建前端所需要树结构
* *
* @param depts 部门列表 * @param depts 部门列表
* @return 树结构列表 * @return 树结构列表
*/ */
@Override @Override
public List<SysDept> buildDeptTree(List<SysDept> depts) public List<SysDept> buildDeptTree(List<SysDept> depts) {
{
List<SysDept> returnList = new ArrayList<SysDept>(); List<SysDept> returnList = new ArrayList<SysDept>();
List<Long> tempList = depts.stream().map(SysDept::getDeptId).collect(Collectors.toList()); List<Long> tempList = depts.stream().map(SysDept::getDeptId).collect(Collectors.toList());
for (SysDept dept : depts) for (SysDept dept : depts) {
{
// 如果是顶级节点, 遍历该父节点的所有子节点 // 如果是顶级节点, 遍历该父节点的所有子节点
if (!tempList.contains(dept.getParentId())) if (!tempList.contains(dept.getParentId())) {
{
recursionFn(depts, dept); recursionFn(depts, dept);
returnList.add(dept); returnList.add(dept);
} }
} }
if (returnList.isEmpty()) if (returnList.isEmpty()) {
{
returnList = depts; returnList = depts;
} }
return returnList; return returnList;
@ -91,93 +84,85 @@ public class SysDeptServiceImpl implements ISysDeptService
/** /**
* 构建前端所需要下拉树结构 * 构建前端所需要下拉树结构
* *
* @param depts 部门列表 * @param depts 部门列表
* @return 下拉树结构列表 * @return 下拉树结构列表
*/ */
@Override @Override
public List<TreeSelect> buildDeptTreeSelect(List<SysDept> depts) public List<TreeSelect> buildDeptTreeSelect(List<SysDept> depts) {
{
List<SysDept> deptTrees = buildDeptTree(depts); List<SysDept> deptTrees = buildDeptTree(depts);
return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList()); return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
} }
/** /**
* 根据角色ID查询部门树信息 * 根据角色ID查询部门树信息
* *
* @param roleId 角色ID * @param roleId 角色ID
* @return 选中部门列表 * @return 选中部门列表
*/ */
@Override @Override
public List<Long> selectDeptListByRoleId(Long roleId) public List<Long> selectDeptListByRoleId(Long roleId) {
{
SysRole role = roleMapper.selectRoleById(roleId); SysRole role = roleMapper.selectRoleById(roleId);
return deptMapper.selectDeptListByRoleId(roleId, role.isDeptCheckStrictly()); return deptMapper.selectDeptListByRoleId(roleId, role.isDeptCheckStrictly());
} }
/** /**
* 根据部门ID查询信息 * 根据部门ID查询信息
* *
* @param deptId 部门ID * @param deptId 部门ID
* @return 部门信息 * @return 部门信息
*/ */
@Override @Override
public SysDept selectDeptById(Long deptId) public SysDept selectDeptById(Long deptId) {
{
return deptMapper.selectDeptById(deptId); return deptMapper.selectDeptById(deptId);
} }
/** /**
* 根据ID查询所有子部门正常状态 * 根据ID查询所有子部门正常状态
* *
* @param deptId 部门ID * @param deptId 部门ID
* @return 子部门数 * @return 子部门数
*/ */
@Override @Override
public int selectNormalChildrenDeptById(Long deptId) public int selectNormalChildrenDeptById(Long deptId) {
{
return deptMapper.selectNormalChildrenDeptById(deptId); return deptMapper.selectNormalChildrenDeptById(deptId);
} }
/** /**
* 是否存在子节点 * 是否存在子节点
* *
* @param deptId 部门ID * @param deptId 部门ID
* @return 结果 * @return 结果
*/ */
@Override @Override
public boolean hasChildByDeptId(Long deptId) public boolean hasChildByDeptId(Long deptId) {
{
int result = deptMapper.hasChildByDeptId(deptId); int result = deptMapper.hasChildByDeptId(deptId);
return result > 0; return result > 0;
} }
/** /**
* 查询部门是否存在用户 * 查询部门是否存在用户
* *
* @param deptId 部门ID * @param deptId 部门ID
* @return 结果 true 存在 false 不存在 * @return 结果 true 存在 false 不存在
*/ */
@Override @Override
public boolean checkDeptExistUser(Long deptId) public boolean checkDeptExistUser(Long deptId) {
{
int result = deptMapper.checkDeptExistUser(deptId); int result = deptMapper.checkDeptExistUser(deptId);
return result > 0; return result > 0;
} }
/** /**
* 校验部门名称是否唯一 * 校验部门名称是否唯一
* *
* @param dept 部门信息 * @param dept 部门信息
* @return 结果 * @return 结果
*/ */
@Override @Override
public boolean checkDeptNameUnique(SysDept dept) public boolean checkDeptNameUnique(SysDept dept) {
{
Long deptId = StringUtils.isNull(dept.getDeptId()) ? -1L : dept.getDeptId(); Long deptId = StringUtils.isNull(dept.getDeptId()) ? -1L : dept.getDeptId();
SysDept info = deptMapper.checkDeptNameUnique(dept.getDeptName(), dept.getParentId()); SysDept info = deptMapper.checkDeptNameUnique(dept.getDeptName(), dept.getParentId());
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;
} }
return UserConstants.UNIQUE; return UserConstants.UNIQUE;
@ -185,19 +170,16 @@ public class SysDeptServiceImpl implements ISysDeptService
/** /**
* 校验部门是否有数据权限 * 校验部门是否有数据权限
* *
* @param deptId 部门id * @param deptId 部门id
*/ */
@Override @Override
public void checkDeptDataScope(Long deptId) public void checkDeptDataScope(Long deptId) {
{ if (!SysUser.isAdmin(SecurityUtils.getLoginUser().getUserid())) {
if (!SysUser.isAdmin(SecurityUtils.getLoginUser().getUserid()))
{
SysDept dept = new SysDept(); SysDept dept = new SysDept();
dept.setDeptId(deptId); dept.setDeptId(deptId);
List<SysDept> depts = SpringUtils.getAopProxy(this).selectDeptList(dept); List<SysDept> depts = SpringUtils.getAopProxy(this).selectDeptList(dept);
if (StringUtils.isEmpty(depts)) if (StringUtils.isEmpty(depts)) {
{
throw new ServiceException("没有权限访问部门数据!"); throw new ServiceException("没有权限访问部门数据!");
} }
} }
@ -205,17 +187,15 @@ public class SysDeptServiceImpl implements ISysDeptService
/** /**
* 新增保存部门信息 * 新增保存部门信息
* *
* @param dept 部门信息 * @param dept 部门信息
* @return 结果 * @return 结果
*/ */
@Override @Override
public int insertDept(SysDept dept) public int insertDept(SysDept dept) {
{
SysDept info = deptMapper.selectDeptById(dept.getParentId()); SysDept info = deptMapper.selectDeptById(dept.getParentId());
// 如果父节点不为正常状态,则不允许新增子节点 // 如果父节点不为正常状态,则不允许新增子节点
if (!UserConstants.DEPT_NORMAL.equals(info.getStatus())) if (!UserConstants.DEPT_NORMAL.equals(info.getStatus())) {
{
throw new ServiceException("部门停用,不允许新增"); throw new ServiceException("部门停用,不允许新增");
} }
dept.setAncestors(info.getAncestors() + "," + dept.getParentId()); dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
@ -224,17 +204,15 @@ public class SysDeptServiceImpl implements ISysDeptService
/** /**
* 修改保存部门信息 * 修改保存部门信息
* *
* @param dept 部门信息 * @param dept 部门信息
* @return 结果 * @return 结果
*/ */
@Override @Override
public int updateDept(SysDept dept) public int updateDept(SysDept dept) {
{
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.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();
dept.setAncestors(newAncestors); dept.setAncestors(newAncestors);
@ -242,8 +220,7 @@ public class SysDeptServiceImpl implements ISysDeptService
} }
int result = deptMapper.updateDept(dept); int result = deptMapper.updateDept(dept);
if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()) && StringUtils.isNotEmpty(dept.getAncestors()) if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()) && StringUtils.isNotEmpty(dept.getAncestors())
&& !StringUtils.equals("0", dept.getAncestors())) && !StringUtils.equals("0", dept.getAncestors())) {
{
// 如果该部门是启用状态则启用该部门的所有上级部门 // 如果该部门是启用状态则启用该部门的所有上级部门
updateParentDeptStatusNormal(dept); updateParentDeptStatusNormal(dept);
} }
@ -252,11 +229,10 @@ public class SysDeptServiceImpl implements ISysDeptService
/** /**
* 修改该部门的父级部门状态 * 修改该部门的父级部门状态
* *
* @param dept 当前部门 * @param dept 当前部门
*/ */
private void updateParentDeptStatusNormal(SysDept dept) private void updateParentDeptStatusNormal(SysDept dept) {
{
String ancestors = dept.getAncestors(); String ancestors = dept.getAncestors();
Long[] deptIds = Convert.toLongArray(ancestors); Long[] deptIds = Convert.toLongArray(ancestors);
deptMapper.updateDeptStatusNormal(deptIds); deptMapper.updateDeptStatusNormal(deptIds);
@ -264,48 +240,52 @@ public class SysDeptServiceImpl implements ISysDeptService
/** /**
* 修改子元素关系 * 修改子元素关系
* *
* @param deptId 被修改的部门ID * @param deptId 被修改的部门ID
* @param newAncestors 新的父ID集合 * @param newAncestors 新的父ID集合
* @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 = deptMapper.selectChildrenDeptById(deptId);
for (SysDept child : children) for (SysDept child : children) {
{
child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors)); child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
} }
if (children.size() > 0) if (children.size() > 0) {
{
deptMapper.updateDeptChildren(children); deptMapper.updateDeptChildren(children);
} }
} }
/** /**
* 删除部门管理信息 * 删除部门管理信息
* *
* @param deptId 部门ID * @param deptId 部门ID
* @return 结果 * @return 结果
*/ */
@Override @Override
public int deleteDeptById(Long deptId) public int deleteDeptById(Long deptId) {
{
return deptMapper.deleteDeptById(deptId); return deptMapper.deleteDeptById(deptId);
} }
@Override
public String getCompanyByAncestors(String ancestors) {
if (StringUtils.isNotBlank(ancestors)) {
String[] split = ancestors.split(",");
return deptMapper.getCompanyByAncestors(split[0]);
} else {
return "获取所属公司失败";
}
}
/** /**
* 递归列表 * 递归列表
*/ */
private void recursionFn(List<SysDept> list, SysDept t) private void recursionFn(List<SysDept> list, SysDept t) {
{
// 得到子节点列表 // 得到子节点列表
List<SysDept> childList = getChildList(list, t); List<SysDept> childList = getChildList(list, t);
t.setChildren(childList); t.setChildren(childList);
for (SysDept tChild : childList) for (SysDept tChild : childList) {
{ if (hasChild(list, tChild)) {
if (hasChild(list, tChild))
{
recursionFn(list, tChild); recursionFn(list, tChild);
} }
} }
@ -314,15 +294,12 @@ public class SysDeptServiceImpl implements ISysDeptService
/** /**
* 得到子节点列表 * 得到子节点列表
*/ */
private List<SysDept> getChildList(List<SysDept> list, SysDept t) private List<SysDept> getChildList(List<SysDept> list, SysDept t) {
{
List<SysDept> tlist = new ArrayList<SysDept>(); List<SysDept> tlist = new ArrayList<SysDept>();
Iterator<SysDept> it = list.iterator(); Iterator<SysDept> it = list.iterator();
while (it.hasNext()) while (it.hasNext()) {
{
SysDept n = (SysDept) it.next(); SysDept n = (SysDept) it.next();
if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getDeptId().longValue()) if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getDeptId().longValue()) {
{
tlist.add(n); tlist.add(n);
} }
} }
@ -332,8 +309,7 @@ public class SysDeptServiceImpl implements ISysDeptService
/** /**
* 判断是否有子节点 * 判断是否有子节点
*/ */
private boolean hasChild(List<SysDept> list, SysDept t) private boolean hasChild(List<SysDept> list, SysDept t) {
{
return getChildList(list, t).size() > 0 ? true : false; return getChildList(list, t).size() > 0 ? true : false;
} }
} }

View File

@ -162,4 +162,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
#{deptId} #{deptId}
</foreach> </foreach>
</select> </select>
<select id="getCompanyByAncestors" resultType="java.lang.String">
select dept_name from sys_dept where parent_id = #{split}
</select>
</mapper> </mapper>