Blame view

fw-morax-server/src/main/java/cn/fw/morax/server/task/KpiReportRatioTask.java 24.9 KB
60d0b5db   姜超   feature(*): 绩效报表抽取
1
2
3
4
5
6
7
8
9
  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.PublicUtil;
  import cn.fw.morax.domain.db.kpi.KpiGroup;
  import cn.fw.morax.domain.db.kpi.KpiGroupRank;
  import cn.fw.morax.domain.db.kpi.KpiPool;
  import cn.fw.morax.domain.db.kpi.KpiRatio;
60d0b5db   姜超   feature(*): 绩效报表抽取
10
  import cn.fw.morax.domain.dto.GroupCalKpiRatioDTO;
91b69450   姜超   feature(mq): 门店名称...
11
  import cn.fw.morax.domain.enums.ReportDimensionEnum;
60d0b5db   姜超   feature(*): 绩效报表抽取
12
13
  import cn.fw.morax.rpc.ehr.EhrRpcService;
  import cn.fw.morax.rpc.ehr.dto.*;
60d0b5db   姜超   feature(*): 绩效报表抽取
14
15
  import cn.fw.morax.rpc.oop.OopRpcService;
  import cn.fw.morax.rpc.oop.dto.ShopDTO;
60d0b5db   姜超   feature(*): 绩效报表抽取
16
17
18
19
20
21
  import cn.fw.morax.service.data.kpi.KpiGroupRankService;
  import cn.fw.morax.service.data.kpi.KpiGroupService;
  import cn.fw.morax.service.data.kpi.KpiPoolService;
  import cn.fw.morax.service.data.kpi.KpiRatioService;
  import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  import com.google.common.collect.Lists;
7d59a85e   姜超   feature(*): redis...
22
  import lombok.Getter;
60d0b5db   姜超   feature(*): 绩效报表抽取
23
24
  import lombok.RequiredArgsConstructor;
  import lombok.extern.slf4j.Slf4j;
324b51af   姜超   feature(*): 报表字段修改
25
  import org.apache.commons.collections4.map.MultiKeyMap;
60d0b5db   姜超   feature(*): 绩效报表抽取
26
  import org.redisson.api.RLock;
7d59a85e   姜超   feature(*): redis...
27
  import org.springframework.beans.factory.annotation.Value;
60d0b5db   姜超   feature(*): 绩效报表抽取
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  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
1a596c74   姜超   feature(*): 绩效报表
45
46
   * @className : KpiReportRatioTask
   * @description : 绩效报表定时器
