Blame view

fw-morax-server/src/main/java/cn/fw/morax/server/task/SalaryReportTask.java 22.4 KB
584c7bfd   姜超   feature(*): 保存薪酬、...
1
2
3
  package cn.fw.morax.server.task;
  
  import cn.fw.common.cache.locker.DistributedLocker;
584c7bfd   姜超   feature(*): 保存薪酬、...
4
5
6
  import cn.fw.morax.common.constant.TimeTaskConstant;
  import cn.fw.morax.common.utils.DateUtil;
  import cn.fw.morax.common.utils.PublicUtil;
584c7bfd   姜超   feature(*): 保存薪酬、...
7
8
9
10
11
  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;
91b69450   姜超   feature(mq): 门店名称...
12
  import cn.fw.morax.domain.enums.ReportDimensionEnum;
584c7bfd   姜超   feature(*): 保存薪酬、...
13
  import cn.fw.morax.rpc.ehr.EhrRpcService;
ab0e54c3   姜超   feature(mq): 门店名称...
14
  import cn.fw.morax.rpc.ehr.dto.*;
584c7bfd   姜超   feature(*): 保存薪酬、...
15
16
  import cn.fw.morax.rpc.oop.OopRpcService;
  import cn.fw.morax.rpc.oop.dto.ShopDTO;
584c7bfd   姜超   feature(*): 保存薪酬、...
17
18
19
20
  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;
f7d0bbad   姜超   feature(*): 修改报错
21
  import com.alibaba.fastjson.JSON;
584c7bfd   姜超   feature(*): 保存薪酬、...
22
23
  import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  import com.google.common.collect.Lists;
7d59a85e   姜超   feature(*): redis...
24
  import lombok.Getter;
584c7bfd   姜超   feature(*): 保存薪酬、...
25
26
27
28
  import lombok.RequiredArgsConstructor;
  import lombok.extern.slf4j.Slf4j;
  import org.apache.commons.collections4.map.MultiKeyMap;
  import org.redisson.api.RLock;
7d59a85e   姜超   feature(*): redis...
29
  import org.springframework.beans.factory.annotation.Value;
584c7bfd   姜超   feature(*): 保存薪酬、...
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  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
1a596c74   姜超   feature(*): 绩效报表
48
   * @description : 薪酬报表定时器
584c7bfd   姜超   feature(*): 保存薪酬、...
49
50
51
52
53
54
55
56
57
58
59
60
61
   * @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;
584c7bfd   姜超   feature(*): 保存薪酬、...
62
63
64
      private final OopRpcService oopRpcService;
      private final DistributedLocker distributedLocker;
  
7d59a85e   姜超   feature(*): redis...
65
66
67
      @Value("${spring.cache.custom.global-prefix}:salary:group:report")
      @Getter
      private String salaryGroupReport;
584c7bfd   姜超   feature(*): 保存薪酬、...
68
69
70
  
      /**
       * 绩效报表定时任务
584c7bfd   姜超   feature(*): 保存薪酬、...
71
       */
1a596c74   姜超   feature(*): 绩效报表
72
      @Scheduled(cron = TimeTaskConstant.SALARY_REPORT)
584c7bfd   姜超   feature(*): 保存薪酬、...
73
74
75
76
77
78
      @Transactional(rollbackFor = Exception.class)
      public void salaryReportTask() {
          this.salaryReport(LocalDate.now().minusDays(1));
      }
  
      /**
1a596c74   姜超   feature(*): 绩效报表
79
       * 薪酬报表定时任务
584c7bfd   姜超   feature(*): 保存薪酬、...
80
81
82
83
84
       *
       * @param date
       */
      @Transactional(rollbackFor = Exception.class)
      public void salaryReport(LocalDate date) {
7d59a85e   姜超   feature(*): redis...
85
          Lock lock = distributedLocker.lock(getSalaryGroupReport());
0e65b89e   张志伟   Merge remote-trac...
86
          if (!((RLock) lock).isLocked()) {
584c7bfd   姜超   feature(*): 保存薪酬、...
87
88
89
              return;
          }
          try {
1a596c74   姜超   feature(*): 绩效报表
90
              log.info("定时任务【薪酬报表数据抽取】开始执行");
584c7bfd   姜超   feature(*): 保存薪酬、...
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  
              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);
584c7bfd   姜超   feature(*): 保存薪酬、...
113
114
  
                  List<SalaryGroup> salaryGroups = groupKpiGroupMap.getOrDefault(groupId, new ArrayList<>());
f1e68b31   姜超   feature(*): 报表修改
115
116
117
118
119
120
121
122
123
                  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));
  
