IndicatorBizService.java
7.5 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
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
package cn.fw.morax.service.biz.kpi;
import cn.fw.common.exception.BusinessException;
import cn.fw.common.page.AppPage;
import cn.fw.common.web.annotation.DisLock;
import cn.fw.ehr.sdk.api.enums.RoleManagerEnum;
import cn.fw.ehr.sdk.api.result.PostRoleVo;
import cn.fw.morax.common.utils.PublicUtil;
import cn.fw.morax.domain.db.kpi.Indicators;
import cn.fw.morax.domain.db.kpi.KpiGroup;
import cn.fw.morax.domain.dto.IndicatorsDTO;
import cn.fw.morax.domain.dto.query.IndicatorPostQueryDTO;
import cn.fw.morax.domain.dto.query.IndicatorQueryDTO;
import cn.fw.morax.domain.enums.ApplicableRoleEnum;
import cn.fw.morax.domain.vo.kpi.IndicatorsVO;
import cn.fw.morax.rpc.ehr.EhrRpcService;
import cn.fw.morax.rpc.ehr.dto.PostInfoDTO;
import cn.fw.morax.service.data.kpi.IndicatorsService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import static cn.fw.common.businessvalidator.Validator.BV;
@RequiredArgsConstructor
@Service
@Slf4j
public class IndicatorBizService {
private final KpiGroupIndicatorBizService kpiGroupIndicatorBizService;
private final IndicatorsService indicatorsService;
private final EhrRpcService ehrRpcService;
@Value("${spring.cache.custom.global-prefix}:indicator:save:")
@Getter
private String savePrefix;
@Value("${spring.cache.custom.global-prefix}:indicator:enable:")
@Getter
private String enablePrefix;
/**
* 分页查询
*
* @param dto
* @return
*/
public AppPage<IndicatorsVO> indicatorsPage(IndicatorQueryDTO dto) {
if (PublicUtil.isNotEmpty(dto.getRoleCode())) {
List<ApplicableRoleEnum> roleTypes = new ArrayList<ApplicableRoleEnum>(){{
add(ApplicableRoleEnum.ALL);
add(ApplicableRoleEnum.MANAGEMENT_ROLE);
}};
dto.setAllRoleTypes(roleTypes);
}
Long total = indicatorsService.indicatorsPageTotal(dto);
return PublicUtil.<IndicatorsVO, IndicatorQueryDTO>queryPage(dto, total, new Function<IndicatorQueryDTO, List<IndicatorsVO>>() {
@Override
public List<IndicatorsVO> apply(IndicatorQueryDTO basePageQuery) {
return indicatorsService.indicatorsPage(dto);
}
});
}
/**
* 指标详情
*
* @param id
* @return
*/
public IndicatorsVO indicatorDetail(Long id) {
Indicators indicators = indicatorsService.getById(id);
BV.notNull(indicators, "指标不存在,请重试");
return PublicUtil.copy(indicators, IndicatorsVO.class);
}
/**
* 保存指标
*
* @param dto
* @return
*/
@Transactional(rollbackFor = Exception.class)
@DisLock(prefix = "#this.getSavePrefix()", key = "#dto.getIndicatorName()", message = "保存中,请勿重复操作")
public void saveIndicator(IndicatorsDTO dto) {
this.checkIndicatorRole(dto);
this.checkIndicatorName(dto.getIndicatorName());
String indicatorCode = PublicUtil.getUUID();
this.checkIndicatorCode(indicatorCode);
Indicators indicators = dto.convertToPo();
indicators.setIndicatorCode(indicatorCode);
indicatorsService.save(indicators);
}
/**
* 指标库禁用启用
*
* @param indicatorId
* @param enable
*/
@Transactional(rollbackFor = Exception.class)
@DisLock(prefix = "#this.getEnablePrefix()", key = "#indicatorId", message = "修改中,请勿重复操作")
public void enableIndicator(Long indicatorId, Boolean enable) {
Indicators indicators = indicatorsService.getById(indicatorId);
BV.notNull(indicators, "指标不存在,请重试");
BV.isFalse(enable.equals(indicators.getEnable()), "指标已经是" + ((enable) ? "启用": "禁用") + "状态,请重试");
//禁用情况:正在使用的指标不能禁用,并提示哪些地方在用
if (! enable) {
List<KpiGroup> kpiGroups = kpiGroupIndicatorBizService.getKpiGroupByIndicatorId(indicatorId);
Set<String> postNames = kpiGroups.stream().map(KpiGroup::getPostName).collect(Collectors.toSet());
if (PublicUtil.isNotEmpty(postNames)) {
throw new BusinessException(String.join(",", postNames) + "岗位正在使用此绩效指标,不能禁用");
}
}
indicatorsService.update(Wrappers.<Indicators>lambdaUpdate()
.set(Indicators::getEnable, enable)
.eq(Indicators::getId, indicatorId)
);
}
/**
* 检查指标角色配置
*
* @param dto
*/
private void checkIndicatorRole(IndicatorsDTO dto) {
Boolean portionNotSelectRole = ApplicableRoleEnum.PORTION.equals(dto.getRoleType()) &&
PublicUtil.isEmpty(dto.getRoleNames());
BV.isFalse(portionNotSelectRole, "自定义角色,请选择至少一个角色");
}
/**
* 检查指标名称
*
* @param indicatorName
*/
private void checkIndicatorName(String indicatorName) {
long taskCount = indicatorsService.count(Wrappers.<Indicators>lambdaQuery()
.eq(Indicators::getIndicatorName, indicatorName)
.eq(Indicators::getYn, Boolean.TRUE)
);
BV.isFalse(taskCount > 0, "指标名称已存在,请重新输入");
}
/**
* 检查指标编码
*
* @param indicatorCode
*/
private void checkIndicatorCode(String indicatorCode) {
long taskCount = indicatorsService.count(Wrappers.<Indicators>lambdaQuery()
.eq(Indicators::getIndicatorCode, indicatorCode)
);
if (taskCount > 0) {
log.error("指标编码生成重复:{}", indicatorCode);
throw new BusinessException("指标编码已存在,请重试");
}
}
/**
* 获取岗位下的绩效组指标
*
* @param postId
* @return
*/
public List<IndicatorsVO> getPostIndicators(Long postId, String shopIds) {
List<Long> postIds = new ArrayList<Long>(){{
add(postId);
}};
List<PostInfoDTO> postInfoDTOS = ehrRpcService.getPostInfoList(postIds);
List<String> roleCodes = postInfoDTOS.stream()
.map(PostInfoDTO::getRoleList)
.filter(Objects::nonNull)
.flatMap(Collection::stream)
.map(PostRoleVo::getRoleCode)
.distinct().collect(Collectors.toList());
Boolean queryManagerRole = hasManagerRole(roleCodes);
IndicatorPostQueryDTO dto = new IndicatorPostQueryDTO(roleCodes, queryManagerRole);
// IndicatorPostQueryDTO dto = new IndicatorPostQueryDTO(new ArrayList<String>(){{add("FWGW");add("XSGW");}}, Boolean.TRUE);
return indicatorsService.getPostIndicators(dto);
}
/**
* 是否拥有管理角色
*
* @return
*/
private Boolean hasManagerRole(List<String> roleCodes) {
if (PublicUtil.isEmpty(roleCodes)) {
return Boolean.FALSE;
}
HashSet<String> roleCodeSet = new HashSet<>(roleCodes);
return Arrays.stream(RoleManagerEnum.values())
.anyMatch(roleManagerEnum -> roleCodeSet.contains(roleManagerEnum.getCode()));
}
}