鹤壁政务服务热线

DeptController.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. package api.controller.system;
  2. import api.entity.database.system.AddressBook;
  3. import api.entity.database.system.Menu;
  4. import api.service.system.IAddressBookService;
  5. import api.util.annotation.Anonymous;
  6. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  7. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  8. import com.baomidou.mybatisplus.core.metadata.IPage;
  9. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  10. import api.controller.BaseController;
  11. import api.entity.database.system.Dept;
  12. import api.entity.input.PageInput;
  13. import api.mapper.system.DeptMapper;
  14. import api.model.AjaxResult;
  15. import api.service.system.IDeptService;
  16. import api.util.annotation.Log;
  17. import api.util.enums.BusinessType;
  18. import api.util.helper.RedisHelper;
  19. import api.util.helper.StringHelper;
  20. import io.swagger.annotations.Api;
  21. import io.swagger.annotations.ApiOperation;
  22. import org.apache.commons.lang3.ArrayUtils;
  23. import org.apache.commons.math3.analysis.function.Add;
  24. import org.springframework.beans.factory.annotation.Autowired;
  25. import org.springframework.util.StringUtils;
  26. import org.springframework.web.bind.annotation.*;
  27. import org.springframework.web.method.HandlerMethod;
  28. import java.lang.reflect.Method;
  29. import java.util.Date;
  30. import java.util.List;
  31. import java.util.Objects;
  32. import java.util.stream.Collectors;
  33. @Api(value = "部门表", tags = "部门表")
  34. @RestController
  35. @RequestMapping("/system/dept")
  36. public class DeptController extends BaseController {
  37. @Autowired
  38. private IDeptService deptService;
  39. @Autowired
  40. private DeptMapper deptMapper;
  41. @Autowired
  42. private RedisHelper redisHelper;
  43. @Autowired
  44. private IAddressBookService addressBookService;
  45. @ApiOperation("列表")
  46. @Log(title = "查看部门列表", businessType = BusinessType.QUERY)
  47. @GetMapping
  48. public AjaxResult getList(Dept input, PageInput pageInput) {
  49. LambdaQueryWrapper<Dept> qw = new LambdaQueryWrapper();
  50. qw.eq(input.getDeptId() != null && input.getDeptId() > 0, Dept::getDeptId, input.getDeptId());
  51. qw.eq(input.getParentId() != null && input.getParentId() > 0, Dept::getParentId, input.getParentId());
  52. qw.like(!StringHelper.isEmpty(input.getAncestors()), Dept::getAncestors, input.getAncestors());
  53. qw.like(!StringHelper.isEmpty(input.getDeptName()), Dept::getDeptName, input.getDeptName());
  54. qw.eq(input.getOrderNum() != null && input.getOrderNum() > 0, Dept::getOrderNum, input.getOrderNum());
  55. qw.like(!StringHelper.isEmpty(input.getLeader()), Dept::getLeader, input.getLeader());
  56. qw.like(!StringHelper.isEmpty(input.getPhone()), Dept::getPhone, input.getPhone());
  57. qw.like(!StringHelper.isEmpty(input.getEmail()), Dept::getEmail, input.getEmail());
  58. qw.like(!StringHelper.isEmpty(input.getState()), Dept::getState, input.getState());
  59. qw.eq(Dept::getDelFlag, "0");
  60. qw.like(!StringHelper.isEmpty(input.getCreateBy()), Dept::getCreateBy, input.getCreateBy());
  61. qw.eq(input.getCreateTime() != null, Dept::getCreateTime, input.getCreateTime());
  62. qw.like(!StringHelper.isEmpty(input.getUpdateBy()), Dept::getUpdateBy, input.getUpdateBy());
  63. qw.eq(input.getUpdateTime() != null, Dept::getUpdateTime, input.getUpdateTime());
  64. Page<Dept> page = GetPage(pageInput);
  65. if (page != null) {
  66. IPage<Dept> iPage = deptService.getListPage(page, qw);
  67. return Success("成功", iPage.getRecords(), iPage.getTotal());
  68. } else {
  69. return Success("成功", deptService.getList(qw));
  70. }
  71. }
  72. @ApiOperation("新 二级单位交办时,获取子部门列表")
  73. @Log(title = "二级单位交办时,获取子部门列表", businessType = BusinessType.QUERY)
  74. @GetMapping
  75. public AjaxResult getSonList() {
  76. LambdaQueryWrapper<Dept> qw = new LambdaQueryWrapper();
  77. qw.eq(Dept::getState,0L)
  78. .eq(Dept::getIsDept,2L).eq(Dept::getParentId,CurrentUser().getDeptId());
  79. return Success("成功", deptService.getList(qw));
  80. }
  81. @ApiOperation("新 话务员交办时,获取要交办的部门tree")
  82. @GetMapping("/deptZBTree")
  83. public AjaxResult deptZBTree() {
  84. return Success("查询成功", deptService.selectZBDeptTreeList());
  85. }
  86. /**
  87. * 查询部门列表(排除节点)
  88. */
  89. @Log(title = "查询部门列表(排除节点)", businessType = BusinessType.QUERY)
  90. @GetMapping("list/exclude/{deptId}")
  91. public AjaxResult excludeChild(@PathVariable(value = "deptId", required = false) Long deptId) {
  92. LambdaQueryWrapper<Dept> qw = new LambdaQueryWrapper<>();
  93. qw.eq(Dept::getDelFlag, "0");
  94. List<Dept> depts = deptService.getList(qw);
  95. depts.removeIf(d -> d.getDeptId().intValue() == deptId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), deptId + ""));
  96. return Success("查询成功", depts);
  97. }
  98. /**
  99. * 查看部门详情
  100. *
  101. * @param id
  102. * @return
  103. */
  104. @ApiOperation("详情")
  105. @Log(title = "查看部门详情", businessType = BusinessType.QUERY)
  106. @GetMapping("/{id}")
  107. public AjaxResult getInfo(@PathVariable Long id) {
  108. if (id == null) {
  109. return Error("部门编号为空");
  110. }
  111. LambdaQueryWrapper<Dept> qw = new LambdaQueryWrapper<>();
  112. qw.eq(Dept::getDeptId, id).eq(Dept::getDelFlag, "0");
  113. Dept entity = deptService.getEntity(qw);
  114. if (entity == null) {
  115. return Error("部门不存在!");
  116. }
  117. return Success("成功", entity);
  118. }
  119. /**
  120. * 新增部门
  121. *
  122. * @param input
  123. * @return
  124. */
  125. @ApiOperation("新增")
  126. @Log(title = "新增部门", businessType = BusinessType.INSERT)
  127. @PostMapping
  128. public AjaxResult add(@RequestBody Dept input) {
  129. if (!deptService.checkDeptNameUnique(input)) {
  130. return Error("新增部门'" + input.getDeptName() + "'失败,部门名称已存在");
  131. }
  132. input.setCreateBy(CurrentUser().getUserName());
  133. input.setCreateTime(new Date());
  134. Dept parentdept= deptService.getEntity(input.getParentId());
  135. if(input.getParentId()==0){
  136. input.setAncestorsName(input.getDeptName());
  137. }
  138. else{
  139. input.setAncestorsName(parentdept.getAncestorsName()+"/"+input.getDeptName());
  140. }
  141. boolean result = deptService.insert(input);
  142. if (result) {
  143. input.setAncestors(parentdept.getAncestors()+","+input.getDeptId());
  144. deptService.update(input);
  145. if (redisHelper.getCache("deptTree")!=null){
  146. redisHelper.deleteCache("deptTree");
  147. }
  148. return Success("成功");
  149. } else {
  150. return Error("新增失败");
  151. }
  152. }
  153. /**
  154. * 修改部门信息
  155. *
  156. * @param input
  157. * @return
  158. */
  159. @ApiOperation("编辑")
  160. @Log(title = "修改部门信息", businessType = BusinessType.UPDATE)
  161. @PutMapping
  162. public AjaxResult edit(@RequestBody Dept input) {
  163. if (!deptService.checkDeptNameUnique(input)) {
  164. return Error("修改部门'" + input.getDeptName() + "'失败,部门名称已存在");
  165. } else if (input.getParentId().equals(input.getDeptId())) {
  166. return Error("修改部门'" + input.getDeptName() + "'失败,上级部门不能是自己");
  167. } else if ("1".equals(input.getState()) && deptService.selectNormalChildrenDeptById(input.getDeptId()) > 0) {
  168. return Error("该部门包含未停用的子部门!");
  169. }
  170. input.setUpdateBy(CurrentUser().getUserName());
  171. LambdaQueryWrapper<Dept> qw = new LambdaQueryWrapper<>();
  172. qw.eq(Dept::getDelFlag, "0").eq(Dept::getDeptId, input.getDeptId());
  173. Dept olddept=deptService.getEntity(qw);
  174. if (olddept == null) {
  175. return Error("部门不存在!");
  176. }
  177. input.setUpdateTime(new Date());
  178. if (redisHelper.getCache("deptTree")!=null){
  179. redisHelper.deleteCache("deptTree");
  180. }
  181. Dept parentdept= deptService.getEntity(input.getParentId());
  182. input.setAncestorsName(parentdept.getAncestorsName()+input.getDeptName());
  183. input.setAncestors(parentdept.getAncestors()+","+input.getDeptId());
  184. boolean result = deptService.update(input);
  185. if (result) {
  186. //根据name+电话,判断通讯录中是否存在, 存在修改第一条通讯录的数据
  187. if(!Objects.equals(olddept.getPhone(), input.getPhone()) &&!StringHelper.isEmpty(input.getPhone()) )
  188. {
  189. LambdaQueryWrapper<AddressBook> lqwaddress=new LambdaQueryWrapper<>();
  190. lqwaddress.eq(AddressBook::getName,olddept.getDeptName()).eq(AddressBook::getMobile,olddept.getPhone());
  191. List<AddressBook> addressBooks= addressBookService.getList(lqwaddress);
  192. if(addressBooks!=null &&addressBooks.stream().count()>0)
  193. {
  194. LambdaQueryWrapper<AddressBook> lqwaone=new LambdaQueryWrapper<>();
  195. lqwaddress.eq(AddressBook::getName,olddept.getDeptName()).eq(AddressBook::getMobile,olddept.getPhone());
  196. AddressBook addressBook = addressBookService.getList(lqwaone).stream().findFirst().get();
  197. addressBook.setMobile(input.getPhone());
  198. addressBookService.update(addressBook);
  199. }
  200. }
  201. if (redisHelper.getCache("deptTree")!=null){
  202. redisHelper.deleteCache("deptTree");
  203. }
  204. return Success("成功");
  205. } else {
  206. return Error("修改失败");
  207. }
  208. }
  209. /**
  210. * 删除部门
  211. *
  212. * @param ids
  213. * @return
  214. */
  215. @ApiOperation("删除")
  216. @Log(title = "删除部门", businessType = BusinessType.DELETE)
  217. @DeleteMapping("/{ids}")
  218. public AjaxResult delete(@PathVariable Long[] ids) {
  219. for (long id : ids) {
  220. if (deptService.hasChildByDeptId(id)) {
  221. return Error("存在下级部门,不允许删除");
  222. }
  223. if (deptService.checkDeptExistUser(id)) {
  224. return Error("部门存在用户,不允许删除");
  225. }
  226. }
  227. LambdaUpdateWrapper<Dept> qw = new LambdaUpdateWrapper<>();
  228. qw.set(Dept::getDelFlag, "2").in(Dept::getDeptId, ids);
  229. if (redisHelper.getCache("deptTree")!=null){
  230. redisHelper.deleteCache("deptTree");
  231. }
  232. int update = deptMapper.update(null, qw);
  233. if (update > 0) {
  234. if (redisHelper.getCache("deptTree")!=null){
  235. redisHelper.deleteCache("deptTree");
  236. }
  237. return Success("成功");
  238. } else {
  239. return Error("删除失败");
  240. }
  241. }
  242. }