package com.zzg.web.controller.system; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.zzg.common.annotation.Log; import com.zzg.common.constant.UserConstants; import com.zzg.common.core.controller.BaseController; import com.zzg.common.core.domain.AjaxResult; import com.zzg.common.core.domain.entity.system.SysDept; import com.zzg.common.enums.BusinessType; import com.zzg.common.utils.StringUtils; import com.zzg.system.service.system.ISysDeptService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang3.ArrayUtils; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.util.List; /** * 部门信息 * * @author ruoyi */ @RestController @RequestMapping("/system/dept") @Api(tags = "部门管理") public class SysDeptController extends BaseController { @Resource private ISysDeptService deptService; @PostMapping("/listByCity") @ApiOperation(value = "通过行政区划查询对应的部门") public AjaxResult listByCity(@RequestParam("cityIds") List cityIds) { List deptList = deptService.list(new LambdaQueryWrapper().in(SysDept::getCityId, cityIds).eq(SysDept::getDelFlag, "0").eq(SysDept::getStatus, "0")); return success(deptList); } /** * 获取部门列表 */ @PreAuthorize("@ss.hasPermi('system:dept:list')") @GetMapping("/list") @ApiOperation(value = "获取部门列表") public AjaxResult list(SysDept dept) { List depts = deptService.selectDeptList(dept); return success(depts); } /** * 查询部门列表(排除节点) */ @PreAuthorize("@ss.hasPermi('system:dept:list')") @GetMapping("/list/exclude/{deptId}") @ApiOperation(value = "查询部门列表(排除节点)") public AjaxResult excludeChild(@PathVariable(value = "deptId", required = false) String deptId) { List depts = deptService.selectDeptList(new SysDept()); depts.removeIf(d -> d.getDeptId().equals(deptId) || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), deptId + "")); return success(depts); } /** * 根据部门编号获取详细信息 */ @PreAuthorize("@ss.hasPermi('system:dept:query')") @GetMapping(value = "/{deptId}") @ApiOperation(value = "根据部门编号获取详细信息") public AjaxResult getInfo(@PathVariable String deptId) { deptService.checkDeptDataScope(deptId); return success(deptService.selectDeptById(deptId)); } /** * 新增部门 */ @PreAuthorize("@ss.hasPermi('system:dept:add')") @Log(title = "部门管理", businessType = BusinessType.INSERT) @PostMapping @ApiOperation(value = "新增部门") public AjaxResult add(@Validated @RequestBody SysDept dept) { if (!deptService.checkDeptNameUnique(dept)) { return error("新增部门'" + dept.getDeptName() + "'失败,部门名称已存在"); } dept.setCreateBy(getUsername()); return toAjax(deptService.insertDept(dept)); } /** * 修改部门 */ @PreAuthorize("@ss.hasPermi('system:dept:edit')") @Log(title = "部门管理", businessType = BusinessType.UPDATE) @PutMapping @ApiOperation(value = "修改部门") public AjaxResult edit(@Validated @RequestBody SysDept dept) { String deptId = dept.getDeptId(); deptService.checkDeptDataScope(deptId); if (!deptService.checkDeptNameUnique(dept)) { return error("修改部门'" + dept.getDeptName() + "'失败,部门名称已存在"); } else if (dept.getParentId().equals(deptId)) { return error("修改部门'" + dept.getDeptName() + "'失败,上级部门不能是自己"); } else if (UserConstants.DEPT_DISABLE.equals(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(value = "删除部门") public AjaxResult remove(@PathVariable String deptId) { if (deptService.hasChildByDeptId(deptId)) { return warn("存在下级部门,不允许删除"); } if (deptService.checkDeptExistUser(deptId)) { return warn("部门存在用户,不允许删除"); } deptService.checkDeptDataScope(deptId); return toAjax(deptService.deleteDeptById(deptId)); } /** * 获取部门树列表 */ @PreAuthorize("@ss.hasPermi('system:user:list')") @GetMapping("/deptTree") @ApiOperation(value = "获取部门树列表") public AjaxResult deptTree(SysDept dept) { return success(deptService.selectDeptTreeList(dept)); } @PreAuthorize("@ss.hasPermi('system:user:deptTreeNew')") @GetMapping("/deptTreeNew") @ApiOperation(value = "获取部门树列表(新)") public AjaxResult deptTreeNew() { return success(deptService.deptTreeNew()); } }