SysGroupService.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. package com.jtzx.crm.module.sys.service;
  2. import cc.uncarbon.framework.core.exception.BusinessException;
  3. import cc.uncarbon.framework.core.page.PageParam;
  4. import cc.uncarbon.framework.core.page.PageResult;
  5. import cn.hutool.core.bean.BeanUtil;
  6. import cn.hutool.core.collection.CollUtil;
  7. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  8. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  9. import com.jtzx.crm.module.sys.entity.SysGroupEntity;
  10. import com.jtzx.crm.module.sys.mapper.SysGroupMapper;
  11. import com.jtzx.crm.module.sys.model.request.AdminSysGroupInsertOrUpdateDTO;
  12. import com.jtzx.crm.module.sys.model.response.SysGroupBO;
  13. import lombok.RequiredArgsConstructor;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.springframework.stereotype.Service;
  16. import org.springframework.transaction.annotation.Transactional;
  17. import java.util.ArrayList;
  18. import java.util.Collection;
  19. import java.util.Collections;
  20. import java.util.List;
  21. /**
  22. * 后台分组数据
  23. */
  24. @RequiredArgsConstructor
  25. @Service
  26. @Slf4j
  27. public class SysGroupService {
  28. private final SysGroupMapper sysGroupMapper;
  29. /**
  30. * 后台管理-分页列表
  31. */
  32. public PageResult<SysGroupBO> adminList(PageParam pageParam) {
  33. Page<SysGroupEntity> entityPage = sysGroupMapper.selectPage(
  34. new Page<>(pageParam.getPageNum(), pageParam.getPageSize()),
  35. new QueryWrapper<SysGroupEntity>()
  36. .lambda()
  37. // 排序
  38. .orderByDesc(SysGroupEntity::getCreatedAt)
  39. );
  40. return this.entityPage2BOPage(entityPage);
  41. }
  42. /**
  43. * 根据 ID 取详情
  44. *
  45. * @param id 主键ID
  46. * @return null or BO
  47. */
  48. public SysGroupBO getOneById(Long id) {
  49. return this.getOneById(id, false);
  50. }
  51. /**
  52. * 根据 ID 取详情
  53. *
  54. * @param id 主键ID
  55. * @param throwIfInvalidId 是否在 ID 无效时抛出异常
  56. * @return null or BO
  57. */
  58. public SysGroupBO getOneById(Long id, boolean throwIfInvalidId) throws BusinessException {
  59. SysGroupEntity entity = sysGroupMapper.selectById(id);
  60. if (throwIfInvalidId && entity == null) {
  61. throw new BusinessException(400, "无效后台分组数据ID");
  62. }
  63. return this.entity2BO(entity);
  64. }
  65. /**
  66. * 后台管理-新增
  67. */
  68. @Transactional(rollbackFor = Exception.class)
  69. public Long adminInsert(AdminSysGroupInsertOrUpdateDTO dto) {
  70. log.info("[后台管理-后台分组数据-新增] >> DTO={}", dto);
  71. this.checkExistence(dto);
  72. dto.setId(null);
  73. SysGroupEntity entity = new SysGroupEntity();
  74. BeanUtil.copyProperties(dto, entity);
  75. sysGroupMapper.insert(entity);
  76. return entity.getId();
  77. }
  78. /**
  79. * 后台管理-编辑
  80. */
  81. @Transactional(rollbackFor = Exception.class)
  82. public void adminUpdate(AdminSysGroupInsertOrUpdateDTO dto) {
  83. log.info("[后台管理-后台分组数据-编辑] >> DTO={}", dto);
  84. this.checkExistence(dto);
  85. SysGroupEntity entity = new SysGroupEntity();
  86. BeanUtil.copyProperties(dto, entity);
  87. sysGroupMapper.updateById(entity);
  88. }
  89. /**
  90. * 后台管理-删除
  91. */
  92. @Transactional(rollbackFor = Exception.class)
  93. public void adminDelete(Collection<Long> ids) {
  94. log.info("[后台管理-后台分组数据-删除] >> ids={}", ids);
  95. sysGroupMapper.deleteByIds(ids);
  96. }
  97. /*
  98. ----------------------------------------------------------------
  99. 私有方法 private methods
  100. ----------------------------------------------------------------
  101. */
  102. /**
  103. * 实体转 BO
  104. *
  105. * @param entity 实体
  106. * @return BO
  107. */
  108. private SysGroupBO entity2BO(SysGroupEntity entity) {
  109. if (entity == null) {
  110. return null;
  111. }
  112. SysGroupBO bo = new SysGroupBO();
  113. BeanUtil.copyProperties(entity, bo);
  114. // 可以在此处为BO填充字段
  115. return bo;
  116. }
  117. /**
  118. * 实体 List 转 BO List
  119. *
  120. * @param entityList 实体 List
  121. * @return BO List
  122. */
  123. private List<SysGroupBO> entityList2BOs(List<SysGroupEntity> entityList) {
  124. if (CollUtil.isEmpty(entityList)) {
  125. return Collections.emptyList();
  126. }
  127. // 深拷贝
  128. List<SysGroupBO> ret = new ArrayList<>(entityList.size());
  129. entityList.forEach(
  130. entity -> ret.add(this.entity2BO(entity))
  131. );
  132. return ret;
  133. }
  134. /**
  135. * 实体分页转 BO 分页
  136. *
  137. * @param entityPage 实体分页
  138. * @return BO 分页
  139. */
  140. private PageResult<SysGroupBO> entityPage2BOPage(Page<SysGroupEntity> entityPage) {
  141. return new PageResult<SysGroupBO>()
  142. .setCurrent(entityPage.getCurrent())
  143. .setSize(entityPage.getSize())
  144. .setTotal(entityPage.getTotal())
  145. .setRecords(this.entityList2BOs(entityPage.getRecords()));
  146. }
  147. /**
  148. * 检查是否已存在同名数据
  149. *
  150. * @param dto DTO
  151. */
  152. private void checkExistence(AdminSysGroupInsertOrUpdateDTO dto) {
  153. /*
  154. 可以根据自己业务需要,解禁这段代码,修改判断条件和文案
  155. SysGroupEntity existingEntity = sysGroupMapper.selectOne(
  156. new QueryWrapper<SysGroupEntity>()
  157. .lambda()
  158. .select(SysGroupEntity::getId)
  159. .eq(SysGroupEntity::getTitle, dto.getTitle())
  160. .last(HelioConstant.CRUD.SQL_LIMIT_1)
  161. );
  162. if (existingEntity != null && !existingEntity.getId().equals(dto.getId())) {
  163. throw new BusinessException(400, "已存在相同后台分组数据,请重新输入");
  164. }
  165. */
  166. }
  167. }