60d0b5db   姜超   feature(*): 绩效报表抽取
47
48
49
50
51
52
53
54
   * @date : 2022-04-07 15:29
   */
  @Component
  @Slf4j
  @RequiredArgsConstructor
  @ConditionalOnProperty(prefix = "task", name = "switch", havingValue = "on")
  public class KpiReportRatioTask {
  
584c7bfd   姜超   feature(*): 保存薪酬、...
55
56
      private final KpiGroupRankService kpiGroupRankService;
      private final DistributedLocker distributedLocker;
60d0b5db   姜超   feature(*): 绩效报表抽取
57
58
59
60
      private final KpiGroupService kpiGroupService;
      private final KpiPoolService kpiPoolService;
      private final KpiRatioService kpiRatioService;
      private final EhrRpcService ehrRpcService;
60d0b5db   姜超   feature(*): 绩效报表抽取
61
      private final OopRpcService oopRpcService;
60d0b5db   姜超   feature(*): 绩效报表抽取
62
  
7d59a85e   姜超   feature(*): redis...
63
64
65
      @Value("${spring.cache.custom.global-prefix}:kpi:group:report")
      @Getter
      private String kpiGroupReportDistKey;
60d0b5db   姜超   feature(*): 绩效报表抽取
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
  
      /**
       * 绩效报表定时任务
       *
       */
      @Scheduled(cron = TimeTaskConstant.KPI_REPORT)
      @Transactional(rollbackFor = Exception.class)
      public void kpiReportTask() {
          this.kpiReport(LocalDate.now().minusDays(1));
      }
  
      /**
       * 绩效报表定时任务
       *
       * @param date
       */
      @Transactional(rollbackFor = Exception.class)
      public void kpiReport(LocalDate date) {
7d59a85e   姜超   feature(*): redis...
84
          Lock lock = distributedLocker.lock(getKpiGroupReportDistKey());
60d0b5db   姜超   feature(*): 绩效报表抽取
85
86
87
88
          if (! ((RLock) lock).isLocked()) {
              return;
          }
          try {
1a596c74   姜超   feature(*): 绩效报表
89
              log.info("定时任务【绩效报表数据抽取】开始执行");
60d0b5db   姜超   feature(*): 绩效报表抽取
90
91
92
93
94
95
96
97
98
99
100
101
  
              kpiRatioService.update(Wrappers.<KpiRatio>lambdaUpdate()
                      .set(KpiRatio::getYn, Boolean.FALSE)
                      .eq(KpiRatio::getDate, date)
              );
  
              List<KpiGroup> allKpiGroups = kpiGroupService.queryKgiGroupsByDay(date);
              Map<Long, List<KpiGroup>> groupKpiGroupMap = allKpiGroups.stream().collect(Collectors.groupingBy(KpiGroup::getGroupId));
              List<KpiRatio> kpiRatios = Lists.newArrayListWithCapacity(500);
              YearMonth curMonth = YearMonth.from(date);
              YearMonth preMonth = YearMonth.from(date.minusMonths(1));
              YearMonth preTwoMonth = YearMonth.from(date.minusMonths(2));
60d0b5db   姜超   feature(*): 绩效报表抽取
102
  
f1e68b31   姜超   feature(*): 报表修改
103
              //遍历集团
60d0b5db   姜超   feature(*): 绩效报表抽取
104
              oopRpcService.allGroups().stream().forEach(group -> {
f1e68b31   姜超   feature(*): 报表修改
105
                  //构建查询参数
60d0b5db   姜超   feature(*): 绩效报表抽取
106
                  Long groupId = group.getId();
f1e68b31   姜超   feature(*): 报表修改
107
                  GroupCalKpiRatioDTO calDTO = buildParam(curMonth, preMonth, preTwoMonth, groupId);
60d0b5db   姜超   feature(*): 绩效报表抽取
108
                  calDTO.setKpiGroups(groupKpiGroupMap.getOrDefault(groupId, new ArrayList<>()));
60d0b5db   姜超   feature(*): 绩效报表抽取
109
  
f1e68b31   姜超   feature(*): 报表修改
110
111
112
113
114
115
116
                  //查询所有门店
                  Set<Long> shopIds = calDTO.getKpiGroups().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;
                  }
60d0b5db   姜超   feature(*): 绩效报表抽取
117
118
119
                  List<ShopDTO> shopDTOs = oopRpcService.queryShops(new ArrayList<>(shopIds));
  
                  //计算管理者数据
f1e68b31   姜超   feature(*): 报表修改
120
                  calManagerData(calDTO, kpiRatios, shopDTOs, shopIds);
584c7bfd   姜超   feature(*): 保存薪酬、...
121
                  //计算绩效排名组 门店
f1e68b31   姜超   feature(*): 报表修改
122
                  calKpiRankData(calDTO, kpiRatios, shopDTOs);
60d0b5db   姜超   feature(*): 绩效报表抽取
123
124
                  //计算员工
                  calStaffData(calDTO, kpiRatios);
584c7bfd   姜超   feature(*): 保存薪酬、...
125
                  //计算绩效组门店
f1e68b31   姜超   feature(*): 报表修改
126
                  calShopData(calDTO, kpiRatios, shopDTOs);
60d0b5db   姜超   feature(*): 绩效报表抽取
127
128
129
130
              });
              if (PublicUtil.isNotEmpty(kpiRatios)) {
                  kpiRatios.stream().forEach(kpiRatio -> {
                      kpiRatio.setDate(date);
584c7bfd   姜超   feature(*): 保存薪酬、...
131
                      kpiRatio.setYn(Boolean.TRUE);
60d0b5db   姜超   feature(*): 绩效报表抽取
132
133
134
135
                  });
                  kpiRatioService.saveBatch(kpiRatios);
              }
  
60d0b5db   姜超   feature(*): 绩效报表抽取
136
137
138
139
140
141
142
          } catch (Exception e){
              log.error(e.getMessage(), e);
          } finally {
              lock.unlock();
          }
      }
  
