ecc99ff13cca88cf9c0f8ab9467336d5216717d4.svn-base 16 KB

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