SysTenantService.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. package cc.uncarbon.module.sys.service;
  2. import cc.uncarbon.framework.core.constant.HelioConstant;
  3. import cc.uncarbon.framework.core.exception.BusinessException;
  4. import cc.uncarbon.framework.core.page.PageParam;
  5. import cc.uncarbon.framework.core.page.PageResult;
  6. import cc.uncarbon.framework.crud.service.impl.HelioBaseServiceImpl;
  7. import cc.uncarbon.module.sys.entity.SysTenantEntity;
  8. import cc.uncarbon.module.sys.enums.SysErrorEnum;
  9. import cc.uncarbon.module.sys.mapper.SysTenantMapper;
  10. import cc.uncarbon.module.sys.mapper.SysUserMapper;
  11. import cc.uncarbon.module.sys.model.request.AdminListSysTenantDTO;
  12. import cc.uncarbon.module.sys.model.request.AdminUpdateSysTenantDTO;
  13. import cc.uncarbon.module.sys.model.response.SysTenantBO;
  14. import cn.hutool.core.bean.BeanUtil;
  15. import cn.hutool.core.util.ObjectUtil;
  16. import cn.hutool.core.util.StrUtil;
  17. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  18. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  19. import lombok.RequiredArgsConstructor;
  20. import lombok.extern.slf4j.Slf4j;
  21. import org.springframework.stereotype.Service;
  22. import org.springframework.transaction.annotation.Transactional;
  23. import java.util.ArrayList;
  24. import java.util.Collection;
  25. import java.util.List;
  26. /**
  27. * 系统租户
  28. * @author Uncarbon
  29. */
  30. @Slf4j
  31. @Service
  32. @RequiredArgsConstructor
  33. public class SysTenantService extends HelioBaseServiceImpl<SysTenantMapper, SysTenantEntity> {
  34. private final SysUserMapper sysUserMapper;
  35. /**
  36. * 后台管理-分页列表
  37. */
  38. public PageResult<SysTenantBO> adminList(PageParam pageParam, AdminListSysTenantDTO dto) {
  39. Page<SysTenantEntity> entityPage = this.page(
  40. new Page<>(pageParam.getPageNum(), pageParam.getPageSize()),
  41. new QueryWrapper<SysTenantEntity>()
  42. .lambda()
  43. // 租户名
  44. .like(StrUtil.isNotBlank(dto.getTenantName()), SysTenantEntity::getTenantName, StrUtil.cleanBlank(dto.getTenantName()))
  45. // 租户ID
  46. .eq(ObjectUtil.isNotNull(dto.getTenantId()), SysTenantEntity::getTenantId, dto.getTenantId())
  47. // 状态
  48. .eq(ObjectUtil.isNotNull(dto.getStatus()), SysTenantEntity::getStatus, dto.getStatus())
  49. // 排序
  50. .orderByDesc(SysTenantEntity::getCreatedAt)
  51. );
  52. return this.entityPage2BOPage(entityPage);
  53. }
  54. /**
  55. * 根据 ID 取详情
  56. *
  57. * @param id 主键ID
  58. * @return null or BO
  59. */
  60. public SysTenantBO getOneById(Long id) {
  61. return this.getOneById(id, false);
  62. }
  63. /**
  64. * 根据 ID 取详情
  65. *
  66. * @param id 主键ID
  67. * @param throwIfInvalidId 是否在 ID 无效时抛出异常
  68. * @return null or BO
  69. */
  70. public SysTenantBO getOneById(Long id, boolean throwIfInvalidId) throws BusinessException {
  71. SysTenantEntity entity = this.getById(id);
  72. if (throwIfInvalidId) {
  73. SysErrorEnum.INVALID_ID.assertNotNull(entity);
  74. }
  75. return this.entity2BO(entity);
  76. }
  77. /**
  78. * 后台管理-编辑
  79. */
  80. @Transactional(rollbackFor = Exception.class)
  81. public void adminUpdate(AdminUpdateSysTenantDTO dto) {
  82. log.info("[后台管理-编辑系统租户] >> 入参={}", dto);
  83. this.checkExistence(dto);
  84. SysTenantEntity entity = new SysTenantEntity();
  85. BeanUtil.copyProperties(dto, entity);
  86. this.updateById(entity);
  87. }
  88. /**
  89. * 后台管理-删除
  90. */
  91. @Transactional(rollbackFor = Exception.class)
  92. public void adminDelete(Collection<Long> ids) {
  93. log.info("[后台管理-删除系统租户] >> 入参={}", ids);
  94. this.removeByIds(ids);
  95. }
  96. /**
  97. * 根据租户ID(非主键ID),得到租户实体
  98. */
  99. public SysTenantEntity getTenantEntityByTenantId(Long tenantId) {
  100. return this.getOne(
  101. new QueryWrapper<SysTenantEntity>()
  102. .lambda()
  103. .eq(SysTenantEntity::getTenantId, tenantId)
  104. .last(HelioConstant.CRUD.SQL_LIMIT_1)
  105. );
  106. }
  107. /**
  108. * 检查是否已存在相同数据
  109. *
  110. * @param dto DTO
  111. */
  112. public void checkExistence(AdminUpdateSysTenantDTO dto) {
  113. SysTenantEntity existingEntity = this.getOne(
  114. new QueryWrapper<SysTenantEntity>()
  115. .lambda()
  116. // 仅取主键ID
  117. .select(SysTenantEntity::getId)
  118. // 租户ID相同
  119. .eq(SysTenantEntity::getTenantId, dto.getTenantId())
  120. .or()
  121. // 或租户名相同
  122. .eq(SysTenantEntity::getTenantName, dto.getTenantName())
  123. .last(HelioConstant.CRUD.SQL_LIMIT_1)
  124. );
  125. if (existingEntity != null && !existingEntity.getId().equals(dto.getId())) {
  126. throw new BusinessException(400, "已存在相同系统租户,请重新输入");
  127. }
  128. }
  129. /*
  130. ----------------------------------------------------------------
  131. 私有方法 private methods
  132. ----------------------------------------------------------------
  133. */
  134. /**
  135. * 实体转 BO
  136. *
  137. * @param entity 实体
  138. * @return BO
  139. */
  140. private SysTenantBO entity2BO(SysTenantEntity entity) {
  141. if (entity == null) {
  142. return null;
  143. }
  144. SysTenantBO bo = new SysTenantBO();
  145. BeanUtil.copyProperties(entity, bo);
  146. // 可以在此处为BO填充字段
  147. if (ObjectUtil.isNotNull(entity.getTenantAdminUserId())) {
  148. bo
  149. .setTenantAdminUser(sysUserMapper.getBaseInfoByUserId(entity.getTenantAdminUserId()))
  150. ;
  151. }
  152. return bo;
  153. }
  154. /**
  155. * 实体 List 转 BO List
  156. *
  157. * @param entityList 实体 List
  158. * @return BO List
  159. */
  160. private List<SysTenantBO> entityList2BOs(List<SysTenantEntity> entityList) {
  161. // 深拷贝
  162. List<SysTenantBO> ret = new ArrayList<>(entityList.size());
  163. entityList.forEach(
  164. entity -> ret.add(this.entity2BO(entity))
  165. );
  166. return ret;
  167. }
  168. /**
  169. * 实体分页转 BO 分页
  170. *
  171. * @param entityPage 实体分页
  172. * @return BO 分页
  173. */
  174. private PageResult<SysTenantBO> entityPage2BOPage(Page<SysTenantEntity> entityPage) {
  175. return new PageResult<SysTenantBO>()
  176. .setCurrent(entityPage.getCurrent())
  177. .setSize(entityPage.getSize())
  178. .setTotal(entityPage.getTotal())
  179. .setRecords(this.entityList2BOs(entityPage.getRecords()))
  180. ;
  181. }
  182. }