f1e68b31   姜超   feature(*): 报表修改
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
      /**
       *
       * 构建查询参数
       * @param curMonth
       * @param preMonth
       * @param preTwoMonth
       * @param groupId
       * @return
       */
      public GroupCalKpiRatioDTO buildParam(YearMonth curMonth, YearMonth preMonth, YearMonth preTwoMonth, Long groupId) {
  
          List<YearMonth> yearMonths = new ArrayList<YearMonth>(){{add(curMonth);add(preMonth);add(preTwoMonth);}};
  
          List<KpiPool> kpiPools = kpiPoolService.list(Wrappers.<KpiPool>lambdaQuery()
                  .in(KpiPool::getMonthly, yearMonths)
                  .eq(KpiPool::getYn, Boolean.TRUE)
                  .eq(KpiPool::getGroupId, groupId)
          );
          Map<YearMonth, List<KpiPool>> poolMap = kpiPools.stream().collect(Collectors.groupingBy(KpiPool::getMonthly));
          List<KpiPool> curMonthPools = poolMap.getOrDefault(curMonth, new ArrayList<>());
          List<KpiPool> preMonthPools = poolMap.getOrDefault(preMonth, new ArrayList<>());
          List<KpiPool> preTwoMonthPools = poolMap.getOrDefault(preTwoMonth, new ArrayList<>());
  
          List<KpiGroupRank> kpiGroupRanks = kpiGroupRankService.list(Wrappers.<KpiGroupRank>lambdaQuery()
                  .eq(KpiGroupRank::getYn, Boolean.TRUE)
                  .eq(KpiGroupRank::getGroupId, groupId)
          );
  
          GroupCalKpiRatioDTO calDTO = new GroupCalKpiRatioDTO();
          calDTO.setCurMonthPools(curMonthPools);
          calDTO.setPreMonthPools(preMonthPools);
          calDTO.setPreTwoMonthPools(preTwoMonthPools);
          calDTO.setGroupId(groupId);
          calDTO.setKpiGroupRanks(kpiGroupRanks);
  
          return calDTO;
60d0b5db   姜超   feature(*): 绩效报表抽取
179
  
f1e68b31   姜超   feature(*): 报表修改
180
      }
