admin
2025-02-25 30d8e227e8d823b6c38c3b9c90ac2df03b63befe
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
package com.yeshi.fanli.service.impl.redpack;
 
import java.util.Date;
import java.util.List;
 
import javax.annotation.Resource;
 
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
 
import com.yeshi.fanli.dao.mybatis.redpack.RedPackConfigMapper;
import com.yeshi.fanli.entity.redpack.RedPackConfig;
import com.yeshi.fanli.exception.redpack.RedPackConfigException;
import com.yeshi.fanli.service.inter.redpack.RedPackConfigService;
import com.yeshi.fanli.util.StringUtil;
 
@Service
public class RedPackConfigServiceImpl implements RedPackConfigService {
 
    @Resource
    private RedPackConfigMapper redPackConfigMapper;
 
    @Override
    public RedPackConfig getByKey(String key) {
        return redPackConfigMapper.getByKey(key, new Date());
    }
 
    @Override
    @Cacheable(value = "redpackCache", key = "'getValueByKey-' + #key")
    public String getValueByKey(String key) {
        RedPackConfig config = redPackConfigMapper.getByKey(key, new Date());
        if (config != null) {
            return config.getValue();
        }
        return null;
    }
 
    @Override
    public RedPackConfig getByKey(String key, Date date) {
        if (date == null)
            return getByKey(key);
        return redPackConfigMapper.getByKey(key, date);
    }
 
    @Override
    public String getValueByKey(String key, Date date) {
        if (date == null)
            return getValueByKey(key);
        RedPackConfig config = redPackConfigMapper.getByKey(key, date);
        if (config != null) {
            return config.getValue();
        }
        return null;
    }
    
    @Override
    public List<RedPackConfig> query(int page, int pageSize, String key) {
        return redPackConfigMapper.query((page - 1) * pageSize, pageSize, key);
    }
    
    @Override
    public long count(String key) {
        return redPackConfigMapper.count(key);
    }
    
    
    @Override
    public void save(RedPackConfig record) throws RedPackConfigException {
        String name = record.getName();
        if (StringUtil.isNullOrEmpty(name))
            throw new RedPackConfigException(1, "名称不能为空");
        
        if (StringUtil.isNullOrEmpty(record.getValue()))
            throw new RedPackConfigException(1, "值不能为空");
        
        if (StringUtil.isNullOrEmpty(record.getKey()))
            throw new RedPackConfigException(1, "标识不能为空");
 
        String remark = record.getRemark();
        if ("null".equalsIgnoreCase(remark)) {
            record.setRemark("");
        }
        
        record.setUpdateTime(new Date());
        if (record.getId() == null) {
            record.setCreateTime(new Date());
            redPackConfigMapper.insert(record);
        } else {
            RedPackConfig resultObj = redPackConfigMapper.selectByPrimaryKey(record.getId());
            if (resultObj == null)
                throw new RedPackConfigException(1, "修改内容已不存在");
            record.setCreateTime(resultObj.getCreateTime());
            redPackConfigMapper.updateByPrimaryKey(record);
        }
    }
    
    
    @Override
    public void delete(List<Long> idsList) {
        if (idsList != null)
            for (Long id : idsList)
                redPackConfigMapper.deleteByPrimaryKey(id);
 
    }
 
}