f0df763af9191d09203f193699e8efacef64baf5.svn-base 28 KB

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