60d0b5db   姜超   feature(*): 绩效报表抽取
181
182
183
184
185
  
      /**
       * 计算管理者维度数据 (门店、绩效组)
       *
       * @param calDto
60d0b5db   姜超   feature(*): 绩效报表抽取
186
187
       */
      public void calManagerData(GroupCalKpiRatioDTO calDto,
f1e68b31   姜超   feature(*): 报表修改
188
189
190
                                 List<KpiRatio> kpiRatios,
                                 List<ShopDTO> shopDTOs,
                                 Set<Long> shopIds
60d0b5db   姜超   feature(*): 绩效报表抽取
191
192
193
194
195
196
      ) {
          List<KpiPool> curMonthPools = calDto.getCurMonthPools();
          List<KpiPool> preMonthPools = calDto.getPreMonthPools();
          List<KpiPool> preTwoMonthPools = calDto.getPreTwoMonthPools();
          List<KpiGroup> kpiGroups = calDto.getKpiGroups();
  
f1e68b31   姜超   feature(*): 报表修改
197
198
199
200
          Map<Long, Set<Long>> kpiGroupPostShopIds = kpiGroups.stream().collect(Collectors.toMap(
                  KpiGroup::getPostId, kpiGroup -> new HashSet<>(kpiGroup.getShopIds()),
                  (v1, v2) -> {v1.addAll(v2);return v1;}));
          MultiKeyMap<Long, Long> kpiGroupPostShopIdMap = new MultiKeyMap<>();
324b51af   姜超   feature(*): 报表字段修改
201
202
          for (KpiGroup kpiGroup : kpiGroups) {
              for (Long shopId : kpiGroup.getShopIds()) {
f1e68b31   姜超   feature(*): 报表修改
203
                  kpiGroupPostShopIdMap.put(kpiGroup.getPostId(), shopId, kpiGroup.getId());
324b51af   姜超   feature(*): 报表字段修改
204
205
206
              }
          }
  
f1e68b31   姜超   feature(*): 报表修改
207
          List<ManagerDTO> managerDTOS = ehrRpcService.getRealTimeShopManager(new ArrayList<>(shopIds));
324b51af   姜超   feature(*): 报表字段修改
208
  
60d0b5db   姜超   feature(*): 绩效报表抽取
209
          for (ManagerDTO manager : managerDTOS) {
91b69450   姜超   feature(mq): 门店名称...
210
              Map<Long, Set<Long>> createShopPostIdMap = new HashMap<>();
f1e68b31   姜超   feature(*): 报表修改
211
212
213
214
215
216
217
              Set<Long> createKpiGroupIds = new HashSet<>();
  
              //管理岗位
              for (ManagerStaffDTO managerStaffDTO : manager.getScopeList()) {
                  Long postId = managerStaffDTO.getPostId();
                  //绩效组所有岗位
                  if (! kpiGroupPostShopIds.keySet().contains(postId)) {
ab0e54c3   姜超   feature(mq): 门店名称...
218
219
                      continue;
                  }
f1e68b31   姜超   feature(*): 报表修改
220
221
                  //管理门店
                  for (ManagerStaffShopDTO shopDTO : managerStaffDTO.getShopList()) {
91b69450   姜超   feature(mq): 门店名称...
222
                      //绩效组岗位所有门店
f1e68b31   姜超   feature(*): 报表修改
223
224
225
                      if (! kpiGroupPostShopIds.get(postId).contains(shopDTO.getShopId())) {
                          continue;
                      }
91b69450   姜超   feature(mq): 门店名称...
226
227
228
229
230
231
232
233
  
                      Long shopId = shopDTO.getShopId();
                      if (createShopPostIdMap.containsKey(shopId)) {
                          createShopPostIdMap.get(shopId).add(postId);
                      } else {
                          createShopPostIdMap.put(shopId, new HashSet<Long>(){{add(postId);}});
                      }
  
f1e68b31   姜超   feature(*): 报表修改
234
235
236
237
238
239
240
                      if (kpiGroupPostShopIdMap.containsKey(postId, shopDTO.getShopId())) {
                          createKpiGroupIds.add(kpiGroupPostShopIdMap.get(postId, shopDTO.getShopId()));
                      }
                  }
              }
  
              //管理者门店、岗位 与 绩效组门店、岗位没有匹配
91b69450   姜超   feature(mq): 门店名称...
241
              if (PublicUtil.isEmpty(createShopPostIdMap)) {
f1e68b31   姜超   feature(*): 报表修改
242
243
244
245
246
247
                  continue;
              }
  
              Set<Long> manageStaffIds = manager.getManageStaffList().stream().map(StaffBaseInfoDTO::getId).collect(Collectors.toSet());
  
              kpiGroups.stream().filter(kpiGroup -> createKpiGroupIds.contains(kpiGroup.getId())).forEach(kpiGroup -> {
91b69450   姜超   feature(mq): 门店名称...
248
                  KpiRatio kpiRatio = new KpiRatio(ReportDimensionEnum.MANAGER_KPI_GROUP, calDto.getGroupId());
f1e68b31   姜超   feature(*): 报表修改
249
250
251
252
                  kpiRatio.setS1(manager.getStaffName());
                  kpiRatio.setL1(manager.getStaffId());
                  kpiRatio.setS4(kpiGroup.getName());
                  kpiRatio.setL4(kpiGroup.getId());
1efe930e   姜超   fix(*): 修改bug
253
254
                  kpiRatio.setS3(kpiGroup.getPostName());
                  kpiRatio.setL3(kpiGroup.getPostId());
f1e68b31   姜超   feature(*): 报表修改
255
256
257
258
259
260
261
262
                  Predicate<KpiPool> predicate = pool -> {
                      return kpiGroup.getId().equals(pool.getKpiGroupId()) && manageStaffIds.contains(pool.getUserId())
                              && Boolean.TRUE.equals(pool.getInclusion());
                  };
                  kpiRatio.setB1(averageRatio(curMonthPools, predicate));
                  kpiRatio.setB2(monthRatio(curMonthPools, predicate));
                  kpiRatio.setB3(monthRatio(preMonthPools, predicate));
                  kpiRatio.setB4(monthRatio(preTwoMonthPools, predicate));
1efe930e   姜超   fix(*): 修改bug
263
264
  
                  kpiRatio.setS8(String.join(",", kpiGroup.getShopIds().stream().map(String::valueOf).collect(Collectors.toList())));
f1e68b31   姜超   feature(*): 报表修改
265
266
267
                  kpiRatios.add(kpiRatio);
              });
  
91b69450   姜超   feature(mq): 门店名称...
268
              Set<Long> createShopIds = createShopPostIdMap.keySet();
f1e68b31   姜超   feature(*): 报表修改
269
              shopDTOs.stream().filter(shopDTO -> createShopIds.contains(shopDTO.getId())).forEach(shopDTO -> {
91b69450   姜超   feature(mq): 门店名称...
270
                  KpiRatio kpiRatio = new KpiRatio(ReportDimensionEnum.MANAGER_SHOP, calDto.getGroupId());
f1e68b31   姜超   feature(*): 报表修改
271
272
273
274
275
276
                  Long shopId = shopDTO.getId();
                  String shopName = shopDTO.getShopName();
                  kpiRatio.setS1(manager.getStaffName());
                  kpiRatio.setL1(manager.getStaffId());
                  kpiRatio.setL2(shopId);
                  kpiRatio.setS2(shopName);
91b69450   姜超   feature(mq): 门店名称...
277
278
279
280
281
282
  
                  Set<Long> postIds = createShopPostIdMap.get(shopDTO.getId());
                  if (PublicUtil.isNotEmpty(postIds)) {
                      kpiRatio.setS8(String.join(",", postIds.stream().map(String::valueOf).collect(Collectors.toList())));
                  }
  
f1e68b31   姜超   feature(*): 报表修改
283
284
285
286
287
288
289
290
                  Predicate<KpiPool> predicate = pool -> {
                      return shopId.equals(pool.getShopId()) && manageStaffIds.contains(pool.getUserId())
                              && Boolean.TRUE.equals(pool.getInclusion());
                  };
                  kpiRatio.setB1(averageRatio(curMonthPools, predicate));
                  kpiRatio.setB2(monthRatio(curMonthPools, predicate));
                  kpiRatio.setB3(monthRatio(preMonthPools, predicate));
                  kpiRatio.setB4(monthRatio(preTwoMonthPools, predicate));
84f9d303   姜超   feature(*): 定时任务修改
291
                  kpiRatio.setB7(countStaffNum(curMonthPools, predicate));
f1e68b31   姜超   feature(*): 报表修改
292
293
294
295
                  kpiRatios.add(kpiRatio);
              });
  
  
91b69450   姜超   feature(mq): 门店名称...
296
              KpiRatio kpiRatio = new KpiRatio(ReportDimensionEnum.MANAGER, calDto.getGroupId());
f1e68b31   姜超   feature(*): 报表修改
297
298
299
300
301
302
303
304
305
306
307
              kpiRatio.setS1(manager.getStaffName());
              kpiRatio.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)) {
91b69450   姜超   feature(mq): 门店名称...
308
                  kpiRatio.setS6(String.join(",", roleCodes));
f1e68b31   姜超   feature(*): 报表修改
309
310
              }
              if (PublicUtil.isNotEmpty(roleNames)) {
91b69450   姜超   feature(mq): 门店名称...
311
312
313
314
                  kpiRatio.setS7(String.join(",", roleNames));
              }
              if (PublicUtil.isNotEmpty(createShopIds)) {
                  kpiRatio.setS9(String.join(",", createShopIds.stream().map(String::valueOf).collect(Collectors.toList())));
60d0b5db   姜超   feature(*): 绩效报表抽取
315
              }
