SysConfigService.java 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package com.izouma.nineth.service;
  2. import com.izouma.nineth.domain.SysConfig;
  3. import com.izouma.nineth.dto.PageQuery;
  4. import com.izouma.nineth.exception.BusinessException;
  5. import com.izouma.nineth.repo.SysConfigRepo;
  6. import com.izouma.nineth.utils.JpaUtils;
  7. import lombok.AllArgsConstructor;
  8. import org.springframework.cache.annotation.Cacheable;
  9. import org.springframework.data.domain.Page;
  10. import org.springframework.stereotype.Service;
  11. import javax.annotation.PostConstruct;
  12. import java.math.BigDecimal;
  13. import java.time.LocalTime;
  14. import java.time.format.DateTimeFormatter;
  15. @Service
  16. @AllArgsConstructor
  17. public class SysConfigService {
  18. private SysConfigRepo sysConfigRepo;
  19. public Page<SysConfig> all(PageQuery pageQuery) {
  20. return sysConfigRepo.findAll(JpaUtils.toSpecification(pageQuery, SysConfig.class), JpaUtils.toPageRequest(pageQuery));
  21. }
  22. @Cacheable("SysConfigServiceGetBigDecimal")
  23. public BigDecimal getBigDecimal(String name) {
  24. return sysConfigRepo.findByName(name).map(sysConfig -> new BigDecimal(sysConfig.getValue()))
  25. .orElse(BigDecimal.ZERO);
  26. }
  27. @Cacheable("SysConfigServiceGetTime")
  28. public LocalTime getTime(String name) {
  29. String str = sysConfigRepo.findByName(name).map(SysConfig::getValue)
  30. .orElseThrow(new BusinessException("配置不存在"));
  31. DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm");
  32. return LocalTime.from(dateTimeFormatter.parse(str));
  33. }
  34. @Cacheable("SysConfigServiceGetBoolean")
  35. public boolean getBoolean(String name) {
  36. String str = sysConfigRepo.findByName(name).map(SysConfig::getValue)
  37. .orElseThrow(new BusinessException("配置不存在"));
  38. return str.equals("1");
  39. }
  40. @Cacheable("SysConfigServiceGetInt")
  41. public int getInt(String name) {
  42. String str = sysConfigRepo.findByName(name).map(SysConfig::getValue)
  43. .orElseThrow(new BusinessException("配置不存在"));
  44. return Integer.parseInt(str);
  45. }
  46. @PostConstruct
  47. public void init() {
  48. if (!sysConfigRepo.findByName("gift_gas_fee").isPresent()) {
  49. sysConfigRepo.save(SysConfig.builder()
  50. .name("gift_gas_fee")
  51. .desc("转赠gas费")
  52. .type(SysConfig.ValueType.NUMBER)
  53. .value("1")
  54. .build());
  55. }
  56. if (!sysConfigRepo.findByName("enable_wx_pub").isPresent()) {
  57. sysConfigRepo.save(SysConfig.builder()
  58. .name("enable_wx_pub")
  59. .desc("使用公众号支付")
  60. .type(SysConfig.ValueType.BOOLEAN)
  61. .value("FALSE")
  62. .build());
  63. }
  64. }
  65. }