TenantService.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.tenant.dao.TenantDao;
  5. import com.yihu.hos.tenant.model.TenantModel;
  6. import com.yihu.hos.web.framework.model.Result;
  7. import com.yihu.hos.web.framework.util.GridFSUtil;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.beans.factory.annotation.Value;
  10. import org.springframework.stereotype.Service;
  11. import org.springframework.transaction.annotation.Transactional;
  12. import java.io.OutputStream;
  13. import java.util.Date;
  14. import java.util.List;
  15. import java.util.Map;
  16. import java.util.UUID;
  17. /**
  18. * @author HZY
  19. * @vsrsion 1.0
  20. * Created at 2016/12/2.
  21. */
  22. @Service("TenantService")
  23. public class TenantService {
  24. public static final String BEAN_ID = "TenantService";
  25. @Autowired
  26. private TenantDao tenantDao;
  27. @Autowired
  28. private MongoConfig mongoConfig;
  29. @Value("${spring.data.mongodb.gridFsDatabase}")
  30. private String dbName;
  31. public TenantModel findTenantByName(String name) throws Exception {
  32. List<TenantModel> list = tenantDao.getTenantList(name);
  33. if (list!=null && !list.isEmpty()){
  34. return list.get(0);
  35. }
  36. return null;
  37. }
  38. public Result getTenantList(Map<String, Object> params) throws Exception {
  39. return tenantDao.getTenantList(params);
  40. }
  41. public TenantModel getTenantById(Long id) throws Exception {
  42. return tenantDao.getEntity(TenantModel.class,id);
  43. }
  44. public Result addTenant(TenantModel obj) throws Exception {
  45. String code = UUID.randomUUID().toString();
  46. obj.setCode(code);
  47. obj.setCreated(new Date());
  48. obj.setCreatedUnix(0);
  49. obj.setUpdated(new Date());
  50. obj.setUpdatedUnix(0);
  51. tenantDao.saveEntity(obj);
  52. return Result.success("保存成功");
  53. }
  54. @Transactional
  55. public Result updateTenant(TenantModel obj) throws Exception {
  56. TenantModel tenant = tenantDao.getEntity(TenantModel.class, obj.getId());
  57. tenant.setName(obj.getName());
  58. tenant.setLoginName(obj.getLoginName());
  59. tenant.setSchema(obj.getSchema());
  60. tenant.setPassword(obj.getPassword());
  61. tenant.setValid(obj.getValid());
  62. tenant.setUpdated(new Date());
  63. tenant.setUpdatedUnix(1);
  64. tenantDao.updateEntity(tenant);
  65. return Result.success("更新成功");
  66. }
  67. @Transactional
  68. public Result deleteTenant(Long id) throws Exception {
  69. TenantModel systemApp = tenantDao.getEntity(TenantModel.class, id);
  70. tenantDao.deleteEntity(systemApp);
  71. return Result.success("删除成功");
  72. }
  73. public Result readFile(OutputStream os, String fileName) {
  74. try {
  75. fileName = DES.decrypt(fileName, DES.COMMON_PASSWORD);
  76. GridFSUtil.readFile(mongoConfig.mongoClient().getDatabase(dbName), os, fileName);
  77. return Result.success("读取成功");
  78. } catch (Exception e) {
  79. e.printStackTrace();
  80. }
  81. return Result.error("读取失败");
  82. }
  83. }