SettingBizService.java
3.8 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
102
103
104
105
package cn.fw.valhalla.service.bus.setting;
import cn.fw.valhalla.domain.dto.SettingDTO;
import cn.fw.valhalla.domain.enums.FollowTypeEnum;
import cn.fw.valhalla.domain.enums.SettingTypeEnum;
import cn.fw.valhalla.domain.vo.setting.SettingVO;
import cn.fw.valhalla.service.bus.setting.strategy.SettingStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import java.util.*;
import java.util.stream.Collectors;
import static cn.fw.common.businessvalidator.Validator.BV;
/**
* @author : kurisu
* @className : SettingBizService
* @description : 设置处理服务
* @date: 2020-08-14 09:17
*/
@Slf4j
@Service
public class SettingBizService {
/**
* 设置处理器map
*/
private final Map<FollowTypeEnum, SettingStrategy> processorMap;
@Autowired
public SettingBizService(final List<SettingStrategy> settingStrategyList) {
this.processorMap = settingStrategyList.stream().collect(Collectors.toMap(SettingStrategy::getFollowType, v -> v));
}
/**
* 根据跟进类型查询设置
*
* @param type
* @param groupId
* @return
*/
public List<SettingVO> querySettingByCat(Integer type, Long groupId) {
FollowTypeEnum followTypeEnum = FollowTypeEnum.ofValue(type);
Assert.notNull(followTypeEnum, "跟进类型不正确");
SettingStrategy strategy = processorMap.get(followTypeEnum);
Assert.notNull(strategy, "strategy cannot be null");
return Optional.ofNullable(strategy.getList(groupId)).orElse(new ArrayList<>());
}
/**
* 保存设置
*
* @param groupId
* @param list
*/
@Transactional(rollbackFor = Exception.class)
public boolean saveSetting(List<SettingDTO> list, Long groupId) {
Assert.isTrue(!CollectionUtils.isEmpty(list), "设置项不能为空");
final Integer category = list.get(0).getCategory();
FollowTypeEnum followTypeEnum = FollowTypeEnum.ofValue(category);
boolean allMatch = list.stream().allMatch(r -> Objects.equals(category, r.getCategory()));
boolean expression = Objects.nonNull(followTypeEnum) && Boolean.TRUE.equals(allMatch);
for (SettingDTO dto : list) {
if (SettingTypeEnum.NOTICE_CYCLE.getValue().equals(dto.getType())) {
BV.isTrue(dto.getDetailValue() > 5, () -> "消息推送间隔必须大于5天");
}
if (SettingTypeEnum.NOTICE_TIMES.getValue().equals(dto.getType())) {
BV.isTrue(dto.getDetailValue() <= 2, () -> "消息提醒次数不能大于两次");
}
}
Assert.isTrue(expression, "跟进类型不正确");
SettingStrategy strategy = processorMap.get(followTypeEnum);
Assert.notNull(strategy, "strategy cannot be null");
return strategy.save(list, groupId);
}
/**
* 根据设置类型查询设置信息
*
* @param followTypeEnum
* @param settingTypeEnum
* @param groupId
* @return
*/
public Optional<SettingVO> querySettingByType(FollowTypeEnum followTypeEnum, SettingTypeEnum settingTypeEnum, Long groupId) {
if (Objects.isNull(followTypeEnum)) {
log.error("跟进类型不正确");
return Optional.empty();
}
if (Objects.isNull(settingTypeEnum)) {
log.error("设置类型不正确");
return Optional.empty();
}
SettingStrategy strategy = processorMap.get(followTypeEnum);
Assert.notNull(strategy, "strategy cannot be null");
return Optional.ofNullable(strategy.getBySettingType(settingTypeEnum, groupId));
}
}