TenantService.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package com.yihu.hos.tenant.service;
  2. import com.yihu.hos.config.MongoConfig;
  3. import com.yihu.hos.core.encrypt.DES;
  4. import com.yihu.hos.services.ServiceMycatEventService;
  5. import com.yihu.hos.tenant.dao.TenantDao;
  6. import com.yihu.hos.tenant.model.DBInfoModel;
  7. import com.yihu.hos.tenant.model.TenantModel;
  8. import com.yihu.hos.web.framework.constant.MycatConstant;
  9. import com.yihu.hos.web.framework.model.Result;
  10. import com.yihu.hos.web.framework.model.bo.ServiceMycat;
  11. import com.yihu.hos.web.framework.util.GridFSUtil;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.beans.factory.annotation.Value;
  14. import org.springframework.stereotype.Service;
  15. import org.springframework.transaction.annotation.Transactional;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.io.OutputStream;
  19. import java.util.Date;
  20. import java.util.List;
  21. import java.util.Map;
  22. import java.util.UUID;
  23. /**
  24. * @author HZY
  25. * @vsrsion 1.0
  26. * Created at 2016/12/2.
  27. */
  28. @Service("TenantService")
  29. public class TenantService {
  30. public static final String BEAN_ID = "TenantService";
  31. @Autowired
  32. private TenantDao tenantDao;
  33. @Autowired
  34. private MongoConfig mongoConfig;
  35. @Value("${spring.data.mongodb.gridFsDatabase}")
  36. private String dbName;
  37. @Value("${hos.mysql.filePath}")
  38. private String sqlFilePath;
  39. @Autowired
  40. private DBInfoService dbInfoService;
  41. @Autowired
  42. private MySqlImportAndExport mySqlImportAndExport;
  43. @Autowired
  44. private ServiceMycatEventService serviceMycatEventService;
  45. public TenantModel findTenantByName(String name) throws Exception {
  46. List<TenantModel> list = tenantDao.getTenantList(name);
  47. if (list != null && !list.isEmpty()) {
  48. return list.get(0);
  49. }
  50. return null;
  51. }
  52. public Result getTenantList(Map<String, Object> params) throws Exception {
  53. return tenantDao.getTenantList(params);
  54. }
  55. public TenantModel getTenantById(Long id) throws Exception {
  56. return tenantDao.getEntity(TenantModel.class, id);
  57. }
  58. // @Transactional(rollbackFor={RuntimeException.class, Exception.class})
  59. public Result addTenant(TenantModel obj) {
  60. String code = UUID.randomUUID().toString();
  61. obj.setCode(code);
  62. obj.setCreated(new Date());
  63. obj.setCreatedUnix(0);
  64. obj.setUpdated(new Date());
  65. obj.setUpdatedUnix(0);
  66. String errorMessage = null;
  67. try {
  68. // 建库建表操作
  69. DBInfoModel dbInfo = dbInfoService.getDBInfoById(obj.getDataSourceId());
  70. DBInfoModel db = new DBInfoModel();
  71. db.setUserName(dbInfo.getUserName());
  72. db.setPassword(dbInfo.getPassword());
  73. db.setHost(dbInfo.getHost());
  74. db.setPort(dbInfo.getPort());
  75. db.setDbName(MycatConstant.DATA_BASE + obj.getSchema());//新建的数据库命名规则为"db_"+ schema
  76. //建库,建表,数据复制操作
  77. errorMessage = mySqlImportAndExport.importSql(db, sqlFilePath);
  78. if (errorMessage ==null){
  79. //TODO mycat操作消息发送;使用zbus;tenant: "mycat"+ IP
  80. ServiceMycat serrviceMycat = new ServiceMycat();
  81. serrviceMycat.setSchema(obj.getSchema());
  82. serrviceMycat.setTenant(MycatConstant.MYCAT+db.getHost());
  83. serrviceMycat.setLoginName(obj.getLoginName());
  84. serrviceMycat.setPassword(obj.getPassword());
  85. serviceMycatEventService.executeMycatConfig(serrviceMycat, MycatConstant.MYCAT+dbInfo.getHost());
  86. tenantDao.saveEntity(obj);
  87. return Result.success("保存成功");
  88. }else {
  89. return Result.error("建库建表操作失败,异常信息\r\n" + errorMessage);
  90. }
  91. } catch (IOException ex) {
  92. ex.printStackTrace();
  93. } catch (Exception e) {
  94. e.printStackTrace();
  95. }
  96. return Result.error("保存租户信息失败,异常信息\n" + errorMessage);
  97. }
  98. @Transactional
  99. public Result updateTenant(TenantModel obj) throws Exception {
  100. TenantModel tenant = tenantDao.getEntity(TenantModel.class, obj.getId());
  101. tenant.setName(obj.getName());
  102. tenant.setLoginName(obj.getLoginName());
  103. tenant.setSchema(obj.getSchema());
  104. tenant.setPassword(obj.getPassword());
  105. tenant.setValid(obj.getValid());
  106. tenant.setUpdated(new Date());
  107. tenant.setUpdatedUnix(1);
  108. tenantDao.updateEntity(tenant);
  109. return Result.success("更新成功");
  110. }
  111. @Transactional
  112. public Result deleteTenant(Long id) throws Exception {
  113. TenantModel systemApp = tenantDao.getEntity(TenantModel.class, id);
  114. tenantDao.deleteEntity(systemApp);
  115. return Result.success("删除成功");
  116. }
  117. public Result dowFile(OutputStream os, String fileName) {
  118. try {
  119. fileName = DES.decrypt(fileName, DES.COMMON_PASSWORD);
  120. boolean succ = GridFSUtil.readFile(mongoConfig.mongoClient().getDatabase(dbName), os, fileName);
  121. if (succ) {
  122. return Result.success("读取文件成功");
  123. } else {
  124. return Result.success("读取文件失败");
  125. }
  126. } catch (Exception e) {
  127. e.printStackTrace();
  128. return Result.error("读取文件异常");
  129. }
  130. }
  131. public Result uploadFile(InputStream inputStream, String fileName) {
  132. try {
  133. fileName = DES.decrypt(fileName, DES.COMMON_PASSWORD);
  134. String saveFileName = GridFSUtil.uploadFile(inputStream, fileName, null);
  135. if (saveFileName != null) {
  136. return Result.success("上传文件成功");
  137. } else {
  138. return Result.error("上传文件失败");
  139. }
  140. } catch (Exception e) {
  141. e.printStackTrace();
  142. return Result.error("上传文件异常");
  143. }
  144. }
  145. public Result delFile(String fileName) {
  146. try {
  147. fileName = DES.decrypt(fileName, DES.COMMON_PASSWORD);
  148. boolean succ = GridFSUtil.deleteFile(fileName);
  149. if (succ) {
  150. return Result.success("删除文件成功");
  151. } else {
  152. return Result.success("删除文件失败");
  153. }
  154. } catch (Exception e) {
  155. e.printStackTrace();
  156. return Result.error("删除文件异常");
  157. }
  158. }
  159. @Transactional
  160. public Result createDB(String dbName) throws Exception {
  161. tenantDao.createDB(dbName);
  162. return Result.success("新建成功");
  163. }
  164. }