package com.ozs.web.controller.system; import java.util.ArrayList; import java.util.List; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang3.ArrayUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.util.ObjectUtils; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.ozs.common.annotation.Log; import com.ozs.common.constant.UserConstants; import com.ozs.common.core.controller.BaseController; import com.ozs.common.core.domain.AjaxResult; import com.ozs.common.core.domain.entity.SysDept; import com.ozs.common.enums.BusinessType; import com.ozs.common.utils.StringUtils; import com.ozs.system.service.ISysDeptService; /** * 部门信息 * * @author ruoyi */ @Api(tags = "部门信息") @RestController @RequestMapping("/system/dept") public class SysDeptController extends BaseController { @Autowired private ISysDeptService deptService; /** * 获取部门列表 */ @PreAuthorize("@ss.hasPermi('system:dept:list')") @GetMapping("/list") @ApiOperation("获取部门列表") public AjaxResult list(SysDept dept) { List depts = deptService.selectDeptList(dept); return success(depts); } /** * 获取部门列表(包含自己上一级) */ @GetMapping("/listAndUp") @ApiOperation("获取部门列表(包含自己上一级)") public AjaxResult listAndUp(SysDept dept) { List depts = deptService.selectDeptListAndUp(dept); return success(depts); } /** * 获取部门列表 */ @GetMapping("/listByUser") @ApiOperation("根据获取本级和子孙级部门列表") public AjaxResult listByUser() { Long deptId = getDeptId(); List list = new ArrayList<>(); SysDept s = deptService.selectById(deptId); if (!ObjectUtils.isEmpty(s)) { list.add(s); List sl = deptService.selectChildrenDeptById(deptId); if (!ObjectUtils.isEmpty(sl)) { list.addAll(sl); } } return success(list); } /** * 查询部门列表(排除节点) */ @PreAuthorize("@ss.hasPermi('system:dept:list')") @GetMapping("/list/exclude/{deptId}") @ApiOperation("查询部门列表(排除节点)") public AjaxResult excludeChild(@PathVariable(value = "deptId" , required = false) Long deptId) { List depts = deptService.selectDeptList(new SysDept()); depts.removeIf(d -> d.getDeptId().intValue() == deptId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), deptId + "")); return success(depts); } /** * 根据部门编号获取详细信息 */ @GetMapping(value = "/{deptId}") @ApiOperation("根据部门编号获取详细信息") public AjaxResult getInfo(@PathVariable Long deptId) { deptService.checkDeptDataScope(deptId); return success(deptService.selectDeptById(deptId)); } /** * 新增部门 */ @PreAuthorize("@ss.hasPermi('system:dept:add')") @Log(title = "部门管理" , businessType = BusinessType.INSERT) @PostMapping @ApiOperation("新增部门") public AjaxResult add(@Validated @RequestBody SysDept dept) { if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept))) { return error("新增部门'" + dept.getDeptName() + "'失败,部门名称已存在"); } dept.setCreateBy(getUsername()); return deptService.insertDept(dept); } /** * 修改部门 */ @PreAuthorize("@ss.hasPermi('system:dept:edit')") @Log(title = "部门管理" , businessType = BusinessType.UPDATE) @PutMapping @ApiOperation("修改部门") public AjaxResult edit(@Validated @RequestBody SysDept dept) { Long deptId = dept.getDeptId(); deptService.checkDeptDataScope(deptId); if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept))) { return error("修改部门'" + dept.getDeptName() + "'失败,部门名称已存在"); } else if (dept.getParentId().equals(deptId)) { return error("修改部门'" + dept.getDeptName() + "'失败,上级部门不能是自己"); } else if (StringUtils.equals(UserConstants.DEPT_DISABLE, dept.getStatus()) && deptService.selectNormalChildrenDeptById(deptId) > 0) { return error("该部门包含未停用的子部门!"); } dept.setUpdateBy(getUsername()); return toAjax(deptService.updateDept(dept)); } /** * 删除部门 */ @PreAuthorize("@ss.hasPermi('system:dept:remove')") @Log(title = "部门管理" , businessType = BusinessType.DELETE) @DeleteMapping("/{deptId}") @ApiOperation("删除部门") public AjaxResult remove(@PathVariable Long deptId) { if (deptService.hasChildByDeptId(deptId)) { return warn("存在下级部门,不允许删除"); } if (deptService.checkDeptExistUser(deptId)) { return warn("部门存在用户,不允许删除"); } deptService.checkDeptDataScope(deptId); return toAjax(deptService.deleteDeptById(deptId)); } }