SysDeptController.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. package com.boman.system.controller;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.boman.common.core.utils.SecurityUtils;
  4. import com.boman.common.core.utils.StringUtils;
  5. import com.boman.common.core.web.controller.BaseController;
  6. import com.boman.common.log.annotation.Log;
  7. import com.boman.common.log.enums.BusinessType;
  8. import com.boman.common.security.annotation.PreAuthorize;
  9. import com.boman.domain.SysDept;
  10. import com.boman.domain.constant.UserConstants;
  11. import com.boman.domain.dto.AjaxResult;
  12. import com.boman.system.mapper.SysDeptMapper;
  13. import com.boman.system.service.ISysDeptService;
  14. import org.apache.commons.lang3.ArrayUtils;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.validation.annotation.Validated;
  17. import org.springframework.web.bind.annotation.*;
  18. import java.io.BufferedWriter;
  19. import java.io.File;
  20. import java.io.FileWriter;
  21. import java.util.ArrayList;
  22. import java.util.Iterator;
  23. import java.util.List;
  24. /**
  25. * 部门信息
  26. *
  27. * @author ruoyi
  28. */
  29. @RestController
  30. @RequestMapping("/dept")
  31. public class SysDeptController extends BaseController
  32. {
  33. @Autowired
  34. private ISysDeptService deptService;
  35. @Autowired // 整理数据测试用的
  36. private SysDeptMapper deptMapper;
  37. /**
  38. * 获取部门列表
  39. */
  40. // @PreAuthorize(hasPermi = "system:dept:list")
  41. @GetMapping("/list")
  42. public AjaxResult list(SysDept dept) {
  43. List<SysDept> depts = deptService.selectDeptList(dept);
  44. return AjaxResult.success(depts);
  45. }
  46. /**
  47. * 获取部门列表
  48. */
  49. @GetMapping("/deptList")
  50. public AjaxResult deptList(SysDept dept) {
  51. List<SysDept> depts = deptService.selectDepts(dept);
  52. return AjaxResult.success(depts);
  53. }
  54. /**
  55. * 查询部门列表(排除节点)
  56. */
  57. @PreAuthorize(hasPermi = "system:dept:list")
  58. @GetMapping("/list/exclude/{id}")
  59. public AjaxResult excludeChild(@PathVariable(value = "id", required = false) Long id)
  60. {
  61. List<SysDept> depts = deptService.selectDeptList(new SysDept());
  62. Iterator<SysDept> it = depts.iterator();
  63. while (it.hasNext())
  64. {
  65. SysDept d = (SysDept) it.next();
  66. if (d.getId().intValue() == id
  67. || ArrayUtils.contains(StringUtils.split(d.getAncestors(), ","), id + ""))
  68. {
  69. it.remove();
  70. }
  71. }
  72. return AjaxResult.success(depts);
  73. }
  74. /**
  75. * 功能描述: 拿到部门下所有的部门, 包含传过来的deptId
  76. *
  77. * @param deptId deptId
  78. * @return com.boman.domain.dto.AjaxResult
  79. */
  80. @GetMapping("/list/children/depts/{deptId}")
  81. public List<SysDept> listChildrenDepts(@PathVariable(value = "deptId") Long deptId) {
  82. return deptService.listChildrenDepts(deptId);
  83. }
  84. /**
  85. * 根据部门编号获取详细信息
  86. */
  87. @PreAuthorize(hasPermi = "system:dept:query")
  88. @GetMapping(value = "/{id}")
  89. public AjaxResult getInfo(@PathVariable Long id)
  90. {
  91. return AjaxResult.success(deptService.selectDeptById(id));
  92. }
  93. /**
  94. * 根据部门编号获取详细信息
  95. */
  96. @PreAuthorize(hasPermi = "system:dept:query")
  97. @GetMapping(value = "/getById/{id}")
  98. public SysDept getById(@PathVariable("id") Long id)
  99. {
  100. return deptService.selectDeptById(id);
  101. }
  102. /**
  103. * 根据部门名称获取该用户属于哪一个部门(疫苗新增/修改时使用)
  104. */
  105. @GetMapping(value = "/selectByDeptName/{deptName}")
  106. public List<SysDept> selectByDeptName(@PathVariable("deptName") String deptName)
  107. {
  108. return deptService.selectByDeptName(deptName);
  109. }
  110. /**
  111. * 获取部门下拉树列表
  112. */
  113. @GetMapping("/treeselect")
  114. public AjaxResult treeselect(SysDept dept)
  115. {
  116. List<SysDept> depts = deptService.selectDeptList(dept);
  117. return AjaxResult.success(deptService.buildDeptTreeSelect(depts));
  118. }
  119. /**
  120. * 获取部门下拉树列表
  121. */
  122. @GetMapping("/depts")
  123. public AjaxResult depts(SysDept dept)
  124. {
  125. List<SysDept> depts = deptService.selectDepts(dept);
  126. return AjaxResult.success(deptService.buildDeptTreeSelect(depts));
  127. }
  128. /**
  129. * 加载对应角色部门列表树
  130. */
  131. @GetMapping(value = "/roleDeptTreeselect/{roleId}")
  132. public AjaxResult roleDeptTreeselect(@PathVariable("roleId") Long roleId)
  133. {
  134. List<SysDept> depts = deptService.selectDeptList(new SysDept());
  135. AjaxResult ajax = AjaxResult.success();
  136. ajax.put("checkedKeys", deptService.selectDeptListByRoleId(roleId));
  137. ajax.put("depts", deptService.buildDeptTreeSelect(depts));
  138. return ajax;
  139. }
  140. /**
  141. * 新增部门
  142. */
  143. @PreAuthorize(hasPermi = "system:dept:add")
  144. @Log(title = "部门管理", businessType = BusinessType.INSERT)
  145. @PostMapping
  146. public AjaxResult add(@Validated @RequestBody SysDept dept)
  147. {
  148. if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept)))
  149. {
  150. return AjaxResult.error("新增部门'" + dept.getDeptName() + "'失败,部门名称已存在");
  151. }
  152. dept.setCreateBy(SecurityUtils.getUsername());
  153. return toAjax(deptService.insertDept(dept));
  154. }
  155. /**
  156. * 修改部门
  157. */
  158. @PreAuthorize(hasPermi = "system:dept:edit")
  159. @Log(title = "部门管理", businessType = BusinessType.UPDATE)
  160. @PutMapping
  161. public AjaxResult edit(@Validated @RequestBody SysDept dept)
  162. {
  163. if (UserConstants.NOT_UNIQUE.equals(deptService.checkDeptNameUnique(dept)))
  164. {
  165. return AjaxResult.error("修改部门'" + dept.getDeptName() + "'失败,部门名称已存在");
  166. }
  167. else if (dept.getParentId().equals(dept.getId()))
  168. {
  169. return AjaxResult.error("修改部门'" + dept.getDeptName() + "'失败,上级部门不能是自己");
  170. }
  171. else if (StringUtils.equals(UserConstants.DEPT_DISABLE, dept.getStatus())
  172. && deptService.selectNormalChildrenDeptById(dept.getId()) > 0)
  173. {
  174. return AjaxResult.error("该部门包含未停用的子部门!");
  175. }
  176. dept.setUpdateBy(SecurityUtils.getUsername());
  177. return toAjax(deptService.updateDept(dept));
  178. }
  179. /**
  180. * 删除部门
  181. */
  182. @PreAuthorize(hasPermi = "system:dept:remove")
  183. @Log(title = "部门管理", businessType = BusinessType.DELETE)
  184. @DeleteMapping("/{id}")
  185. public AjaxResult remove(@PathVariable Long id)
  186. {
  187. if (deptService.hasChildById(id))
  188. {
  189. return AjaxResult.error("存在下级部门,不允许删除");
  190. }
  191. if (deptService.checkDeptExistUser(id))
  192. {
  193. return AjaxResult.error("部门存在用户,不允许删除");
  194. }
  195. return toAjax(deptService.deleteDeptById(id));
  196. }
  197. @GetMapping("/auth/moveData/{limit}")
  198. public String moveYmjzData( @PathVariable("limit") int limit) throws Exception {
  199. //List<String> idCardList = mapper.listIdCard(limit, offset);
  200. List<JSONObject> ymjzList = deptService.syncData(limit);
  201. int moveData = 0;
  202. StringBuilder stringBuilder = new StringBuilder();
  203. List<String> enpty = new ArrayList<>();
  204. for (int i = 0; i < ymjzList.size(); i++) {
  205. JSONObject ymjz = ymjzList.get(i);
  206. String idCard = ymjz.getString("id_card");
  207. Long deptId = ymjz.getLong("dept_id");
  208. String sql = "";
  209. if(deptId == null) {
  210. enpty.add(idCard);
  211. }else {
  212. sql = String.format("UPDATE vaccine_info set dept_id = %s where id_card = '%s'; -- %s \r\n", deptId, idCard, i);
  213. stringBuilder.append(sql);
  214. }
  215. System.err.println("count: " + i + ", " + sql);
  216. moveData++;
  217. }
  218. // File file = new File("E:\\\\work\\vainfo.sql");
  219. File file = new File("/usr/local/sql/vaccine_info.sql");
  220. if (!file.exists()) {
  221. file.createNewFile();
  222. }
  223. FileWriter fileWriter = new FileWriter(file, true);
  224. BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
  225. bufferedWriter.write(stringBuilder.toString());
  226. bufferedWriter.close();
  227. return "循环了 " + moveData + " 条";
  228. }
  229. /**
  230. * 查找子部门,不递归
  231. */
  232. @GetMapping("/parentId/{parentId}")
  233. public List<SysDept> getByParentId(@PathVariable("parentId") Long parentId) {
  234. return deptService.getByParentId(parentId);
  235. }
  236. /**
  237. * 功能描述: 把部门表中的ancestors按照规则塞进值
  238. *
  239. * @return com.boman.domain.dto.AjaxResult
  240. */
  241. @GetMapping("/auth")
  242. public AjaxResult setAncestors() {
  243. List<SysDept> sysDepts = deptService.selectDeptsList();
  244. for (SysDept sysDept : sysDepts) {
  245. String ancestors = sysDept.getAncestors();
  246. if (StringUtils.isEmpty(ancestors)) {
  247. Long parentId = sysDept.getParentId();
  248. //根据子的parentId找到对应的父对象
  249. SysDept parent1 = deptService.getParentById(parentId);
  250. if (parent1 == null) {
  251. continue;
  252. }
  253. Long id1 = parent1.getId();
  254. Long parentId1 = parent1.getParentId();
  255. SysDept parent2 = deptService.getParentById(parentId1);
  256. if (parent2 == null) {
  257. sysDept.setAncestors(id1 + "");
  258. deptMapper.updateDept(sysDept);
  259. continue;
  260. }
  261. Long id2 = parent2.getId();
  262. Long parentId2 = parent2.getParentId();
  263. SysDept parent3 = deptService.getParentById(parentId2);
  264. if (parent3 == null) {
  265. sysDept.setAncestors(id2 + "," + id1);
  266. deptMapper.updateDept(sysDept);
  267. continue;
  268. }
  269. Long id3 = parent3.getId();
  270. sysDept.setAncestors(id3 + "," + id2 + "," + id1);
  271. deptMapper.updateDept(sysDept);
  272. }
  273. }
  274. // System.out.println(JSON.toJSONString(sysDepts));
  275. return AjaxResult.success();
  276. }
  277. /* @GetMapping("/auth/syncData")
  278. public void syncData() {
  279. try {
  280. deptService.syncData();
  281. } catch (Exception e) {
  282. e.printStackTrace();
  283. }
  284. }*/
  285. }