SysDeptController.java 11 KB

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