6
jinlin
2023-12-03 c8d8a511f45c96ed3a5123a88e48de2ffdbf632a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package com.zt.modules.sys.service;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zt.common.constant.CacheKey;
import com.zt.common.constant.Constant;
import com.zt.common.db.query.QueryFilter;
import com.zt.common.exception.ErrorCode;
import com.zt.common.exception.RenException;
import com.zt.common.service.BaseService;
import com.zt.common.utils.CacheUtils;
import com.zt.core.sys.service.ISysParamsService;
import com.zt.modules.sys.dao.SysParamsDao;
import com.zt.modules.sys.dto.ConfigDto;
import com.zt.modules.sys.enums.ParamValueType;
import com.zt.modules.sys.model.SysParams;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * 参数管理
 *
 * @author hehz
 * @since 1.0.0
 */
@Service
public class SysParamsService extends BaseService<SysParamsDao, SysParams> implements ISysParamsService {
 
    public List<SysParams> page(QueryFilter queryFilter) {
        return super.query(queryFilter);
    }
 
    public List<ConfigDto> getConfigs() {
        List<ConfigDto> configs = (List<ConfigDto>) CacheUtils.get(Constant.Cache.SYS, CacheKey.SYS_CONFIG.getKey());
        if (configs == null) {
            List<SysParams> params = baseDao.selectList(new QueryWrapper<SysParams>()
                    .eq("is_to_web", Constant.Bool.YES).eq(Constant.TableColumn.IS_DELETE, Constant.Bool.NO)
                    .eq(Constant.TableColumn.TENANT_ID, Constant.Sys.PLATFORM_TENANT_ID));
            configs = params.stream().map(p -> {
                ConfigDto dto = new ConfigDto();
                dto.setKey(p.getParamCode());
                String value = p.getParamValue();
                if (ParamValueType.STRING.getValue().equals(p.getParamValueType())) {
                    dto.setValue(value);
                } else if (ParamValueType.INT.getValue().equals(p.getParamValueType())) {
                    dto.setValue(Integer.parseInt(value));
                } else if (ParamValueType.LONG.getValue().equals(p.getParamValueType())) {
                    dto.setValue(Long.parseLong(value));
                } else if (ParamValueType.NUMBER.getValue().equals(p.getParamValueType())) {
                    dto.setValue(Double.parseDouble(value));
                } else if (ParamValueType.BOOLEAN.getValue().equals(p.getParamValueType())) {
                    dto.setValue(Boolean.parseBoolean(value));
                }
                return dto;
            }).collect(Collectors.toList());
 
            CacheUtils.put(Constant.Cache.SYS, CacheKey.SYS_CONFIG.getKey(), configs);
        }
        return configs;
    }
 
    public SysParams get(Long id) {
        return baseDao.selectById(id);
    }
 
    @Transactional(rollbackFor = Exception.class)
    public void insert(SysParams entity) {
        super.insert(entity);
        CacheUtils.remove(Constant.Cache.SYS, CacheKey.SYS_CONFIG.getKey());// 清楚config缓存
    }
 
    @Transactional(rollbackFor = Exception.class)
    public void update(SysParams entity) {
        super.update(entity);
        CacheUtils.put(Constant.Cache.SYS, entity.getParamCode(), entity.getParamValue());
        CacheUtils.remove(Constant.Cache.SYS, CacheKey.SYS_CONFIG.getKey());// 清楚config缓存
    }
 
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long[] ids) {
        super.deleteLogic(ids);
        CacheUtils.remove(Constant.Cache.SYS, CacheKey.SYS_CONFIG.getKey());// 清楚config缓存
    }
 
    @Override
    public String getValue(String paramCode) {
        String paramValue = (String) CacheUtils.get(Constant.Cache.SYS, paramCode);
        if (paramValue == null) {
            paramValue = baseDao.getValueByCode(paramCode);
 
            CacheUtils.put(Constant.Cache.SYS, paramCode, paramValue);
        }
        return paramValue;
    }
 
    @Override
    public boolean getBooleanValue(String paramCode, boolean defaultValue) {
        String value = getValue(paramCode);
        return StringUtils.isEmpty(value) ? defaultValue : String.valueOf(Constant.Bool.YES).equals(value);
    }
 
    @Override
    public <T> T getValueObject(String paramCode, Class<T> clazz) {
        String paramValue = getValue(paramCode);
        if (StringUtils.isNotBlank(paramValue)) {
            return JSON.parseObject(paramValue, clazz);
        }
 
        try {
            return clazz.newInstance();
        } catch (Exception e) {
            throw new RenException(ErrorCode.PARAMS_GET_ERROR);
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateValueByCode(String paramCode, String paramValue) {
        int count = baseDao.updateValueByCode(paramCode, paramValue);
        CacheUtils.remove(Constant.Cache.SYS, CacheKey.SYS_CONFIG.getKey());// 清楚config缓存
        return count;
    }
}