MrShi
11 小时以前 9eeb62c02a7b3c7b95c20678b6a9c74e7f12f943
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package com.doumee.service.system.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.doumee.core.constants.ResponseStatus;
import com.doumee.core.exception.BusinessException;
import com.doumee.core.douyin.DouyinClient;
import com.doumee.dao.business.web.request.LocaltionDTO;
import com.doumee.dao.business.web.response.DouyinConfigDTO;
import com.doumee.dao.business.web.response.MiniProgrammeDTO;
import com.doumee.dao.system.SystemDictMapper;
import com.doumee.dao.system.model.SystemDict;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.doumee.core.model.PageData;
import com.doumee.core.model.PageWrap;
import com.doumee.dao.system.SystemDictDataMapper;
import com.doumee.dao.system.dto.QuerySystemDictDataDTO;
import com.doumee.dao.system.model.SystemDictData;
import com.doumee.dao.system.vo.SystemDictDataListVO;
import com.doumee.service.system.SystemDictDataService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
 
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
 
import com.doumee.core.constants.Constants;
 
/**
 * 字典数据Service实现
 * @author Eva.Caesar Liu
 * @date 2022/03/15 09:54
 */
@Service
@Slf4j
public class SystemDictDataServiceImpl implements SystemDictDataService {
 
    @Autowired
    private SystemDictDataMapper systemDictDataMapper;
    @Autowired
    private SystemDictMapper systemDictMapper;
    /** 抖音 HTTP 客户端:用于改完应用配置后清空 client_token 缓存 */
    @Autowired
    private DouyinClient douyinClient;
 
    @Override
    public String create(SystemDictData systemDictData) {
        systemDictData.setId(UUID.randomUUID().toString());
        systemDictDataMapper.insert(systemDictData);
        return systemDictData.getId();
    }
 
    @Override
    public void deleteById(String id) {
        SystemDictData systemDictData = new SystemDictData();
        systemDictData.setId(id);
        systemDictData.setDeleted(Constants.ONE);
        this.updateById(systemDictData);
    }
 
    @Override
    @Transactional
    public void deleteByIdInBatch(List<String> ids) {
        if (CollectionUtils.isEmpty(ids)) return;
        for (String id : ids) {
            this.deleteById(id);
        }
    }
 
    @Override
    public void updateById(SystemDictData systemDictData) {
        systemDictDataMapper.updateById(systemDictData);
    }
 
    @Override
    @Transactional
    public void updateByIdInBatch(List<SystemDictData> systemDictDatas) {
        if (CollectionUtils.isEmpty(systemDictDatas)) return;
        for (SystemDictData systemDictData: systemDictDatas) {
            this.updateById(systemDictData);
        }
    }
 
    @Override
    public SystemDictData findById(String id) {
        return systemDictDataMapper.selectById(id);
    }
 
    @Override
    public SystemDictData findOne(SystemDictData systemDictData) {
        Wrapper<SystemDictData> wrapper = new QueryWrapper<>(systemDictData).last(" limit 1");
        return systemDictDataMapper.selectOne(wrapper );
    }
 
    @Override
    public List<SystemDictData> findList(SystemDictData systemDictData) {
        Wrapper<SystemDictData> wrapper = new QueryWrapper<>(systemDictData).orderByAsc("sort");
        return systemDictDataMapper.selectList(wrapper);
    }
 
    @Override
    public PageData<SystemDictDataListVO> findPage(PageWrap<QuerySystemDictDataDTO> pageWrap) {
        PageHelper.startPage(pageWrap.getPage(), pageWrap.getCapacity());
        return PageData.from(new PageInfo<>(systemDictDataMapper.selectManageList(pageWrap.getModel())));
    }
 
    @Override
    public long count(SystemDictData systemDictData) {
        Wrapper<SystemDictData> wrapper = new QueryWrapper<>(systemDictData);
        return systemDictDataMapper.selectCount(wrapper);
    }
 
