| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- package com.izouma.nineth.service;
- import com.izouma.nineth.domain.SysConfig;
- import com.izouma.nineth.dto.PageQuery;
- import com.izouma.nineth.exception.BusinessException;
- import com.izouma.nineth.repo.SysConfigRepo;
- import com.izouma.nineth.utils.JpaUtils;
- import lombok.AllArgsConstructor;
- import org.springframework.cache.annotation.Cacheable;
- import org.springframework.data.domain.Page;
- import org.springframework.stereotype.Service;
- import javax.annotation.PostConstruct;
- import java.math.BigDecimal;
- import java.time.LocalTime;
- import java.time.format.DateTimeFormatter;
- @Service
- @AllArgsConstructor
- public class SysConfigService {
- private SysConfigRepo sysConfigRepo;
- public Page<SysConfig> all(PageQuery pageQuery) {
- return sysConfigRepo.findAll(JpaUtils.toSpecification(pageQuery, SysConfig.class), JpaUtils.toPageRequest(pageQuery));
- }
- @Cacheable("SysConfigServiceGetBigDecimal")
- public BigDecimal getBigDecimal(String name) {
- return sysConfigRepo.findByName(name).map(sysConfig -> new BigDecimal(sysConfig.getValue()))
- .orElse(BigDecimal.ZERO);
- }
- @Cacheable("SysConfigServiceGetTime")
- public LocalTime getTime(String name) {
- String str = sysConfigRepo.findByName(name).map(SysConfig::getValue)
- .orElseThrow(new BusinessException("配置不存在"));
- DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("HH:mm");
- return LocalTime.from(dateTimeFormatter.parse(str));
- }
- @Cacheable("SysConfigServiceGetBoolean")
- public boolean getBoolean(String name) {
- String str = sysConfigRepo.findByName(name).map(SysConfig::getValue)
- .orElseThrow(new BusinessException("配置不存在"));
- return str.equals("1");
- }
- @Cacheable("SysConfigServiceGetInt")
- public int getInt(String name) {
- String str = sysConfigRepo.findByName(name).map(SysConfig::getValue)
- .orElseThrow(new BusinessException("配置不存在"));
- return Integer.parseInt(str);
- }
- @PostConstruct
- public void init() {
- if (!sysConfigRepo.findByName("gift_gas_fee").isPresent()) {
- sysConfigRepo.save(SysConfig.builder()
- .name("gift_gas_fee")
- .desc("转赠gas费")
- .type(SysConfig.ValueType.NUMBER)
- .value("1")
- .build());
- }
- if (!sysConfigRepo.findByName("enable_wx_pub").isPresent()) {
- sysConfigRepo.save(SysConfig.builder()
- .name("enable_wx_pub")
- .desc("使用公众号支付")
- .type(SysConfig.ValueType.BOOLEAN)
- .value("FALSE")
- .build());
- }
- }
- }
|