123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- package com.yihu.hos.tenant.service;
- import com.yihu.hos.config.MongoConfig;
- import com.yihu.hos.core.encrypt.DES;
- 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.model.Result;
- import com.yihu.hos.web.framework.model.bo.ServiceMycat;
- import com.yihu.hos.web.framework.util.GridFSUtil;
- 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;
- import java.util.UUID;
- /**
- * @author HZY
- * @vsrsion 1.0
- * Created at 2016/12/2.
- */
- @Service("TenantService")
- public class TenantService {
- public static final String BEAN_ID = "TenantService";
- @Autowired
- private TenantDao tenantDao;
- @Autowired
- private MongoConfig mongoConfig;
- @Value("${spring.data.mongodb.gridFsDatabase}")
- private String dbName;
- @Value("${hos.mysql.filePath}")
- private String sqlFilePath;
- @Autowired
- private DBInfoService dbInfoService;
- @Autowired
- private MySqlImportAndExport mySqlImportAndExport;
- @Autowired
- private ServiceMycatEventService serviceMycatEventService;
- public TenantModel findTenantByName(String name) throws Exception {
- List<TenantModel> list = tenantDao.getTenantList(name);
- if (list != null && !list.isEmpty()) {
- return list.get(0);
- }
- return null;
- }
- public Result getTenantList(Map<String, Object> params) throws Exception {
- return tenantDao.getTenantList(params);
- }
- public TenantModel getTenantById(Long id) throws Exception {
- return tenantDao.getEntity(TenantModel.class, id);
- }
- // @Transactional(rollbackFor={RuntimeException.class, Exception.class})
- public Result addTenant(TenantModel obj) {
- String code = UUID.randomUUID().toString();
- obj.setCode(code);
- obj.setCreated(new Date());
- obj.setCreatedUnix(0);
- obj.setUpdated(new Date());
- obj.setUpdatedUnix(0);
- String errorMessage = null;
- try {
- // 建库建表操作
- 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(MycatConstant.DATA_BASE + obj.getSchema());//新建的数据库命名规则为"db_"+ schema
- //建库,建表,数据复制操作
- errorMessage = mySqlImportAndExport.importSql(db, sqlFilePath);
- if (errorMessage ==null){
- //TODO mycat操作消息发送;使用zbus;tenant: "mycat"+ IP
- ServiceMycat serrviceMycat = new ServiceMycat();
- serrviceMycat.setSchema(obj.getSchema());
- serrviceMycat.setTenant(MycatConstant.MYCAT+db.getHost());
- serrviceMycat.setLoginName(obj.getLoginName());
- serrviceMycat.setPassword(obj.getPassword());
- serviceMycatEventService.executeMycatConfig(serrviceMycat, MycatConstant.MYCAT+dbInfo.getHost());
- tenantDao.saveEntity(obj);
- return Result.success("保存成功");
- }else {
- return Result.error("建库建表操作失败,异常信息\r\n" + errorMessage);
- }
- } catch (IOException ex) {
- ex.printStackTrace();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return Result.error("保存租户信息失败,异常信息\n" + errorMessage);
- }
- @Transactional
- public Result updateTenant(TenantModel obj) throws Exception {
- TenantModel tenant = tenantDao.getEntity(TenantModel.class, obj.getId());
- tenant.setName(obj.getName());
- tenant.setLoginName(obj.getLoginName());
- tenant.setSchema(obj.getSchema());
- tenant.setPassword(obj.getPassword());
- tenant.setValid(obj.getValid());
- tenant.setUpdated(new Date());
- tenant.setUpdatedUnix(1);
- tenantDao.updateEntity(tenant);
- return Result.success("更新成功");
- }
- @Transactional
- public Result deleteTenant(Long id) throws Exception {
- TenantModel systemApp = tenantDao.getEntity(TenantModel.class, id);
- tenantDao.deleteEntity(systemApp);
- return Result.success("删除成功");
- }
- public Result dowFile(OutputStream os, String fileName) {
- try {
- fileName = DES.decrypt(fileName, DES.COMMON_PASSWORD);
- boolean succ = GridFSUtil.readFile(mongoConfig.mongoClient().getDatabase(dbName), os, fileName);
- if (succ) {
- return Result.success("读取文件成功");
- } else {
- return Result.success("读取文件失败");
- }
- } catch (Exception e) {
- e.printStackTrace();
- return Result.error("读取文件异常");
- }
- }
- public Result uploadFile(InputStream inputStream, String fileName) {
- try {
- fileName = DES.decrypt(fileName, DES.COMMON_PASSWORD);
- String saveFileName = GridFSUtil.uploadFile(inputStream, fileName, null);
- if (saveFileName != null) {
- return Result.success("上传文件成功");
- } else {
- return Result.error("上传文件失败");
- }
- } catch (Exception e) {
- e.printStackTrace();
- return Result.error("上传文件异常");
- }
- }
- public Result delFile(String fileName) {
- try {
- fileName = DES.decrypt(fileName, DES.COMMON_PASSWORD);
- boolean succ = GridFSUtil.deleteFile(fileName);
- if (succ) {
- return Result.success("删除文件成功");
- } else {
- return Result.success("删除文件失败");
- }
- } catch (Exception e) {
- e.printStackTrace();
- return Result.error("删除文件异常");
- }
- }
- @Transactional
- public Result createDB(String dbName) throws Exception {
- tenantDao.createDB(dbName);
- return Result.success("新建成功");
- }
- }
|