    @Override
    public MiniProgrammeDTO getMiniProgrammeDTO() {
        try {
            String jasonStr = MiniProgrammeDTO.toUnderlineJSONString(new MiniProgrammeDTO());
            JSONObject parse = (JSONObject) JSONObject.parse(jasonStr);
            List<String> collect = parse.entrySet().stream().map(s -> s.getKey().toUpperCase()).collect(Collectors.toList());
            QueryWrapper<SystemDictData> wrapper = new QueryWrapper<>();
            wrapper.lambda()
                    .in(SystemDictData::getLabel,collect);
            List<SystemDictData> systemDictData = systemDictDataMapper.selectList(wrapper);
            if (CollectionUtils.isEmpty(systemDictData)){
                throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(),"字典不存在");
            }
            systemDictData.forEach(s->{
                parse.put(s.getLabel().toLowerCase(),s.getCode());
            });
            String s = parse.toJSONString();
            MiniProgrammeDTO miniProgrammeDTO = MiniProgrammeDTO.toSnakeObject(s, MiniProgrammeDTO.class);
            return miniProgrammeDTO;
        } catch (Exception e) {
           throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"字典值解析有误");
        }
    }
 
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    @Override
    public void updateMiniProgrammeDTO(MiniProgrammeDTO miniProgrammeDTO) {
        try {
            if(miniProgrammeDTO.getParkLatLngList()!=null){
                try {
                    TypeReference typeReference =  new TypeReference<List<LocaltionDTO>>(){};
                    List<LocaltionDTO> response = JSONObject.parseObject(miniProgrammeDTO.getParkLatLngList(), typeReference.getType());
                }catch (Exception e){
                    e.printStackTrace();
                    throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"运营区域参数解析有误!");
                }
            }
            String jasonStr = MiniProgrammeDTO.toUnderlineJSONString(miniProgrammeDTO);
            JSONObject parse = (JSONObject) JSONObject.parse(jasonStr);
            parse.entrySet().forEach(s->{
//                if (StringUtils.isNotBlank((String)s.getValue())){
                    UpdateWrapper<SystemDictData> wrapper = new UpdateWrapper<>();
                    wrapper.lambda()
                            .eq(SystemDictData::getLabel,s.getKey().toUpperCase())
                            .set(SystemDictData::getCode,s.getValue());
                    systemDictDataMapper.update(null,wrapper);
//                }
            });
        } catch (JsonProcessingException e) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"字典值解析有误");
        }
    }
 
    @Override
    public DouyinConfigDTO getDouyinConfigDTO() {
        try {
            // 复用 MiniProgrammeDTO 的驼峰⇄下划线工具:对象属性 → {client_key,client_secret,account_id,poi_id}
            String jasonStr = MiniProgrammeDTO.toUnderlineJSONString(new DouyinConfigDTO());
            JSONObject parse = (JSONObject) JSONObject.parse(jasonStr);
            List<String> collect = parse.entrySet().stream().map(s -> s.getKey().toUpperCase()).collect(Collectors.toList());
            QueryWrapper<SystemDictData> wrapper = new QueryWrapper<>();
            wrapper.lambda()
                    .in(SystemDictData::getLabel,collect);
            List<SystemDictData> systemDictData = systemDictDataMapper.selectList(wrapper);
            if (CollectionUtils.isEmpty(systemDictData)){
                throw new BusinessException(ResponseStatus.DATA_EXISTS.getCode(),"字典不存在");
            }
            systemDictData.forEach(s->{
                parse.put(s.getLabel().toLowerCase(),s.getCode());
            });
            String s = parse.toJSONString();
            return MiniProgrammeDTO.toSnakeObject(s, DouyinConfigDTO.class);
        } catch (BusinessException e) {
            throw e;
        } catch (Exception e) {
            throw new BusinessException(ResponseStatus.SERVER_ERROR.getCode(),"字典值解析有误");
        }
    }
 
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    @Override
    public void updateDouyinAppConfigDTO(DouyinConfigDTO douyinConfigDTO) {
        try {
            String jasonStr = MiniProgrammeDTO.toUnderlineJSONString(douyinConfigDTO);
            JSONObject parse = (JSONObject) JSONObject.parse(jasonStr);
            // 仅更新抖音应用三项(client_key/client_secret/account_id),poi_id 由独立接口维护
            parse.entrySet().forEach(s->{
                UpdateWrapper<SystemDictData> wrapper = new UpdateWrapper<>();
                wrapper.lambda()
                        .eq(SystemDictData::getLabel,s.getKey().toUpperCase())
                        .set(SystemDictData::getCode,s.getValue());
                systemDictDataMapper.update(null,wrapper);
            });
        } catch (JsonProcessingException e) {
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"字典值解析有误");
        }
        // client_key/client_secret 改了之后,缓存的旧 access-token 已失效,清掉后下次调用才用新配置换取新 token。
        // 清缓存属于副作用,失败不回滚配置(配置已正确入库,token 下次过期自愈)。
        try {
            douyinClient.clearAccessToken();
        } catch (Exception e) {
            log.warn("更新抖音应用配置后清空 access-token 失败", e);
        }
    }
 
    @Transactional(rollbackFor = {Exception.class,BusinessException.class})
    @Override
    public void updateDouyinPoiIdDTO(String poiId) {
        if (StringUtils.isBlank(poiId)){
            throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(),"门店ID不能为空");
        }
        UpdateWrapper<SystemDictData> wrapper = new UpdateWrapper<>();
        wrapper.lambda()
                .eq(SystemDictData::getLabel, Constants.DOUYIN_POI_ID)
                .set(SystemDictData::getCode,poiId);
        systemDictDataMapper.update(null,wrapper);
    }
}