| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- 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<Dept> 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<Dept> page = GetPage(pageInput);
- if (page != null) {
- IPage<Dept> 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<Dept> 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<Dept> qw = new LambdaQueryWrapper<>();
- qw.eq(Dept::getDelFlag, "0");
- List<Dept> 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<Dept> 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<Dept> 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<AddressBook> lqwaddress=new LambdaQueryWrapper<>();
- lqwaddress.eq(AddressBook::getName,olddept.getDeptName()).eq(AddressBook::getMobile,olddept.getPhone());
- List<AddressBook> addressBooks= addressBookService.getList(lqwaddress);
- if(addressBooks!=null &&addressBooks.stream().count()>0)
- {
- LambdaQueryWrapper<AddressBook> 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<Dept> 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("删除失败");
- }
- }
- }
|