package com.yihu.ehr.thread; import com.yihu.ehr.config.SysConfig; import com.yihu.ehr.config.ThreadConfig; import com.yihu.ehr.dbhelper.jdbc.DBHelper; import com.yihu.ehr.util.HttpsClientUtil; import com.yihu.ehr.util.log.LogUtil; import net.sf.json.JSONObject; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import org.springframework.util.StringUtils; import java.util.ArrayList; import java.util.List; import java.util.UUID; /** * Created by chenweida on 2016/2/27. */ public class SQLThread implements Runnable { private String orgCode = ""; private String systemCode = ""; private int sleepTime = 60 * 1000;//睡眠时间 private String fillMiningResponString;//补采的返回数据 private String hisResponString;//his的返回数据 private DBHelper db = new DBHelper(); private DBHelper localdb = null; private String token; private boolean flag = true; @Override public void run() { while (ThreadManage.sqlIsRunning) { try { token = HttpsClientUtil.getToken(); LogUtil.info("-----------sql线程开始启动------------token:" + token); if (!StringUtils.isEmpty(token)) { //初始化参数 initParam(); //执行补采操作 LogUtil.info("-----------开始执行补采任务判断------------"); fillMining(); LogUtil.info("-----------结束执行补采任务判断------------"); //执行his穿透 LogUtil.info("-----------开始执行his穿透任务判断------------"); hisPenetration(); LogUtil.info("-----------结束执行his穿透任务判断------------"); //休眠 } } catch (Exception e) { LogUtil.info("-----------sql线程执行失败-----------:" + e.getMessage()); } finally { try { sleep(); } catch (Exception e) { e.printStackTrace(); } } } } private void initParam() { if (StringUtils.isEmpty(orgCode) || StringUtils.isEmpty(systemCode)) { try { List listORG = db.query("select * from system_param where param_key='ORG_CODE'"); List listSYSTEM = db.query("select * from system_param where param_key='SYSTEM_CODE'"); if (listORG != null && listORG.size() > 0) { orgCode = listORG.get(0).getString("param_value"); } if (listSYSTEM != null && listSYSTEM.size() > 0) { systemCode = listSYSTEM.get(0).getString("param_value"); } else { String sql = "insert into system_param (id,param_key,param_value) values " + "('" + UUID.randomUUID() + "'," + " 'SYSTEM_CODE' ," + " '" + ThreadConfig.SYSTEM_CODE + "'" + ")"; db.execute(sql); } } catch (Exception e) { LogUtil.error("初始化参数失败:" + e.getMessage()); } } LogUtil.info("初始化参数成功orgCode:" + orgCode + "---systemCode:" + systemCode); } /** * his穿透 */ private void hisPenetration() { //判断是否有his穿透的SQL if (hasHisPenetration()) { //判断jsonObject是不是有效的 if (!StringUtils.isEmpty(hisResponString)) { JSONObject jo = JSONObject.fromObject(hisResponString); try { //得到id String id = (String) jo.get("id"); //得到sql String sql = (String) jo.get("sql"); //执行sql返回结果 String resultObj = excuteSQL(sql.replace("[", "").replace("]", "")); //保存结果的到总支撑 save(id, resultObj); } catch (Exception e) { LogUtil.error("hql穿透返回结果解析失败:" + jo.toString()); } } } } /** * 保存结果的到总支撑 * * @param id * @param resultObj */ private void save(String id, String resultObj) { try { // Map params = new HashMap(); // params.put("id", id); //params.put("result", resultObj); //params.put("status", 1); // HttpClientUtil.doPost(ThreadConfig.getURL(ThreadConfig.SQL_THREAD_CHANGEHISPENETRATIONSTATUS), params, ThreadConfig.SERVICE_USERNAME, ThreadConfig.SERVICE_PASSWORD); List formParams = new ArrayList(); formParams.add(new BasicNameValuePair("id", id)); if (flag) { formParams.add(new BasicNameValuePair("message", "执行失败")); } else { formParams.add(new BasicNameValuePair("message", "执行成功")); } formParams.add(new BasicNameValuePair("result", resultObj)); formParams.add(new BasicNameValuePair("access_token", token)); formParams.add(new BasicNameValuePair("status", "1")); hisResponString = HttpsClientUtil.post(ThreadConfig.SERVICE_URL + ThreadConfig.SQL_THREAD_CHANGEHISPENETRATIONSTATUS, formParams, ThreadConfig.SERVICE_USERNAME, ThreadConfig.SERVICE_PASSWORD); LogUtil.info("保存结果成功 " + hisResponString + ":ID=" + id); } catch (Exception e) { LogUtil.error("保存结果失败:ID=" + id + e.getMessage()); } } /** * 执行sql返回结果 * * @param sql * @return */ private String excuteSQL(String sql) { if (localdb == null) { synchronized (SQLThread.class) { //初始化数据库连接池 LogUtil.info("初始化数据库连接池:"); List list = db.query("select * from system_datasource where id='1'"); if (list != null && list.size() > 0) { org.json.JSONObject jo = list.get(0); LogUtil.info("得到数据源:" + (String) jo.get("config")); localdb = new DBHelper((String) jo.get("id"), (String) jo.get("config")); LogUtil.info("初始化数据源成功:" + localdb); } } } try { List locallist = localdb.query(sql); LogUtil.info("执行语句得到数据数目:" + locallist.size()); flag = true; return locallist.toString(); } catch (Exception e) { flag = false; LogUtil.error("执行语句失败:" + e.getMessage()); return ""; } } /** * 判断是否有his穿透的语句 * * @return */ private boolean hasHisPenetration() { try { // Map params = new HashMap(); // params.put("systemCode", systemCode); //params.put("orgCode", orgCode); //得到执行的hisSql语句 {id:xxx,sql:xxx} //hisResponString = HttpClientUtil.doPost(ThreadConfig.getURL(ThreadConfig.SQL_THREAD_HISPENETRATION), params, ThreadConfig.SERVICE_USERNAME, ThreadConfig.SERVICE_PASSWORD); //hisResponString = "{\"id\":\"aaaa\",\"sql\":\"select * from HDSA00_01 t where t.patient_id = '10126751'\"}"; List formParams = new ArrayList(); formParams.add(new BasicNameValuePair("systemCode", systemCode)); formParams.add(new BasicNameValuePair("orgCode", orgCode)); formParams.add(new BasicNameValuePair("access_token", token)); String path = ThreadConfig.SERVICE_URL + ThreadConfig.SQL_THREAD_HISPENETRATION; hisResponString = HttpsClientUtil.post(path, formParams, ThreadConfig.SERVICE_USERNAME, ThreadConfig.SERVICE_PASSWORD); LogUtil.info("得到要执行的his穿透任务:" + hisResponString); if (!StringUtils.isEmpty(hisResponString)) { return true; } else { return false; } } catch (Exception e) { e.printStackTrace(); LogUtil.error("没有要执行的his穿透任务:" + e.getMessage()); return false; } } /** * 补采 */ private void fillMining() { //判断是否补采 if (hasFillning()) { //把数据插入到补采的表 if (!StringUtils.isEmpty(fillMiningResponString)) { try { // {"startTime":"yyyyMMdd HH:mm:ss","endTime":"yyyyMMdd HH:mm:ss"} JSONObject jo = JSONObject.fromObject(fillMiningResponString); String id = (String) jo.get("id"); String startTime = (String) jo.get("startTime"); String endtime = (String) jo.get("endTime"); String sql = "insert into crawler_supply (id,start_time,end_time,status,remote_id) values " + "('" + UUID.randomUUID() + "'," + "'" + startTime + "' ," + "'" + endtime + "'," + "'0','" + id + "'" + ")"; db.execute(sql); LogUtil.info("-----------补采数据插入数据库sql:" + sql); LogUtil.info("-----------修改总平台的补采数据状态:"); changeFillmining(); } catch (Exception e) { e.printStackTrace(); LogUtil.info("-----------补采数据插入数据库失败"); } } } } /** * 判断是否补采 * * @return */ private boolean hasFillning() { try { // Map params = new HashMap(); //params.put("systemCode", systemCode); //params.put("orgCode", orgCode); // fillMiningResponString = HttpClientUtil.doPost(ThreadConfig.getURL(ThreadConfig.SQL_THREAD_FILLMINING).trim(), params, ThreadConfig.SERVICE_USERNAME, ThreadConfig.SERVICE_PASSWORD); //fillMiningResponString = "{\"id\":\"111\",\"startTime\":\"2014-01-01 12:12:12\",\"endTime\":\"2014-01-01 12:12:12\"}"; List formParams = new ArrayList(); formParams.add(new BasicNameValuePair("systemCode", systemCode)); formParams.add(new BasicNameValuePair("access_token", token)); formParams.add(new BasicNameValuePair("orgCode", orgCode)); fillMiningResponString = HttpsClientUtil.post(ThreadConfig.SERVICE_URL + ThreadConfig.SQL_THREAD_FILLMINING, formParams, ThreadConfig.SERVICE_USERNAME, ThreadConfig.SERVICE_PASSWORD); LogUtil.info("-----------返回的執行的補採任務:responString:" + fillMiningResponString); if (!StringUtils.isEmpty(fillMiningResponString)) { return true; } else { return false; } } catch (Exception e) { LogUtil.error("-----------沒有要執行的補採任務------------:" + e.getMessage()); return false; } } /** * 休眠 * * @throws Exception */ private void sleep() throws Exception { LogUtil.info("SQL线程开始睡眠,睡眠时间(分钟):" + ThreadConfig.SQL_THREAD_SLEEP_TIME); Thread.sleep(sleepTime * ThreadConfig.SQL_THREAD_SLEEP_TIME); } /** * 得到补采的参数 * * @return */ public String changeFillmining() { String result = ""; JSONObject jo = JSONObject.fromObject(fillMiningResponString); String id = (String) jo.get("id"); try { // Map params = new HashMap(); // params.put("systemCode", systemCode); // params.put("orgCode", orgCode); // params.put("id", id); // params.put("status", 2); // result = HttpClientUtil.doPost(ThreadConfig.getURL(ThreadConfig.SQL_THREAD_CHANGEFILLMININGSTATUS), params, ThreadConfig.SERVICE_USERNAME, ThreadConfig.SERVICE_PASSWORD); List formParams = new ArrayList(); formParams.add(new BasicNameValuePair("message", "修改成功")); formParams.add(new BasicNameValuePair("id", id)); formParams.add(new BasicNameValuePair("access_token", token)); formParams.add(new BasicNameValuePair("status", "2")); fillMiningResponString = HttpsClientUtil.post(ThreadConfig.SERVICE_URL + ThreadConfig.SQL_THREAD_CHANGEFILLMININGSTATUS, formParams, ThreadConfig.SERVICE_USERNAME, ThreadConfig.SERVICE_PASSWORD); LogUtil.info("修改总平台补采数据状态为2成功:" + fillMiningResponString); } catch (Exception e) { LogUtil.error("得到补采的参数失败:" + e.getMessage()); return ""; } return result; } /** * 得到his穿透的语句 * * @return */ public String getHisSql() { String result = ""; try { List formParams = new ArrayList(); formParams.add(new BasicNameValuePair("systemCode", systemCode)); formParams.add(new BasicNameValuePair("orgCode", orgCode)); formParams.add(new BasicNameValuePair("access_token", token)); result = HttpsClientUtil.post(SysConfig.getInstance().getPlatformUrl() + ThreadConfig.SQL_THREAD_FILLMINING, formParams, ThreadConfig.SERVICE_USERNAME, ThreadConfig.SERVICE_PASSWORD); LogUtil.info("得到需要执行的his穿透任务的语句成功:" + result); } catch (Exception e) { LogUtil.error("得到需要执行的his穿透任务的语句失败"); } return result; } }