cloudroam
2025-01-07 1d0fc6126fb664e81a1a3737d8eaf4a618e7cb0f
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package com.mzl.flower.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mzl.flower.config.exception.ValidationException;
import com.mzl.flower.config.security.SecurityUtils;
import com.mzl.flower.dto.request.configParam.ConfigParamDTO;
import com.mzl.flower.dto.request.configParam.QueryConfigParamDTO;
import com.mzl.flower.dto.request.configParam.UpdateConfigParamDTO;
import com.mzl.flower.dto.response.configParam.ConfigCustomerServiceVO;
import com.mzl.flower.dto.response.configParam.ConfigParamVO;
import com.mzl.flower.entity.configParam.ConfigParamDO;
import com.mzl.flower.enums.ConfigParamEnum;
import com.mzl.flower.mapper.configParam.ConfigParamMapper;
import com.mzl.flower.mapper.configParam.ConfigParamMapperCustom;
import com.mzl.flower.service.ConfigParamService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author @TaoJie
 * @since 2024-12-02
 */
@Service
@RequiredArgsConstructor
public class ConfigParamServiceImpl extends ServiceImpl<ConfigParamMapper, ConfigParamDO> implements ConfigParamService {
 
    private final ConfigParamMapperCustom configParamMapperCustom;
 
    private final ConfigParamMapper configParamMapper;
    private final RedissonClient redissonClient;
    private static final String CONFIG_PARAM= "com.mzl.flower.service.impl:config_param:%s";
 
    @Override
    public List<ConfigParamDO> getList(QueryConfigParamDTO dto) {
        QueryWrapper<ConfigParamDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(ConfigParamDO::getDeleted,false)
                .eq(Objects.nonNull(dto.getParamGroupId()),ConfigParamDO::getParamGroupId,dto.getParamGroupId())
                .eq(StringUtils.isNotBlank(dto.getParamKey()),ConfigParamDO::getParamKey,dto.getParamKey())
                .eq(StringUtils.isNotBlank(dto.getParamGroup()),ConfigParamDO::getParamGroup,dto.getParamGroup())
                .eq(StringUtils.isNotBlank(dto.getParamGroupName()),ConfigParamDO::getParamGroupName,dto.getParamGroupName())
                .eq(StringUtils.isNotBlank(dto.getParamName()),ConfigParamDO::getParamName,dto.getParamName())
                ;
        return list(queryWrapper);
    }
 
    @Transactional
    @Override
    public boolean updateConfigParamBatch(UpdateConfigParamDTO dto) {
 
        if(CollectionUtils.isNotEmpty(dto.getParamList())){
            // 遍历循环查找是否存在
            List<ConfigParamDO> configParamUpdateList = dto.getParamList().stream().map(configParamDO -> {
                final ConfigParamDO configParamForUpdate = configParamMapperCustom.selectById(configParamDO.getId());
                if (null == configParamForUpdate) {
                    throw new ValidationException(String.format("ID:{}不存在!", configParamDO.getId()));
                }
                configParamForUpdate.setParamValue(configParamDO.getParamValue());
 
                //更新订单自动取消(分钟); 订单自动收货(天)
                if ("order".equals(configParamForUpdate.getParamGroup())) {
                    // 设置值到 Redis
                    RBucket<String> bucket = redissonClient.getBucket(CONFIG_PARAM + configParamForUpdate.getParamGroup() + configParamForUpdate.getParamKey());
                    bucket.set(configParamForUpdate.getParamValue());
                }
 
                return configParamForUpdate;
            }).collect(Collectors.toList());
 
            if(CollectionUtils.isNotEmpty(configParamUpdateList)){
                return saveOrUpdateBatch(configParamUpdateList);
            }
 
        }
        return false;
    }
 
    @Override
    public Map<String,Object> getBaseInfo() {
        QueryConfigParamDTO dto=new QueryConfigParamDTO();
        dto.setParamGroup(ConfigParamEnum.BASE.getName());
        final List<ConfigParamDO> list = getList(dto);
        // 把数据转换成map ,只需要获取list中单个item的paramKey ,paramValue
        Map<String, Object> resultMap=new HashMap<>();
        list.stream().forEach(item -> {
            resultMap.put(item.getParamKey(),item.getParamValue());
        });
 
        return resultMap;
    }
 
    @Override
    public String getBaseString(String paramGroup, String paramKey) {
 
        String bucketName = CONFIG_PARAM + paramGroup + paramKey;
        RBucket<String> bucket = redissonClient.getBucket(bucketName);
        String value = bucket.get();
 
        if (StringUtils.isNotBlank(value)) {
            return value;
        } else {
            String baseString = configParamMapper.getBaseString(paramGroup, paramKey);
            if (StringUtils.isNotBlank(baseString)) {
                return baseString;
            } else {
                if ("order_cancel_time".equals(paramKey) && "order".equals(paramGroup)) {
                    // 默认15分钟
                    return "15";
                }
                if ("order_auto_receive".equals(paramKey) && "order".equals(paramGroup)) {
                    // 默认5前的订单自动收货
                    return "5";
                }
            }
        }
        return null;
    }
 
    @Override
    public void saveConfigParam(ConfigParamDTO dto) {
        ConfigParamDO configParamDO = new ConfigParamDO();
        BeanUtils.copyProperties(dto, configParamDO);
        configParamDO.create(SecurityUtils.getUserId());
        configParamMapper.insert(configParamDO);
    }
 
    @Override
    public void updateConfigParam(ConfigParamDTO dto) {
        ConfigParamDO configParamDO = configParamMapper.selectById(dto.getId());
        BeanUtils.copyProperties(dto, configParamDO);
        configParamDO.update(SecurityUtils.getUserId());
        configParamMapper.updateById(configParamDO);
    }
 
    @Override
    public void deleteConfigParam(Long id) {
        configParamMapper.deleteById(id);
    }
 
    @Override
    public Page<ConfigParamVO> queryPage(QueryConfigParamDTO dto, Page page) {
        List<ConfigParamVO> list = configParamMapper.queryPage(dto, page);
        page.setRecords(list);
        return page;
    }
 
}