SalaryReportTask.java
22.4 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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
package cn.fw.morax.server.task;
import cn.fw.common.cache.locker.DistributedLocker;
import cn.fw.morax.common.constant.TimeTaskConstant;
import cn.fw.morax.common.utils.DateUtil;
import cn.fw.morax.common.utils.PublicUtil;
import cn.fw.morax.domain.db.kpi.KpiPool;
import cn.fw.morax.domain.db.salary.SalaryGroup;
import cn.fw.morax.domain.db.salary.SalaryPool;
import cn.fw.morax.domain.db.salary.SalaryReward;
import cn.fw.morax.domain.dto.query.GroupCalSalaryDTO;
import cn.fw.morax.domain.enums.ReportDimensionEnum;
import cn.fw.morax.rpc.ehr.EhrRpcService;
import cn.fw.morax.rpc.ehr.dto.*;
import cn.fw.morax.rpc.oop.OopRpcService;
import cn.fw.morax.rpc.oop.dto.ShopDTO;
import cn.fw.morax.service.data.kpi.KpiPoolService;
import cn.fw.morax.service.data.salary.SalaryGroupService;
import cn.fw.morax.service.data.salary.SalaryPoolService;
import cn.fw.morax.service.data.salary.SalaryRewardService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.map.MultiKeyMap;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.YearMonth;
import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* @author : jiangchao
* @className : SalaryReportTask
* @description : 薪酬报表定时器
* @date : 2022-04-07 15:29
*/
@Component
@Slf4j
@RequiredArgsConstructor
@ConditionalOnProperty(prefix = "task", name = "switch", havingValue = "on")
public class SalaryReportTask {
private final SalaryGroupService salaryGroupService;
private final SalaryPoolService salaryPoolService;
private final SalaryRewardService salaryRewardService;
private final EhrRpcService ehrRpcService;
private final KpiPoolService kpiPoolService;
private final OopRpcService oopRpcService;
private final DistributedLocker distributedLocker;
@Value("${spring.cache.custom.global-prefix}:salary:group:report")
@Getter
private String salaryGroupReport;
/**
* 绩效报表定时任务
*/
@Scheduled(cron = TimeTaskConstant.SALARY_REPORT)
@Transactional(rollbackFor = Exception.class)
public void salaryReportTask() {
this.salaryReport(LocalDate.now().minusDays(1));
}
/**
* 薪酬报表定时任务
*
* @param date
*/
@Transactional(rollbackFor = Exception.class)
public void salaryReport(LocalDate date) {
Lock lock = distributedLocker.lock(getSalaryGroupReport());
if (!((RLock) lock).isLocked()) {
return;
}
try {
log.info("定时任务【薪酬报表数据抽取】开始执行");
salaryRewardService.update(Wrappers.<SalaryReward>lambdaUpdate()
.set(SalaryReward::getYn, Boolean.FALSE)
.eq(SalaryReward::getDate, date)
);
List<SalaryGroup> allSalaryGroups = salaryGroupService.getAllEffectGroups(DateUtil.localDate2Date(date));
Map<Long, List<SalaryGroup>> groupKpiGroupMap = allSalaryGroups.stream().collect(Collectors.groupingBy(SalaryGroup::getGroupId));
List<SalaryReward> salarys = Lists.newArrayListWithCapacity(500);
oopRpcService.allGroups().stream().forEach(group -> {
Long groupId = group.getId();
List<SalaryPool> pools = salaryPoolService.list(Wrappers.<SalaryPool>lambdaQuery()
.eq(SalaryPool::getMonthly, YearMonth.from(date))
.eq(SalaryPool::getYn, Boolean.TRUE)
.eq(SalaryPool::getGroupId, groupId)
);
GroupCalSalaryDTO calDTO = new GroupCalSalaryDTO();
calDTO.setPools(pools);
calDTO.setDate(date);
calDTO.setGroupId(groupId);
List<SalaryGroup> salaryGroups = groupKpiGroupMap.getOrDefault(groupId, new ArrayList<>());
Set<Long> shopIds = salaryGroups.stream().map(salaryGroup -> {
return salaryGroup.getShopIds().stream().map(Long::new).collect(Collectors.toList());
}).collect(HashSet::new, Set::addAll, Set::addAll);
if (PublicUtil.isEmpty(shopIds)) {
return;
}
List<ShopDTO> shopDTOs = oopRpcService.queryShops(new ArrayList<>(shopIds));
//计算管理者数据
calManagerShopPostData(calDTO, salarys, salaryGroups, shopDTOs, shopIds);
//计算员工
calStaffData(calDTO, salarys);
//门店
calShopData(calDTO, salarys, salaryGroups, shopDTOs);
//岗位
calPostData(calDTO, salarys, salaryGroups, shopDTOs);
});
if (PublicUtil.isNotEmpty(salarys)) {
salarys.stream().forEach(salary -> {
salary.setDate(date);
salary.setYn(Boolean.TRUE);
});
salaryRewardService.saveBatch(salarys);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
} finally {
lock.unlock();
}
}
/**
* 计算管理者维度数据 (门店、绩效组)
*
* @param calDto
*/
public void calManagerShopPostData(GroupCalSalaryDTO calDto,
List<SalaryReward> salarys,
List<SalaryGroup> salaryGroups,
List<ShopDTO> shopDTOs,
Set<Long> shopIds
) {
List<SalaryPool> pools = calDto.getPools();
Map<Long, Set<Long>> salaryGroupPostShopIds = salaryGroups.stream().collect(Collectors.toMap(
SalaryGroup::getPostId, salaryGroup -> new HashSet<>(salaryGroup.getShopIds()),
(v1, v2) -> {
v1.addAll(v2);
return v1;
}));
List<ManagerDTO> managerDTOS = ehrRpcService.getRealTimeShopManager(new ArrayList<>(shopIds));
log.info("查询门店实时管理者:{},{}", JSON.toJSONString(shopIds), JSON.toJSONString(managerDTOS));
Map<Long, String> postMap = salaryGroups.stream().collect(Collectors.toMap(SalaryGroup::getPostId,
SalaryGroup::getPostName, (v1, v2) -> v1));
for (ManagerDTO manager : managerDTOS) {
Map<Long, Set<Long>> createShopPostIdMap = new HashMap<>();
Map<Long, Set<Long>> createPostShopIdMap = new HashMap<>();
//管理岗位
for (ManagerStaffDTO managerStaffDTO : manager.getScopeList()) {
Long postId = managerStaffDTO.getPostId();
//绩效组所有岗位
if (!salaryGroupPostShopIds.keySet().contains(postId)) {
continue;
}
//管理门店
for (ManagerStaffShopDTO shopDTO : managerStaffDTO.getShopList()) {
//绩效组所有门店
if (!salaryGroupPostShopIds.get(postId).contains(shopDTO.getShopId())) {
continue;
}
Long shopId = shopDTO.getShopId();
if (createShopPostIdMap.containsKey(shopId)) {
createShopPostIdMap.get(shopId).add(postId);
} else {
createShopPostIdMap.put(shopId, new HashSet<Long>() {{
add(postId);
}});
}
if (createPostShopIdMap.containsKey(postId)) {
createPostShopIdMap.get(postId).add(postId);
} else {
createPostShopIdMap.put(postId, new HashSet<Long>() {{
add(shopId);
}});
}
}
}
//管理者门店、岗位 与 绩效组门店、岗位没有匹配
if (PublicUtil.isEmpty(createShopPostIdMap)) {
continue;
}
Set<Long> manageStaffIds = manager.getManageStaffList().stream().map(StaffBaseInfoDTO::getId).collect(Collectors.toSet());
Set<Long> createPostIds = createPostShopIdMap.keySet();
for (Long postId : createPostIds) {
SalaryReward salary = new SalaryReward(ReportDimensionEnum.MANAGER_POST, calDto.getGroupId());
salary.setS1(manager.getStaffName());
salary.setL1(manager.getStaffId());
salary.setL3(postId);
salary.setS3(postMap.getOrDefault(postId, ""));
Set<Long> postShopIds = createPostShopIdMap.get(postId);
if (PublicUtil.isNotEmpty(postShopIds)) {
salary.setS6(String.join(",", postShopIds.stream().map(String::valueOf).collect(Collectors.toList())));
}
Predicate<SalaryPool> predicate = pool -> {
return postId.equals(pool.getPostId()) && manageStaffIds.contains(pool.getUserId());
};
salary.setB1(totalSalary(pools, predicate));
salary.setB2(averageSalary(pools, predicate));
salary.setB3(null);
salary.setB4(null);
salarys.add(salary);
}
Set<Long> createShopIds = createShopPostIdMap.keySet();
shopDTOs.stream().filter(shopDTO -> createShopIds.contains(shopDTO.getId())).forEach(shopDTO -> {
SalaryReward salary = new SalaryReward(ReportDimensionEnum.MANAGER_SHOP, calDto.getGroupId());
Long shopId = shopDTO.getId();
String shopName = shopDTO.getShopName();
salary.setS1(manager.getStaffName());
salary.setL1(manager.getStaffId());
salary.setL2(shopId);
salary.setS2(shopName);
Set<Long> postIds = createShopPostIdMap.get(shopDTO.getId());
if (PublicUtil.isNotEmpty(postIds)) {
salary.setS6(String.join(",", postIds.stream().map(String::valueOf).collect(Collectors.toList())));
}
Predicate<SalaryPool> predicate = pool -> {
return shopId.equals(pool.getShopId()) && manageStaffIds.contains(pool.getUserId());
};
salary.setB1(totalSalary(pools, predicate));
salary.setB5(countStaffNum(pools, predicate));
salary.setB2(averageSalary(pools, predicate));
salary.setB3(null);
salary.setB4(null);
salarys.add(salary);
});
SalaryReward salary = new SalaryReward(ReportDimensionEnum.MANAGER, calDto.getGroupId());
salary.setS1(manager.getStaffName());
salary.setL1(manager.getStaffId());
Set<String> roleCodes = manager.getScopeList().stream()
.flatMap(managerStaffVo -> managerStaffVo.getRoleList().stream())
.map(ManagerStaffRoleDTO::getRoleCode)
.collect(Collectors.toSet());
Set<String> roleNames = manager.getScopeList().stream()
.flatMap(managerStaffVo -> managerStaffVo.getRoleList().stream())
.map(ManagerStaffRoleDTO::getRoleName)
.collect(Collectors.toSet());
if (PublicUtil.isNotEmpty(roleCodes)) {
salary.setS4(String.join(",", roleCodes));
}
if (PublicUtil.isNotEmpty(roleNames)) {
salary.setS5(String.join(",", roleNames));
}
if (PublicUtil.isNotEmpty(createShopIds)) {
salary.setS6(String.join(",", createShopIds.stream().map(String::valueOf).collect(Collectors.toList())));
}
Predicate<SalaryPool> predicate = pool -> {
return manageStaffIds.contains(pool.getUserId());
};
salary.setB1(totalSalary(pools, predicate));
salary.setB2(averageSalary(pools, predicate));
salary.setB3(null);
salary.setB4(null);
salarys.add(salary);
}
}
/**
* 计算员工维度数据
*
* @param calDto
*/
public void calStaffData(GroupCalSalaryDTO calDto, List<SalaryReward> salarys) {
List<SalaryPool> pools = calDto.getPools();
List<KpiPool> kpiPools = kpiPoolService.list(Wrappers.<KpiPool>lambdaQuery()
.eq(KpiPool::getMonthly, YearMonth.from(calDto.getDate()))
.eq(KpiPool::getYn, Boolean.TRUE)
.eq(KpiPool::getGroupId, calDto.getGroupId())
);
MultiKeyMap<Long, Integer> staffShopPostStar = new MultiKeyMap();
kpiPools.stream()
.filter(pool -> PublicUtil.isNotEmpty(pool.getStarLevel()))
.forEach(pool -> {
staffShopPostStar.put(pool.getUserId(), pool.getShopId(), pool.getPostId(), pool.getStarLevel().getValue());
});
for (SalaryPool pool : pools) {
SalaryReward salary = new SalaryReward(ReportDimensionEnum.STAFF, calDto.getGroupId());
salary.setL1(pool.getUserId());
salary.setS1(pool.getUserName());
salary.setL2(pool.getShopId());
salary.setS2(pool.getShopName());
salary.setL3(pool.getPostId());
salary.setS3(pool.getPostName());
salary.setL4(pool.getId());
salary.setB1(pool.getReward());
salary.setB2(pool.getReward());
if (staffShopPostStar.containsKey(pool.getUserId(), pool.getShopId(), pool.getPostId())) {
salary.setI1(staffShopPostStar.get(pool.getUserId(), pool.getShopId(), pool.getPostId()));
}
salarys.add(salary);
}
}
/**
* 计算绩效排名组维度数据
*/
public void calShopData(GroupCalSalaryDTO calDto,
List<SalaryReward> salarys,
List<SalaryGroup> salaryGroups,
List<ShopDTO> shopDTOs
) {
List<SalaryPool> pools = calDto.getPools();
//门店 岗位
Map<Long, Set<Long>> salaryGroupShopPostMap = new HashMap<>();
for (SalaryGroup salaryGroup : salaryGroups) {
Long postId = salaryGroup.getPostId();
for (Long shopId : salaryGroup.getShopIds()) {
if (salaryGroupShopPostMap.containsKey(shopId)) {
salaryGroupShopPostMap.get(shopId).add(postId);
} else {
salaryGroupShopPostMap.put(shopId, new HashSet<Long>() {{
add(postId);
}});
}
}
}
Map<Long, String> postMap = salaryGroups.stream().collect(Collectors.toMap(SalaryGroup::getPostId,
SalaryGroup::getPostName, (v1, v2) -> v1));
Set<Long> createShopIds = salaryGroupShopPostMap.keySet();
shopDTOs.stream().filter(shop -> createShopIds.contains(shop.getId())).forEach(shop -> {
SalaryReward salary = new SalaryReward(ReportDimensionEnum.SHOP, calDto.getGroupId());
Long shopId = shop.getId();
String shopName = shop.getShopName();
Predicate<SalaryPool> predicate = pool -> {
return shopId.equals(pool.getShopId());
};
salary.setL2(shopId);
salary.setS2(shopName);
salary.setB1(totalSalary(pools, predicate));
salary.setB5(countStaffNum(pools, predicate));
salary.setB2(averageSalary(pools, predicate));
salary.setB3(null);
salary.setB4(null);
salarys.add(salary);
for (Long postId : salaryGroupShopPostMap.get(shop.getId())) {
SalaryReward salaryPost = new SalaryReward(ReportDimensionEnum.SHOP_POST, calDto.getGroupId());
Predicate<SalaryPool> predicatePost = pool -> {
return shopId.equals(pool.getShopId()) && postId.equals(pool.getPostId());
};
salaryPost.setL2(shopId);
salaryPost.setS2(shopName);
salaryPost.setL3(postId);
salaryPost.setS3(postMap.getOrDefault(postId, ""));
salaryPost.setB1(totalSalary(pools, predicatePost));
salaryPost.setB2(averageSalary(pools, predicatePost));
salaryPost.setB3(null);
salaryPost.setB4(null);
salarys.add(salaryPost);
}
});
}
/**
* 计算绩效排名组维度数据
*
*/
public void calPostData(GroupCalSalaryDTO calDto,
List<SalaryReward> salarys,
List<SalaryGroup> salaryGroups,
List<ShopDTO> shopDTOs
) {
List<SalaryPool> pools = calDto.getPools();
//门店 岗位
Map<Long, Set<Long>> salaryGroupPostShopMap = new HashMap<>();
for (SalaryGroup salaryGroup : salaryGroups) {
Long postId = salaryGroup.getPostId();
if (salaryGroupPostShopMap.containsKey(postId)) {
salaryGroupPostShopMap.get(postId).addAll(salaryGroup.getShopIds());
} else {
salaryGroupPostShopMap.put(postId, new HashSet<Long>(){{addAll(salaryGroup.getShopIds());}});
}
}
Map<Long, String> postMap = salaryGroups.stream().collect(Collectors.toMap(SalaryGroup::getPostId,
SalaryGroup::getPostName, (v1, v2) -> v1));
for (Map.Entry<Long, Set<Long>> entry : salaryGroupPostShopMap.entrySet()) {
SalaryReward salary = new SalaryReward(ReportDimensionEnum.POST, calDto.getGroupId());
Long postId = entry.getKey();
Set<Long> createShopIds = entry.getValue();
Predicate<SalaryPool> predicate = pool -> {
return postId.equals(pool.getPostId());
};
salary.setL3(postId);
salary.setS3(postMap.getOrDefault(postId, ""));
if (PublicUtil.isNotEmpty(createShopIds)) {
salary.setS6(String.join(",", createShopIds.stream().map(String::valueOf).collect(Collectors.toList())));
}
salary.setB1(totalSalary(pools, predicate));
salary.setB2(averageSalary(pools, predicate));
salary.setB3(null);
salary.setB4(null);
salarys.add(salary);
shopDTOs.stream().filter(shop -> createShopIds.contains(shop.getId())).forEach(shop -> {
SalaryReward salaryShop = new SalaryReward(ReportDimensionEnum.POST_SHOP, calDto.getGroupId());
Long shopId = shop.getId();
String shopName = shop.getShopName();
Predicate<SalaryPool> predicateShop = pool -> {
return postId.equals(pool.getPostId()) && shopId.equals(pool.getShopId());
};
salaryShop.setL2(shopId);
salaryShop.setS2(shopName);
salaryShop.setL3(postId);
salaryShop.setS3(postMap.getOrDefault(postId, ""));
salaryShop.setB1(totalSalary(pools, predicateShop));
salaryShop.setB5(countStaffNum(pools, predicateShop));
salaryShop.setB2(averageSalary(pools, predicateShop));
salaryShop.setB3(null);
salaryShop.setB4(null);
salarys.add(salaryShop);
});
}
}
/**
* 绩效池平均绩效得分率
*
* @param pools
* @return
*/
public BigDecimal countStaffNum(Collection<SalaryPool> pools, Predicate<SalaryPool> predicate) {
if (CollectionUtils.isEmpty(pools)) {
return BigDecimal.ZERO;
}
Long staffNum = pools.stream()
.filter(predicate)
.count();
return new BigDecimal(staffNum);
}
/**
* 绩效池平均绩效得分率
*
* @param pools
* @return
*/
public BigDecimal averageSalary(Collection<SalaryPool> pools, Predicate<SalaryPool> predicate) {
if (CollectionUtils.isEmpty(pools)) {
return BigDecimal.ZERO;
}
Double averageRatioDouble = pools.stream()
.filter(predicate)
.mapToDouble(r -> Optional.ofNullable(r.getReward()).orElse(BigDecimal.ZERO).doubleValue())
.average()
.orElse(0);
BigDecimal averageRatio = new BigDecimal(averageRatioDouble.toString());
return averageRatio.setScale(2, RoundingMode.DOWN);
}
/**
* 总薪酬
*
* @param pools
* @return
*/
public BigDecimal totalSalary(Collection<SalaryPool> pools, Predicate<SalaryPool> predicate) {
if (CollectionUtils.isEmpty(pools)) {
return BigDecimal.ZERO;
}
BigDecimal totalSalary = pools.stream()
.filter(predicate)
.map(r -> Optional.ofNullable(r.getReward()).orElse(BigDecimal.ZERO))
.reduce(BigDecimal.ZERO, BigDecimal::add);
return totalSalary.setScale(2, RoundingMode.DOWN);
}
//
// Map<Long, Set<Long>> shopPostIdMap = new HashMap<>();
// for (ManagerStaffDTO managerStaffDTO : manager.getScopeList()) {
// Long postId = managerStaffDTO.getPostId();
//
// if (! postShopIds.keySet().contains(postId)) {
// continue;
// }
// for (ManagerStaffShopDTO shopDTO : managerStaffDTO.getShopList()) {
//
// if () {
//
// }
// Long shopId = shopDTO.getShopId();
// if (shopPostIdMap.containsKey(shopId)) {
// shopPostIdMap.get(shopId).add(postId);
// } else {
// shopPostIdMap.put(shopId, new HashSet<Long>(){{add(postId);}});
// }
// }
// }
// //管理者门店、岗位没有
// if (PublicUtil.isEmpty(shopPostIdMap)) {
// continue;
// }
}