f1e68b31   姜超   feature(*): 报表修改
316
317
318
319
320
321
322
323
              Predicate<KpiPool> predicate = pool -> {
                  return manageStaffIds.contains(pool.getUserId()) && Boolean.TRUE.equals(pool.getInclusion());
              };
              kpiRatio.setB1(averageRatio(curMonthPools, predicate));
              kpiRatio.setB2(monthRatio(curMonthPools, predicate));
              kpiRatio.setB3(monthRatio(preMonthPools, predicate));
              kpiRatio.setB4(monthRatio(preTwoMonthPools, predicate));
              kpiRatios.add(kpiRatio);
60d0b5db   姜超   feature(*): 绩效报表抽取
324
  
60d0b5db   姜超   feature(*): 绩效报表抽取
325
326
          }
      }
f1e68b31   姜超   feature(*): 报表修改
327
      
60d0b5db   姜超   feature(*): 绩效报表抽取
328
329
330
331
332
333
      /**
       * 计算绩效排名组-门店维度数据
       *
       * @param calDto
       * @param kpiRatios
       */
f1e68b31   姜超   feature(*): 报表修改
334
335
336
      public void calKpiRankData(GroupCalKpiRatioDTO calDto,
                                 List<KpiRatio> kpiRatios,
                                 List<ShopDTO> shopDTOs
60d0b5db   姜超   feature(*): 绩效报表抽取
337
338
339
340
341
342
343
344
345
346
347
      ) {
          List<KpiPool> curMonthPools = calDto.getCurMonthPools();
          List<KpiPool> preMonthPools = calDto.getPreMonthPools();
          List<KpiPool> preTwoMonthPools = calDto.getPreTwoMonthPools();
          List<KpiGroup> kpiGroups = calDto.getKpiGroups();
  
          for (KpiGroupRank rank : calDto.getKpiGroupRanks()) {
              List<String> kgcs = rank.getKgcs();
              List<KpiGroup> rankKpiGroups = kpiGroups.stream()
                      .filter(kpiGroup -> kgcs.contains(kpiGroup.getKgc()))
                      .collect(Collectors.toList());
91b69450   姜超   feature(mq): 门店名称...
348
              Set<Long> createShopIds = rankKpiGroups.stream().flatMap(kpiGroup -> kpiGroup.getShopIds().stream()).collect(Collectors.toSet());
60d0b5db   姜超   feature(*): 绩效报表抽取
349
  
91b69450   姜超   feature(mq): 门店名称...
350
351
              shopDTOs.stream().filter(shop -> createShopIds.contains(shop.getId())).forEach(shop -> {
                  KpiRatio kpiRatio = new KpiRatio(ReportDimensionEnum.KPI_RANK_SHOP, calDto.getGroupId());;
60d0b5db   姜超   feature(*): 绩效报表抽取
352
353
354
  
                  Long shopId = shop.getId();
                  Predicate<KpiPool> predicate = pool -> {
c53529fd   姜超   fix(*): 报表筛选修改
355
                      return shopId.equals(pool.getShopId()) && kgcs.contains(pool.getKgc()) && Boolean.TRUE.equals(pool.getInclusion());
60d0b5db   姜超   feature(*): 绩效报表抽取
356
357
358
359
360
361
                  };
                  kpiRatio.setS5(rank.getName());
                  kpiRatio.setL5(rank.getId());
                  kpiRatio.setL2(shopId);
                  kpiRatio.setS2(shop.getShopName());
                  kpiRatio.setB1(averageRatio(curMonthPools, predicate));
84f9d303   姜超   feature(*): 定时任务修改
362
                  kpiRatio.setB7(countStaffNum(curMonthPools, predicate));
60d0b5db   姜超   feature(*): 绩效报表抽取
363
364
365
366
367
                  kpiRatio.setB2(monthRatio(curMonthPools, predicate));
                  kpiRatio.setB3(monthRatio(preMonthPools, predicate));
                  kpiRatio.setB4(monthRatio(preTwoMonthPools, predicate));
                  kpiRatios.add(kpiRatio);
              });
f1e68b31   姜超   feature(*): 报表修改
368
  
91b69450   姜超   feature(mq): 门店名称...
369
              KpiRatio kpiRatio = new KpiRatio(ReportDimensionEnum.KPI_RANK, calDto.getGroupId());;
f1e68b31   姜超   feature(*): 报表修改
370
371
372
373
374
              Predicate<KpiPool> predicate = pool -> {
                  return kgcs.contains(pool.getKgc()) && Boolean.TRUE.equals(pool.getInclusion());
              };
              kpiRatio.setS5(rank.getName());
              kpiRatio.setL5(rank.getId());
91b69450   姜超   feature(mq): 门店名称...
375
376
377
378
              if (PublicUtil.isNotEmpty(createShopIds)) {
                  kpiRatio.setS9(String.join(",", createShopIds.stream().map(String::valueOf).collect(Collectors.toList())));
              }
  
f1e68b31   姜超   feature(*): 报表修改
379
380
381
382
383
384
              kpiRatio.setB1(averageRatio(curMonthPools, predicate));
              kpiRatio.setB2(monthRatio(curMonthPools, predicate));
              kpiRatio.setB3(monthRatio(preMonthPools, predicate));
              kpiRatio.setB4(monthRatio(preTwoMonthPools, predicate));
              kpiRatios.add(kpiRatio);
  
60d0b5db   姜超   feature(*): 绩效报表抽取
385
386
387
388
389
390
391
392
393
          }
      }
  
      /**
       * 计算绩效组门店维度数据
       *
       * @param calDto
       * @param kpiRatios
       */
