jiangqs
2023-06-07 1fc05375474ca0393d6c29fd2ab1d182c65a3eaa
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package com.ruoyi.system.service.impl.config;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.api.constant.ConfigEnum;
import com.ruoyi.system.api.domain.poji.config.Activeness;
import com.ruoyi.system.domain.dto.MgtMemberConfigEditDto;
import com.ruoyi.system.domain.dto.MgtOtherConfigEditDto;
import com.ruoyi.system.domain.dto.MgtServiceMobileEditDto;
import com.ruoyi.system.domain.dto.MgtShopConfigEditDto;
import com.ruoyi.system.domain.pojo.config.Advert;
import com.ruoyi.system.domain.pojo.config.CustomConfig;
import com.ruoyi.system.domain.vo.MgtActivenessVo;
import com.ruoyi.system.domain.vo.MgtCustomConfigVo;
import com.ruoyi.system.mapper.config.CustomConfigMapper;
import com.ruoyi.system.service.config.ActivenessService;
import com.ruoyi.system.service.config.AdvertService;
import com.ruoyi.system.service.config.CustomConfigService;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 系统配置 服务实现类
 * </p>
 *
 * @author jqs
 * @since 2023-04-25
 */
@Service
public class CustomConfigServiceImpl extends ServiceImpl<CustomConfigMapper, CustomConfig> implements CustomConfigService {
 
    @Resource
    private CustomConfigMapper customConfigMapper;
 
    @Resource
    private AdvertService advertService;
 
    @Resource
    private ActivenessService activenessService;
 
 
    /**
     * @param key
     * @return CustomConfig
     * @description 通过key获取系统自定义配置
     * @author jqs
     * @date 2023/6/6 11:55
     */
    @Override
    public CustomConfig getByKey(String key) {
        return customConfigMapper.getByKey(key);
    }
 
    /**
     * @param mgtServiceMobileEditDto
     * @return void
     * @description 修改客服号码
     * @author jqs
     * @date 2023/6/6 11:53
     */
    @Override
    public void editServiceMobile(MgtServiceMobileEditDto mgtServiceMobileEditDto) {
        // 获取平台服务电话的配置
        CustomConfig customConfig = getByKey(ConfigEnum.PLATFORM_SERVICE_PHONE.getKey());
        // 如果配置不存在,则创建一个新的配置
        if (customConfig == null) {
            customConfig = new CustomConfig();
            customConfig.setCreateTime(new Date());
        }
        // 设置配置的类型、键、名称、值和更新时间
        customConfig.setType(ConfigEnum.PLATFORM_SERVICE_PHONE.getKeyType());
        customConfig.setKey(ConfigEnum.PLATFORM_SERVICE_PHONE.getKey());
        customConfig.setName(ConfigEnum.PLATFORM_SERVICE_PHONE.getKeyName());
        customConfig.setValue(mgtServiceMobileEditDto.getServiceMobile());
        customConfig.setUpdateTime(new Date());
        // 保存或更新配置
        this.saveOrUpdate(customConfig);
    }
 
