package com.yihu.ehr.service; import com.yihu.ehr.common.config.SysConfig; import com.yihu.ehr.common.config.ThreadConfig; import com.yihu.ehr.dbhelper.common.enums.CommonEnum; import com.yihu.ehr.dbhelper.jdbc.DBDriver; import com.yihu.ehr.dbhelper.jdbc.DBHelper; import com.yihu.ehr.framework.model.Result; import com.yihu.ehr.model.DataSource; import com.yihu.ehr.model.SystemParams; import com.yihu.ehr.model.UserInfo; import com.yihu.ehr.service.intf.ISystemManager; import com.yihu.ehr.service.thread.StandardUpdateThread; import com.yihu.ehr.service.thread.ThreadManage; import com.yihu.ehr.util.httpclient.EsbHttp; import com.yihu.ehr.util.httpclient.Response; import com.yihu.ehr.ws.SQLWebService; import org.apache.commons.dbcp2.BasicDataSource; import org.json.JSONObject; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.servlet.http.HttpSession; import java.sql.Connection; import java.sql.Driver; import java.sql.DriverManager; import java.util.Enumeration; import java.util.List; import java.util.UUID; /** * 总平台交互 * Created by HZP on 2016/02/26. */ @Service("systemManager") public class SystemManager implements ISystemManager { DBHelper db = new DBHelper(); /* 获取系统参数 */ @Override public SystemParams getSystemParams() throws Exception { List list = db.query("select * from system_param"); SystemParams re = new SystemParams(); if (list != null) { for (JSONObject obj : list) { String key = obj.getString("param_key"); String value = obj.getString("param_value"); if (key.equals("ORG_CODE")) { re.setOrgCode(value); } else if (key.equals("ORG_NAME")) { re.setOrgName(value); } else if (key.equals("INTERVAL")) { re.setInterval(value); } else if (key.equals("UPDATE_INTERVAL")) { re.setUpdateInterval(value); } } } return re; } /* 根据名称获取系统参数 */ @Override public String getSystemParam(String name) throws Exception { Object obj = db.scalar("select param_value from system_param where param_key='" + name + "'"); if (obj != null) { return obj.toString(); } else { return ""; } } /* 获取数据源 */ @Override public DataSource getDataSource() throws Exception { //第一行 return db.load(DataSource.class, "select * from system_datasource"); } /* 新增系统参数 */ private void addSystemParams(String key, String value) throws Exception { String sql = "insert into system_param (id,param_key,param_value) values ('" + UUID.randomUUID() + "','" + key + "','" + value + "')"; if (!db.execute(sql)) { throw new Exception(db.errorMessage); } } /* 保存系统参数 */ @Transactional private void saveSystemParams(String key, String value) throws Exception { Object obj = db.scalar("select 1 from system_param where param_key='" + key + "'"); if (obj == null) { addSystemParams(key, value); } else { String sql = "update system_param set param_value = '" + value + "' where param_key='" + key + "'"; if (!db.execute(sql)) { throw new Exception(db.errorMessage); } } } /* 保存系统参数 */ @Override @Transactional public Result saveParams(String interval, String updateInterval) throws Exception { //采集频率 saveSystemParams("INTERVAL", interval); ThreadConfig.CRAWLER_THREAD_SLEEP_TIME = Integer.parseInt(interval); //更新频率 saveSystemParams("UPDATE_INTERVAL", updateInterval); ThreadConfig.UPDATE_THREAD_SLEEP_TIME = Integer.parseInt(updateInterval); return Result.success("保存成功!"); } /* 保存数据源 */ @Override @Transactional public Result saveDataSource(String id, String name, String config) throws Exception { String sql = "insert into system_datasource (id,name,config) values ('" + UUID.randomUUID() + "','" + name + "','" + config + "')"; if (id != null && id.length() > 0) { sql = "update system_datasource set name = '" + name + "',config='" + config + "' where id='" + id + "'"; } if (db.execute(sql)) { //更新数据源//jdbc:oracle:thin:hos/hos@//172.19.103.71:1521/orcl //jdbc:mysql://172.19.103.71:1521/orcl?user=hos&password=hos&useUnicode=true&characterEncoding=UTF-8 //jdbc:sqlserver://172.19.103.71:1521/orcl?user=hos&password=hos BasicDataSource dasicDataSource = new BasicDataSource(); if (config.contains("oracle")) { //oracle数据库 dasicDataSource.setUrl(config); dasicDataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver"); String[] s1 = config.split("@"); String[] s2 = s1[0].split(":"); String[] s3 = s2[3].split("/"); dasicDataSource.setUsername(s3[0]); dasicDataSource.setPassword(s3[1]); System.out.println(config); System.out.println("username:" + s3[0]); System.out.println("password:" + s3[1]); } else if (config.contains("mysql")) { //mysql数据库 dasicDataSource.setUrl(config); dasicDataSource.setDriverClassName("com.mysql.jdbc.Driver"); String[] s1 = config.split("\\?"); String[] s2 = s1[1].split("&"); dasicDataSource.setUsername(s2[0].split("=")[1]); dasicDataSource.setPassword(s2[1].split("=")[1]); System.out.println(config); System.out.println("username:" + s2[0].split("=")[1]); System.out.println("password:" + s2[1].split("=")[1]); } else if (config.contains("sqlserver")) { //sqlserver数据库 dasicDataSource.setUrl(config); dasicDataSource.setDriverClassName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); String[] s1 = config.split("\\?"); String[] s2 = s1[1].split("&"); dasicDataSource.setUsername(s2[0].split("=")[1]); dasicDataSource.setPassword(s2[1].split("=")[1]); System.out.println(config); System.out.println("username:" + s2[0].split("=")[1]); System.out.println("password:" + s2[1].split("=")[1]); } SQLWebService.jdbcTemplate = new JdbcTemplate(); SQLWebService.jdbcTemplate.setDataSource(dasicDataSource); return Result.success("保存成功!"); } else { return Result.error(db.errorMessage); } } /* 测试数据源 */ @Override public Result testDataSource(String uri) { try { DBDriver.registerDriver(uri); Connection conn = DriverManager.getConnection(uri); if (conn != null) { String message = "连接测试成功!"; if (conn.isClosed()) { message = "连接测试失败!"; } conn.close(); return Result.success(message); } else { return Result.error("连接测试失败!"); } } catch (Exception ex) { return Result.error(ex.getMessage()); } } /* 登录操作 */ @Override public Result loginAction(HttpSession session, String user, String password) { try { //特殊用户 if (user.equals("admin") && password.equals("JKZL")) { UserInfo obj = new UserInfo(); obj.setLoginCode("admin"); obj.setRealName("管理员"); obj.setOrgCode("JKZL"); obj.setOrgName("健康之路"); session.setAttribute("userInfo", obj); return Result.success("登录成功!"); } //根据用户名/密码到总平台校验 Response response = EsbHttp.loginAction(user, password); if (response != null && response.getStatusCode() == 200) { JSONObject obj = new JSONObject(response.getBody()); if (obj.has("token"))//登录成功 { String token = obj.getString("token"); //获取用户信息 Response re = EsbHttp.getUserInfo(user, token); if (re != null && re.getStatusCode() == 200) { JSONObject userObject = new JSONObject(re.getBody()); UserInfo userInfo = new UserInfo(); userInfo.setLoginCode(user); userInfo.setRealName(userObject.getString("realName")); String orgCode = ""; String orgName = ""; if (userObject.has("organization") && !userObject.get("organization").equals(null)) { JSONObject orgInfo = userObject.getJSONObject("organization"); orgCode = orgInfo.getString("orgCode"); orgName = orgInfo.getString("fullName"); userInfo.setOrgCode(orgCode); userInfo.setOrgName(orgName); } else { return Result.error("该用户未配置机构!"); } session.setAttribute("userInfo", userInfo); //判断组织编码是否为空 String orgCodeOld = this.getSystemParam("ORG_CODE"); if (orgCodeOld == null || orgCodeOld.length() == 0) { this.saveSystemParams("FINGER_PRINT", UUID.randomUUID().toString()); this.saveSystemParams("ORG_CODE", orgCode); this.saveSystemParams("ORG_NAME", orgName); //启动标准 SysConfig.getInstance().setOrgCode(orgCode); Thread standardUpdateThread = new Thread(new StandardUpdateThread()); ThreadManage.add(ThreadManage.STANDARD_UPDATE_THREAD, standardUpdateThread); ThreadManage.setUpdateInterval(); if (!standardUpdateThread.isAlive()) { standardUpdateThread.start(); } return Result.success("登录成功!"); } else { if (orgCodeOld.equals(orgCode)) { return Result.success("登录成功!"); } else { return Result.error("机构编码不一致!"); } } } else { return Result.error("获取用户信息失败!"); } } else { if (obj.has("message")) { return Result.error("登录失败!" + obj.getString("message")); } else { return Result.error("登录失败!"); } } } else { String msg = "登录失败。"; if (response != null) { msg += "(错误代码:" + response.getStatusCode() + ",错误信息:" + response.getBody() + ")"; } return Result.error(msg); } } catch (Exception ex) { return Result.error(ex.getMessage()); } } }