584c7bfd   姜超   feature(*): 保存薪酬、...
124
                  //计算管理者数据
f1e68b31   姜超   feature(*): 报表修改
125
                  calManagerShopPostData(calDTO, salarys, salaryGroups, shopDTOs, shopIds);
584c7bfd   姜超   feature(*): 保存薪酬、...
126
127
                  //计算员工
                  calStaffData(calDTO, salarys);
91b69450   姜超   feature(mq): 门店名称...
128
129
130
131
                  //门店
                  calShopData(calDTO, salarys, salaryGroups, shopDTOs);
                  //岗位
                  calPostData(calDTO, salarys, salaryGroups, shopDTOs);
584c7bfd   姜超   feature(*): 保存薪酬、...
132
133
134
135
136
137
138
139
140
              });
              if (PublicUtil.isNotEmpty(salarys)) {
                  salarys.stream().forEach(salary -> {
                      salary.setDate(date);
                      salary.setYn(Boolean.TRUE);
                  });
                  salaryRewardService.saveBatch(salarys);
              }
  
0e65b89e   张志伟   Merge remote-trac...
141
          } catch (Exception e) {
584c7bfd   姜超   feature(*): 保存薪酬、...
142
143
144
145
146
147
              log.error(e.getMessage(), e);
          } finally {
              lock.unlock();
          }
      }
  
584c7bfd   姜超   feature(*): 保存薪酬、...
148
149
150
151
152
      /**
       * 计算管理者维度数据 (门店、绩效组)
       *
       * @param calDto
       */
