EvalKpiBaseCalculator.java
20 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
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
package cn.fw.morax.service.biz.calculator.eval.kpi;
import cn.fw.morax.common.utils.PublicUtil;
import cn.fw.morax.domain.bo.eval.EvalGroupUserShop;
import cn.fw.morax.domain.db.eval.*;
import cn.fw.morax.domain.db.kpi.*;
import cn.fw.morax.domain.enums.*;
import cn.fw.morax.service.biz.calculator.Calculator;
import cn.fw.morax.service.data.eval.*;
import cn.fw.morax.service.data.kpi.*;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
/**
* 考评基础计算器
*
* @author : kurisu
* @version : 2.0
* @className : BaseCalculator
* @description : 考评基础计算器
* @date : 2022-12-26 14:17
*/
@Slf4j
public abstract class EvalKpiBaseCalculator implements Calculator<EvalGroupUserShop, EvalGroupIndicator, BigDecimal> {
@Autowired
protected EvalGroupIndicatorHitLogService evalGroupIndicatorHitLogService;
@Autowired
protected IndicatorUserValueService indicatorUserValueService;
@Autowired
protected IndicatorUserStageValueService indicatorUserStageValueService;
@Autowired
protected EvalIndicatorValueService evalIndicatorValueService;
@Autowired
protected EvalGroupIndicatorParamService evalGroupIndicatorParamService;
@Autowired
protected EvalGroupIndicatorPreconditionService evalGroupIndicatorPreconditionService;
@Autowired
protected EvalGroupIndicatorPreconditionHitLogService preconditionHitLogService;
@Autowired
protected EvalGroupIndicatorPreconditionLaddersService preconditionLaddersService;
@Autowired
protected EvalGroupIndicatorTargetHitLogService evalGroupIndicatorTargetHitLogService;
/**
* 获取设置类型
*
* @return
*/
public abstract ScoreWayEnum getCalMethod();
/**
* 通过前置条件计算绩效得分系数
*
* @param preconditions
* @return
*/
protected BigDecimal calcCoefficient(EvalGroupUserShop obj, List<EvalGroupIndicatorPrecondition> preconditions) {
BigDecimal coefficient = BigDecimal.ONE;
if (CollectionUtils.isEmpty(preconditions)) {
return coefficient;
}
for (int i = 0; i < preconditions.size(); i++) {
EvalGroupIndicatorPrecondition precondition = preconditions.get(i);
TargetCalcTypeEnum targetCalcType = precondition.getTargetCalcType();
TargetTypeEnum targetType = precondition.getTargetType();
Optional<BigDecimal> valueOptional = queryValue(obj, precondition.getCodeType(), precondition.getCode());
if (! valueOptional.isPresent()) {
return BigDecimal.ZERO;
}
final BigDecimal originValue = valueOptional.get();
BigDecimal indicatorValue = originValue;
EvalUseTargetEnum useTarget = EvalUseTargetEnum.NO;
if (!TargetTypeEnum.NO.equals(targetType)) {
useTarget = EvalUseTargetEnum.FIRST_TARGET;
BigDecimal targetValue = precondition.getTargetValue();
if (TargetCalcTypeEnum.TARGET_VALUE.equals(targetCalcType) || TargetCalcTypeEnum.STAGE_TARGET.equals(targetCalcType)) {
indicatorValue = indicatorValue.divide(targetValue, 4, RoundingMode.HALF_UP);
} else if (TargetCalcTypeEnum.MINIMUM.equals(targetCalcType)){
BigDecimal difference = BigDecimal.ONE.subtract(targetValue);
if (BigDecimal.ZERO.compareTo(difference) == 0) {
difference = BigDecimal.ONE;
}
BigDecimal userDiffValue = indicatorValue.subtract(targetValue);
if (userDiffValue.compareTo(BigDecimal.ZERO) <= 0) {
indicatorValue = BigDecimal.ZERO;
} else {
indicatorValue = userDiffValue.divide(difference, 4, RoundingMode.HALF_UP);
}
}
}
EvalGroupIndicatorTargetHitLog log = new EvalGroupIndicatorTargetHitLog();
log.setValue(originValue);
log.setReachValue(indicatorValue);
log.setUseTarget(useTarget);
saveTargetHitLog(obj, precondition.getId(), IndicatorTypeEnum.PRE, originValue, log);
coefficient = coefficient(obj, precondition.getId(), indicatorValue);
}
return coefficient;
}
protected BigDecimal coefficient(EvalGroupUserShop obj, Long preconditionId, BigDecimal value) {
EvalGroupIndicatorPreconditionLadders ladder = preconditionLaddersService.getOne(Wrappers.<EvalGroupIndicatorPreconditionLadders>lambdaQuery()
.eq(EvalGroupIndicatorPreconditionLadders::getPreconditionId, preconditionId)
.gt(EvalGroupIndicatorPreconditionLadders::getUpper, value)
.le(EvalGroupIndicatorPreconditionLadders::getLower, value)
.eq(EvalGroupIndicatorPreconditionLadders::getYn, Boolean.TRUE),
Boolean.FALSE
);
if (Objects.isNull(ladder)) {
savePreconditionHitLog(obj, preconditionId, hitLog -> {
hitLog.setPreconditionLaddersId(-1L);
hitLog.setValue(value);
});
return BigDecimal.ZERO;
}
savePreconditionHitLog(obj, preconditionId, hitLog -> {
hitLog.setPreconditionLaddersId(ladder.getId());
hitLog.setValue(value);
});
return ladder.getScorePercent();
}
/**
* 计算最终的指标值
*
* @param params
* @param userShop
* @param evalGroupIndicatorId
* @return
*/
protected BigDecimal calculateFinalIndValue(List<EvalGroupIndicatorParam> params, EvalGroupUserShop userShop, Long evalGroupIndicatorId) {
BigDecimal rate = BigDecimal.ZERO;
if (CollectionUtils.isEmpty(params)) {
log.info("指标[{}]配置不存在", evalGroupIndicatorId);
return rate;
}
for (EvalGroupIndicatorParam param : params) {
Optional<BigDecimal> valueOptional = queryValue(userShop, param.getCodeType(), param.getCode());
if (! valueOptional.isPresent()) {
log.info("[{}] [{}]指标[{}]值不存在", userShop.getScopeType().getName(), userShop.getReferId(), param.getCode());
continue;
}
TargetTypeEnum targetType = param.getTargetType();
TargetTypeEnum extraTargetType = param.getExtraTargetType();
final BigDecimal originValue = valueOptional.get();
BigDecimal indicatorValue = originValue;
BigDecimal reachValue = BigDecimal.ZERO;
BigDecimal extraReachValue = BigDecimal.ZERO;
EvalUseTargetEnum useTarget = EvalUseTargetEnum.NO;
if (PublicUtil.isNotEmpty(targetType) && !TargetTypeEnum.NO.equals(targetType)) {
reachValue = calcTarget(param.getTargetCalcType(), originValue, param.getTargetValue());
useTarget = EvalUseTargetEnum.FIRST_TARGET;
indicatorValue = reachValue;
}
if (PublicUtil.isNotEmpty(extraTargetType) && (!TargetTypeEnum.NO.equals(extraTargetType))) {
extraReachValue = calcTarget(param.getExtraTargetCalcType(), originValue, param.getExtraTargetValue());
//额外计算类型值 原始计算类型值 用大的那个值
if (PublicUtil.isEmpty(reachValue) || (extraReachValue.compareTo(reachValue) > 0)) {
indicatorValue = extraReachValue;
useTarget = EvalUseTargetEnum.EXTRA_TARGET;
}
}
EvalGroupIndicatorTargetHitLog log = new EvalGroupIndicatorTargetHitLog();
log.setValue(originValue);
log.setReachValue(reachValue);
log.setExtraReachValue(extraReachValue);
log.setUseTarget(useTarget);
saveTargetHitLog(userShop, param.getId(), IndicatorTypeEnum.EXAMINE, originValue, log);
boolean isCap = Boolean.TRUE.equals(param.getCap());
BigDecimal proportion = param.getProportion();
if (isCap) {
indicatorValue = BigDecimal.ONE.compareTo(indicatorValue) > 0 ? indicatorValue : BigDecimal.ONE;
}
rate = rate.add(proportion.multiply(indicatorValue));
}
return rate;
}
/**
* 计算目标
*/
public BigDecimal calcTarget(TargetCalcTypeEnum targetCalcType, BigDecimal indicatorValue, BigDecimal targetValue) {
if (PublicUtil.isEmpty(targetValue) || BigDecimal.ZERO.compareTo(targetValue) == 0) {
return BigDecimal.ZERO;
}
if (TargetCalcTypeEnum.MINIMUM.equals(targetCalcType)) {
BigDecimal difference = BigDecimal.ONE.subtract(targetValue);
if (BigDecimal.ZERO.compareTo(difference) == 0) {
difference = BigDecimal.ONE;
}
BigDecimal userDiffValue = indicatorValue.subtract(targetValue);
if (userDiffValue.compareTo(BigDecimal.ZERO) <= 0) {
return BigDecimal.ZERO;
} else {
return userDiffValue.divide(difference, 4, RoundingMode.HALF_UP);
}
}
return indicatorValue.divide(targetValue, 4, RoundingMode.HALF_UP);
}
/**
* 查询指标值
*
* @return
*/
protected Optional<BigDecimal> queryValue(EvalGroupUserShop obj, IndicatorCodeTypeEnum codeType, String indicatorCode) {
DimensionTypeEnum dimensionType = EvalScopeEnum.STAFF.equals(obj.getScopeType()) ? DimensionTypeEnum.STAFF : DimensionTypeEnum.SHOP;
if (IndicatorCodeTypeEnum.INDICATOR.equals(codeType)) {
//多阶段查询
if (Boolean.TRUE.equals(obj.getMultiStage())) {
LambdaQueryWrapper<IndicatorUserStageValue> queryWrapper = Wrappers.<IndicatorUserStageValue>lambdaQuery()
.eq(IndicatorUserStageValue::getIndicatorCode, indicatorCode)
.eq(IndicatorUserStageValue::getBeginDate, obj.getBeginDate())
.eq(IndicatorUserStageValue::getEndDate, obj.getDataDate())
.eq(IndicatorUserStageValue::getDimensionType, dimensionType)
.eq(IndicatorUserStageValue::getYn, Boolean.TRUE)
.eq(IndicatorUserStageValue::getGroupId, obj.getGroupId());
if (EvalScopeEnum.STAFF.equals(obj.getScopeType())) {
queryWrapper.eq(IndicatorUserStageValue::getUserId, obj.getReferId());
} else {
queryWrapper.eq(IndicatorUserStageValue::getShopId, obj.getReferId());
}
IndicatorUserStageValue indicatorUserStageValue = indicatorUserStageValueService.getOne(queryWrapper, Boolean.FALSE);
return queryKpiIndicatorValue(indicatorUserStageValue);
}
//单阶段查询
LambdaQueryWrapper<IndicatorUserValue> queryWrapper = Wrappers.<IndicatorUserValue>lambdaQuery()
.eq(IndicatorUserValue::getIndicatorCode, indicatorCode)
.eq(IndicatorUserValue::getDataDate, obj.getDataDate())
.eq(IndicatorUserValue::getDimensionType, dimensionType)
.eq(IndicatorUserValue::getYn, Boolean.TRUE)
.eq(IndicatorUserValue::getGroupId, obj.getGroupId());
if (EvalScopeEnum.STAFF.equals(obj.getScopeType())) {
queryWrapper.eq(IndicatorUserValue::getUserId, obj.getReferId());
} else {
queryWrapper.eq(IndicatorUserValue::getShopId, obj.getReferId());
}
IndicatorUserValue indicatorUserValue = indicatorUserValueService.getOne(queryWrapper, Boolean.FALSE);
return queryKpiIndicatorValue(indicatorUserValue);
}
EvalIndicatorValue evalIndicatorValue = evalIndicatorValueService.queryLastValue(obj.getReferId(),
dimensionType, indicatorCode, obj.getDataDate());
return Optional.ofNullable(evalIndicatorValue).map(EvalIndicatorValue::getIndicatorValue);
}
/**
* 查询前置条件
*
* @param evalGroupIndicatorId
* @return
*/
protected List<EvalGroupIndicatorPrecondition> queryPrecondition(Long evalGroupIndicatorId) {
List<EvalGroupIndicatorPrecondition> preList = evalGroupIndicatorPreconditionService.list(Wrappers.<EvalGroupIndicatorPrecondition>lambdaQuery()
.eq(EvalGroupIndicatorPrecondition::getEvalGroupIndicatorId, evalGroupIndicatorId)
.eq(EvalGroupIndicatorPrecondition::getYn, Boolean.TRUE)
.orderByAsc(EvalGroupIndicatorPrecondition::getSort)
);
return Optional.ofNullable(preList).orElse(new ArrayList<>());
}
/**
* 查询指标配置
*
* @param evalGroupIndicatorId
* @return
*/
protected List<EvalGroupIndicatorParam> queryParam(Long evalGroupIndicatorId) {
List<EvalGroupIndicatorParam> list = evalGroupIndicatorParamService.list(Wrappers.<EvalGroupIndicatorParam>lambdaQuery()
.eq(EvalGroupIndicatorParam::getEvalGroupIndicatorId, evalGroupIndicatorId)
.eq(EvalGroupIndicatorParam::getYn, Boolean.TRUE)
);
return Optional.ofNullable(list).orElse(new ArrayList<>());
}
/**
* 查询具体指标值
*
* @param indicatorUserStageValue
* @return
*/
protected Optional<BigDecimal> queryKpiIndicatorValue(IndicatorUserStageValue indicatorUserStageValue) {
if (PublicUtil.isEmpty(indicatorUserStageValue) || PublicUtil.isEmpty(indicatorUserStageValue.getIndicatorValue())) {
return Optional.empty();
}
BigDecimal value = BigDecimal.ZERO;
try {
JSONObject jsonObject = JSONObject.parseObject(indicatorUserStageValue.getIndicatorValue());
value = Optional.ofNullable(jsonObject.getBigDecimal(indicatorUserStageValue.getIndicatorCode())).orElse(BigDecimal.ZERO);
} catch (Exception e) {
log.error("[{}]指标值转化失败", indicatorUserStageValue, e);
}
return Optional.of(value);
}
/**
* 查询具体指标值
*
* @param indicatorUserValue
* @return
*/
protected Optional<BigDecimal> queryKpiIndicatorValue(IndicatorUserValue indicatorUserValue) {
if (PublicUtil.isEmpty(indicatorUserValue) || PublicUtil.isEmpty(indicatorUserValue.getIndicatorValue())) {
return Optional.empty();
}
BigDecimal value = BigDecimal.ZERO;
try {
JSONObject jsonObject = JSONObject.parseObject(indicatorUserValue.getIndicatorValue());
value = Optional.ofNullable(jsonObject.getBigDecimal(indicatorUserValue.getIndicatorCode())).orElse(BigDecimal.ZERO);
} catch (Exception e) {
log.error("[{}]指标值转化失败", indicatorUserValue, e);
}
return Optional.of(value);
}
/**
* 查询具体指标值
*
* @param indicatorCode
* @param originalValue
* @return
*/
protected BigDecimal queryIndicatorValue(String indicatorCode, String originalValue) {
BigDecimal value = BigDecimal.ZERO;
try {
JSONObject jsonObject = JSONObject.parseObject(originalValue);
value = Optional.ofNullable(jsonObject.getBigDecimal(indicatorCode)).orElse(BigDecimal.ZERO);
} catch (Exception e) {
log.error("[{}]指标值转化失败", indicatorCode, e);
}
return value;
}
protected void savePreconditionHitLog(EvalGroupUserShop userShop, Long preconditionId, Consumer<EvalGroupIndicatorPreconditionHitLog> consumer) {
Long poolId = userShop.getPoolId();
EvalScopeEnum evalScope = userShop.getScopeType();
preconditionHitLogService.remove(Wrappers.<EvalGroupIndicatorPreconditionHitLog>lambdaQuery()
.eq(EvalGroupIndicatorPreconditionHitLog::getPoolId, poolId)
.eq(EvalGroupIndicatorPreconditionHitLog::getPreconditionId, preconditionId)
.eq(EvalGroupIndicatorPreconditionHitLog::getDataDate, userShop.getDataDate())
.eq(EvalGroupIndicatorPreconditionHitLog::getGroupId, userShop.getGroupId())
);
EvalGroupIndicatorPreconditionHitLog hitLog = new EvalGroupIndicatorPreconditionHitLog();
hitLog.setPreconditionId(preconditionId);
hitLog.setPoolId(poolId);
hitLog.setScopeType(evalScope);
hitLog.setDataDate(userShop.getDataDate());
hitLog.setGroupId(userShop.getGroupId());
if (consumer != null) {
consumer.accept(hitLog);
}
preconditionHitLogService.save(hitLog);
}
protected void saveTargetHitLog(EvalGroupUserShop userShop, Long referId, IndicatorTypeEnum targetType, BigDecimal value, EvalGroupIndicatorTargetHitLog log) {
evalGroupIndicatorTargetHitLogService.remove(Wrappers.<EvalGroupIndicatorTargetHitLog>lambdaQuery()
.eq(EvalGroupIndicatorTargetHitLog::getPoolId, userShop.getPoolId())
.eq(EvalGroupIndicatorTargetHitLog::getScopeType, userShop.getScopeType())
.eq(EvalGroupIndicatorTargetHitLog::getTargetType, targetType)
.eq(EvalGroupIndicatorTargetHitLog::getDataDate, userShop.getDataDate())
.eq(EvalGroupIndicatorTargetHitLog::getReferId, referId)
);
log.setPoolId(userShop.getPoolId());
log.setScopeType(userShop.getScopeType());
log.setReferId(referId);
log.setTargetType(targetType);
log.setValue(value);
log.setDataDate(userShop.getDataDate());
log.setGroupId(userShop.getGroupId());
log.setYn(Boolean.TRUE);
evalGroupIndicatorTargetHitLogService.save(log);
}
protected void saveTargetHitLog(EvalGroupUserShop userShop, Long referId, IndicatorTypeEnum targetType, BigDecimal value, BigDecimal reachValue) {
evalGroupIndicatorTargetHitLogService.remove(Wrappers.<EvalGroupIndicatorTargetHitLog>lambdaQuery()
.eq(EvalGroupIndicatorTargetHitLog::getPoolId, userShop.getPoolId())
.eq(EvalGroupIndicatorTargetHitLog::getScopeType, userShop.getScopeType())
.eq(EvalGroupIndicatorTargetHitLog::getTargetType, targetType)
.eq(EvalGroupIndicatorTargetHitLog::getDataDate, userShop.getDataDate())
.eq(EvalGroupIndicatorTargetHitLog::getReferId, referId)
);
EvalGroupIndicatorTargetHitLog log = new EvalGroupIndicatorTargetHitLog();
log.setPoolId(userShop.getPoolId());
log.setScopeType(userShop.getScopeType());
log.setReferId(referId);
log.setTargetType(targetType);
log.setValue(value);
log.setReachValue(reachValue);
log.setUseTarget(EvalUseTargetEnum.FIRST_TARGET);
log.setDataDate(userShop.getDataDate());
log.setGroupId(userShop.getGroupId());
log.setYn(Boolean.TRUE);
evalGroupIndicatorTargetHitLogService.save(log);
}
/**
* 储存命中记录
*
* @param evalGroupIndicatorId
* @param userShop
*/
protected void saveParamHitLog(Long evalGroupIndicatorId, EvalGroupUserShop userShop,
Consumer<EvalGroupIndicatorHitLog> consumer) {
EvalGroupIndicatorHitLog hitLog = new EvalGroupIndicatorHitLog();
hitLog.setEvalGroupIndicatorId(evalGroupIndicatorId);
hitLog.setPoolId(userShop.getPoolId());
hitLog.setScopeType(userShop.getScopeType());
hitLog.setDataDate(userShop.getDataDate());
hitLog.setGroupId(userShop.getGroupId());
hitLog.setYn(Boolean.TRUE);
if (consumer != null) {
consumer.accept(hitLog);
}
evalGroupIndicatorHitLogService.saveUnique(hitLog);
}
}