f1e68b31   姜超   feature(*): 报表修改
394
395
396
      public void calShopData(GroupCalKpiRatioDTO calDto,
                              List<KpiRatio> kpiRatios,
                              List<ShopDTO> shopDTOs
60d0b5db   姜超   feature(*): 绩效报表抽取
397
398
399
400
401
402
      ) {
          List<KpiPool> curMonthPools = calDto.getCurMonthPools();
          List<KpiPool> preMonthPools = calDto.getPreMonthPools();
          List<KpiPool> preTwoMonthPools = calDto.getPreTwoMonthPools();
          List<KpiGroup> kpiGroups = calDto.getKpiGroups();
  
f1e68b31   姜超   feature(*): 报表修改
403
          shopDTOs.stream().forEach(shop -> {
91b69450   姜超   feature(mq): 门店名称...
404
              KpiRatio kpiRatio = new KpiRatio(ReportDimensionEnum.SHOP, calDto.getGroupId());
f1e68b31   姜超   feature(*): 报表修改
405
406
407
408
409
410
411
              Long shopId = shop.getId();
              Predicate<KpiPool> predicate = pool -> {
                  return shopId.equals(pool.getShopId()) && Boolean.TRUE.equals(pool.getInclusion());
              };
              kpiRatio.setL2(shopId);
              kpiRatio.setS2(shop.getShopName());
              kpiRatio.setB1(averageRatio(curMonthPools, predicate));
84f9d303   姜超   feature(*): 定时任务修改
412
              kpiRatio.setB7(countStaffNum(curMonthPools, predicate));
f1e68b31   姜超   feature(*): 报表修改
413
414
415
416
417
418
419
420
421
422
              kpiRatio.setB2(monthRatio(curMonthPools, predicate));
              kpiRatio.setB3(monthRatio(preMonthPools, predicate));
              kpiRatio.setB4(monthRatio(preTwoMonthPools, predicate));
              kpiRatios.add(kpiRatio);
  
              for (KpiGroup kpiGroup : kpiGroups) {
                  List<Long> shopIds = kpiGroup.getShopIds();
                  if (! shopIds.contains(shopId)) {
                      continue;
                  }
60d0b5db   姜超   feature(*): 绩效报表抽取
423
  
91b69450   姜超   feature(mq): 门店名称...
424
                  KpiRatio kpiRatioKpiGroup = new KpiRatio(ReportDimensionEnum.SHOP_KPI_GROUP, calDto.getGroupId());
f1e68b31   姜超   feature(*): 报表修改
425
                  Predicate<KpiPool> predicateKpiGroup = pool -> {
91b69450   姜超   feature(mq): 门店名称...
426
                      return shopId.equals(pool.getShopId()) && kpiGroup.getId().equals(pool.getKpiGroupId()) && Boolean.TRUE.equals(pool.getInclusion());
f1e68b31   姜超   feature(*): 报表修改
427
428
429
430
431
432
433
434
435
436
                  };
                  kpiRatioKpiGroup.setL2(shopId);
                  kpiRatioKpiGroup.setS2(shop.getShopName());
                  kpiRatioKpiGroup.setS4(kpiGroup.getName());
                  kpiRatioKpiGroup.setL4(kpiGroup.getId());
                  kpiRatioKpiGroup.setB1(averageRatio(curMonthPools, predicateKpiGroup));
                  kpiRatioKpiGroup.setB2(monthRatio(curMonthPools, predicateKpiGroup));
                  kpiRatioKpiGroup.setB3(monthRatio(preMonthPools, predicateKpiGroup));
                  kpiRatioKpiGroup.setB4(monthRatio(preTwoMonthPools, predicateKpiGroup));
                  kpiRatios.add(kpiRatioKpiGroup);
60d0b5db   姜超   feature(*): 绩效报表抽取
437
  
60d0b5db   姜超   feature(*): 绩效报表抽取
438
  
f1e68b31   姜超   feature(*): 报表修改
439
440
441
              }
  
          });
60d0b5db   姜超   feature(*): 绩效报表抽取
442
  
60d0b5db   姜超   feature(*): 绩效报表抽取
443
  
39abadd4   姜超   feature(mq): 门店名称...
444
  
39abadd4   姜超   feature(mq): 门店名称...
445
  
60d0b5db   姜超   feature(*): 绩效报表抽取
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
      }
  
      /**
       * 计算员工维度数据
       *
       * @param calDto
       */
      public void calStaffData(GroupCalKpiRatioDTO calDto,
                             List<KpiRatio> kpiRatios
      ) {
          List<KpiPool> curMonthPools = calDto.getCurMonthPools();
          List<KpiPool> preMonthPools = calDto.getPreMonthPools();
          List<KpiPool> preTwoMonthPools = calDto.getPreTwoMonthPools();
          List<KpiGroup> kpiGroups = calDto.getKpiGroups();
          List<KpiGroupRank> kpiGroupRanks = calDto.getKpiGroupRanks();
  
          Map<Long, String> kpiGroupNameMap = kpiGroups.stream().collect(Collectors.toMap(KpiGroup::getId, KpiGroup::getName, (v1, v2) -> v1));
          Map<String, KpiGroupRank> kgcRankMap = new HashMap<>();
          for (KpiGroupRank rank : kpiGroupRanks) {
              for (String kgc : rank.getKgcs()) {
                  kgcRankMap.put(kgc, rank);
              }
          }
  
          for (KpiPool pool : curMonthPools) {
91b69450   姜超   feature(mq): 门店名称...
471
              KpiRatio kpiRatio = new KpiRatio(ReportDimensionEnum.STAFF, calDto.getGroupId());;
60d0b5db   姜超   feature(*): 绩效报表抽取
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
              Long staffId = pool.getUserId();
  
              kpiRatio.setS1(pool.getUserName());
              kpiRatio.setS2(pool.getShopName());
              kpiRatio.setS3(pool.getPostName());
              kpiRatio.setI2(pool.getRank());
              kpiRatio.setB5(pool.getInclusion());
              kpiRatio.setI1(pool.getActualStar().getValue());
              kpiRatio.setL1(pool.getUserId());
              kpiRatio.setL2(pool.getShopId());
              kpiRatio.setL3(pool.getPostId());
              kpiRatio.setL6(pool.getId());
              kpiRatio.setL4(pool.getKpiGroupId());
              kpiRatio.setS4(kpiGroupNameMap.getOrDefault(pool.getKpiGroupId(), " "));
  
              if (kgcRankMap.containsKey(pool.getKgc())) {
                  KpiGroupRank rank = kgcRankMap.get(pool.getKgc());
                  kpiRatio.setL5(rank.getId());
                  kpiRatio.setS5(rank.getName());
              }
  
              Predicate<KpiPool> predicate = poolDto -> staffId.equals(poolDto.getUserId()) && Boolean.TRUE.equals(pool.getInclusion());
              kpiRatio.setB1(averageRatio(curMonthPools, predicate));
  
              Optional<BigDecimal> curMonthOp = curMonthPools.stream()
                      .filter(poolDto -> poolDto.getUserId().equals(staffId) && Boolean.TRUE.equals(pool.getInclusion()))
                      .findFirst()
                      .map(KpiPool::getKpiScoreRatio);
  
              Optional<BigDecimal> preMonthOp = preMonthPools.stream()
                      .filter(poolDto -> poolDto.getUserId().equals(staffId) && Boolean.TRUE.equals(pool.getInclusion()))
                      .findFirst()
                      .map(KpiPool::getKpiScoreRatio);
  
              Optional<BigDecimal> preTwoMonthOp = preTwoMonthPools.stream()
                      .filter(poolDto -> poolDto.getUserId().equals(staffId) && Boolean.TRUE.equals(pool.getInclusion()))
                      .findFirst()
                      .map(KpiPool::getKpiScoreRatio);
  
              if (curMonthOp.isPresent()) {
                  kpiRatio.setB2(curMonthOp.get().multiply(new BigDecimal("100")).setScale(2, RoundingMode.DOWN));
              }
              if (preMonthOp.isPresent()) {
                  kpiRatio.setB3(preMonthOp.get().multiply(new BigDecimal("100")).setScale(2, RoundingMode.DOWN));
              }
              if (preTwoMonthOp.isPresent()) {
                  kpiRatio.setB4(preTwoMonthOp.get().multiply(new BigDecimal("100")).setScale(2, RoundingMode.DOWN));
              }
  
              kpiRatios.add(kpiRatio);
          }
      }
  