ab0e54c3   姜超   feature(mq): 门店名称...
153
154
      public void calManagerShopPostData(GroupCalSalaryDTO calDto,
                                         List<SalaryReward> salarys,
f1e68b31   姜超   feature(*): 报表修改
155
156
157
                                         List<SalaryGroup> salaryGroups,
                                         List<ShopDTO> shopDTOs,
                                         Set<Long> shopIds
584c7bfd   姜超   feature(*): 保存薪酬、...
158
159
      ) {
          List<SalaryPool> pools = calDto.getPools();
f1e68b31   姜超   feature(*): 报表修改
160
161
          Map<Long, Set<Long>> salaryGroupPostShopIds = salaryGroups.stream().collect(Collectors.toMap(
                  SalaryGroup::getPostId, salaryGroup -> new HashSet<>(salaryGroup.getShopIds()),
0e65b89e   张志伟   Merge remote-trac...
162
163
164
165
                  (v1, v2) -> {
                      v1.addAll(v2);
                      return v1;
                  }));
584c7bfd   姜超   feature(*): 保存薪酬、...
166
          List<ManagerDTO> managerDTOS = ehrRpcService.getRealTimeShopManager(new ArrayList<>(shopIds));
6e774960   姜超   fix(report): 查询管理...
167
          log.info("查询门店实时管理者:{},{}", JSON.toJSONString(shopIds), JSON.toJSONString(managerDTOS));
584c7bfd   姜超   feature(*): 保存薪酬、...
168
  
f1e68b31   姜超   feature(*): 报表修改
169
170
          Map<Long, String> postMap = salaryGroups.stream().collect(Collectors.toMap(SalaryGroup::getPostId,
                  SalaryGroup::getPostName, (v1, v2) -> v1));
324b51af   姜超   feature(*): 报表字段修改
171
172
  
  
584c7bfd   姜超   feature(*): 保存薪酬、...
173
          for (ManagerDTO manager : managerDTOS) {
f1e68b31   姜超   feature(*): 报表修改
174
              Map<Long, Set<Long>> createShopPostIdMap = new HashMap<>();
1efe930e   姜超   fix(*): 修改bug
175
              Map<Long, Set<Long>> createPostShopIdMap = new HashMap<>();
f1e68b31   姜超   feature(*): 报表修改
176
177
178
179
180
  
              //管理岗位
              for (ManagerStaffDTO managerStaffDTO : manager.getScopeList()) {
                  Long postId = managerStaffDTO.getPostId();
                  //绩效组所有岗位
0e65b89e   张志伟   Merge remote-trac...
181
                  if (!salaryGroupPostShopIds.keySet().contains(postId)) {
ab0e54c3   姜超   feature(mq): 门店名称...
182
                      continue;
584c7bfd   姜超   feature(*): 保存薪酬、...
183
                  }
f1e68b31   姜超   feature(*): 报表修改
184
185
186
                  //管理门店
                  for (ManagerStaffShopDTO shopDTO : managerStaffDTO.getShopList()) {
                      //绩效组所有门店
0e65b89e   张志伟   Merge remote-trac...
187
                      if (!salaryGroupPostShopIds.get(postId).contains(shopDTO.getShopId())) {
f1e68b31   姜超   feature(*): 报表修改
188
189
190
191
192
193
                          continue;
                      }
                      Long shopId = shopDTO.getShopId();
                      if (createShopPostIdMap.containsKey(shopId)) {
                          createShopPostIdMap.get(shopId).add(postId);
                      } else {
0e65b89e   张志伟   Merge remote-trac...
194
195
196
                          createShopPostIdMap.put(shopId, new HashSet<Long>() {{
                              add(postId);
                          }});
f1e68b31   姜超   feature(*): 报表修改
197
198
                      }
  
1efe930e   姜超   fix(*): 修改bug
199
200
201
202
203
204
205
206
                      if (createPostShopIdMap.containsKey(postId)) {
                          createPostShopIdMap.get(postId).add(postId);
                      } else {
                          createPostShopIdMap.put(postId, new HashSet<Long>() {{
                              add(shopId);
                          }});
                      }
  
f1e68b31   姜超   feature(*): 报表修改
207
                  }
ab0e54c3   姜超   feature(mq): 门店名称...
208
209
              }
  
f1e68b31   姜超   feature(*): 报表修改
210
211
212
213
214
215
              //管理者门店、岗位 与 绩效组门店、岗位没有匹配
              if (PublicUtil.isEmpty(createShopPostIdMap)) {
                  continue;
              }
  
              Set<Long> manageStaffIds = manager.getManageStaffList().stream().map(StaffBaseInfoDTO::getId).collect(Collectors.toSet());
1efe930e   姜超   fix(*): 修改bug
216
              Set<Long> createPostIds = createPostShopIdMap.keySet();
f1e68b31   姜超   feature(*): 报表修改
217
              for (Long postId : createPostIds) {
91b69450   姜超   feature(mq): 门店名称...
218
                  SalaryReward salary = new SalaryReward(ReportDimensionEnum.MANAGER_POST, calDto.getGroupId());
f1e68b31   姜超   feature(*): 报表修改
219
220
221
222
223
                  salary.setS1(manager.getStaffName());
                  salary.setL1(manager.getStaffId());
                  salary.setL3(postId);
                  salary.setS3(postMap.getOrDefault(postId, ""));
  
1efe930e   姜超   fix(*): 修改bug
224
225
226
227
228
                  Set<Long> postShopIds = createPostShopIdMap.get(postId);
                  if (PublicUtil.isNotEmpty(postShopIds)) {
                      salary.setS6(String.join(",", postShopIds.stream().map(String::valueOf).collect(Collectors.toList())));
                  }
  
f1e68b31   姜超   feature(*): 报表修改
229
230
231
232
233
234
235
236
237
238
                  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);
              }
  
91b69450   姜超   feature(mq): 门店名称...
239
240
241
              Set<Long> createShopIds = createShopPostIdMap.keySet();
              shopDTOs.stream().filter(shopDTO -> createShopIds.contains(shopDTO.getId())).forEach(shopDTO -> {
                  SalaryReward salary = new SalaryReward(ReportDimensionEnum.MANAGER_SHOP, calDto.getGroupId());
f1e68b31   姜超   feature(*): 报表修改
242
243
244
245
246
247
                  Long shopId = shopDTO.getId();
                  String shopName = shopDTO.getShopName();
                  salary.setS1(manager.getStaffName());
                  salary.setL1(manager.getStaffId());
                  salary.setL2(shopId);
                  salary.setS2(shopName);
91b69450   姜超   feature(mq): 门店名称...
248
249
250
251
                  Set<Long> postIds = createShopPostIdMap.get(shopDTO.getId());
                  if (PublicUtil.isNotEmpty(postIds)) {
                      salary.setS6(String.join(",", postIds.stream().map(String::valueOf).collect(Collectors.toList())));
                  }
f1e68b31   姜超   feature(*): 报表修改
252
253
254
255
                  Predicate<SalaryPool> predicate = pool -> {
                      return shopId.equals(pool.getShopId()) && manageStaffIds.contains(pool.getUserId());
                  };
                  salary.setB1(totalSalary(pools, predicate));
84f9d303   姜超   feature(*): 定时任务修改
256
                  salary.setB5(countStaffNum(pools, predicate));
f1e68b31   姜超   feature(*): 报表修改
257
258
259
260
261
262
263
                  salary.setB2(averageSalary(pools, predicate));
                  salary.setB3(null);
                  salary.setB4(null);
                  salarys.add(salary);
              });
  
  
91b69450   姜超   feature(mq): 门店名称...
264
              SalaryReward salary = new SalaryReward(ReportDimensionEnum.MANAGER, calDto.getGroupId());
f1e68b31   姜超   feature(*): 报表修改
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
              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));
              }