    /**
     * @param
     * @return MgtCustomConfigVo
     * @description 平台获取自定义配置
     * @author jqs
     * @date 2023/6/7 15:02
     */
    @Override
    public MgtCustomConfigVo getCustomConfig() {
        MgtCustomConfigVo mgtCustomConfigVo = new MgtCustomConfigVo();
        //获取自定义配置
        List<CustomConfig> customConfigList = customConfigMapper.selectList(new LambdaQueryWrapper<CustomConfig>().eq(CustomConfig::getDelFlag, 0));
        Optional.ofNullable(customConfigList).ifPresent(list -> {
            Map<String, CustomConfig> configMap = list.stream().collect(Collectors.toMap(CustomConfig::getKey, Function.identity()));
            // 遍历配置映射,设置相应的属性值
            configMap.forEach((key, value) -> {
                switch (key) {
                    case "SHARE_INTEGRAL":
                        mgtCustomConfigVo.setShareIntegral(Integer.valueOf(value.getValue()));
                        break;
                    case "SUGGEST_PLATFORM_INTEGRAL":
                        mgtCustomConfigVo.setSuggestPlatformIntegral(Integer.valueOf(value.getValue()));
                        break;
                    case "SIGN_IN_INTEGRAL":
                        mgtCustomConfigVo.setSignInIntegral(Integer.valueOf(value.getValue()));
                        break;
                    case "SUGGEST_SHOP_INTEGRAL":
                        mgtCustomConfigVo.setSuggestShopIntegral(Integer.valueOf(value.getValue()));
                        break;
                    case "PAY_MONEY_INTEGRAL":
                        mgtCustomConfigVo.setPayMoneyIntegral(Integer.valueOf(value.getValue()));
                        break;
                    case "ORDER_AUTO_CANCEL_TIME":
                        mgtCustomConfigVo.setOrderAutoCancelTime(Integer.valueOf(value.getValue()));
                        break;
                    case "HOME_STYLE":
                        mgtCustomConfigVo.setHomeStyle(Integer.valueOf(value.getValue()));
                        break;
                    case "SHOP_EMERGENCY_DEGREE":
                        mgtCustomConfigVo.setShopEmergencyDegree(value.getValue());
                        break;
                    case "SHOP_FOLLOW_TYPE":
                        mgtCustomConfigVo.setShopFollowType(value.getValue());
                        break;
                    case "SHOP_CUSTOM_STATUS":
                        mgtCustomConfigVo.setShopCustomStatus(value.getValue());
                        break;
                    case "SHOP_SOURCE_CHANNEL":
                        mgtCustomConfigVo.setShopSourceChannel(value.getValue());
                        break;
                    case "MEMBER_SOURCE_CHANNEL":
                        mgtCustomConfigVo.setMemberSourceChannel(value.getValue());
                        break;
                    case "MEMBER_CHARACTER":
                        mgtCustomConfigVo.setMemberCharacter(value.getValue());
                        break;
                    case "MEMBER_LEVEL":
                        mgtCustomConfigVo.setMemberLevel(value.getValue());
                        break;
                    case "MEMBER_TYPE":
                        mgtCustomConfigVo.setMemberType(value.getValue());
                        break;
                    case "NURSE_PROBLEM":
                        mgtCustomConfigVo.setNurseProblem(value.getValue());
                        break;
                    default:
                        break;
                }
            });
        });
        //获取广告配置
        Advert advert = advertService.getAdvert();
        if (advert != null) {
            mgtCustomConfigVo.setHomeSlogan(advert.getAdContent());
            mgtCustomConfigVo.setHomeLogo(advert.getLogoUrl());
            mgtCustomConfigVo.setAdUrl(advert.getAdUrl());
            mgtCustomConfigVo.setTargetType(advert.getTargetType());
            mgtCustomConfigVo.setLinkUrl(advert.getLinkUrl());
            mgtCustomConfigVo.setJumpType(advert.getJumpType());
            mgtCustomConfigVo.setJumpId(advert.getJumpId());
        }
        //获取活跃度配置
        List<Activeness> activenessList = activenessService.listActiveness();
        if (activenessList != null && !activenessList.isEmpty()) {
            List<MgtActivenessVo> mgtActivenessVoList = activenessList.stream()
                    .map(activeness -> {
                        MgtActivenessVo mgtActivenessVo = new MgtActivenessVo();
                        mgtActivenessVo.setActivenessId(activeness.getActivenessId());
                        mgtActivenessVo.setActivenessName(activeness.getActivenessName());
                        mgtActivenessVo.setStartDay(activeness.getStartDay());
                        mgtActivenessVo.setEndDay(activeness.getEndDay());
                        return mgtActivenessVo;
                    })
                    .collect(Collectors.toList());
            mgtCustomConfigVo.setMgtActivenessVoList(mgtActivenessVoList);
        }
        return mgtCustomConfigVo;
    }
 
 
    /**
     * @param mgtOtherConfigEditDto
     * @return void
     * @description 修改其他设置
     * @author jqs
     * @date 2023/6/7 16:07
     */
    @Override
    public void editOtherConfig(MgtOtherConfigEditDto mgtOtherConfigEditDto) {
        // 获取当前时间
        Date updateTime = new Date();
        // 删除历史配置
        deletePastConfig(updateTime);
        // 新建配置
        createNewConfig(mgtOtherConfigEditDto, updateTime);
    }
 
    /**
     * @param updateTime
     * @return void
     * @description 删除历史配置
     * @author jqs
     * @date 2023/6/7 16:37
     */
    private void deletePastConfig(Date updateTime) {
        List<CustomConfig> customConfigList = customConfigMapper.selectList(new LambdaQueryWrapper<CustomConfig>()
                .eq(CustomConfig::getDelFlag, 0)
                .eq(CustomConfig::getType, 2));
        for (CustomConfig customConfig : customConfigList) {
            customConfig.setDelFlag(1);
            customConfig.setUpdateTime(updateTime);
        }
        this.saveOrUpdateBatch(customConfigList);
    }
 
