SysParamService.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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.annotation.SysLog;
  8. import cc.uncarbon.module.sys.entity.SysParamEntity;
  9. import cc.uncarbon.module.sys.enums.SysErrorEnum;
  10. import cc.uncarbon.module.sys.mapper.SysParamMapper;
  11. import cc.uncarbon.module.sys.model.request.AdminInsertOrUpdateSysParamDTO;
  12. import cc.uncarbon.module.sys.model.request.AdminListSysParamDTO;
  13. import cc.uncarbon.module.sys.model.response.SysParamBO;
  14. import cn.hutool.core.bean.BeanUtil;
  15. import cn.hutool.core.collection.CollUtil;
  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 java.util.ArrayList;
  20. import java.util.Collection;
  21. import java.util.Collections;
  22. import java.util.List;
  23. import lombok.extern.slf4j.Slf4j;
  24. import org.springframework.stereotype.Service;
  25. import org.springframework.transaction.annotation.Transactional;
  26. /**
  27. * 系统参数
  28. *
  29. * @author Uncarbon
  30. */
  31. @Slf4j
  32. @Service
  33. public class SysParamService extends HelioBaseServiceImpl<SysParamMapper, SysParamEntity> {
  34. /**
  35. * 后台管理-分页列表
  36. */
  37. public PageResult<SysParamBO> adminList(PageParam pageParam, AdminListSysParamDTO dto) {
  38. Page<SysParamEntity> entityPage = this.page(
  39. new Page<>(pageParam.getPageNum(), pageParam.getPageSize()),
  40. new QueryWrapper<SysParamEntity>()
  41. .lambda()
  42. // 键名
  43. .like(StrUtil.isNotBlank(dto.getName()), SysParamEntity::getName, StrUtil.cleanBlank(dto.getName()))
  44. // 描述
  45. .like(StrUtil.isNotBlank(dto.getDescription()), SysParamEntity::getDescription, StrUtil.cleanBlank(dto.getDescription()))
  46. // 排序
  47. .orderByDesc(SysParamEntity::getCreatedAt)
  48. );
  49. return this.entityPage2BOPage(entityPage);
  50. }
  51. /**
  52. * 根据 ID 取详情
  53. *
  54. * @param id 主键ID
  55. * @return null or BO
  56. */
  57. public SysParamBO getOneById(Long id) {
  58. return this.getOneById(id, false);
  59. }
  60. /**
  61. * 根据 ID 取详情
  62. *
  63. * @param id 主键ID
  64. * @param throwIfInvalidId 是否在 ID 无效时抛出异常
  65. * @return null or BO
  66. */
  67. public SysParamBO getOneById(Long id, boolean throwIfInvalidId) throws BusinessException {
  68. SysParamEntity entity = this.getById(id);
  69. if (throwIfInvalidId) {
  70. SysErrorEnum.INVALID_ID.assertNotNull(entity);
  71. }
  72. return this.entity2BO(entity);
  73. }
  74. /**
  75. * 后台管理-新增
  76. */
  77. @SysLog(value = "新增系统参数")
  78. @Transactional(rollbackFor = Exception.class)
  79. public Long adminInsert(AdminInsertOrUpdateSysParamDTO dto) {
  80. log.info("[后台管理-新增系统参数] >> 入参={}", dto);
  81. this.checkExistence(dto);
  82. dto.setId(null);
  83. SysParamEntity entity = new SysParamEntity();
  84. BeanUtil.copyProperties(dto, entity);
  85. this.save(entity);
  86. return entity.getId();
  87. }
  88. /**
  89. * 后台管理-编辑
  90. */
  91. @SysLog(value = "编辑系统参数")
  92. @Transactional(rollbackFor = Exception.class)
  93. public void adminUpdate(AdminInsertOrUpdateSysParamDTO dto) {
  94. log.info("[后台管理-编辑系统参数] >> 入参={}", dto);
  95. this.checkExistence(dto);
  96. SysParamEntity entity = new SysParamEntity();
  97. BeanUtil.copyProperties(dto, entity);
  98. this.updateById(entity);
  99. }
  100. /**
  101. * 后台管理-删除
  102. */
  103. @SysLog(value = "删除系统参数")
  104. @Transactional(rollbackFor = Exception.class)
  105. public void adminDelete(Collection<Long> ids) {
  106. log.info("[后台管理-删除系统参数] >> 入参={}", ids);
  107. this.removeByIds(ids);
  108. }
  109. /**
  110. * 根据键名取值
  111. *
  112. * @param name 键名
  113. * @return 成功返回键值,失败返回 null
  114. */
  115. public String getParamValueByName(String name) {
  116. SysParamEntity sysParamEntity = this.getOne(
  117. new QueryWrapper<SysParamEntity>()
  118. .lambda()
  119. .select(SysParamEntity::getValue)
  120. .eq(SysParamEntity::getName, name)
  121. .last(HelioConstant.CRUD.SQL_LIMIT_1)
  122. );
  123. if (sysParamEntity == null) {
  124. return null;
  125. }
  126. return sysParamEntity.getValue();
  127. }
  128. /**
  129. * 根据键名取值
  130. *
  131. * @param name 键名
  132. * @param defaultValue 默认值
  133. * @return 成功返回键值,失败返回 defaultValue
  134. */
  135. public String getParamValueByName(String name, String defaultValue) {
  136. String value = this.getParamValueByName(name);
  137. if (value == null) {
  138. return defaultValue;
  139. }
  140. return value;
  141. }
  142. /*
  143. ----------------------------------------------------------------
  144. 私有方法 private methods
  145. ----------------------------------------------------------------
  146. */
  147. /**
  148. * 实体转 BO
  149. *
  150. * @param entity 实体
  151. * @return BO
  152. */
  153. private SysParamBO entity2BO(SysParamEntity entity) {
  154. if (entity == null) {
  155. return null;
  156. }
  157. SysParamBO bo = new SysParamBO();
  158. BeanUtil.copyProperties(entity, bo);
  159. // 可以在此处为BO填充字段
  160. return bo;
  161. }
  162. /**
  163. * 实体 List 转 BO List
  164. *
  165. * @param entityList 实体 List
  166. * @return BO List
  167. */
  168. private List<SysParamBO> entityList2BOs(List<SysParamEntity> entityList) {
  169. if (CollUtil.isEmpty(entityList)) {
  170. return Collections.emptyList();
  171. }
  172. // 深拷贝
  173. List<SysParamBO> ret = new ArrayList<>(entityList.size());
  174. entityList.forEach(
  175. entity -> ret.add(this.entity2BO(entity))
  176. );
  177. return ret;
  178. }
  179. /**
  180. * 实体分页转 BO 分页
  181. *
  182. * @param entityPage 实体分页
  183. * @return BO 分页
  184. */
  185. private PageResult<SysParamBO> entityPage2BOPage(Page<SysParamEntity> entityPage) {
  186. PageResult<SysParamBO> ret = new PageResult<>();
  187. BeanUtil.copyProperties(entityPage, ret);
  188. ret.setRecords(this.entityList2BOs(entityPage.getRecords()));
  189. return ret;
  190. }
  191. /**
  192. * 检查是否已存在相同数据
  193. *
  194. * @param dto DTO
  195. */
  196. private void checkExistence(AdminInsertOrUpdateSysParamDTO dto) {
  197. SysParamEntity existingEntity = this.getOne(
  198. new QueryWrapper<SysParamEntity>()
  199. .lambda()
  200. // 仅取主键ID
  201. .select(SysParamEntity::getId)
  202. // 描述相同
  203. .eq(SysParamEntity::getDescription, dto.getDescription())
  204. .or()
  205. // 或键名相同
  206. .eq(SysParamEntity::getName, dto.getName())
  207. .last(HelioConstant.CRUD.SQL_LIMIT_1)
  208. );
  209. if (existingEntity != null && !existingEntity.getId().equals(dto.getId())) {
  210. throw new BusinessException(400, "已存在相同系统参数,请重新输入");
  211. }
  212. }
  213. }