91b69450   姜超   feature(mq): 门店名称...
281
282
283
              if (PublicUtil.isNotEmpty(createShopIds)) {
                  salary.setS6(String.join(",", createShopIds.stream().map(String::valueOf).collect(Collectors.toList())));
              }
f1e68b31   姜超   feature(*): 报表修改
284
285
286
287
288
289
290
291
292
              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);
  
584c7bfd   姜超   feature(*): 保存薪酬、...
293
294
295
296
          }
      }
  
  
584c7bfd   姜超   feature(*): 保存薪酬、...
297
298
299
300
301
      /**
       * 计算员工维度数据
       *
       * @param calDto
       */
1a596c74   姜超   feature(*): 绩效报表
302
      public void calStaffData(GroupCalSalaryDTO calDto, List<SalaryReward> salarys) {
584c7bfd   姜超   feature(*): 保存薪酬、...
303
304
305
306
307
308
309
310
          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()
8c524c4b   姜超   fix(report): 查询星级...
311
                  .filter(pool -> PublicUtil.isNotEmpty(pool.getStarLevel()))
584c7bfd   姜超   feature(*): 保存薪酬、...
312
313
                  .forEach(pool -> {
                      staffShopPostStar.put(pool.getUserId(), pool.getShopId(), pool.getPostId(), pool.getStarLevel().getValue());
0e65b89e   张志伟   Merge remote-trac...
314
                  });
584c7bfd   姜超   feature(*): 保存薪酬、...
315
316
317
  
  
          for (SalaryPool pool : pools) {
91b69450   姜超   feature(mq): 门店名称...
318
              SalaryReward salary = new SalaryReward(ReportDimensionEnum.STAFF, calDto.getGroupId());
584c7bfd   姜超   feature(*): 保存薪酬、...
319
  
1a596c74   姜超   feature(*): 绩效报表
320
              salary.setL1(pool.getUserId());
584c7bfd   姜超   feature(*): 保存薪酬、...
321
              salary.setS1(pool.getUserName());
584c7bfd   姜超   feature(*): 保存薪酬、...
322
  
584c7bfd   姜超   feature(*): 保存薪酬、...
323
              salary.setL2(pool.getShopId());
1a596c74   姜超   feature(*): 绩效报表
324
325
              salary.setS2(pool.getShopName());
  
584c7bfd   姜超   feature(*): 保存薪酬、...
326
              salary.setL3(pool.getPostId());
1a596c74   姜超   feature(*): 绩效报表
327
328
329
              salary.setS3(pool.getPostName());
  
              salary.setL4(pool.getId());
584c7bfd   姜超   feature(*): 保存薪酬、...
330
331
332
333
334
335
336
337
338
339
340
341
342
  
              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);
          }
      }
  
      /**
       * 计算绩效排名组维度数据
584c7bfd   姜超   feature(*): 保存薪酬、...
343
       */
