1e9870924835e3c1f6defc3c4da8c9fa46036733.svn-base 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. package com.synyi.edc.controller;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import javax.annotation.Resource;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpServletResponse;
  11. import org.apache.commons.lang3.StringUtils;
  12. import org.springframework.stereotype.Controller;
  13. import org.springframework.ui.Model;
  14. import org.springframework.util.CollectionUtils;
  15. import org.springframework.web.bind.annotation.RequestMapping;
  16. import com.alibaba.fastjson.JSON;
  17. import com.alibaba.fastjson.JSONObject;
  18. import com.synyi.edc.pojo.Parameter;
  19. import com.synyi.edc.service.IUserService;
  20. @Controller
  21. @RequestMapping("/sqlbuilder")
  22. public class SqlbuilderController {
  23. @Resource
  24. private IUserService userService;
  25. @RequestMapping("/toIndex")
  26. public String toJ(HttpServletRequest request,Model model){
  27. // int userId = Integer.parseInt(request.getParameter("id"));
  28. // User user = this.userService.getUserById(userId);
  29. // model.addAttribute("user", user);
  30. return "index";
  31. }
  32. @RequestMapping("/generate")
  33. public String toIndex(HttpServletRequest request,Model model,Parameter param,HttpServletResponse res){
  34. System.out.println(param);
  35. System.out.println();
  36. String jsonStr = param.getEventData();
  37. String stepStr = param.getGroupData();
  38. String returnSql = genGroupSql(jsonStr,stepStr);
  39. System.out.println(returnSql);
  40. printJson(res,returnSql);
  41. return null;
  42. }
  43. public static String genGroupSql(String jsonStr,String stepStr){
  44. Map<String,Map> temp1 = null; //入排条件1
  45. Map temp2 = null; //入排条件2
  46. List<Map> lis = (List)JSON.parse(jsonStr);
  47. if(!CollectionUtils.isEmpty(lis)){
  48. temp1 = lis.get(0);
  49. if(lis.size()>1){
  50. temp2 = lis.get(1);
  51. }
  52. }
  53. System.out.println(stepStr);
  54. Map stepMap = (Map)JSON.parse(stepStr);;
  55. int maxValue = 0;
  56. int minValue = 0;
  57. int interval = 0;
  58. try{
  59. if(StringUtils.isNotBlank((String)stepMap.get("maxValue"))){
  60. maxValue = Integer.parseInt((String)stepMap.get("maxValue"));
  61. }
  62. if(StringUtils.isNotBlank((String)stepMap.get("minValue"))){
  63. minValue = Integer.parseInt((String)stepMap.get("minValue"));
  64. }
  65. if(StringUtils.isNotBlank((String)stepMap.get("interval"))){
  66. interval = Integer.parseInt((String)stepMap.get("interval"));
  67. }
  68. }catch(Exception e){}
  69. String groupType = stepMap.get("groupType")==null?"":(String)stepMap.get("groupType");
  70. String returnSql = "with temp1 as ("+event_diagnose(temp1,stepMap)+")";
  71. if(!CollectionUtils.isEmpty(temp2)){
  72. returnSql += ", \r\n temp2 as ("+event_diagnose(temp2,stepMap)+")";
  73. String rel = (String)temp2.get("关系");
  74. if("or".equals(rel)){
  75. returnSql += " \r\n select ";
  76. if(StringUtils.isNotBlank(groupType)){
  77. switch((String)stepMap.get("groupType")){
  78. case "年龄段":returnSql += generateAgePart(minValue,maxValue,interval)+" 年龄段,";break;
  79. case "年份":returnSql += "to_char(diag_time,'yyyy') 年份,";break;
  80. case "医院":returnSql += "t.org_code,org.org_name,";break;
  81. case "就诊类型":returnSql += "visit_type,";break;
  82. case "性别":returnSql += "case when c.sex_code in ('2','9') then '女' when c.sex_code ='1' then '男' else '未知' end 性别,";break;
  83. }
  84. }
  85. if("patientsNumber".equals((String)stepMap.get("queryType"))){
  86. returnSql += "count(distinct t.patient_id) 人数";
  87. }else{
  88. returnSql += "count(t.patient_id) 人次";
  89. }
  90. returnSql += " \r\n from (select * from temp1 union all select * from temp2 )t ";
  91. if("性别".equals((String)stepMap.get("groupType"))||"年龄段".equals((String)stepMap.get("groupType"))){
  92. returnSql += " join patient.patient_master_info c on t.patient_id=c.patient_id \r\n";
  93. }
  94. if("医院".equals((String)stepMap.get("groupType"))){
  95. returnSql += " join mdm.organization org on t.org_code=org.org_code \r\n";
  96. }
  97. if(StringUtils.isNotBlank(groupType)){
  98. returnSql += " group by ";
  99. switch((String)stepMap.get("groupType")){
  100. case "年龄段":returnSql += generateAgePart(minValue,maxValue,interval);break;
  101. case "年份":returnSql += "to_char(diag_time,'yyyy') ";break;
  102. case "医院":returnSql += "t.org_code,org.org_name";break;
  103. case "就诊类型":returnSql += "visit_type";break;
  104. case "性别":returnSql += "case when c.sex_code in ('2','9') then '女' when c.sex_code ='1' then '男' else '未知' end";break;
  105. }
  106. }
  107. }else if("and".equals(rel)){
  108. returnSql += "select count(1) 人数 from (select distinct temp1.patient_id from temp1)a join (select distinct temp2.patient_id from temp2 ) b on a.patient_id=b.patient_id";
  109. }
  110. }else{//只有temp1的情况
  111. returnSql += "\r\n select ";
  112. if(StringUtils.isNotBlank(groupType)){
  113. switch((String)stepMap.get("groupType")){
  114. case "年龄段":returnSql += generateAgePart(minValue,maxValue,interval)+" 年龄段,";break;
  115. case "年份":returnSql += "to_char(diag_time,'yyyy') 年份,";break;
  116. case "医院":returnSql += "temp1.org_code,org.org_name,";break;
  117. case "就诊类型":returnSql += "visit_type,";break;
  118. case "性别":returnSql += "case when c.sex_code in ('2','9') then '女' when c.sex_code ='1' then '男' else '未知' end 性别,\r\n";break;
  119. }
  120. }
  121. if("patientsNumber".equals((String)stepMap.get("queryType"))){
  122. returnSql += "count(distinct temp1.patient_id) 人数";
  123. }else{ //visitsNumber 人次
  124. returnSql += "count(temp1.patient_id) 人次";
  125. }
  126. returnSql += " from temp1 ";
  127. if("性别".equals((String)stepMap.get("groupType"))||"年龄段".equals((String)stepMap.get("groupType"))){
  128. returnSql += " join patient.patient_master_info c on temp1.patient_id=c.patient_id \r\n ";
  129. }
  130. if("医院".equals((String)stepMap.get("groupType"))){
  131. returnSql += " join mdm.organization org on temp1.org_code=org.org_code \r\n";
  132. }
  133. if(StringUtils.isNotBlank(groupType)){
  134. returnSql += "group by ";
  135. switch((String)stepMap.get("groupType")){
  136. case "年龄段":returnSql += generateAgePart(minValue,maxValue,interval);break;
  137. case "年份":returnSql += "to_char(diag_time,'yyyy') ";break;
  138. case "医院":returnSql += "temp1.org_code,org.org_name";break;
  139. case "就诊类型":returnSql += "visit_type";break;
  140. case "性别":returnSql += "case when c.sex_code in ('2','9') then '女' when c.sex_code ='1' then '男' else '未知' end";break;
  141. }
  142. }
  143. }
  144. return returnSql;
  145. }
  146. public static String generateAgePart(int min,int max,int interval){
  147. String str = " case";
  148. int temp = min;
  149. for(int i=min;i<max;i+=interval){
  150. if(i>min){
  151. //System.out.println(temp+":"+i);
  152. if(temp==min){
  153. str += " when extract(year from age(diag_time,c.birth_date)) >="+temp +" and extract(year from age(diag_time,c.birth_date)) <"+i+" then '"+temp+"到"+i+"'\r\n";
  154. //
  155. }else if(i==max){
  156. // str += " when extract(year from age(temp1.diag_time,c.birth_date)) <="+max+" then '"+temp+"到"+max+"' end ";
  157. }else{
  158. str += " when extract(year from age(diag_time,c.birth_date)) <"+i+" then '"+temp+"到"+i+"'\r\n";
  159. }
  160. temp = i;
  161. }
  162. }
  163. str += " when extract(year from age(diag_time,c.birth_date)) <="+max+" then '"+temp+"到"+max+"' end \r\n ";
  164. // System.out.println(str);
  165. return str;
  166. }
  167. public static String event_diagnose(Map<String,Map> temp1,Map groupMap){
  168. /*{"事件":{"diag_standard":"I21,I22,I23","event_type":"诊断"},"医院":{"org_code":"9983838x,111999441,2224589985,112445580"},
  169. * "就诊类型":{"visit_type":"门诊"},"时间":{"time_from":"绝对时间:2017-01-01","time_to":"绝对时间:2017-01-10"}}
  170. *
  171. * {\"事件\":{\"diag_standard\":[\"I220\",\"I230\"],\"diag_primitive\":[\"I220\",\"I230\"],\"event_type\":\"diagnosis\"},
  172. * \"医院\":{\"org_code\":[\"111111\",\"222222\"]},\"就诊类型\":{\"visit_type\":\"hospitalization\"},\"时间\":{\"time_from\":\"2018-7-2\",\"time_to\":\"2018-8-12\"},
  173. * \"关系\":\"or\"}
  174. *
  175. * */
  176. String resultSql = "";
  177. String condition = "";
  178. //String eventType = "";
  179. for(String str:temp1.keySet()){
  180. if("事件".equals(str)){ //"事件":{"diag_standard":"I21,I22,I23","event_type":"诊断"}
  181. Map temp = temp1.get(str);
  182. //if(temp.get("event_type").equals("diagnosis")){
  183. //eventType = (String)temp.get("event_type");
  184. //,diag_time,org_code,case when visit_type='O' then '门诊' when visit_type='I' then '住院' when is_emergency=true then '急诊' else '未知' end visit_type
  185. resultSql += " select * from(select a.patient_id";
  186. String groupType = groupMap.get("groupType")==null?"":(String)groupMap.get("groupType");
  187. if(StringUtils.isNotBlank(groupType)){//"{\"groupType\":\"医院\",\"interval\":\"\",\"maxValue\":\"\",\"minValue\":\"\"}";
  188. switch(groupType){
  189. case "年龄段":resultSql += ",coalesce(a.diag_time,b.visit_time) diag_time";break;
  190. case "年份":resultSql += ",coalesce(a.diag_time,b.visit_time) diag_time";break;
  191. case "医院":resultSql += ",a.org_code";break;
  192. case "就诊类型":resultSql += ",case when b.visit_type='O' then '门诊' when b.visit_type='I' then '住院' when is_emergency=true then '急诊' else '未知' end visit_type";break;
  193. case "性别":resultSql += "";break;
  194. }
  195. }
  196. resultSql += ",jsonb_array_elements(diag_sycode_set) "+
  197. "as sycode \r\n from diag.patient_diagnose a ";
  198. String time_from = temp1.get("时间")==null?"":(String)temp1.get("时间").get("time_from");
  199. String visit_type = temp1.get("就诊类型")==null?"":(String)temp1.get("就诊类型").get("visit_type");
  200. if(StringUtils.isNotBlank(time_from)||StringUtils.isNotBlank(visit_type)){
  201. resultSql += " left join visit.visit_record b on a.visit_id = b.visit_id ";
  202. }else{
  203. if(StringUtils.isNotBlank(groupType)){
  204. switch((String)groupMap.get("groupType")){
  205. case "年龄段":resultSql += " left join visit.visit_record b on a.visit_id = b.visit_id ";break;
  206. case "年份":resultSql += " left join visit.visit_record b on a.visit_id = b.visit_id ";break;
  207. case "就诊类型":resultSql += " left join visit.visit_record b on a.visit_id = b.visit_id ";break;
  208. //case "性别":resultSql += "";break;
  209. }
  210. }
  211. }
  212. resultSql += "where 1=1 $condition$ ) t1 \r\n ";
  213. List<String> diag_list = (List)temp.get("diag_standard");
  214. if(!CollectionUtils.isEmpty(diag_list)){
  215. resultSql += " join (select diag_code,diag_code2,diag_name from mdm.diagnose a where a.code_sys_id=30 \r\n ";
  216. if(diag_list.size()>1){
  217. for(int i = 0;i<diag_list.size();i++){
  218. String diag_c = diag_list.get(i);
  219. if(i==0){
  220. resultSql += " and ( diag_code2 like '"+diag_c+"%'";
  221. }else if(i==diag_list.size()-1){
  222. resultSql += " or diag_code2 like '"+diag_c+"%')";
  223. }else{
  224. resultSql += " or diag_code2 like '"+diag_c+"%'";
  225. }
  226. }
  227. }else{
  228. resultSql += " and diag_code2 like '"+diag_list.get(0)+"%'";
  229. }
  230. resultSql += " \r\n )t2 on replace(t1.sycode::varchar(50),'\"','') =t2.diag_code";
  231. }
  232. //}
  233. }else if("医院".equals(str)){ //"医院":{"org_code":"9983838x,111999441,2224589985,112445580"}
  234. Map temp = temp1.get(str);
  235. try{
  236. List<String> org_list = (List)temp.get("org_code");
  237. if(!CollectionUtils.isEmpty(org_list)){
  238. if(org_list.size()>1){
  239. for(int i = 0;i<org_list.size();i++){
  240. String org_code = org_list.get(i);
  241. if(i==0){
  242. condition += " and a.org_code in ('"+org_code+"',";
  243. }else if(i==org_list.size()-1){
  244. condition += "'"+org_code+"')";
  245. }else{
  246. condition += "'"+org_code+"',";
  247. }
  248. }
  249. }else{
  250. condition += " and a.org_code = '"+org_list.get(0)+"'";
  251. }
  252. }
  253. }catch(java.lang.ClassCastException e){}
  254. }else if("时间".equals(str)){//"时间":{"time_from":"绝对时间:2017-01-01","time_to":"绝对时间:2017-01-10","time_type":"诊断时间"}
  255. Map temp = temp1.get(str);
  256. String time_from = (String)temp.get("time_from");
  257. String time_to = (String)temp.get("time_to");
  258. condition += " \r\n ";
  259. if(StringUtils.isNoneBlank(time_from)&&StringUtils.isNoneBlank(time_to)){
  260. condition += " and coalesce(a.diag_time,b.visit_time) between '"+time_from +"' and '"+time_to+"'";
  261. }
  262. // switch(eventType){
  263. // case "诊断":condition+= "and coalesce(a.diag_time,b.visit_time) ";break;
  264. // case "检验时间":condition+= "and report_time ";break;
  265. // case "用药时间":condition+= "and drug_time ";break;
  266. // case "手术时间":condition+= "and oper_time ";break;
  267. // }
  268. }else if("就诊类型".equals(str)){//"就诊类型":{"visit_type":"门诊"}
  269. Map temp = temp1.get(str);
  270. String visit_type = (String)temp.get("visit_type");
  271. condition += " \r\n ";
  272. switch(visit_type){
  273. case "outpat":condition+= " and b.visit_type='O' ";break;
  274. case "inpat":condition+= " and b.visit_type='I' ";break;
  275. case "emergency":condition+= " and b.is_emergency=true ";break;
  276. }
  277. }
  278. }
  279. resultSql = resultSql.replace("$condition$", condition);
  280. return resultSql;
  281. }
  282. public void printJson(HttpServletResponse res,String str){
  283. res.setContentType("text/plain");
  284. res.setCharacterEncoding("utf-8");
  285. res.setHeader("Access-Control-Allow-Origin", "*");
  286. res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
  287. res.setHeader("Access-Control-Max-Age", "3600");
  288. res.setHeader("Access-Control-Allow-Headers", "x-requested-with");
  289. PrintWriter pw = null;
  290. try {
  291. pw = res.getWriter();
  292. pw.write(str);
  293. } catch (IOException e) {
  294. e.printStackTrace();
  295. }finally{
  296. pw.flush();
  297. pw.close();
  298. }
  299. }
  300. @RequestMapping("/getAllOrg")
  301. public String getAllOrg(HttpServletRequest request,Model model,HttpServletResponse res){
  302. Map map1 = new HashMap();
  303. map1.put("value", "488099744");
  304. map1.put("label", "福州一院");
  305. Map map2 = new HashMap();
  306. map2.put("value", "48809974x");
  307. map2.put("label", "福州儿院");
  308. Map map3 = new HashMap();
  309. map3.put("value", "488099743");
  310. map3.put("label", "福州中医院");
  311. Map map4 = new HashMap();
  312. map4.put("value", "488099757");
  313. map4.put("label", "福州传染病医院");
  314. List list = new ArrayList();
  315. list.add(map1);
  316. list.add(map2);
  317. list.add(map3);
  318. list.add(map4);
  319. String jsonString = JSONObject.toJSON(list).toString();
  320. res.setContentType("text/plain");
  321. res.setCharacterEncoding("utf-8");
  322. res.setHeader("Access-Control-Allow-Origin", "*");
  323. res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
  324. res.setHeader("Access-Control-Max-Age", "3600");
  325. res.setHeader("Access-Control-Allow-Headers", "x-requested-with");
  326. System.out.println(res);
  327. PrintWriter pw = null;
  328. try {
  329. pw = res.getWriter();
  330. pw.write(jsonString);
  331. } catch (IOException e) {
  332. e.printStackTrace();
  333. }finally{
  334. pw.flush();
  335. pw.close();
  336. }
  337. return null;
  338. }
  339. }