package api.controller.system; import api.entity.database.system.AddressBook; import api.entity.database.system.Menu; import api.service.system.IAddressBookService; import api.util.annotation.Anonymous; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import api.controller.BaseController; import api.entity.database.system.Dept; import api.entity.input.PageInput; import api.mapper.system.DeptMapper; import api.model.AjaxResult; import api.service.system.IDeptService; import api.util.annotation.Log; import api.util.enums.BusinessType; import api.util.helper.RedisHelper; import api.util.helper.StringHelper; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.math3.analysis.function.Add; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.*; import org.springframework.web.method.HandlerMethod; import java.lang.reflect.Method; import java.util.Date; import java.util.List; import java.util.Objects; import java.util.stream.Collectors; @Api(value = "部门表", tags = "部门表") @RestController @RequestMapping("/system/dept") public class DeptController extends BaseController { @Autowired private IDeptService deptService; @Autowired private DeptMapper deptMapper; @Autowired private RedisHelper redisHelper; @Autowired private IAddressBookService addressBookService; @ApiOperation("列表") @Log(title = "查看部门列表", businessType = BusinessType.QUERY) @GetMapping public AjaxResult getList(Dept input, PageInput pageInput) { LambdaQueryWrapper qw = new LambdaQueryWrapper(); qw.eq(input.getDeptId() != null && input.getDeptId() > 0, Dept::getDeptId, input.getDeptId()); qw.eq(input.getParentId() != null && input.getParentId() > 0, Dept::getParentId, input.getParentId()); qw.like(!StringHelper.isEmpty(input.getAncestors()), Dept::getAncestors, input.getAncestors()); qw.like(!StringHelper.isEmpty(input.getDeptName()), Dept::getDeptName, input.getDeptName()); qw.eq(input.getOrderNum() != null && input.getOrderNum() > 0, Dept::getOrderNum, input.getOrderNum()); qw.like(!StringHelper.isEmpty(input.getLeader()), Dept::getLeader, input.getLeader()); qw.like(!StringHelper.isEmpty(input.getPhone()), Dept::getPhone, input.getPhone()); qw.like(!StringHelper.isEmpty(input.getEmail()), Dept::getEmail, input.getEmail()); qw.like(!StringHelper.isEmpty(input.getState()), Dept::getState, input.getState()); qw.eq(Dept::getDelFlag, "0"); qw.like(!StringHelper.isEmpty(input.getCreateBy()), Dept::getCreateBy, input.getCreateBy()); qw.eq(input.getCreateTime() != null, Dept::getCreateTime, input.getCreateTime()); qw.like(!StringHelper.isEmpty(input.getUpdateBy()), Dept::getUpdateBy, input.getUpdateBy()); qw.eq(input.getUpdateTime() != null, Dept::getUpdateTime, input.getUpdateTime()); Page page = GetPage(pageInput); if (page != null) { IPage iPage = deptService.getListPage(page, qw); return Success("成功", iPage.getRecords(), iPage.getTotal()); } else { return Success("成功", deptService.getList(qw)); } } @ApiOperation("新 二级单位交办时,获取子部门列表") @Log(title = "二级单位交办时,获取子部门列表", businessType = BusinessType.QUERY) @GetMapping public AjaxResult getSonList() { LambdaQueryWrapper qw = new LambdaQueryWrapper(); qw.eq(Dept::getState,0L) .eq(Dept::getIsDept,2L).eq(Dept::getParentId,CurrentUser().getDeptId()); return Success("成功", deptService.getList(qw)); } @ApiOperation("新 话务员交办时,获取要交办的部门tree") @GetMapping("/deptZBTree") public AjaxResult deptZBTree() { return Success("查询成功", deptService.selectZBDeptTreeList()); } /** * 查询部门列表(排除节点) */ @Log(title = "查询部门列表(排除节点)", businessType = BusinessType.QUERY) @GetMapping("list/exclude/{deptId}") public AjaxResult excludeChild(@PathVariable(value = "deptId", required = false) Long deptId) { LambdaQueryWrapper qw = new LambdaQueryWrapper<>(); qw.eq(Dept::getDelFlag, "0"); List depts = deptService.getList(qw); depts.removeIf(d -> d.getDeptId().intValue() == deptId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), deptId + "")); return Success("查询成功", depts); } /** * 查看部门详情 * * @param id * @return */ @ApiOperation("详情") @Log(title = "查看部门详情", businessType = BusinessType.QUERY) @GetMapping("/{id}") public AjaxResult getInfo(@PathVariable Long id) { if (id == null) { return Error("部门编号为空"); } LambdaQueryWrapper qw = new LambdaQueryWrapper<>(); qw.eq(Dept::getDeptId, id).eq(Dept::getDelFlag, "0"); Dept entity = deptService.getEntity(qw); if (entity == null) { return Error("部门不存在!"); } return Success("成功", entity); } /** * 新增部门 * * @param input * @return */ @ApiOperation("新增") @Log(title = "新增部门", businessType = BusinessType.INSERT) @PostMapping public AjaxResult add(@RequestBody Dept input) { if (!deptService.checkDeptNameUnique(input)) { return Error("新增部门'" + input.getDeptName() + "'失败,部门名称已存在"); } input.setCreateBy(CurrentUser().getUserName()); input.setCreateTime(new Date()); Dept parentdept= deptService.getEntity(input.getParentId()); if(input.getParentId()==0){ input.setAncestorsName(input.getDeptName()); } else{ input.setAncestorsName(parentdept.getAncestorsName()+"/"+input.getDeptName()); } boolean result = deptService.insert(input); if (result) { input.setAncestors(parentdept.getAncestors()+","+input.getDeptId()); deptService.update(input); if (redisHelper.getCache("deptTree")!=null){ redisHelper.deleteCache("deptTree"); } return Success("成功"); } else { return Error("新增失败"); } } /** * 修改部门信息 * * @param input * @return */ @ApiOperation("编辑") @Log(title = "修改部门信息", businessType = BusinessType.UPDATE) @PutMapping public AjaxResult edit(@RequestBody Dept input) { if (!deptService.checkDeptNameUnique(input)) { return Error("修改部门'" + input.getDeptName() + "'失败,部门名称已存在"); } else if (input.getParentId().equals(input.getDeptId())) { return Error("修改部门'" + input.getDeptName() + "'失败,上级部门不能是自己"); } else if ("1".equals(input.getState()) && deptService.selectNormalChildrenDeptById(input.getDeptId()) > 0) { return Error("该部门包含未停用的子部门!"); } input.setUpdateBy(CurrentUser().getUserName()); LambdaQueryWrapper qw = new LambdaQueryWrapper<>(); qw.eq(Dept::getDelFlag, "0").eq(Dept::getDeptId, input.getDeptId()); Dept olddept=deptService.getEntity(qw); if (olddept == null) { return Error("部门不存在!"); } input.setUpdateTime(new Date()); if (redisHelper.getCache("deptTree")!=null){ redisHelper.deleteCache("deptTree"); } Dept parentdept= deptService.getEntity(input.getParentId()); input.setAncestorsName(parentdept.getAncestorsName()+input.getDeptName()); input.setAncestors(parentdept.getAncestors()+","+input.getDeptId()); boolean result = deptService.update(input); if (result) { //根据name+电话,判断通讯录中是否存在, 存在修改第一条通讯录的数据 if(!Objects.equals(olddept.getPhone(), input.getPhone()) &&!StringHelper.isEmpty(input.getPhone()) ) { LambdaQueryWrapper lqwaddress=new LambdaQueryWrapper<>(); lqwaddress.eq(AddressBook::getName,olddept.getDeptName()).eq(AddressBook::getMobile,olddept.getPhone()); List addressBooks= addressBookService.getList(lqwaddress); if(addressBooks!=null &&addressBooks.stream().count()>0) { LambdaQueryWrapper lqwaone=new LambdaQueryWrapper<>(); lqwaddress.eq(AddressBook::getName,olddept.getDeptName()).eq(AddressBook::getMobile,olddept.getPhone()); AddressBook addressBook = addressBookService.getList(lqwaone).stream().findFirst().get(); addressBook.setMobile(input.getPhone()); addressBookService.update(addressBook); } } if (redisHelper.getCache("deptTree")!=null){ redisHelper.deleteCache("deptTree"); } return Success("成功"); } else { return Error("修改失败"); } } /** * 删除部门 * * @param ids * @return */ @ApiOperation("删除") @Log(title = "删除部门", businessType = BusinessType.DELETE) @DeleteMapping("/{ids}") public AjaxResult delete(@PathVariable Long[] ids) { for (long id : ids) { if (deptService.hasChildByDeptId(id)) { return Error("存在下级部门,不允许删除"); } if (deptService.checkDeptExistUser(id)) { return Error("部门存在用户,不允许删除"); } } LambdaUpdateWrapper qw = new LambdaUpdateWrapper<>(); qw.set(Dept::getDelFlag, "2").in(Dept::getDeptId, ids); if (redisHelper.getCache("deptTree")!=null){ redisHelper.deleteCache("deptTree"); } int update = deptMapper.update(null, qw); if (update > 0) { if (redisHelper.getCache("deptTree")!=null){ redisHelper.deleteCache("deptTree"); } return Success("成功"); } else { return Error("删除失败"); } } }