YpZdClassController.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. package cn.hnthyy.thmz.controller.mz;
  2. import cn.hnthyy.thmz.comment.UserLoginToken;
  3. import cn.hnthyy.thmz.entity.his.mz.YpZdClass;
  4. import cn.hnthyy.thmz.service.his.mz.*;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.apache.commons.lang3.StringUtils;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.web.bind.annotation.*;
  9. import java.util.*;
  10. @Slf4j
  11. @RestController
  12. public class YpZdClassController {
  13. @Autowired
  14. private YpZdClassService ypZdClassService;
  15. /**
  16. * 查询所有药品、检验、检查的分类
  17. *
  18. * @return
  19. */
  20. @UserLoginToken
  21. @RequestMapping(value = "/getYpZdClasses", method = {RequestMethod.GET})
  22. public Map<String, Object> getYpZdClasses() {
  23. Map<String, Object> resultMap = new HashMap<>();
  24. try {
  25. List<YpZdClass> ypZdClasses = ypZdClassService.queryYpZdClasses();
  26. List<Map<String, Object>> treeList = null;
  27. if (ypZdClasses == null || ypZdClasses.size() == 0) {
  28. log.info("未查询到药品分类");
  29. } else {
  30. treeList = getYpZdClassTreeList(ypZdClasses);
  31. }
  32. List<Map<String, Object>> jyZdClasses = ypZdClassService.queryJyZdClasses();
  33. List<Map<String, Object>> jyTreeList = null;
  34. if (ypZdClasses == null || ypZdClasses.size() == 0) {
  35. log.info("未查询到检验分类");
  36. } else {
  37. jyTreeList = getJyZdClassTreeList(jyZdClasses);
  38. }
  39. List<Map<String, Object>> jcZdClasses = ypZdClassService.queryJcZdClasses();
  40. List<Map<String, Object>> jcTreeList = null;
  41. if (jcZdClasses == null || jcZdClasses.size() == 0) {
  42. log.info("未查询到检查分类");
  43. } else {
  44. jcTreeList = getJcZdClassTreeList(jcZdClasses);
  45. }
  46. List<Map<String, Object>> fullTree = new ArrayList<>();
  47. if(treeList!=null){
  48. Map<String, Object> parentMap = new HashMap<>();
  49. fullTree.add(parentMap);
  50. //初始化时不知道自己有子节点,所以设置为非父节点
  51. parentMap.put("isParent", true);
  52. parentMap.put("id", 1);
  53. parentMap.put("name", "西成药");
  54. parentMap.put("children", treeList);
  55. }
  56. if(jyTreeList!=null){
  57. Map<String, Object> parentMap = new HashMap<>();
  58. fullTree.add(parentMap);
  59. //初始化时不知道自己有子节点,所以设置为非父节点
  60. parentMap.put("isParent", true);
  61. parentMap.put("id", 2);
  62. parentMap.put("name", "检验项目");
  63. parentMap.put("children", jyTreeList);
  64. }
  65. if(jcTreeList!=null){
  66. Map<String, Object> parentMap = new HashMap<>();
  67. fullTree.add(parentMap);
  68. //初始化时不知道自己有子节点,所以设置为非父节点
  69. parentMap.put("isParent", true);
  70. parentMap.put("id", 3);
  71. parentMap.put("name", "检查项目");
  72. parentMap.put("children", jcTreeList);
  73. }
  74. resultMap.put("code", 0);
  75. resultMap.put("message", "查询所有的分类成功");
  76. resultMap.put("data", fullTree);
  77. return resultMap;
  78. } catch (Exception e) {
  79. e.printStackTrace();
  80. log.error("查询所有药品、检验、检查的分类失败,错误信息{}", e.getMessage());
  81. resultMap.put("code", -1);
  82. resultMap.put("message", "查询所有药品、检验、检查的分类失败,错误原因【" + e.getMessage() + "】");
  83. return resultMap;
  84. }
  85. }
  86. /**
  87. * 获取药品字典树
  88. *
  89. * @param ypZdClasses
  90. * @return
  91. */
  92. private List<Map<String, Object>> getYpZdClassTreeList(List<YpZdClass> ypZdClasses) {
  93. List<Map<String, Object>> treeList = new ArrayList<>();
  94. //当前一级父节点
  95. Map<String, Object> grandParentMap = null;
  96. //当前二级父节点
  97. Map<String, Object> parentMap = null;
  98. for (YpZdClass ypZdClass : ypZdClasses) {
  99. if (ypZdClass.getCode().endsWith("0000")) {
  100. grandParentMap = new HashMap<>();
  101. treeList.add(grandParentMap);
  102. //初始化时不知道自己有子节点,所以设置为非父节点
  103. grandParentMap.put("isParent", false);
  104. grandParentMap.put("id", ypZdClass.getCode());
  105. grandParentMap.put("name", ypZdClass.getName());
  106. } else if (ypZdClass.getCode().endsWith("00")) {
  107. //此时已经有子节点,设置成父节点
  108. grandParentMap.put("isParent", true);
  109. Map<String, Object> temMap = new HashMap<>();
  110. //初始化时不知道自己有子节点,所以设置为非父节点
  111. temMap.put("isParent", false);
  112. temMap.put("id", ypZdClass.getCode());
  113. temMap.put("name", ypZdClass.getName());
  114. List<Map<String, Object>> children = (List<Map<String, Object>>) grandParentMap.get("children");
  115. if (children == null) {
  116. children = new ArrayList<>();
  117. grandParentMap.put("children", children);
  118. }
  119. children.add(temMap);
  120. parentMap = temMap;
  121. } else {
  122. parentMap.put("isParent", true);
  123. Map<String, Object> temMap = new HashMap<>();
  124. //初始化时不知道自己有子节点,所以设置为非父节点
  125. temMap.put("isParent", false);
  126. temMap.put("id", ypZdClass.getCode());
  127. temMap.put("name", ypZdClass.getName());
  128. List<Map<String, Object>> children = (List<Map<String, Object>>) parentMap.get("children");
  129. if (children == null) {
  130. children = new ArrayList<>();
  131. parentMap.put("children", children);
  132. }
  133. children.add(temMap);
  134. }
  135. }
  136. return treeList;
  137. }
  138. /**
  139. * 获取所有检验分类字典树
  140. *
  141. * @param jyZdClasses
  142. * @return
  143. */
  144. private List<Map<String, Object>> getJyZdClassTreeList(List<Map<String, Object>> jyZdClasses) {
  145. List<Map<String, Object>> treeList = new ArrayList<>();
  146. //父节点
  147. Map<String, Object> parentMap = null;
  148. //上一条数据的父编码
  149. String prevDlCode = null;
  150. for (Map<String, Object> jyZdClass : jyZdClasses) {
  151. String xlCode = (String) jyZdClass.get("xl_code");
  152. if(StringUtils.isBlank(xlCode)){
  153. continue;
  154. }
  155. String dlCode = (String) jyZdClass.get("dl_code");
  156. String dlName = (String) jyZdClass.get("dl_name");
  157. String xlName = (String) jyZdClass.get("xl_name");
  158. if(prevDlCode==null || !prevDlCode.equals(dlCode)){
  159. parentMap = new HashMap<>();
  160. treeList.add(parentMap);
  161. parentMap.put("isParent", true);
  162. parentMap.put("id", dlCode);
  163. parentMap.put("name", dlName);
  164. List<Map<String, Object>> children = new ArrayList<>();
  165. parentMap.put("children", children);
  166. Map<String, Object> temMap = new HashMap<>();
  167. //初始化时不知道自己有子节点,所以设置为非父节点
  168. temMap.put("isParent", false);
  169. temMap.put("id", xlCode);
  170. temMap.put("name", xlName);
  171. children.add(temMap);
  172. }else {
  173. List<Map<String, Object>> children = (List<Map<String, Object>>) parentMap.get("children");
  174. Map<String, Object> temMap = new HashMap<>();
  175. //初始化时不知道自己有子节点,所以设置为非父节点
  176. temMap.put("isParent", false);
  177. temMap.put("id", xlCode);
  178. temMap.put("name", xlName);
  179. children.add(temMap);
  180. }
  181. prevDlCode=dlCode;
  182. }
  183. return treeList;
  184. }
  185. /**
  186. * 获取所有检查分类字典树
  187. *
  188. * @param jyZcClasses
  189. * @return
  190. */
  191. private List<Map<String, Object>> getJcZdClassTreeList(List<Map<String, Object>> jyZcClasses) {
  192. List<Map<String, Object>> treeList = new ArrayList<>();
  193. //当前一级父节点
  194. Map<String, Object> grandParentMap = null;
  195. //当前二级父节点
  196. Map<String, Object> parentMap = null;
  197. //当前数据的祖父编码
  198. String currentGrandParentDlCode = null;
  199. //当前数据的父编码
  200. String currentParentDlCode = null;
  201. for (Map<String, Object> jcZdClass : jyZcClasses) {
  202. String dlCode = (String) jcZdClass.get("dl_code");
  203. if(StringUtils.isBlank(dlCode)){
  204. continue;
  205. }
  206. String dlName = (String) jcZdClass.get("dl_name");
  207. String xlCode = (String) jcZdClass.get("xl_code");
  208. String xlName = (String) jcZdClass.get("xl_name");
  209. //父编码是2位数 说明是一级树
  210. if(dlCode.length()==2){
  211. grandParentMap = new HashMap<>();
  212. treeList.add(grandParentMap);
  213. //初始化时不知道自己有子节点,所以设置为非父节点
  214. grandParentMap.put("isParent", false);
  215. grandParentMap.put("id", dlCode);
  216. grandParentMap.put("name", dlName);
  217. currentGrandParentDlCode=dlCode;
  218. if(StringUtils.isNotBlank(xlCode)){
  219. grandParentMap.put("isParent", true);
  220. List<Map<String, Object>> children = new ArrayList<>();
  221. grandParentMap.put("children", children);
  222. Map<String, Object> temMap = new HashMap<>();
  223. //初始化时不知道自己有子节点,所以设置为非父节点
  224. temMap.put("isParent", false);
  225. temMap.put("id", xlCode);
  226. temMap.put("name", xlName);
  227. children.add(temMap);
  228. }
  229. //以当前的祖父节点的编码做为编码起始,说明示二级菜单
  230. }else if(dlCode.startsWith(currentGrandParentDlCode)){
  231. //此时已经有子节点,设置成父节点
  232. grandParentMap.put("isParent", true);
  233. List<Map<String, Object>> children = (List<Map<String, Object>>) grandParentMap.get("children");
  234. if (children == null) {
  235. children = new ArrayList<>();
  236. grandParentMap.put("children", children);
  237. }
  238. if(parentMap==null || !currentParentDlCode.equals(dlCode)){
  239. parentMap=new HashMap<>();
  240. //初始化时不知道自己有子节点,所以设置为非父节点
  241. parentMap.put("isParent", false);
  242. parentMap.put("id", dlCode);
  243. parentMap.put("name", dlName.substring(dlName.indexOf(" ")+1));
  244. children.add(parentMap);
  245. currentParentDlCode=dlCode;
  246. }
  247. if(StringUtils.isNotBlank(xlCode)){
  248. parentMap.put("isParent", true);
  249. List<Map<String, Object>> grandchildren = (List<Map<String, Object>>) parentMap.get("children");
  250. if (grandchildren == null) {
  251. grandchildren = new ArrayList<>();
  252. parentMap.put("children", grandchildren);
  253. }
  254. parentMap.put("children", grandchildren);
  255. Map<String, Object> temMap = new HashMap<>();
  256. //初始化时不知道自己有子节点,所以设置为非父节点
  257. temMap.put("isParent", false);
  258. temMap.put("id", xlCode);
  259. temMap.put("name", xlName);
  260. grandchildren.add(temMap);
  261. }
  262. }
  263. }
  264. return treeList;
  265. }
  266. }