84f9d303   姜超   feature(*): 定时任务修改
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
      /**
       * 绩效池平均绩效得分率
       *
       * @param pools
       * @return
       */
      public BigDecimal countStaffNum(Collection<KpiPool> pools, Predicate<KpiPool> predicate) {
          if (CollectionUtils.isEmpty(pools)) {
              return BigDecimal.ZERO;
          }
          Long staffNum = pools.stream()
                  .filter(predicate)
                  .count();
          return new BigDecimal(staffNum);
      }
60d0b5db   姜超   feature(*): 绩效报表抽取
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
  
      /**
       * 绩效池平均绩效得分率
       *
       * @param pools
       * @return
       */
      public BigDecimal averageRatio(Collection<KpiPool> pools, Predicate<KpiPool> predicate) {
          if (CollectionUtils.isEmpty(pools)) {
              return BigDecimal.ZERO;
          }
          Double averageRatioDouble = pools.stream()
                  .filter(predicate)
                  .mapToDouble(r -> Optional.ofNullable(r.getAverageKpiScoreRatio()).orElse(BigDecimal.ZERO).doubleValue())
                  .average()
                  .orElse(0);
          BigDecimal averageRatio = new BigDecimal(averageRatioDouble.toString());
          return averageRatio.multiply(new BigDecimal("100")).setScale(2, RoundingMode.DOWN);
      }
  
      /**
       * 当月绩效池平均绩效得分率
       *
       * @param pools
       * @return
       */
      public BigDecimal monthRatio(Collection<KpiPool> pools, Predicate<KpiPool> predicate) {
          if (CollectionUtils.isEmpty(pools)) {
              return BigDecimal.ZERO;
          }
          Double averageRatioDouble = pools.stream()
                  .filter(predicate)
                  .mapToDouble(r -> Optional.ofNullable(r.getKpiScoreRatio()).orElse(BigDecimal.ZERO).doubleValue())
                  .average()
                  .orElse(0);
          BigDecimal averageRatio = new BigDecimal(averageRatioDouble.toString());
          return averageRatio.multiply(new BigDecimal("100")).setScale(2, RoundingMode.DOWN);
      }
  
  }