package com.ruoyi.web.controller.system;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.ArrayUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 部门信æ¯
*
* @author Lion Li
*/
@Validated
@Api(value = "部门控制器", tags = {"部门管ç†"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/dept")
public class SysDeptController extends BaseController {
private final ISysDeptService deptService;
/**
* 获å–部门列表
*/
@ApiOperation("获å–部门列表")
@PreAuthorize("@ss.hasPermi('system:dept:list')")
@GetMapping("/list")
public AjaxResult<List<SysDept>> list(SysDept dept) {
List<SysDept> depts = deptService.selectDeptList(dept);
return AjaxResult.success(depts);
}
/**
* 查询部门列表(排除节点)
*/
@ApiOperation("查询部门列表(排除节点)")
@PreAuthorize("@ss.hasPermi('system:dept:list')")
@GetMapping("/list/exclude/{deptId}")
public AjaxResult<List<SysDept>> excludeChild(@ApiParam("部门ID") @PathVariable(value = "deptId", required = false) Long deptId) {
List<SysDept> depts = deptService.selectDeptList(new SysDept());
depts.removeIf(d -> d.getDeptId().equals(deptId)
|| ArrayUtil.contains(StringUtils.split(d.getAncestors(), ","), deptId + ""));
return AjaxResult.success(depts);
}
/**
* æ ¹æ®éƒ¨é—¨ç¼–å·èŽ·å–详细信æ¯
*/
@ApiOperation("æ ¹æ®éƒ¨é—¨ç¼–å·èŽ·å–详细信æ¯")
@PreAuthorize("@ss.hasPermi('system:dept:query')")
@GetMapping(value = "/{deptId}")
public AjaxResult<SysDept> getInfo(@ApiParam("部门ID") @PathVariable Long deptId) {
deptService.checkDeptDataScope(deptId);
return AjaxResult.success(deptService.selectDeptById(deptId));
}
/**
* 获å–éƒ¨é—¨ä¸‹æ‹‰æ ‘åˆ—è¡¨
*/
@ApiOperation("获å–éƒ¨é—¨ä¸‹æ‹‰æ ‘åˆ—è¡¨")
@GetMapping("/treeselect")
public AjaxResult<List<Tree<Long>>> treeselect(SysDept dept) {
List<SysDept> depts = deptService.selectDeptList(dept);
return AjaxResult.success(deptService.buildDeptTreeSelect(depts));
}
/**
* åŠ è½½å¯¹åº”è§’è‰²éƒ¨é—¨åˆ—è¡¨æ ‘
*/
@ApiOperation("åŠ è½½å¯¹åº”è§’è‰²éƒ¨é—¨åˆ—è¡¨æ ‘")
@GetMapping(value = "/roleDeptTreeselect/{roleId}")
public AjaxResult<Map<String, Object>> roleDeptTreeselect(@ApiParam("角色ID") @PathVariable("roleId") Long roleId) {
List<SysDept> depts = deptService.selectDeptList(new SysDept());
Map<String, Object> ajax = new HashMap<>();
ajax.put("checkedKeys", deptService.selectDeptListByRoleId(roleId));
ajax.put("depts", deptService.buildDeptTreeSelect(depts));
return AjaxResult.success(ajax);
}
/**
* 新增部门
*/
@ApiOperation("新增部门")
@PreAuthorize("@ss.hasPermi('system:dept:add')")
@Log(title = "部门管ç†", businessType = BusinessType.INSERT)
@PostMapping
public AjaxResult<Void> add(@Validated @RequestBody SysDept dept) {
if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept))) {
return AjaxResult.error("新增部门'" + dept.getDeptName() + "'失败,部门åç§°å·²å˜åœ¨");
}
return toAjax(deptService.insertDept(dept));
}
/**
* 修改部门
*/
@ApiOperation("修改部门")
@PreAuthorize("@ss.hasPermi('system:dept:edit')")
@Log(title = "部门管ç†", businessType = BusinessType.UPDATE)
@PutMapping
public AjaxResult<Void> edit(@Validated @RequestBody SysDept dept) {
if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept))) {
return AjaxResult.error("修改部门'" + dept.getDeptName() + "'失败,部门åç§°å·²å˜åœ¨");
} else if (dept.getParentId().equals(dept.getDeptId())) {
return AjaxResult.error("修改部门'" + dept.getDeptName() + "'失败,上级部门ä¸èƒ½æ˜¯è‡ªå·±");
} else if (StringUtils.equals(UserConstants.DEPT_DISABLE, dept.getStatus())
&& deptService.selectNormalChildrenDeptById(dept.getDeptId()) > 0) {
return AjaxResult.error("è¯¥éƒ¨é—¨åŒ…å«æœªåœç”¨çš„å部门ï¼");
}
return toAjax(deptService.updateDept(dept));
}
/**
* åˆ é™¤éƒ¨é—¨
*/
@ApiOperation("åˆ é™¤éƒ¨é—¨")
@PreAuthorize("@ss.hasPermi('system:dept:remove')")
@Log(title = "部门管ç†", businessType = BusinessType.DELETE)
@DeleteMapping("/{deptId}")
public AjaxResult<Void> remove(@ApiParam("部门ID串") @PathVariable Long deptId) {
if (deptService.hasChildByDeptId(deptId)) {
return AjaxResult.error("å˜åœ¨ä¸‹çº§éƒ¨é—¨,ä¸å…è®¸åˆ é™¤");
}
if (deptService.checkDeptExistUser(deptId)) {
return AjaxResult.error("部门å˜åœ¨ç”¨æˆ·,ä¸å…è®¸åˆ é™¤");
}
return toAjax(deptService.deleteDeptById(deptId));
}
}