获取当前用户所属公司

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

View File

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

View File

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

View File

@ -28,8 +28,7 @@ import java.util.stream.Collectors;
* @author ruoyi
*/
@Service
public class SysDeptServiceImpl implements ISysDeptService
{
public class SysDeptServiceImpl implements ISysDeptService {
@Autowired
private SysDeptMapper deptMapper;
@ -44,8 +43,7 @@ public class SysDeptServiceImpl implements ISysDeptService
*/
@Override
@DataScope(deptAlias = "d")
public List<SysDept> selectDeptList(SysDept dept)
{
public List<SysDept> selectDeptList(SysDept dept) {
return deptMapper.selectDeptList(dept);
}
@ -56,8 +54,7 @@ public class SysDeptServiceImpl implements ISysDeptService
* @return 部门树信息集合
*/
@Override
public List<TreeSelect> selectDeptTreeList(SysDept dept)
{
public List<TreeSelect> selectDeptTreeList(SysDept dept) {
List<SysDept> depts = SpringUtils.getAopProxy(this).selectDeptList(dept);
return buildDeptTreeSelect(depts);
}
@ -69,21 +66,17 @@ public class SysDeptServiceImpl implements ISysDeptService
* @return 树结构列表
*/
@Override
public List<SysDept> buildDeptTree(List<SysDept> depts)
{
public List<SysDept> buildDeptTree(List<SysDept> depts) {
List<SysDept> returnList = new ArrayList<SysDept>();
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);
returnList.add(dept);
}
}
if (returnList.isEmpty())
{
if (returnList.isEmpty()) {
returnList = depts;
}
return returnList;
@ -96,8 +89,7 @@ public class SysDeptServiceImpl implements ISysDeptService
* @return 下拉树结构列表
*/
@Override
public List<TreeSelect> buildDeptTreeSelect(List<SysDept> depts)
{
public List<TreeSelect> buildDeptTreeSelect(List<SysDept> depts) {
List<SysDept> deptTrees = buildDeptTree(depts);
return deptTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
}
@ -109,8 +101,7 @@ public class SysDeptServiceImpl implements ISysDeptService
* @return 选中部门列表
*/
@Override
public List<Long> selectDeptListByRoleId(Long roleId)
{
public List<Long> selectDeptListByRoleId(Long roleId) {
SysRole role = roleMapper.selectRoleById(roleId);
return deptMapper.selectDeptListByRoleId(roleId, role.isDeptCheckStrictly());
}
@ -122,8 +113,7 @@ public class SysDeptServiceImpl implements ISysDeptService
* @return 部门信息
*/
@Override
public SysDept selectDeptById(Long deptId)
{
public SysDept selectDeptById(Long deptId) {
return deptMapper.selectDeptById(deptId);
}
@ -134,8 +124,7 @@ public class SysDeptServiceImpl implements ISysDeptService
* @return 子部门数
*/
@Override
public int selectNormalChildrenDeptById(Long deptId)
{
public int selectNormalChildrenDeptById(Long deptId) {
return deptMapper.selectNormalChildrenDeptById(deptId);
}
@ -146,8 +135,7 @@ public class SysDeptServiceImpl implements ISysDeptService
* @return 结果
*/
@Override
public boolean hasChildByDeptId(Long deptId)
{
public boolean hasChildByDeptId(Long deptId) {
int result = deptMapper.hasChildByDeptId(deptId);
return result > 0;
}
@ -159,8 +147,7 @@ public class SysDeptServiceImpl implements ISysDeptService
* @return 结果 true 存在 false 不存在
*/
@Override
public boolean checkDeptExistUser(Long deptId)
{
public boolean checkDeptExistUser(Long deptId) {
int result = deptMapper.checkDeptExistUser(deptId);
return result > 0;
}
@ -172,12 +159,10 @@ public class SysDeptServiceImpl implements ISysDeptService
* @return 结果
*/
@Override
public boolean checkDeptNameUnique(SysDept dept)
{
public boolean checkDeptNameUnique(SysDept dept) {
Long deptId = StringUtils.isNull(dept.getDeptId()) ? -1L : dept.getDeptId();
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.UNIQUE;
@ -189,15 +174,12 @@ public class SysDeptServiceImpl implements ISysDeptService
* @param deptId 部门id
*/
@Override
public void checkDeptDataScope(Long deptId)
{
if (!SysUser.isAdmin(SecurityUtils.getLoginUser().getUserid()))
{
public void checkDeptDataScope(Long deptId) {
if (!SysUser.isAdmin(SecurityUtils.getLoginUser().getUserid())) {
SysDept dept = new SysDept();
dept.setDeptId(deptId);
List<SysDept> depts = SpringUtils.getAopProxy(this).selectDeptList(dept);
if (StringUtils.isEmpty(depts))
{
if (StringUtils.isEmpty(depts)) {
throw new ServiceException("没有权限访问部门数据!");
}
}
@ -210,12 +192,10 @@ public class SysDeptServiceImpl implements ISysDeptService
* @return 结果
*/
@Override
public int insertDept(SysDept dept)
{
public int insertDept(SysDept dept) {
SysDept info = deptMapper.selectDeptById(dept.getParentId());
// 如果父节点不为正常状态,则不允许新增子节点
if (!UserConstants.DEPT_NORMAL.equals(info.getStatus()))
{
if (!UserConstants.DEPT_NORMAL.equals(info.getStatus())) {
throw new ServiceException("部门停用,不允许新增");
}
dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
@ -229,12 +209,10 @@ public class SysDeptServiceImpl implements ISysDeptService
* @return 结果
*/
@Override
public int updateDept(SysDept dept)
{
public int updateDept(SysDept dept) {
SysDept newParentDept = deptMapper.selectDeptById(dept.getParentId());
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 oldAncestors = oldDept.getAncestors();
dept.setAncestors(newAncestors);
@ -242,8 +220,7 @@ public class SysDeptServiceImpl implements ISysDeptService
}
int result = deptMapper.updateDept(dept);
if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()) && StringUtils.isNotEmpty(dept.getAncestors())
&& !StringUtils.equals("0", dept.getAncestors()))
{
&& !StringUtils.equals("0", dept.getAncestors())) {
// 如果该部门是启用状态则启用该部门的所有上级部门
updateParentDeptStatusNormal(dept);
}
@ -255,8 +232,7 @@ public class SysDeptServiceImpl implements ISysDeptService
*
* @param dept 当前部门
*/
private void updateParentDeptStatusNormal(SysDept dept)
{
private void updateParentDeptStatusNormal(SysDept dept) {
String ancestors = dept.getAncestors();
Long[] deptIds = Convert.toLongArray(ancestors);
deptMapper.updateDeptStatusNormal(deptIds);
@ -269,15 +245,12 @@ public class SysDeptServiceImpl implements ISysDeptService
* @param newAncestors 新的父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);
for (SysDept child : children)
{
for (SysDept child : children) {
child.setAncestors(child.getAncestors().replaceFirst(oldAncestors, newAncestors));
}
if (children.size() > 0)
{
if (children.size() > 0) {
deptMapper.updateDeptChildren(children);
}
}
@ -289,23 +262,30 @@ public class SysDeptServiceImpl implements ISysDeptService
* @return 结果
*/
@Override
public int deleteDeptById(Long deptId)
{
public int deleteDeptById(Long 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);
t.setChildren(childList);
for (SysDept tChild : childList)
{
if (hasChild(list, tChild))
{
for (SysDept tChild : childList) {
if (hasChild(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>();
Iterator<SysDept> it = list.iterator();
while (it.hasNext())
{
while (it.hasNext()) {
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);
}
}
@ -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;
}
}

View File

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