SysParamService.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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.util.StrUtil;
  16. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  17. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  18. import lombok.extern.slf4j.Slf4j;
  19. import org.springframework.stereotype.Service;
  20. import org.springframework.transaction.annotation.Transactional;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. /**
  24. * 系统参数
  25. * @author Uncarbon
  26. */
  27. @Slf4j
  28. @Service
  29. public class SysParamService extends HelioBaseServiceImpl<SysParamMapper, SysParamEntity> {
  30. /**
  31. * 后台管理-分页列表
  32. */
  33. public PageResult<SysParamBO> adminList(PageParam pageParam, AdminListSysParamDTO dto) {
  34. Page<SysParamEntity> entityPage = this.page(
  35. new Page<>(pageParam.getPageNum(), pageParam.getPageSize()),
  36. new QueryWrapper<SysParamEntity>()
  37. .lambda()
  38. // 键名
  39. .like(StrUtil.isNotBlank(dto.getKey()), SysParamEntity::getKey, StrUtil.cleanBlank(dto.getKey()))
  40. // 描述
  41. .like(StrUtil.isNotBlank(dto.getDescription()), SysParamEntity::getDescription, StrUtil.cleanBlank(dto.getDescription()))
  42. // 排序
  43. .orderByDesc(SysParamEntity::getCreatedAt)
  44. );
  45. return this.entityPage2BOPage(entityPage);
  46. }
  47. /**
  48. * 通用-详情
  49. */
  50. public SysParamBO getOneById(Long entityId) {
  51. SysParamEntity entity = this.getById(entityId);
  52. SysErrorEnum.INVALID_ID.assertNotNull(entity);
  53. return this.entity2BO(entity);
  54. }
  55. /**
  56. * 后台管理-新增
  57. */
  58. @SysLog(value = "新增系统参数")
  59. @Transactional(rollbackFor = Exception.class)
  60. public Long adminInsert(AdminInsertOrUpdateSysParamDTO dto) {
  61. this.checkExist(dto);
  62. dto.setId(null);
  63. SysParamEntity entity = new SysParamEntity();
  64. BeanUtil.copyProperties(dto, entity);
  65. this.save(entity);
  66. return entity.getId();
  67. }
  68. /**
  69. * 后台管理-编辑
  70. */
  71. @SysLog(value = "编辑系统参数")
  72. @Transactional(rollbackFor = Exception.class)
  73. public void adminUpdate(AdminInsertOrUpdateSysParamDTO dto) {
  74. this.checkExist(dto);
  75. SysParamEntity entity = new SysParamEntity();
  76. BeanUtil.copyProperties(dto, entity);
  77. this.updateById(entity);
  78. }
  79. /**
  80. * 后台管理-删除
  81. */
  82. @SysLog(value = "删除系统参数")
  83. @Transactional(rollbackFor = Exception.class)
  84. public void adminDelete(List<Long> ids) {
  85. this.removeByIds(ids);
  86. }
  87. /**
  88. * 根据键名取值
  89. * @param key 键名
  90. * @return 成功返回值, 失败返回null
  91. */
  92. public String getParamValueByKey(String key) {
  93. SysParamEntity sysParamEntity = this.getOne(
  94. new QueryWrapper<SysParamEntity>()
  95. .select(" value ")
  96. .lambda()
  97. .eq(SysParamEntity::getKey, key)
  98. .last(HelioConstant.CRUD.SQL_LIMIT_1)
  99. );
  100. if (sysParamEntity == null) {
  101. return null;
  102. }
  103. return sysParamEntity.getValue();
  104. }
  105. /**
  106. * 根据键名取值
  107. *
  108. * @param key 键名
  109. * @param defaultValue 默认值
  110. * @return 成功返回值, 失败返回defaultValue
  111. */
  112. public String getParamValueByKey(String key, String defaultValue) {
  113. String value = this.getParamValueByKey(key);
  114. if (value == null) {
  115. return defaultValue;
  116. }
  117. return value;
  118. }
  119. /*
  120. 私有方法
  121. ------------------------------------------------------------------------------------------------
  122. */
  123. private SysParamBO entity2BO(SysParamEntity entity) {
  124. if (entity == null) {
  125. return null;
  126. }
  127. SysParamBO bo = new SysParamBO();
  128. BeanUtil.copyProperties(entity, bo);
  129. // 可以在此处为BO填充字段
  130. return bo;
  131. }
  132. private PageResult<SysParamBO> entityPage2BOPage(Page<SysParamEntity> entityPage) {
  133. // 深拷贝
  134. List<SysParamBO> boRecords = new ArrayList<>(entityPage.getRecords().size());
  135. entityPage.getRecords().forEach(
  136. entity -> boRecords.add(this.entity2BO(entity))
  137. );
  138. PageResult<SysParamBO> ret = new PageResult<>();
  139. BeanUtil.copyProperties(entityPage, ret);
  140. ret.setRecords(boRecords);
  141. return ret;
  142. }
  143. /**
  144. * 检查是否已存在同名数据
  145. * @param dto DTO
  146. */
  147. private void checkExist(AdminInsertOrUpdateSysParamDTO dto) {
  148. SysParamEntity existEntity = this.getOne(
  149. new QueryWrapper<SysParamEntity>()
  150. .select( " id ")
  151. .lambda()
  152. .eq(SysParamEntity::getDescription, dto.getDescription())
  153. .or()
  154. .eq(SysParamEntity::getKey, dto.getKey())
  155. .last(HelioConstant.CRUD.SQL_LIMIT_1)
  156. );
  157. if (existEntity != null && !existEntity.getId().equals(dto.getId())) {
  158. throw new BusinessException(400, "已存在相同系统参数,请重新输入");
  159. }
  160. }
  161. }