|
@ -3,6 +3,7 @@ package com.yihu.hos.tenant.service;
|
|
import com.yihu.hos.config.MongoConfig;
|
|
import com.yihu.hos.config.MongoConfig;
|
|
import com.yihu.hos.core.encrypt.DES;
|
|
import com.yihu.hos.core.encrypt.DES;
|
|
import com.yihu.hos.tenant.dao.TenantDao;
|
|
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.tenant.model.TenantModel;
|
|
import com.yihu.hos.web.framework.model.Result;
|
|
import com.yihu.hos.web.framework.model.Result;
|
|
import com.yihu.hos.web.framework.util.GridFSUtil;
|
|
import com.yihu.hos.web.framework.util.GridFSUtil;
|
|
@ -11,6 +12,7 @@ import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStream;
|
|
import java.io.OutputStream;
|
|
import java.io.OutputStream;
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
@ -37,10 +39,19 @@ public class TenantService {
|
|
@Value("${spring.data.mongodb.gridFsDatabase}")
|
|
@Value("${spring.data.mongodb.gridFsDatabase}")
|
|
private String dbName;
|
|
private String dbName;
|
|
|
|
|
|
|
|
@Value("${hos.mysql.filePath}")
|
|
|
|
private String sqlFilePath;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private DBInfoService dbInfoService;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private MySqlImportAndExport mySqlImportAndExport;
|
|
|
|
|
|
|
|
|
|
public TenantModel findTenantByName(String name) throws Exception {
|
|
public TenantModel findTenantByName(String name) throws Exception {
|
|
List<TenantModel> list = tenantDao.getTenantList(name);
|
|
List<TenantModel> list = tenantDao.getTenantList(name);
|
|
if (list!=null && !list.isEmpty()){
|
|
|
|
|
|
if (list != null && !list.isEmpty()) {
|
|
return list.get(0);
|
|
return list.get(0);
|
|
}
|
|
}
|
|
return null;
|
|
return null;
|
|
@ -51,18 +62,36 @@ public class TenantService {
|
|
}
|
|
}
|
|
|
|
|
|
public TenantModel getTenantById(Long id) throws Exception {
|
|
public TenantModel getTenantById(Long id) throws Exception {
|
|
return tenantDao.getEntity(TenantModel.class,id);
|
|
|
|
|
|
return tenantDao.getEntity(TenantModel.class, id);
|
|
}
|
|
}
|
|
|
|
|
|
public Result addTenant(TenantModel obj) throws Exception {
|
|
|
|
|
|
public Result addTenant(TenantModel obj) {
|
|
String code = UUID.randomUUID().toString();
|
|
String code = UUID.randomUUID().toString();
|
|
obj.setCode(code);
|
|
obj.setCode(code);
|
|
obj.setCreated(new Date());
|
|
obj.setCreated(new Date());
|
|
obj.setCreatedUnix(0);
|
|
obj.setCreatedUnix(0);
|
|
obj.setUpdated(new Date());
|
|
obj.setUpdated(new Date());
|
|
obj.setUpdatedUnix(0);
|
|
obj.setUpdatedUnix(0);
|
|
tenantDao.saveEntity(obj);
|
|
|
|
return Result.success("保存成功");
|
|
|
|
|
|
String message = "";
|
|
|
|
try {
|
|
|
|
//TODO 建库建表操作
|
|
|
|
DBInfoModel dbInfo = dbInfoService.getDBInfoById(obj.getDataSourceId());
|
|
|
|
DBInfoModel db = new DBInfoModel();
|
|
|
|
db.setUserName(dbInfo.getUserName());
|
|
|
|
db.setPassword(dbInfo.getPassword());
|
|
|
|
db.setHost(dbInfo.getHost());
|
|
|
|
db.setPort(dbInfo.getPort());
|
|
|
|
db.setDbName(obj.getSchema());
|
|
|
|
//建库,建表,数据复制操作
|
|
|
|
message = mySqlImportAndExport.importSql(db, sqlFilePath);
|
|
|
|
tenantDao.saveEntity(obj);
|
|
|
|
return Result.success("保存成功");
|
|
|
|
} catch (IOException ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return Result.error("保存租户信息失败:" + message);
|
|
}
|
|
}
|
|
|
|
|
|
@Transactional
|
|
@Transactional
|
|
@ -90,9 +119,9 @@ public class TenantService {
|
|
try {
|
|
try {
|
|
fileName = DES.decrypt(fileName, DES.COMMON_PASSWORD);
|
|
fileName = DES.decrypt(fileName, DES.COMMON_PASSWORD);
|
|
boolean succ = GridFSUtil.readFile(mongoConfig.mongoClient().getDatabase(dbName), os, fileName);
|
|
boolean succ = GridFSUtil.readFile(mongoConfig.mongoClient().getDatabase(dbName), os, fileName);
|
|
if (succ){
|
|
|
|
|
|
if (succ) {
|
|
return Result.success("读取文件成功");
|
|
return Result.success("读取文件成功");
|
|
}else {
|
|
|
|
|
|
} else {
|
|
return Result.success("读取文件失败");
|
|
return Result.success("读取文件失败");
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
@ -104,10 +133,10 @@ public class TenantService {
|
|
public Result uploadFile(InputStream inputStream, String fileName) {
|
|
public Result uploadFile(InputStream inputStream, String fileName) {
|
|
try {
|
|
try {
|
|
fileName = DES.decrypt(fileName, DES.COMMON_PASSWORD);
|
|
fileName = DES.decrypt(fileName, DES.COMMON_PASSWORD);
|
|
String saveFileName = GridFSUtil.uploadFile(inputStream,fileName,null);
|
|
|
|
if (saveFileName != null){
|
|
|
|
|
|
String saveFileName = GridFSUtil.uploadFile(inputStream, fileName, null);
|
|
|
|
if (saveFileName != null) {
|
|
return Result.success("上传文件成功");
|
|
return Result.success("上传文件成功");
|
|
}else {
|
|
|
|
|
|
} else {
|
|
return Result.error("上传文件失败");
|
|
return Result.error("上传文件失败");
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
@ -116,13 +145,13 @@ public class TenantService {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public Result delFile( String fileName) {
|
|
|
|
|
|
public Result delFile(String fileName) {
|
|
try {
|
|
try {
|
|
fileName = DES.decrypt(fileName, DES.COMMON_PASSWORD);
|
|
fileName = DES.decrypt(fileName, DES.COMMON_PASSWORD);
|
|
boolean succ = GridFSUtil.deleteFile( fileName);
|
|
|
|
if (succ){
|
|
|
|
|
|
boolean succ = GridFSUtil.deleteFile(fileName);
|
|
|
|
if (succ) {
|
|
return Result.success("删除文件成功");
|
|
return Result.success("删除文件成功");
|
|
}else {
|
|
|
|
|
|
} else {
|
|
return Result.success("删除文件失败");
|
|
return Result.success("删除文件失败");
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|