    /**
     * @param mgtOtherConfigEditDto
     * @param updateTime
     * @return void
     * @description 新建配置
     * @author jqs
     * @date 2023/6/7 16:37
     */
    private void createNewConfig(MgtOtherConfigEditDto mgtOtherConfigEditDto, Date updateTime) {
        for (ConfigEnum configEnum : ConfigEnum.values()) {
            CustomConfig customConfig = new CustomConfig();
            customConfig.setDelFlag(0);
            customConfig.setType(configEnum.getKeyType());
            customConfig.setKey(configEnum.getKey());
            customConfig.setName(configEnum.getKeyName());
            customConfig.setValue(getConfigValue(configEnum, mgtOtherConfigEditDto));
            customConfig.setCreateTime(updateTime);
            customConfig.setUpdateTime(updateTime);
            this.saveOrUpdate(customConfig);
        }
    }
 
    /**
     * @param configEnum
     * @param mgtOtherConfigEditDto
     * @return String
     * @description 获取配置参数
     * @author jqs
     * @date 2023/6/7 16:37
     */
    private String getConfigValue(ConfigEnum configEnum, MgtOtherConfigEditDto mgtOtherConfigEditDto) {
        switch (configEnum) {
            case SHARE_INTEGRAL:
                return mgtOtherConfigEditDto.getShareIntegral().toString();
            case SUGGEST_PLATFORM_INTEGRAL:
                return mgtOtherConfigEditDto.getSuggestPlatformIntegral().toString();
            case SIGN_IN_INTEGRAL:
                return mgtOtherConfigEditDto.getSignInIntegral().toString();
            case SUGGEST_SHOP_INTEGRAL:
                return mgtOtherConfigEditDto.getSuggestShopIntegral().toString();
            case ORDER_AUTO_CANCEL_TIME:
                return mgtOtherConfigEditDto.getOrderAutoCancelTime().toString();
            case PAY_MONEY_INTEGRAL:
                return mgtOtherConfigEditDto.getPayMoneyIntegral().toString();
            case HOME_STYLE:
                return mgtOtherConfigEditDto.getHomeStyle().toString();
            default:
                throw new IllegalArgumentException("Invalid ConfigEnum");
        }
    }
 
    /**
     * @param mgtShopConfigEditDto
     * @return void
     * @description 修改合作商配置
     * @author jqs
     * @date 2023/6/7 16:41
     */
    @Override
    public void editShopConfig(MgtShopConfigEditDto mgtShopConfigEditDto) {
        // 获取当前时间
        Date updateTime = new Date();
        // 查询所有未删除、类型为3的自定义配置
        List<CustomConfig> customConfigList = customConfigMapper.selectList(
                new LambdaQueryWrapper<CustomConfig>()
                        .eq(CustomConfig::getDelFlag, 0)
                        .eq(CustomConfig::getType, 3)
        );
        if (customConfigList != null && !customConfigList.isEmpty()) {
            String key;
            String oldValue;
            String newValue = null;
            List<CustomConfig> updatedCustomConfigs = new ArrayList<>();
            // 遍历配置列表,根据 key 更新配置值
            for (CustomConfig customConfig : customConfigList) {
                key = customConfig.getKey();
                oldValue = customConfig.getValue();
                switch (key) {
                    case "SHOP_EMERGENCY_DEGREE":
                        newValue = mgtShopConfigEditDto.getShopEmergencyDegree();
                        break;
                    case "SHOP_FOLLOW_TYPE":
                        newValue = mgtShopConfigEditDto.getShopFollowType();
                        break;
                    case "SHOP_CUSTOM_STATUS":
                        newValue = mgtShopConfigEditDto.getShopCustomStatus();
                        break;
                    case "SHOP_SOURCE_CHANNEL":
                        newValue = mgtShopConfigEditDto.getShopSourceChannel();
                        break;
                    default:
                        break;
                }
                customConfig.setValue(newValue);
                customConfig.setUpdateTime(updateTime);
                handleChangeConfig(oldValue, newValue, key);
                updatedCustomConfigs.add(customConfig);
            }
            this.saveOrUpdateBatch(updatedCustomConfigs);
        } else {
            // 如果没有配置,则创建新配置
            createNewConfig(ConfigEnum.SHOP_EMERGENCY_DEGREE, mgtShopConfigEditDto.getShopEmergencyDegree(), updateTime);
            createNewConfig(ConfigEnum.SHOP_FOLLOW_TYPE, mgtShopConfigEditDto.getShopFollowType(), updateTime);
            createNewConfig(ConfigEnum.SHOP_CUSTOM_STATUS, mgtShopConfigEditDto.getShopCustomStatus(), updateTime);
            createNewConfig(ConfigEnum.SHOP_SOURCE_CHANNEL, mgtShopConfigEditDto.getShopSourceChannel(), updateTime);
        }
    }
 
