bfac73649594cb267445467a2992b557557e791b.svn-base 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. package com.synyi.edc.util;
  2. import java.util.List;
  3. import java.util.Map;
  4. import org.apache.commons.lang3.StringUtils;
  5. import org.springframework.util.CollectionUtils;
  6. import com.alibaba.fastjson.JSON;
  7. public class SqlBuilderUtils {
  8. /**
  9. * sql生成分组方法
  10. * @param jsonStr
  11. * @param stepStr
  12. * @return
  13. */
  14. public static String genGroupSql(String jsonStr,String stepStr){
  15. Map<String,Map> temp1 = null; //入排条件1
  16. Map temp2 = null; //入排条件2
  17. List<Map> lis = (List)JSON.parse(jsonStr);
  18. if(!CollectionUtils.isEmpty(lis)){
  19. temp1 = lis.get(0);
  20. if(lis.size()>1){
  21. temp2 = lis.get(1);
  22. }
  23. }
  24. System.out.println(stepStr);
  25. Map stepMap = (Map)JSON.parse(stepStr);;
  26. int maxValue = 0;
  27. int minValue = 0;
  28. int interval = 0;
  29. try{
  30. if(StringUtils.isNotBlank((String)stepMap.get("maxValue"))){
  31. maxValue = Integer.parseInt((String)stepMap.get("maxValue"));
  32. }
  33. if(StringUtils.isNotBlank((String)stepMap.get("minValue"))){
  34. minValue = Integer.parseInt((String)stepMap.get("minValue"));
  35. }
  36. if(StringUtils.isNotBlank((String)stepMap.get("interval"))){
  37. interval = Integer.parseInt((String)stepMap.get("interval"));
  38. }
  39. }catch(Exception e){}
  40. String groupType = stepMap.get("groupType")==null?"":(String)stepMap.get("groupType");
  41. String event1 = (String)temp1.get("事件").get("event_type");
  42. String event2 = temp2!=null?(String)((Map)temp2.get("事件")).get("event_type"):"";
  43. String returnSql = "";
  44. switch(event1){
  45. case "diagnosis":returnSql = "with temp1 as ("+event_diagnose(temp1,stepMap)+")";break;
  46. case "laboratoryExamination":returnSql = "with temp1 as ("+event_lab(temp1,stepMap)+")";break;
  47. case "drugUse":returnSql = "with temp1 as ("+event_drug(temp1,stepMap)+")";break;
  48. case "operation":returnSql = "with temp1 as ("+event_lab(temp1,stepMap)+")";break;
  49. }
  50. if(!CollectionUtils.isEmpty(temp2)){
  51. switch(event2){
  52. case "diagnosis":returnSql += ", \r\n temp2 as ("+event_diagnose(temp2,stepMap)+")";break;
  53. case "laboratoryExamination":returnSql += ", \r\n temp2 as ("+event_lab(temp2,stepMap)+")";break;
  54. case "drugUse":returnSql += ", \r\n temp2 as ("+event_drug(temp2,stepMap)+")";break;
  55. case "operation":returnSql += ", \r\n temp2 as ("+event_lab(temp2,stepMap)+")";break;
  56. }
  57. String rel = (String)temp2.get("关系");
  58. if("or".equals(rel)){
  59. returnSql += " <br/> select ";
  60. if(StringUtils.isNotBlank(groupType)){
  61. switch((String)stepMap.get("groupType")){
  62. case "年龄段":returnSql += generateAgePart(minValue,maxValue,interval)+" 年龄段,";break;
  63. case "年份":returnSql += "to_char(diag_time,'yyyy') 年份,";break;
  64. case "医院":returnSql += "t.org_code,org.org_name,";break;
  65. case "就诊类型":returnSql += "visit_type,";break;
  66. case "性别":returnSql += "case when c.sex_code in ('2','9') then '女' when c.sex_code ='1' then '男' else '未知' end 性别,";break;
  67. }
  68. }
  69. if("patientsNumber".equals((String)stepMap.get("queryType"))){
  70. returnSql += "count(distinct t.patient_id) 人数";
  71. }else{
  72. returnSql += "count(t.patient_id) 人次";
  73. }
  74. returnSql += " <br/> from (select * from temp1 union all select * from temp2 )t ";
  75. if("性别".equals((String)stepMap.get("groupType"))||"年龄段".equals((String)stepMap.get("groupType"))){
  76. returnSql += " join patient.patient_master_info c on t.patient_id=c.patient_id <br/>";
  77. }
  78. if("医院".equals((String)stepMap.get("groupType"))){
  79. returnSql += " join mdm.organization org on t.org_code=org.org_code <br/>";
  80. }
  81. if(StringUtils.isNotBlank(groupType)){
  82. returnSql += " group by ";
  83. switch((String)stepMap.get("groupType")){
  84. case "年龄段":returnSql += generateAgePart(minValue,maxValue,interval);break;
  85. case "年份":returnSql += "to_char(diag_time,'yyyy') ";break;
  86. case "医院":returnSql += "t.org_code,org.org_name";break;
  87. case "就诊类型":returnSql += "visit_type";break;
  88. case "性别":returnSql += "case when c.sex_code in ('2','9') then '女' when c.sex_code ='1' then '男' else '未知' end";break;
  89. }
  90. }
  91. }else if("and".equals(rel)){
  92. 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";
  93. }
  94. }else{//只有temp1的情况
  95. returnSql += "<br/> select ";
  96. if(StringUtils.isNotBlank(groupType)){
  97. switch((String)stepMap.get("groupType")){
  98. case "年龄段":returnSql += generateAgePart(minValue,maxValue,interval)+" 年龄段,";break;
  99. case "年份":returnSql += "to_char(diag_time,'yyyy') 年份,";break;
  100. case "医院":returnSql += "temp1.org_code,org.org_name,";break;
  101. case "就诊类型":returnSql += "visit_type,";break;
  102. case "性别":returnSql += "case when c.sex_code in ('2','9') then '女' when c.sex_code ='1' then '男' else '未知' end 性别,<br/>";break;
  103. }
  104. }
  105. if("patientsNumber".equals((String)stepMap.get("queryType"))){
  106. returnSql += "count(distinct temp1.patient_id) 人数";
  107. }else{ //visitsNumber 人次
  108. returnSql += "count(temp1.patient_id) 人次";
  109. }
  110. returnSql += " from temp1 ";
  111. if("性别".equals((String)stepMap.get("groupType"))||"年龄段".equals((String)stepMap.get("groupType"))){
  112. returnSql += " join patient.patient_master_info c on temp1.patient_id=c.patient_id <br/> ";
  113. }
  114. if("医院".equals((String)stepMap.get("groupType"))){
  115. returnSql += " join mdm.organization org on temp1.org_code=org.org_code <br/>";
  116. }
  117. if(StringUtils.isNotBlank(groupType)){
  118. returnSql += "group by ";
  119. switch((String)stepMap.get("groupType")){
  120. case "年龄段":returnSql += generateAgePart(minValue,maxValue,interval);break;
  121. case "年份":returnSql += "to_char(diag_time,'yyyy') ";break;
  122. case "医院":returnSql += "temp1.org_code,org.org_name";break;
  123. case "就诊类型":returnSql += "visit_type";break;
  124. case "性别":returnSql += "case when c.sex_code in ('2','9') then '女' when c.sex_code ='1' then '男' else '未知' end";break;
  125. }
  126. }
  127. }
  128. return returnSql;
  129. }
  130. /**
  131. * 按照年龄段分组方法
  132. * @param min 最小年龄
  133. * @param max 最大年龄
  134. * @param interval 年龄间隔
  135. * @return
  136. */
  137. public static String generateAgePart(int min,int max,int interval){
  138. String str = " case";
  139. int temp = min;
  140. for(int i=min;i<max;i+=interval){
  141. if(i>min){
  142. //System.out.println(temp+":"+i);
  143. if(temp==min){
  144. 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/>";
  145. //
  146. }else if(i==max){
  147. // str += " when extract(year from age(temp1.diag_time,c.birth_date)) <="+max+" then '"+temp+"到"+max+"' end ";
  148. }else{
  149. str += " when extract(year from age(diag_time,c.birth_date)) <"+i+" then '"+temp+"到"+i+"'<br/>";
  150. }
  151. temp = i;
  152. }
  153. }
  154. str += " when extract(year from age(diag_time,c.birth_date)) <="+max+" then '"+temp+"到"+max+"' end <br/> ";
  155. // System.out.println(str);
  156. return str;
  157. }
  158. /**
  159. * 按照事件 为 诊断 的sql语句生成方法
  160. * @param temp1
  161. * @param groupMap
  162. * @return
  163. */
  164. public static String event_diagnose(Map<String,Map> temp1,Map groupMap){
  165. /*{"事件":{"diag_standard":"I21,I22,I23","event_type":"诊断"},"医院":{"org_code":"9983838x,111999441,2224589985,112445580"},
  166. * "就诊类型":{"visit_type":"门诊"},"时间":{"time_from":"绝对时间:2017-01-01","time_to":"绝对时间:2017-01-10"}}
  167. *
  168. * {\"事件\":{\"diag_standard\":[\"I220\",\"I230\"],\"diag_primitive\":[\"I220\",\"I230\"],\"event_type\":\"diagnosis\"},
  169. * \"医院\":{\"org_code\":[\"111111\",\"222222\"]},\"就诊类型\":{\"visit_type\":\"hospitalization\"},\"时间\":{\"time_from\":\"2018-7-2\",\"time_to\":\"2018-8-12\"},
  170. * \"关系\":\"or\"}
  171. *
  172. * */
  173. String resultSql = "";
  174. String condition = "";
  175. //String eventType = "";
  176. for(String str:temp1.keySet()){
  177. if("事件".equals(str)){ //"事件":{"diag_standard":"I21,I22,I23","event_type":"诊断"}
  178. Map temp = temp1.get(str);
  179. //if(temp.get("event_type").equals("diagnosis")){
  180. //eventType = (String)temp.get("event_type");
  181. //,diag_time,org_code,case when visit_type='O' then '门诊' when visit_type='I' then '住院' when is_emergency=true then '急诊' else '未知' end visit_type
  182. resultSql += " select * from(select a.patient_id";
  183. String groupType = groupMap.get("groupType")==null?"":(String)groupMap.get("groupType");
  184. if(StringUtils.isNotBlank(groupType)){//"{\"groupType\":\"医院\",\"interval\":\"\",\"maxValue\":\"\",\"minValue\":\"\"}";
  185. switch(groupType){
  186. case "年龄段":resultSql += ",coalesce(a.diag_time,b.visit_time) diag_time";break;
  187. case "年份":resultSql += ",coalesce(a.diag_time,b.visit_time) diag_time";break;
  188. case "医院":resultSql += ",a.org_code";break;
  189. 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;
  190. case "性别":resultSql += "";break;
  191. }
  192. }
  193. resultSql += ",jsonb_array_elements(diag_sycode_set) "+
  194. "as sycode <br/> from diag.patient_diagnose a ";
  195. String time_from = temp1.get("时间")==null?"":(String)temp1.get("时间").get("time_from");
  196. String visit_type = temp1.get("就诊类型")==null?"":(String)temp1.get("就诊类型").get("visit_type");
  197. if(StringUtils.isNotBlank(time_from)||StringUtils.isNotBlank(visit_type)){
  198. resultSql += " left join visit.visit_record b on a.visit_id = b.visit_id ";
  199. }else{
  200. if(StringUtils.isNotBlank(groupType)){
  201. switch((String)groupMap.get("groupType")){
  202. case "年龄段":resultSql += " left join visit.visit_record b on a.visit_id = b.visit_id ";break;
  203. case "年份":resultSql += " left join visit.visit_record b on a.visit_id = b.visit_id ";break;
  204. case "就诊类型":resultSql += " left join visit.visit_record b on a.visit_id = b.visit_id ";break;
  205. //case "性别":resultSql += "";break;
  206. }
  207. }
  208. }
  209. resultSql += "where 1=1 $condition$ ) t1 <br/> ";
  210. List<String> diag_list = (List)temp.get("diag_standard");
  211. List<String> diag_primitive = (List)temp.get("diag_primitive");
  212. if(diag_list!=null&&diag_list.size()>0){
  213. resultSql += " join (select diag_code,diag_code2,diag_name from mdm.diagnose a where a.code_sys_id=30 <br/> ";
  214. if(diag_list.size()>1){
  215. for(int i = 0;i<diag_list.size();i++){
  216. String diag_c = diag_list.get(i);
  217. if(i==0){
  218. resultSql += " and ( diag_code2 like '"+diag_c+"%'";
  219. }else if(i==diag_list.size()-1){
  220. resultSql += " or diag_code2 like '"+diag_c+"%')";
  221. }else{
  222. resultSql += " or diag_code2 like '"+diag_c+"%'";
  223. }
  224. }
  225. }else{
  226. resultSql += " and diag_code2 like '"+diag_list.get(0)+"%'";
  227. }
  228. resultSql += " <br/> )t2 on replace(t1.sycode::varchar(50),'\"','') =t2.diag_code";
  229. }else if(diag_primitive!=null&&diag_primitive.size()>0){
  230. resultSql += " join (select diag_code,diag_code2,diag_name from mdm.diagnose a where a.code_sys_id=30 <br/> ";
  231. if(diag_primitive.size()>1){
  232. for(int i = 0;i<diag_primitive.size();i++){
  233. String diag_c = diag_primitive.get(i);
  234. if(i==0){
  235. resultSql += " and ( diag_name like '%"+diag_c+"%'";
  236. }else if(i==diag_primitive.size()-1){
  237. resultSql += " or diag_name like '%"+diag_c+"%')";
  238. }else{
  239. resultSql += " or diag_name like '%"+diag_c+"%'";
  240. }
  241. }
  242. }else{
  243. resultSql += " and diag_name like '%"+diag_primitive.get(0)+"%'";
  244. }
  245. resultSql += " <br/> )t2 on replace(t1.sycode::varchar(50),'\"','') =t2.diag_code";
  246. }
  247. //}
  248. }else if("医院".equals(str)){ //"医院":{"org_code":"9983838x,111999441,2224589985,112445580"}
  249. Map temp = temp1.get(str);
  250. try{
  251. List<String> org_list = (List)temp.get("org_code");
  252. if(!CollectionUtils.isEmpty(org_list)){
  253. if(org_list.size()>1){
  254. for(int i = 0;i<org_list.size();i++){
  255. String org_code = org_list.get(i);
  256. if(i==0){
  257. condition += " and a.org_code in ('"+org_code+"',";
  258. }else if(i==org_list.size()-1){
  259. condition += "'"+org_code+"')";
  260. }else{
  261. condition += "'"+org_code+"',";
  262. }
  263. }
  264. }else{
  265. condition += " and a.org_code = '"+org_list.get(0)+"'";
  266. }
  267. }
  268. }catch(java.lang.ClassCastException e){}
  269. }else if("时间".equals(str)){//"时间":{"time_from":"绝对时间:2017-01-01","time_to":"绝对时间:2017-01-10","time_type":"诊断时间"}
  270. Map temp = temp1.get(str);
  271. String time_from = (String)temp.get("time_from");
  272. String time_to = (String)temp.get("time_to");
  273. condition += " <br/> ";
  274. if(StringUtils.isNoneBlank(time_from)&&StringUtils.isNoneBlank(time_to)){
  275. condition += " and coalesce(a.diag_time,b.visit_time) between '"+time_from +"' and '"+time_to+"'";
  276. }
  277. // switch(eventType){
  278. // case "诊断":condition+= "and coalesce(a.diag_time,b.visit_time) ";break;
  279. // case "检验时间":condition+= "and report_time ";break;
  280. // case "用药时间":condition+= "and drug_time ";break;
  281. // case "手术时间":condition+= "and oper_time ";break;
  282. // }
  283. }else if("就诊类型".equals(str)){//"就诊类型":{"visit_type":"门诊"}
  284. Map temp = temp1.get(str);
  285. String visit_type = (String)temp.get("visit_type");
  286. condition += " <br/> ";
  287. switch(visit_type){
  288. case "outpat":condition+= " and b.visit_type='O' ";break;
  289. case "inpat":condition+= " and b.visit_type='I' ";break;
  290. case "emergency":condition+= " and b.is_emergency=true ";break;
  291. }
  292. }
  293. }
  294. resultSql = resultSql.replace("$condition$", condition);
  295. return resultSql;
  296. }
  297. public static String event_lab(Map<String,Map> temp1,Map groupMap){
  298. /*{"事件":{"diag_standard":"I21,I22,I23","event_type":"诊断"},"医院":{"org_code":"9983838x,111999441,2224589985,112445580"},
  299. * "就诊类型":{"visit_type":"门诊"},"时间":{"time_from":"绝对时间:2017-01-01","time_to":"绝对时间:2017-01-10"}}
  300. *
  301. * {\"事件\":{\"diag_standard\":[\"I220\",\"I230\"],\"diag_primitive\":[\"I220\",\"I230\"],\"event_type\":\"diagnosis\"},
  302. * \"医院\":{\"org_code\":[\"111111\",\"222222\"]},\"就诊类型\":{\"visit_type\":\"hospitalization\"},\"时间\":{\"time_from\":\"2018-7-2\",\"time_to\":\"2018-8-12\"},
  303. * \"关系\":\"or\"}
  304. *
  305. * */
  306. String resultSql = "";
  307. String condition = "";
  308. //String eventType = "";
  309. for(String str:temp1.keySet()){
  310. if("事件".equals(str)){ //"事件":{"diag_standard":"I21,I22,I23","event_type":"诊断"}
  311. Map temp = temp1.get(str);
  312. //if(temp.get("event_type").equals("diagnosis")){
  313. //eventType = (String)temp.get("event_type");
  314. //,diag_time,org_code,case when visit_type='O' then '门诊' when visit_type='I' then '住院' when is_emergency=true then '急诊' else '未知' end visit_type
  315. resultSql += " select d.patient_id";
  316. String groupType = groupMap.get("groupType")==null?"":(String)groupMap.get("groupType");
  317. if(StringUtils.isNotBlank(groupType)){//"{\"groupType\":\"医院\",\"interval\":\"\",\"maxValue\":\"\",\"minValue\":\"\"}";
  318. switch(groupType){
  319. case "年龄段":resultSql += ",d.report_time event_time";break;
  320. case "年份":resultSql += ",d.report_time event_time";break;
  321. case "医院":resultSql += ",d.org_code";break;
  322. case "就诊类型":resultSql += ",case when e.visit_type='O' then '门诊' when e.visit_type='I' then '住院' when is_emergency=true then '急诊' else '未知' end visit_type";break;
  323. case "性别":resultSql += "";break;
  324. }
  325. }
  326. resultSql += " from lab.lab_report_result a join mdm.mdm_map b on a.test_item_id=b.source_id"+
  327. " \r\n join mdm.lis_item c on b.map_id=c.item_id"+
  328. " join lab.lab_report d on a.report_id=d.report_id";
  329. String visit_type = temp1.get("就诊类型")==null?"":(String)temp1.get("就诊类型").get("visit_type");
  330. if(StringUtils.isNotBlank(visit_type)){
  331. resultSql += " left join visit.visit_record e \r\n on d.visit_id = e.visit_id ";
  332. }
  333. resultSql += "where b.md_type = 'lis_item' and c.code_sys_id =197 $condition$ \r\n ";
  334. //{\"lab_standard\":[\"220\",\"230\"],\"character_result\":[\"偏高\",\"异常\"],\"exception_symbol\":[\"1\",\"2\"],\"number_result\":[\">=1\"],\"event_type\":\"lab\"}
  335. List<String> lab_list = (List)temp.get("lab_standard");
  336. List<String> character_result = (List)temp.get("character_result");
  337. List<String> exception_symbol = (List)temp.get("exception_symbol");
  338. List<String> number_result = (List)temp.get("number_result");
  339. if(lab_list!=null&&lab_list.size()>0){
  340. resultSql += " and c.item_id in (";
  341. String tempLab = "";
  342. for(String lab:lab_list){
  343. tempLab += "'"+lab+"',";
  344. }
  345. resultSql += tempLab.substring(0,tempLab.length()-1)+")";
  346. }
  347. if(character_result!=null&&character_result.size()>0){
  348. resultSql += " and a.text_value in (";
  349. String tempLab = "";
  350. for(String lab:character_result){
  351. tempLab += "'"+lab+"',";
  352. }
  353. resultSql += tempLab.substring(0,tempLab.length()-1)+")";
  354. }
  355. if(exception_symbol!=null&&exception_symbol.size()>0){
  356. resultSql += " and a.abnormal_flag_name in (";
  357. String tempLab = "";
  358. for(String lab:exception_symbol){
  359. tempLab += "'"+lab+"',";
  360. }
  361. resultSql += tempLab.substring(0,tempLab.length()-1)+")";
  362. }
  363. if(number_result!=null&&number_result.size()>0){
  364. resultSql += " and a.numerical_value "+number_result.get(0);
  365. }
  366. //}
  367. }else if("医院".equals(str)){ //"医院":{"org_code":"9983838x,111999441,2224589985,112445580"}
  368. Map temp = temp1.get(str);
  369. try{
  370. List<String> org_list = (List)temp.get("org_code");
  371. if(!CollectionUtils.isEmpty(org_list)){
  372. if(org_list.size()>1){
  373. for(int i = 0;i<org_list.size();i++){
  374. String org_code = org_list.get(i);
  375. if(i==0){
  376. condition += " and d.org_code in ('"+org_code+"',";
  377. }else if(i==org_list.size()-1){
  378. condition += "'"+org_code+"')";
  379. }else{
  380. condition += "'"+org_code+"',";
  381. }
  382. }
  383. }else{
  384. condition += " and d.org_code = '"+org_list.get(0)+"'";
  385. }
  386. }
  387. }catch(java.lang.ClassCastException e){}
  388. }else if("时间".equals(str)){//"时间":{"time_from":"绝对时间:2017-01-01","time_to":"绝对时间:2017-01-10","time_type":"诊断时间"}
  389. Map temp = temp1.get(str);
  390. String time_from = (String)temp.get("time_from");
  391. String time_to = (String)temp.get("time_to");
  392. condition += " \r\n ";
  393. if(StringUtils.isNoneBlank(time_from)&&StringUtils.isNoneBlank(time_to)){
  394. condition += " and d.report_time between '"+time_from +"' and '"+time_to+"'";
  395. }
  396. // switch(eventType){
  397. // case "诊断":condition+= "and coalesce(a.diag_time,b.visit_time) ";break;
  398. // case "检验时间":condition+= "and report_time ";break;
  399. // case "用药时间":condition+= "and drug_time ";break;
  400. // case "手术时间":condition+= "and oper_time ";break;
  401. // }
  402. }else if("就诊类型".equals(str)){//"就诊类型":{"visit_type":"门诊"}
  403. Map temp = temp1.get(str);
  404. String visit_type = (String)temp.get("visit_type");
  405. condition += " \r\n ";
  406. switch(visit_type){
  407. case "outpat":condition+= " and e.visit_type='O' ";break;
  408. case "inpat":condition+= " and e.visit_type='I' ";break;
  409. case "emergency":condition+= " and e.is_emergency=true ";break;
  410. }
  411. }
  412. }
  413. resultSql = resultSql.replace("$condition$", condition);
  414. return resultSql;
  415. }
  416. public static String event_drug(Map<String,Map> temp1,Map groupMap){
  417. /*{\"事件\":{\"drug_standard\":[\"220\",\"230\"],\"drug_category\":[\"330\",\"543\"],\"event_type\":\"drug\"},\"医院\":{\"org_code\":[\"111111\",\"222222\"]},
  418. * \"就诊类型\":{\"visit_type\":\"inpat\"},\"时间\":{\"time_from\":\"2018-7-2\",\"time_to\":\"2018-8-12\"},\"关系\":\"or\"},
  419. *
  420. * */
  421. String resultSql1 = "";
  422. String resultSql2 = "";
  423. String condition1 = "";
  424. String condition2 = "";
  425. //String eventType = "";
  426. for(String str:temp1.keySet()){
  427. if("事件".equals(str)){ //"事件":{"diag_standard":"I21,I22,I23","event_type":"诊断"}
  428. Map temp = temp1.get(str);
  429. //if(temp.get("event_type").equals("diagnosis")){
  430. //eventType = (String)temp.get("event_type");
  431. //,diag_time,org_code,case when visit_type='O' then '门诊' when visit_type='I' then '住院' when is_emergency=true then '急诊' else '未知' end visit_type
  432. resultSql1 += " select t1.patient_id";
  433. resultSql2 += " select t1.patient_id";
  434. String groupType = groupMap.get("groupType")==null?"":(String)groupMap.get("groupType");
  435. if(StringUtils.isNotBlank(groupType)){//"{\"groupType\":\"医院\",\"interval\":\"\",\"maxValue\":\"\",\"minValue\":\"\"}";
  436. switch(groupType){
  437. case "年龄段":resultSql1 += ",t1.begin_time event_time";resultSql2 += " , t4.recipe_time event_time";break;
  438. case "年份":resultSql1 += ",t1.begin_time event_time";resultSql2 += ",t4.recipe_time event_time";break;
  439. case "医院":resultSql1 += ",t1.org_code";resultSql2 += ",t1.org_code";break;
  440. case "就诊类型":resultSql1 += ",'住院' visit_type";resultSql2 += ",'门诊' visit_type";break;
  441. case "性别":resultSql1 += "";break;
  442. }
  443. }
  444. List<String> drug_category = (List)temp.get("drug_category");
  445. resultSql1 += " FROM orders.inpat_drug_order t1 join mdm.mdm_map t2 on t1.drug_id=t2.source_id "+
  446. " \r\n join mdm.drug t3 on t2.map_id=t3.drug_id";
  447. resultSql2 += " FROM orders.outpat_recipe_detail t1 join mdm.mdm_map t2 on t1.drug_id=t2.source_id "+
  448. " \r\n join mdm.drug t3 on t2.map_id=t3.drug_id join orders.outpat_recipe t4 on t1.recipe_id=t4.recipe_id ";
  449. if(drug_category!=null&&drug_category.size()>0){
  450. String tempLab = " in (";
  451. for(String cat:drug_category){
  452. tempLab += "'"+cat+"',";
  453. }
  454. tempLab = tempLab.substring(0,tempLab.length()-1)+")";
  455. resultSql1 +=" join ( \r\n with RECURSIVE temp1 as ( select id from drug_new where id "+tempLab+" union ALL "+
  456. " select d.id from drug_new d ,temp1 where d.parent=temp1.id )select * from temp1) ca \r\n on t3.drug_code=ca.id";
  457. resultSql2 +=" join ( \r\n with RECURSIVE temp1 as ( select id from drug_new where id "+tempLab+" union ALL "+
  458. " select d.id from drug_new d ,temp1 where d.parent=temp1.id )select * from temp1) ca \r\n on t3.drug_code=ca.id";
  459. }
  460. resultSql1 += " where t2.md_type='drug' and t3.code_sys_id = 51 $condition$ \r\n ";
  461. resultSql2 += " where t2.md_type='drug' and t3.code_sys_id = 51 $condition$ \r\n ";
  462. //{\"lab_standard\":[\"220\",\"230\"],\"character_result\":[\"偏高\",\"异常\"],\"exception_symbol\":[\"1\",\"2\"],\"number_result\":[\">=1\"],\"event_type\":\"lab\"}
  463. List<String> lab_list = (List)temp.get("drug_standard");
  464. if(lab_list!=null&&lab_list.size()>0){
  465. resultSql1 += " and t3.drug_id in (";
  466. resultSql2 += " and t3.drug_id in (";
  467. String tempLab = "";
  468. for(String lab:lab_list){
  469. tempLab += "'"+lab+"',";
  470. }
  471. resultSql1 += tempLab.substring(0,tempLab.length()-1)+")";
  472. resultSql2 += tempLab.substring(0,tempLab.length()-1)+")";
  473. }
  474. //}
  475. }else if("医院".equals(str)){ //"医院":{"org_code":"9983838x,111999441,2224589985,112445580"}
  476. Map temp = temp1.get(str);
  477. try{
  478. List<String> org_list = (List)temp.get("org_code");
  479. if(!CollectionUtils.isEmpty(org_list)){
  480. if(org_list.size()>1){
  481. for(int i = 0;i<org_list.size();i++){
  482. String org_code = org_list.get(i);
  483. if(i==0){
  484. condition1 += " and t1.org_code in ('"+org_code+"',";
  485. condition2 += " and t1.org_code in ('"+org_code+"',";
  486. }else if(i==org_list.size()-1){
  487. condition1 += "'"+org_code+"')";
  488. condition2 += "'"+org_code+"')";
  489. }else{
  490. condition1 += "'"+org_code+"',";
  491. condition2 += "'"+org_code+"',";
  492. }
  493. }
  494. }else{
  495. condition1 += " and t1.org_code = '"+org_list.get(0)+"'";
  496. condition2 += " and t1.org_code = '"+org_list.get(0)+"'";
  497. }
  498. }
  499. }catch(java.lang.ClassCastException e){}
  500. }else if("时间".equals(str)){//"时间":{"time_from":"绝对时间:2017-01-01","time_to":"绝对时间:2017-01-10","time_type":"诊断时间"}
  501. Map temp = temp1.get(str);
  502. String time_from = (String)temp.get("time_from");
  503. String time_to = (String)temp.get("time_to");
  504. if(StringUtils.isNoneBlank(time_from)&&StringUtils.isNoneBlank(time_to)){
  505. condition1 += " and t1.begin_time between '"+time_from +"' and '"+time_to+"'";
  506. condition2 += " and t4.recipe_time between '"+time_from +"' and '"+time_to+"'";
  507. }
  508. // switch(eventType){
  509. // case "诊断":condition+= "and coalesce(a.diag_time,b.visit_time) ";break;
  510. // case "检验时间":condition+= "and report_time ";break;
  511. // case "用药时间":condition+= "and drug_time ";break;
  512. // case "手术时间":condition+= "and oper_time ";break;
  513. // }
  514. }
  515. }
  516. resultSql1 = resultSql1.replace("$condition$", condition1);
  517. resultSql2 = resultSql2.replace("$condition$", condition2);
  518. String returnSql = "";
  519. Map temp = temp1.get("就诊类型");
  520. String visit_type = (String)temp.get("visit_type");
  521. if("inpat".equals(visit_type)){
  522. returnSql = resultSql1;
  523. }else if("outpat".equals(visit_type)){
  524. returnSql+= resultSql2;
  525. }else{
  526. returnSql+= resultSql1 +" union all "+ resultSql2;
  527. }
  528. return returnSql;
  529. }
  530. }