91b69450   姜超   feature(mq): 门店名称...
344
345
346
347
      public void calShopData(GroupCalSalaryDTO calDto,
                              List<SalaryReward> salarys,
                              List<SalaryGroup> salaryGroups,
                              List<ShopDTO> shopDTOs
584c7bfd   姜超   feature(*): 保存薪酬、...
348
349
      ) {
          List<SalaryPool> pools = calDto.getPools();
f1e68b31   姜超   feature(*): 报表修改
350
351
          //门店 岗位
          Map<Long, Set<Long>> salaryGroupShopPostMap = new HashMap<>();
584c7bfd   姜超   feature(*): 保存薪酬、...
352
          for (SalaryGroup salaryGroup : salaryGroups) {
f1e68b31   姜超   feature(*): 报表修改
353
  
584c7bfd   姜超   feature(*): 保存薪酬、...
354
              Long postId = salaryGroup.getPostId();
f1e68b31   姜超   feature(*): 报表修改
355
              for (Long shopId : salaryGroup.getShopIds()) {
584c7bfd   姜超   feature(*): 保存薪酬、...
356
  
f1e68b31   姜超   feature(*): 报表修改
357
358
359
                  if (salaryGroupShopPostMap.containsKey(shopId)) {
                      salaryGroupShopPostMap.get(shopId).add(postId);
                  } else {
0e65b89e   张志伟   Merge remote-trac...
360
361
362
                      salaryGroupShopPostMap.put(shopId, new HashSet<Long>() {{
                          add(postId);
                      }});
f1e68b31   姜超   feature(*): 报表修改
363
                  }
584c7bfd   姜超   feature(*): 保存薪酬、...
364
  
f1e68b31   姜超   feature(*): 报表修改
365
366
              }
          }
584c7bfd   姜超   feature(*): 保存薪酬、...
367
  
f1e68b31   姜超   feature(*): 报表修改
368
369
370
371
372
373
          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 -> {
  
91b69450   姜超   feature(mq): 门店名称...
374
              SalaryReward salary = new SalaryReward(ReportDimensionEnum.SHOP, calDto.getGroupId());
f1e68b31   姜超   feature(*): 报表修改
375
376
377
378
379
380
381
382
383
              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));
84f9d303   姜超   feature(*): 定时任务修改
384
              salary.setB5(countStaffNum(pools, predicate));
f1e68b31   姜超   feature(*): 报表修改
385
386
387
388
389
390
              salary.setB2(averageSalary(pools, predicate));
              salary.setB3(null);
              salary.setB4(null);
              salarys.add(salary);
  
              for (Long postId : salaryGroupShopPostMap.get(shop.getId())) {
91b69450   姜超   feature(mq): 门店名称...
391
                  SalaryReward salaryPost = new SalaryReward(ReportDimensionEnum.SHOP_POST, calDto.getGroupId());
f1e68b31   姜超   feature(*): 报表修改
392
                  Predicate<SalaryPool> predicatePost = pool -> {
584c7bfd   姜超   feature(*): 保存薪酬、...
393
394
                      return shopId.equals(pool.getShopId()) && postId.equals(pool.getPostId());
                  };
f1e68b31   姜超   feature(*): 报表修改
395
396
397
398
399
400
401
402
403
404
405
406
407
                  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);
              }
  
          });
  
584c7bfd   姜超   feature(*): 保存薪酬、...
408
409
      }
  
91b69450   姜超   feature(mq): 门店名称...
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
      /**
       * 计算绩效排名组维度数据
       *
       */
      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));
84f9d303   姜超   feature(*): 定时任务修改
467
                  salaryShop.setB5(countStaffNum(pools, predicateShop));
91b69450   姜超   feature(mq): 门店名称...
468
469
470
471
472
473
474
475
476
477
                  salaryShop.setB2(averageSalary(pools, predicateShop));
                  salaryShop.setB3(null);
                  salaryShop.setB4(null);
                  salarys.add(salaryShop);
              });
  
          }
  
      }
  
84f9d303   姜超   feature(*): 定时任务修改
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
      /**
       * 绩效池平均绩效得分率
       *
       * @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);
      }
584c7bfd   姜超   feature(*): 保存薪酬、...
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
  
      /**
       * 绩效池平均绩效得分率
       *
       * @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);
      }
  
f1e68b31   姜超   feature(*): 报表修改
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
  //
  //    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;
  //    }
584c7bfd   姜超   feature(*): 保存薪酬、...
555
  }