    /**
     * @description  修改会员配置
     * @author  jqs
     * @date    2023/6/7 17:16
     * @param mgtMemberConfigEditDto
     * @return  void
     */
    @Override
    public void editMemberConfig(MgtMemberConfigEditDto mgtMemberConfigEditDto){
        // 获取当前时间
        Date updateTime = new Date();
        // 查询所有未删除、类型为3的自定义配置
        List<CustomConfig> customConfigList = customConfigMapper.selectList(
                new LambdaQueryWrapper<CustomConfig>()
                        .eq(CustomConfig::getDelFlag, 0)
                        .eq(CustomConfig::getType, 4)
        );
        if (customConfigList != null && !customConfigList.isEmpty()) {
            String key;
            String oldValue;
            String newValue = null;
            List<CustomConfig> updatedCustomConfigs = new ArrayList<>();
            // 遍历配置列表,根据 key 更新配置值
            for (CustomConfig customConfig : customConfigList) {
                key = customConfig.getKey();
                oldValue = customConfig.getValue();
                switch (key) {
                    case "MEMBER_SOURCE_CHANNEL":
                        newValue = mgtMemberConfigEditDto.getMemberSourceChannel();
                        break;
                    case "MEMBER_CHARACTER":
                        newValue = mgtMemberConfigEditDto.getMemberCharacter();
                        break;
                    case "MEMBER_LEVEL":
                        newValue = mgtMemberConfigEditDto.getMemberLevel();
                        break;
                    case "MEMBER_TYPE":
                        newValue = mgtMemberConfigEditDto.getMemberType();
                        break;
                    case "NURSE_PROBLEM":
                        newValue = mgtMemberConfigEditDto.getNurseProblem();
                        break;
                    default:
                        break;
                }
                customConfig.setValue(newValue);
                customConfig.setUpdateTime(updateTime);
                handleChangeConfig(oldValue, newValue, key);
                updatedCustomConfigs.add(customConfig);
            }
            this.saveOrUpdateBatch(updatedCustomConfigs);
        } else {
            // 如果没有配置,则创建新配置
            createNewConfig(ConfigEnum.MEMBER_SOURCE_CHANNEL, mgtMemberConfigEditDto.getMemberSourceChannel(), updateTime);
            createNewConfig(ConfigEnum.MEMBER_CHARACTER, mgtMemberConfigEditDto.getMemberCharacter(), updateTime);
            createNewConfig(ConfigEnum.MEMBER_LEVEL, mgtMemberConfigEditDto.getMemberLevel(), updateTime);
            createNewConfig(ConfigEnum.MEMBER_TYPE, mgtMemberConfigEditDto.getMemberType(), updateTime);
            createNewConfig(ConfigEnum.NURSE_PROBLEM, mgtMemberConfigEditDto.getNurseProblem(), updateTime);
        }
    }
 
    /**
     * @description  新建配置
     * @author  jqs
     * @date    2023/6/7 17:06
     * @param configEnum
     * @param value
     * @param updateTime
     * @return  void
     */
    private void createNewConfig(ConfigEnum configEnum, String value, Date updateTime) {
        CustomConfig customConfig = new CustomConfig();
        customConfig.setDelFlag(0);
        customConfig.setType(configEnum.getKeyType());
        customConfig.setKey(configEnum.getKey());
        customConfig.setName(configEnum.getKeyName());
        customConfig.setValue(value);
        customConfig.setCreateTime(updateTime);
        customConfig.setUpdateTime(updateTime);
        this.saveOrUpdate(customConfig);
    }
 
    /**
     * @description  处理变动
     * @author  jqs
     * @date    2023/6/7 17:06
     * @param oldValue
     * @param newValue
     * @param key
     * @return  void
     */
    private void handleChangeConfig(String oldValue, String newValue, String key) {
        Set<String> oldSet = new HashSet<>(Arrays.asList(oldValue.split(",")));
        Set<String> newSet = new HashSet<>(Arrays.asList(newValue.split(",")));
        List<String> changeList = new ArrayList<>(oldSet);
        changeList.removeAll(newSet);
        if(changeList!=null&&!changeList.isEmpty()){
            switch(key){
                case "SHOP_EMERGENCY_DEGREE":
                    break;
                case "SHOP_FOLLOW_TYPE":
                    break;
                case "SHOP_CUSTOM_STATUS":
                    break;
                case "SHOP_SOURCE_CHANNEL":
                    break;
                case "MEMBER_SOURCE_CHANNEL":
                    break;
                case "MEMBER_CHARACTER":
                    break;
                case "MEMBER_LEVEL":
                    break;
                case "MEMBER_TYPE":
                    break;
                case "NURSE_PROBLEM":
                    break;
                default:
                    break;
            }
        }
    }
 
 
}