b26f35c9c9746b110c9465b4d5ee37119b4f9965.svn-base 17 KB

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