| | |
| | | package com.xinquan.system.controller; |
| | | |
| | | import java.util.List; |
| | | |
| | | import com.xinquan.system.service.ISysDeptService; |
| | | import org.apache.commons.lang3.ArrayUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | 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.xinquan.common.core.constant.UserConstants; |
| | | import com.xinquan.common.core.utils.StringUtils; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.xinquan.common.core.domain.R; |
| | | import com.xinquan.common.core.utils.page.PageDTO; |
| | | import com.xinquan.common.core.web.controller.BaseController; |
| | | import com.xinquan.common.core.web.domain.AjaxResult; |
| | | import com.xinquan.common.core.web.page.PageInfo; |
| | | import com.xinquan.common.log.annotation.Log; |
| | | import com.xinquan.common.log.enums.BusinessType; |
| | | import com.xinquan.common.security.annotation.RequiresPermissions; |
| | | import com.xinquan.common.security.service.TokenService; |
| | | import com.xinquan.common.security.utils.SecurityUtils; |
| | | import com.xinquan.system.api.domain.SysDept; |
| | | import com.xinquan.system.api.domain.SysRole; |
| | | import com.xinquan.system.api.domain.SysUser; |
| | | import com.xinquan.system.domain.SysUserDept; |
| | | import com.xinquan.system.domain.dto.SysDeptUpdateStatusDTO; |
| | | import com.xinquan.system.domain.dto.SysUserUpdateStatusDTO; |
| | | import com.xinquan.system.domain.dto.UnfreeDto; |
| | | import com.xinquan.system.service.ISysDeptService; |
| | | import com.xinquan.system.service.ISysUserDeptService; |
| | | import com.xinquan.system.service.ISysUserService; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.validation.annotation.Validated; |
| | | import org.springframework.web.bind.annotation.*; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.LocalDateTime; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * 部门信息 |
| | | * |
| | | * |
| | | * @author ruoyi |
| | | */ |
| | | @RestController |
| | | @RequestMapping("/dept") |
| | | @Api(tags = "部门管理") |
| | | public class SysDeptController extends BaseController |
| | | { |
| | | @Autowired |
| | | private ISysDeptService deptService; |
| | | @Autowired |
| | | private ISysUserDeptService userDeptService; |
| | | |
| | | /** |
| | | * 获取部门列表 |
| | | */ |
| | | @RequiresPermissions("system:dept:list") |
| | | @GetMapping("/list") |
| | | public AjaxResult list(SysDept dept) |
| | | { |
| | | List<SysDept> depts = deptService.selectDeptList(dept); |
| | | return success(depts); |
| | | } |
| | | |
| | | /** |
| | | * 查询部门列表(排除节点) |
| | | */ |
| | | @RequiresPermissions("system:dept:list") |
| | | @GetMapping("/list/exclude/{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); |
| | | } |
| | | |
| | | /** |
| | | * 根据部门编号获取详细信息 |
| | | */ |
| | | @RequiresPermissions("system:dept:query") |
| | | @GetMapping(value = "/{deptId}") |
| | | public AjaxResult getInfo(@PathVariable Long deptId) |
| | | { |
| | | deptService.checkDeptDataScope(deptId); |
| | | return success(deptService.selectDeptById(deptId)); |
| | | @ApiOperation("部门列表") |
| | | public R<PageDTO<SysDept>> list(String deptName, int pageCurr, int pageSize) { |
| | | Page<SysDept> res = new Page<>(pageCurr, pageSize); |
| | | PageInfo<SysDept> pageInfo = new PageInfo<>(pageCurr, pageSize); |
| | | List<SysDept> page = deptService.getList(pageInfo, deptName); |
| | | for (SysDept sysDept : page) { |
| | | int size = userDeptService.lambdaQuery().eq(SysUserDept::getDeptId, sysDept.getDeptId()).list().size(); |
| | | sysDept.setUserCount(size); |
| | | } |
| | | pageInfo.setRecords(page); |
| | | res.setRecords(page); |
| | | PageDTO<SysDept> sysUserVOPageDTO = PageDTO.of(res, SysDept.class); |
| | | return R.ok(sysUserVOPageDTO); |
| | | } |
| | | |
| | | /** |
| | | * 新增部门 |
| | | */ |
| | | @RequiresPermissions("system:dept:add") |
| | | @Log(title = "部门管理", businessType = BusinessType.INSERT) |
| | | @PostMapping |
| | | public AjaxResult add(@Validated @RequestBody SysDept dept) |
| | | @PostMapping("/add") |
| | | @ApiOperation("部门添加") |
| | | public AjaxResult add(@Validated @RequestBody SysDept dto) |
| | | { |
| | | if (!deptService.checkDeptNameUnique(dept)) |
| | | { |
| | | return error("新增部门'" + dept.getDeptName() + "'失败,部门名称已存在"); |
| | | dto.setCreateTime(LocalDateTime.now()); |
| | | if (!deptService.checkDeptNameUnique(dto)){ |
| | | return error("新增部门失败,部门ID已存在"); |
| | | } |
| | | dept.setCreateBy(SecurityUtils.getUsername()); |
| | | return toAjax(deptService.insertDept(dept)); |
| | | dto.setCreateBy(SecurityUtils.getUsername()); |
| | | return toAjax(deptService.insertDept(dto)); |
| | | } |
| | | |
| | | @Resource |
| | | private TokenService tokenService; |
| | | @Resource |
| | | private ISysUserService service; |
| | | /** |
| | | * 修改部门 |
| | | */ |
| | | @RequiresPermissions("system:dept:edit") |
| | | @Log(title = "部门管理", businessType = BusinessType.UPDATE) |
| | | @PutMapping |
| | | public AjaxResult edit(@Validated @RequestBody SysDept dept) |
| | | @PostMapping("/edit") |
| | | @ApiOperation("部门修改") |
| | | public R edit(@Validated @RequestBody SysDept dept) |
| | | { |
| | | Long deptId = dept.getDeptId(); |
| | | deptService.checkDeptDataScope(deptId); |
| | | if (!deptService.checkDeptNameUnique(dept)) |
| | | { |
| | | return error("修改部门'" + dept.getDeptName() + "'失败,部门名称已存在"); |
| | | if (!deptService.checkDeptNameUnique1(dept)){ |
| | | return R.fail("修改部门失败,部门ID已存在"); |
| | | } |
| | | 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(SecurityUtils.getUsername()); |
| | | return toAjax(deptService.updateDept(dept)); |
| | | Long userid = tokenService.getLoginUser().getUserid(); |
| | | SysUser byId = service.getById(userid); |
| | | dept.setUpdateBy(byId.getNickName()+"("+byId.getUserName()+")"); |
| | | dept.setHandleName(byId.getNickName()+"("+byId.getUserName()+")"); |
| | | return R.ok(deptService.updateDept(dept)); |
| | | } |
| | | |
| | | @Autowired |
| | | private ISysUserService userService; |
| | | @ApiOperation(value = "禁用启用") |
| | | @PutMapping("/changeStatus") |
| | | public AjaxResult changeStatus(@RequestBody SysDeptUpdateStatusDTO dto) |
| | | { |
| | | SysDept user = new SysDept(); |
| | | user.setDeptId(dto.getDeptId()); |
| | | user.setStatus(String.valueOf(dto.getStatus())); |
| | | user.setReason(dto.getReason()); |
| | | Long userid = tokenService.getLoginUser().getUserid(); |
| | | SysUser sysUser = userService.selectUserById(userid); |
| | | user.setUpdateBy(sysUser.getNickName()+"("+sysUser.getUserName()+")"); |
| | | user.setHandleName(sysUser.getNickName()+"("+sysUser.getUserName()+")"); |
| | | return AjaxResult.success(userService.updateDeptStatus(user)); |
| | | } |
| | | /** |
| | | * 删除部门 |
| | | */ |
| | | @RequiresPermissions("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)); |
| | | } |
| | | |
| | | |
| | | } |