|  | @ -1,22 +1,25 @@
 | 
	
		
			
				|  |  | package com.yihu.hos.tenant.service;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.yihu.ehr.dbhelper.jdbc.DBHelper;
 | 
	
		
			
				|  |  | import com.yihu.hos.common.constants.ContextAttributes;
 | 
	
		
			
				|  |  | import com.yihu.hos.config.MongoConfig;
 | 
	
		
			
				|  |  | import com.yihu.hos.core.encrypt.DES;
 | 
	
		
			
				|  |  | import com.yihu.hos.core.log.Logger;
 | 
	
		
			
				|  |  | import com.yihu.hos.core.log.LoggerFactory;
 | 
	
		
			
				|  |  | import com.yihu.hos.services.ServiceMycatEventService;
 | 
	
		
			
				|  |  | import com.yihu.hos.tenant.dao.TenantDao;
 | 
	
		
			
				|  |  | import com.yihu.hos.tenant.model.DBInfoModel;
 | 
	
		
			
				|  |  | import com.yihu.hos.tenant.model.TenantModel;
 | 
	
		
			
				|  |  | import com.yihu.hos.web.framework.constant.MycatConstant;
 | 
	
		
			
				|  |  | import com.yihu.hos.web.framework.constant.SqlConstants;
 | 
	
		
			
				|  |  | import com.yihu.hos.web.framework.model.Result;
 | 
	
		
			
				|  |  | import com.yihu.hos.web.framework.model.bo.ServiceMycat;
 | 
	
		
			
				|  |  | import org.json.JSONObject;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Value;
 | 
	
		
			
				|  |  | import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | import org.springframework.transaction.annotation.Transactional;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import java.io.IOException;
 | 
	
		
			
				|  |  | import java.io.InputStream;
 | 
	
		
			
				|  |  | import java.io.OutputStream;
 | 
	
		
			
				|  |  | import java.util.Date;
 | 
	
		
			
				|  |  | import java.util.List;
 | 
	
		
			
				|  |  | import java.util.Map;
 | 
	
	
		
			
				|  | @ -31,6 +34,7 @@ import java.util.UUID;
 | 
	
		
			
				|  |  | public class TenantService {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public static final String BEAN_ID = "TenantService";
 | 
	
		
			
				|  |  |     static private final Logger logger = LoggerFactory.getLogger(TenantService.class);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private TenantDao tenantDao;
 | 
	
	
		
			
				|  | @ -139,4 +143,45 @@ public class TenantService {
 | 
	
		
			
				|  |  |         return Result.success("新建成功");
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Transactional
 | 
	
		
			
				|  |  |     public Result createDB(DBHelper targetDb,String targetDbName) throws Exception {
 | 
	
		
			
				|  |  |         //创建数据库
 | 
	
		
			
				|  |  |         String createDb = "CREATE DATABASE IF NOT EXISTS "+targetDbName;
 | 
	
		
			
				|  |  |         targetDb.execute(createDb);
 | 
	
		
			
				|  |  |         return Result.success("新建成功");
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      *  //TODO 库复制  (测试,可删除)
 | 
	
		
			
				|  |  |      * 数据库复制
 | 
	
		
			
				|  |  |      * @param targetDbName  目标数据库名称
 | 
	
		
			
				|  |  |      * @param targetDbUri   目标数据库连接信息
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public Result copyBaseTables(String targetDbName,String targetDbUri) throws Exception {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String baseDbUri = "jdbc:mysql://172.19.103.57:3306/base_db?user=root&password=xmjkzl";//源数据库连接
 | 
	
		
			
				|  |  |         DBHelper originDb = new DBHelper(ContextAttributes.BASE_DB, baseDbUri);
 | 
	
		
			
				|  |  |         DBHelper targetDb = new DBHelper(targetDbName, targetDbUri);
 | 
	
		
			
				|  |  |         //创建数据库
 | 
	
		
			
				|  |  |         createDB(targetDb,targetDbName);
 | 
	
		
			
				|  |  |         //查询源数据库所有表名
 | 
	
		
			
				|  |  |         String tablesSql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '"+ ContextAttributes.BASE_DB+"';";
 | 
	
		
			
				|  |  |         //创建表
 | 
	
		
			
				|  |  |         List<JSONObject> tableNames = originDb.query(tablesSql);
 | 
	
		
			
				|  |  |         for (JSONObject jsonObject:tableNames){
 | 
	
		
			
				|  |  |             try {
 | 
	
		
			
				|  |  |             String tableName = jsonObject.getString(ContextAttributes.TABLE_NAME);
 | 
	
		
			
				|  |  |             boolean createTable = targetDb.execute(SqlConstants.CREATE_TABLE + targetDbName+"."+tableName + SqlConstants.LIKE + ContextAttributes.BASE_DB+"."+tableName);
 | 
	
		
			
				|  |  |             boolean insertData = targetDb.execute(SqlConstants.INSERT_INTO +  targetDbName+"."+tableName + SqlConstants.SELECT + "*" + SqlConstants.FROM + ContextAttributes.BASE_DB+"."+tableName);
 | 
	
		
			
				|  |  |             System.out.println(insertData);
 | 
	
		
			
				|  |  |             }catch (Exception e){
 | 
	
		
			
				|  |  |                 logger.error("执行异常:"+e.getMessage());
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         //新增表数据
 | 
	
		
			
				|  |  |         return Result.success("新建成功");
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | }
 |