f647e09f81ddd734b6068a59acb69831084ad032.svn-base 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.List;
  4. import java.util.Map;
  5. import org.apache.commons.lang3.StringUtils;
  6. import org.springframework.util.CollectionUtils;
  7. import sun.org.mozilla.javascript.internal.ast.ReturnStatement;
  8. import com.alibaba.fastjson.JSON;
  9. import com.alibaba.fastjson.JSONObject;
  10. public class Test {
  11. public static void main(String args[]) {
  12. List list = new ArrayList();
  13. Map<String,Map> map = new HashMap();
  14. list.add(map);
  15. Map<String,String> map_event = new HashMap();
  16. map.put("事件", map_event);
  17. map_event.put("event_type", "诊断");
  18. map_event.put("diag_standard", "I21,H2,K8");
  19. // Map<String,String> map_time = new HashMap();
  20. // map.put("时间", map_time);
  21. // map_time.put("time_from", "绝对时间:2017-01-01");
  22. // map_time.put("time_to", "绝对时间:2017-01-10");
  23. // Map<String,String> map_hospital = new HashMap();
  24. // map.put("医院", map_hospital);
  25. // map_hospital.put("org_code", "9983838x,111999441,2224589985,112445580");
  26. //
  27. // Map<String,String> map_visitType = new HashMap();
  28. // map.put("就诊类型", map_visitType);
  29. // map_visitType.put("visit_type", "急诊");
  30. //
  31. Map map1 = new HashMap();
  32. map1.putAll(map);
  33. map1.put("关系", "and");
  34. list.add(map1);
  35. String jsonStr = "[{\"事件\":{\"diag_standard\":[\"I220\",\"I230\"],\"diag_primitive\":[\"I220\",\"I230\"],\"event_type\":\"diagnosis\"},\"医院\":{\"org_code\":[\"111111\",\"222222\"]},\"就诊类型\":{\"visit_type\":\"emergency\"},\"时间\":{\"time_from\":\"2018-7-2\",\"time_to\":\"2018-8-12\"},\"关系\":\"or\"},{\"事件\":{\"diag_standard\":[\"I220\",\"I230\"],\"diag_primitive\":[\"I220\",\"I230\"],\"event_type\":\"diagnosis\"},\"医院\":{\"org_code\":[\"111111\",\"222222\",\"333333\"]},\"就诊类型\":{\"visit_type\":\"emergency\"},\"时间\":{\"time_from\":\"2018-7-10\",\"time_to\":\"2018-8-7\"},\"关系\":\"or\"}]";
  36. String setpStr = "{\"groupType\":\"医院\",\"interval\":\"\",\"maxValue\":\"\",\"minValue\":\"\"}";
  37. String jsonString = JSONObject.toJSON(list).toString();
  38. // System.out.println(jsonString);
  39. // System.out.println(jsonString);
  40. Map<String,Map> temp1 = null; //入排条件1
  41. Map temp2 = null; //入排条件2
  42. List<Map> lis = (List)JSON.parse(jsonStr);
  43. if(!CollectionUtils.isEmpty(lis)){
  44. temp1 = lis.get(0);
  45. if(lis.size()>1){
  46. temp2 = lis.get(1);
  47. }
  48. }
  49. //System.out.println(temp2);
  50. // String returnSql = event_diagnose(temp1);
  51. // if(!CollectionUtils.isEmpty(temp2)){
  52. //
  53. // String rel = (String)temp2.get("关系");
  54. // if("or".equals(rel)){
  55. // returnSql += " \r\n union all \r\n "+event_diagnose(temp2);
  56. // }
  57. // }
  58. // String groupType = "年份";
  59. // String selType = "人数";
  60. // if("年份".equals(groupType)){
  61. // String selSql = "";
  62. // if("人数".equals(selType)){
  63. // selSql = "count(distinct patient_id)";
  64. // }else{
  65. // selSql = "count(1)";
  66. // }
  67. //
  68. // returnSql = " select to_char(diag_time,'yyyy') diag_year,"+selSql+" from ( \r\n "
  69. // +returnSql+" )t group by to_char(diag_time,'yyyy') order by diag_year";
  70. // }
  71. Map stepMap = (Map)JSON.parse(setpStr);;
  72. //System.out.println(stepMap);
  73. //stepMap.put("groupType","就诊类型");
  74. stepMap.put("queryType","patientsNumber");
  75. //System.out.println(event_diagnose(temp1,stepMap));
  76. //System.out.println(event_diagnose(temp1,groupMap));
  77. String groupType = stepMap.get("groupType")==null?"":(String)stepMap.get("groupType");
  78. String returnSql = "with temp1 as ("+event_diagnose(temp1,stepMap)+")";
  79. if(!CollectionUtils.isEmpty(temp2)){
  80. returnSql += ", \r\n temp2 as ("+event_diagnose(temp2,stepMap)+")";
  81. String rel = (String)temp2.get("关系");
  82. if("or".equals(rel)){
  83. returnSql += " \r\n select ";
  84. if(StringUtils.isNotBlank(groupType)){
  85. switch((String)stepMap.get("groupType")){
  86. case "年龄段":returnSql += generateAgePart(0,100,10)+" 年龄段,";break;
  87. case "年份":returnSql += "to_char(diag_time,'yyyy') 年份,";break;
  88. case "医院":returnSql += "org_code,";break;
  89. case "就诊类型":returnSql += "visit_type,";break;
  90. case "性别":returnSql += "case when c.sex_code in ('2','9') then '女' when c.sex_code ='1' then '男' else '未知' end 性别,";break;
  91. }
  92. }
  93. if("patientsNumber".equals((String)stepMap.get("queryType"))){
  94. returnSql += "count(distinct t.patient_id) 人数";
  95. }else{
  96. returnSql += "count(t.patient_id) 人次";
  97. }
  98. returnSql += " \r\n from (select * from temp1 union all select * from temp2 )t ";
  99. if("性别".equals((String)stepMap.get("groupType"))||"年龄段".equals((String)stepMap.get("groupType"))){
  100. returnSql += " join patient.patient_master_info c on t.patient_id=c.patient_id \r\n";
  101. }
  102. if(StringUtils.isNotBlank(groupType)){
  103. returnSql += " group by ";
  104. switch((String)stepMap.get("groupType")){
  105. case "年龄段":returnSql += generateAgePart(0,100,10);break;
  106. case "年份":returnSql += "to_char(diag_time,'yyyy') ";break;
  107. case "医院":returnSql += "org_code";break;
  108. case "就诊类型":returnSql += "visit_type";break;
  109. case "性别":returnSql += "case when c.sex_code in ('2','9') then '女' when c.sex_code ='1' then '男' else '未知' end";break;
  110. }
  111. }
  112. }else if("and".equals(rel)){
  113. 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";
  114. }
  115. }else{//只有temp1的情况
  116. returnSql += "\r\n select ";
  117. if(StringUtils.isNotBlank(groupType)){
  118. switch((String)stepMap.get("groupType")){
  119. case "年龄段":returnSql += generateAgePart(0,100,10)+" 年龄段,";break;
  120. case "年份":returnSql += "to_char(diag_time,'yyyy') 年份,";break;
  121. case "医院":returnSql += "org_code,";break;
  122. case "就诊类型":returnSql += "visit_type,";break;
  123. case "性别":returnSql += "case when c.sex_code in ('2','9') then '女' when c.sex_code ='1' then '男' else '未知' end 性别,\r\n";break;
  124. }
  125. }
  126. if("patientsNumber".equals((String)stepMap.get("queryType"))){
  127. returnSql += "count(distinct temp1.patient_id) 人数";
  128. }else{ //visitsNumber 人次
  129. returnSql += "count(temp1.patient_id) 人次";
  130. }
  131. returnSql += " from temp1 ";
  132. if("性别".equals((String)stepMap.get("groupType"))||"年龄段".equals((String)stepMap.get("groupType"))){
  133. returnSql += " join patient.patient_master_info c on temp1.patient_id=c.patient_id \r\n ";
  134. }
  135. if(StringUtils.isNotBlank(groupType)){
  136. returnSql += "group by ";
  137. switch((String)stepMap.get("groupType")){
  138. case "年龄段":returnSql += generateAgePart(0,100,10);break;
  139. case "年份":returnSql += "to_char(diag_time,'yyyy') ";break;
  140. case "医院":returnSql += "org_code";break;
  141. case "就诊类型":returnSql += "visit_type";break;
  142. case "性别":returnSql += "case when c.sex_code in ('2','9') then '女' when c.sex_code ='1' then '男' else '未知' end";break;
  143. }
  144. }
  145. }
  146. System.out.println(returnSql);
  147. //generateAgePart(0,100,10);
  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+"'\r\n";
  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+"'\r\n";
  162. }
  163. temp = i;
  164. }
  165. }
  166. str += " when extract(year from age(diag_time,c.birth_date)) <="+max+" then '"+temp+"到"+max+"' end \r\n ";
  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 \r\n 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 \r\n ";
  216. List<String> diag_list = (List)temp.get("diag_standard");
  217. if(!CollectionUtils.isEmpty(diag_list)){
  218. resultSql += " join (select diag_code,diag_code2,diag_name from mdm.diagnose a where a.code_sys_id=30 \r\n ";
  219. if(diag_list.size()>1){
  220. for(int i = 0;i<diag_list.size();i++){
  221. String diag_c = diag_list.get(i);
  222. if(i==0){
  223. resultSql += " and ( diag_code2 like '"+diag_c+"%'";
  224. }else if(i==diag_list.size()-1){
  225. resultSql += " or diag_code2 like '"+diag_c+"%')";
  226. }else{
  227. resultSql += " or diag_code2 like '"+diag_c+"%'";
  228. }
  229. }
  230. }else{
  231. resultSql += " and diag_code2 like '"+diag_list.get(0)+"%'";
  232. }
  233. resultSql += " \r\n )t2 on replace(t1.sycode::varchar(50),'\"','') =t2.diag_code";
  234. }
  235. //}
  236. }else if("医院".equals(str)){ //"医院":{"org_code":"9983838x,111999441,2224589985,112445580"}
  237. Map temp = temp1.get(str);
  238. List<String> org_list = (List)temp.get("org_code");
  239. if(!CollectionUtils.isEmpty(org_list)){
  240. if(org_list.size()>1){
  241. for(int i = 0;i<org_list.size();i++){
  242. String org_code = org_list.get(i);
  243. if(i==0){
  244. condition += " and a.org_code in ('"+org_code+"',";
  245. }else if(i==org_list.size()-1){
  246. condition += "'"+org_code+"')";
  247. }else{
  248. condition += "'"+org_code+"',";
  249. }
  250. }
  251. }else{
  252. condition += " and a.org_code = '"+org_list.get(0)+"'";
  253. }
  254. }
  255. }else if("时间".equals(str)){//"时间":{"time_from":"绝对时间:2017-01-01","time_to":"绝对时间:2017-01-10","time_type":"诊断时间"}
  256. Map temp = temp1.get(str);
  257. String time_from = (String)temp.get("time_from");
  258. String time_to = (String)temp.get("time_to");
  259. condition += " \r\n ";
  260. if(StringUtils.isNoneBlank(time_from)&&StringUtils.isNoneBlank(time_to)){
  261. condition += " and coalesce(a.diag_time,b.visit_time) between '"+time_from +"' and '"+time_to+"'";
  262. }
  263. // switch(eventType){
  264. // case "诊断":condition+= "and coalesce(a.diag_time,b.visit_time) ";break;
  265. // case "检验时间":condition+= "and report_time ";break;
  266. // case "用药时间":condition+= "and drug_time ";break;
  267. // case "手术时间":condition+= "and oper_time ";break;
  268. // }
  269. }else if("就诊类型".equals(str)){//"就诊类型":{"visit_type":"门诊"}
  270. Map temp = temp1.get(str);
  271. String visit_type = (String)temp.get("visit_type");
  272. condition += " \r\n ";
  273. switch(visit_type){
  274. case "outpat":condition+= " and b.visit_type='O' ";break;
  275. case "inpat":condition+= " and b.visit_type='I' ";break;
  276. case "emergency":condition+= " and b.is_emergency=true ";break;
  277. }
  278. }
  279. }
  280. resultSql = resultSql.replace("$condition$", condition);
  281. return resultSql;
  282. }
  283. }