GlobalConfigServiceImpl.java
3.68 KB
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
package cn.fw.dalaran.service.data.impl;
import cn.fw.dalaran.common.utils.PublicUtil;
import cn.fw.dalaran.dao.GlobalConfigDao;
import cn.fw.dalaran.domain.db.GlobalConfig;
import cn.fw.dalaran.domain.enums.ConfigEnum;
import cn.fw.dalaran.service.data.GlobalConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/**
* @author wmy3969
* @version 1.0
* @date 2022-05-07 15:31:40
* @Description 封面/话题相似度配置
*/
@Service("globalConfigService")
public class GlobalConfigServiceImpl extends ServiceImpl<GlobalConfigDao, GlobalConfig> implements GlobalConfigService {
private GlobalConfigServiceImpl globalConfigService;
@Autowired
public void inject(GlobalConfigServiceImpl globalConfigService) {
this.globalConfigService = globalConfigService;
}
/**
* 获取列表
*
* @param configGroupId 所属配置组id
* @return
*/
@Override
public List<GlobalConfig> queryList(Long configGroupId) {
List<GlobalConfig> list;
list = this.lambdaQuery()
.eq(GlobalConfig::getConfigGroupId, configGroupId)
.list();// 获取数据库中的配置列表
final int length = ConfigEnum.values().length;// 获取枚举中规定的配置数
if (list.size() != length) {
//list = ((GlobalConfigServiceImpl) AopContext.currentProxy()).initConfigList(configGroupId);
list = this.initConfigList(configGroupId);
}
return list;
}
/**
* 更新配置
*
* @param globalConfigs 参数封装
*/
@Override
public void updateConfig(List<GlobalConfig.GlobalConfigVo> globalConfigs) {
PublicUtil.assertFalse(globalConfigs.stream()
.anyMatch(item -> item.getRate().compareTo(BigDecimal.ZERO) < 0), "占比必须 > 0%");
PublicUtil.assertFalse(globalConfigs.stream()
.anyMatch(item -> item.getRate().compareTo(BigDecimal.valueOf(100)) > 0), "占比必须 < 100%");
this.saveOrUpdateBatch(PublicUtil.copyList(globalConfigs, GlobalConfig.class));
}
/**
* 根据枚举初始化视频最低标准配置
*
* @param configGroupId 所属配置组id
* @return
*/
@Transactional(rollbackFor = Exception.class)
public List<GlobalConfig> initConfigList(Long configGroupId) {
final List<GlobalConfig> list = this.lambdaQuery()
.eq(GlobalConfig::getConfigGroupId, configGroupId)
.list();// 获取原来配置
this.removeByIds(list.stream()
.map(GlobalConfig::getId)
.collect(Collectors.toList()));// 删除原来配置
final List<GlobalConfig> collect = Arrays.stream(ConfigEnum.values())
.map(item -> {
BigDecimal rate = BigDecimal.valueOf(100);
List<GlobalConfig> collect1 = list.stream()
.filter(item1 -> Objects.equals(item.getValue(), item1.getType()))
.collect(Collectors.toList());
if (!collect1.isEmpty()) {
rate = collect1.get(0).getRate();
}
return new GlobalConfig(configGroupId, item.getValue(), rate);
})
.collect(Collectors.toList());
this.saveBatch(collect);
//int i = 1 / 0;
return collect;
}
}