Blame view

fw-morax-server/src/main/java/cn/fw/morax/server/task/SalaryGroupTask.java 6.75 KB
1a7eecec   姜超   feature(*): 薪酬组审批...
1
2
  package cn.fw.morax.server.task;
  
6443c00b   张志伟   feature(*): 发放工资&...
3
  import cn.fw.common.cache.locker.DistributedLocker;
b05ea76a   姜超   feature(*): 定时任务日志打印
4
  import cn.fw.morax.common.constant.TimeTaskConstant;
5f57db6b   姜超   fix(*): 薪酬编码修改、绩效...
5
  import cn.fw.morax.common.utils.DateUtil;
1a7eecec   姜超   feature(*): 薪酬组审批...
6
7
8
9
  import cn.fw.morax.common.utils.PublicUtil;
  import cn.fw.morax.domain.db.salary.SalaryGroup;
  import cn.fw.morax.domain.dto.query.SalaryGroupRepeatQueryDTO;
  import cn.fw.morax.domain.enums.SettingStatusEnum;
1a7eecec   姜超   feature(*): 薪酬组审批...
10
  import cn.fw.morax.service.biz.salary.SalaryGroupDataService;
50eb5a99   张志伟   feature(*): 维护薪酬组人员
11
  import cn.fw.morax.service.biz.salary.SalaryGroupUserBizService;
1a7eecec   姜超   feature(*): 薪酬组审批...
12
  import cn.fw.morax.service.data.salary.SalaryGroupService;
e990aa6f   姜超   fix(*): 绩效、薪酬定时任务...
13
  import cn.hutool.core.date.StopWatch;
1a7eecec   姜超   feature(*): 薪酬组审批...
14
  import com.baomidou.mybatisplus.core.toolkit.Wrappers;
7d59a85e   姜超   feature(*): redis...
15
  import lombok.Getter;
1a7eecec   姜超   feature(*): 薪酬组审批...
16
17
  import lombok.RequiredArgsConstructor;
  import lombok.extern.slf4j.Slf4j;
6443c00b   张志伟   feature(*): 发放工资&...
18
  import org.redisson.api.RLock;
7d59a85e   姜超   feature(*): redis...
19
  import org.springframework.beans.factory.annotation.Value;
1a7eecec   姜超   feature(*): 薪酬组审批...
20
21
22
23
24
25
  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 java.time.LocalDate;
5f57db6b   姜超   fix(*): 薪酬编码修改、绩效...
26
  import java.time.LocalDateTime;
1a7eecec   姜超   feature(*): 薪酬组审批...
27
  import java.util.*;
e990aa6f   姜超   fix(*): 绩效、薪酬定时任务...
28
  import java.util.concurrent.TimeUnit;
6443c00b   张志伟   feature(*): 发放工资&...
29
  import java.util.concurrent.locks.Lock;
1a7eecec   姜超   feature(*): 薪酬组审批...
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  import java.util.stream.Collectors;
  
  /**
   * @author : jiangchao
   * @className : SalaryGroupTask
   * @description : 薪酬组配置状态定时器
   * @date : 2022-04-07 15:29
   */
  @Component
  @Slf4j
  @RequiredArgsConstructor
  @ConditionalOnProperty(prefix = "task", name = "switch", havingValue = "on")
  public class SalaryGroupTask {
  
98f0b2aa   姜超   feature(*): 添加yn判断
44
      private final SalaryGroupUserBizService salaryGroupUserBizService;
1a7eecec   姜超   feature(*): 薪酬组审批...
45
      private final SalaryGroupDataService salaryGroupDataService;
1a7eecec   姜超   feature(*): 薪酬组审批...
46
      private final SalaryGroupService salaryGroupService;
6443c00b   张志伟   feature(*): 发放工资&...
47
      private final DistributedLocker distributedLocker;
1a7eecec   姜超   feature(*): 薪酬组审批...
48
  
7d59a85e   姜超   feature(*): redis...
49
50
51
      @Value("${spring.cache.custom.global-prefix}:salary:group")
      @Getter
      private String salaryGroupKey;
e3e0c571   姜超   feature(*): 定时任务添...
52
  
1a7eecec   姜超   feature(*): 薪酬组审批...
53
      /**
b00266cc   姜超   fix(*): 修改重复绩效查询参数
54
       * 每个月1号凌晨3点执行
1a7eecec   姜超   feature(*): 薪酬组审批...
55
56
57
58
       * 1. 将待生效数据改为生效中
       * 2. 将之前的配置设置为失效
       * 3. 处理重复数据,合并、拆分门店的绩效组配置需要把其他的重复配置失效
       */
b05ea76a   姜超   feature(*): 定时任务日志打印
59
      @Scheduled(cron = TimeTaskConstant.SALARY_GROUP)
1a7eecec   姜超   feature(*): 薪酬组审批...
60
61
      @Transactional(rollbackFor = Exception.class)
      public void processCurMonthEffectSalaryGroup() {
7d59a85e   姜超   feature(*): redis...
62
          Lock lock = distributedLocker.lock(getSalaryGroupKey());
e3e0c571   姜超   feature(*): 定时任务添...
63
64
          if (((RLock) lock).isLocked()) {
              try {
b05ea76a   姜超   feature(*): 定时任务日志打印
65
                  log.info("定时任务【每月薪酬组配置状态改变】开始执行");
98f0b2aa   姜超   feature(*): 添加yn判断
66
67
                  //查找待生效数据
                  LocalDate currentTime = LocalDate.now();
e3e0c571   姜超   feature(*): 定时任务添...
68
69
                  List<SalaryGroup> beEffectiveSalaryGroups = salaryGroupService.list(Wrappers.<SalaryGroup>lambdaQuery()
                          .eq(SalaryGroup::getStatus, SettingStatusEnum.BE_EFFECTIVE)
98f0b2aa   姜超   feature(*): 添加yn判断
70
                          .eq(SalaryGroup::getBeginTime, currentTime)
e3e0c571   姜超   feature(*): 定时任务添...
71
72
73
74
75
76
77
                  );
                  if (PublicUtil.isEmpty(beEffectiveSalaryGroups)) {
                      log.info("绩效组定时器:暂无即将生效的绩效组配置");
                      return;
                  }
                  //之前配置都设置为无效(之前配置一定被使用过)
                  List<Long> preIds = beEffectiveSalaryGroups.stream()
98f0b2aa   姜超   feature(*): 添加yn判断
78
79
                          .filter(kpiGroup -> PublicUtil.isNotEmpty(kpiGroup.getPreId()))
                          .map(SalaryGroup::getPreId).collect(Collectors.toList());
e3e0c571   姜超   feature(*): 定时任务添...
80
                  salaryGroupDataService.modifyStatusByIds(preIds, SettingStatusEnum.INEFFECTIVE);
1a7eecec   姜超   feature(*): 薪酬组审批...
81
  
e3e0c571   姜超   feature(*): 定时任务添...
82
                  //以岗位、门店的维度,出现重复的配置,将创建时间最早的设置为失效
98f0b2aa   姜超   feature(*): 添加yn判断
83
                  this.processEffectiveRepeatGroup(beEffectiveSalaryGroups);
1a7eecec   姜超   feature(*): 薪酬组审批...
84
  
e3e0c571   姜超   feature(*): 定时任务添...
85
86
87
88
89
90
91
92
                  //生效的配置
                  salaryGroupDataService.modifyStatusBySalaryGroups(beEffectiveSalaryGroups, SettingStatusEnum.EFFECTIVE);
              } catch (Exception e){
                  log.error(e.getMessage(), e);
              } finally {
                  lock.unlock();
              }
          }
1a7eecec   姜超   feature(*): 薪酬组审批...
93
94
95
      }
  
      /**
98f0b2aa   姜超   feature(*): 添加yn判断
96
       * 处理当月之前的重复生效数据
50eb5a99   张志伟   feature(*): 维护薪酬组人员
97
       *
1a7eecec   姜超   feature(*): 薪酬组审批...
98
99
100
       * @param beEffectiveSalaryGroups
       */
      @Transactional(rollbackFor = Exception.class)
98f0b2aa   姜超   feature(*): 添加yn判断
101
      public void processEffectiveRepeatGroup(List<SalaryGroup> beEffectiveSalaryGroups) {
1a7eecec   姜超   feature(*): 薪酬组审批...
102
103
104
105
106
          Map<Long, List<SalaryGroup>> groupKpiMap = beEffectiveSalaryGroups.stream().collect(Collectors.groupingBy(SalaryGroup::getGroupId));
          Long groupId = null;
          Long postId = null;
          List<SalaryGroup> kpis = null;
          Set<Long> shopIds = null;
1a7eecec   姜超   feature(*): 薪酬组审批...
107
108
109
110
111
112
113
114
115
116
          List<SalaryGroup> beEffectiveKpis = null;
          Map<Long, List<SalaryGroup>> postKpiMap = null;
          for (Map.Entry<Long, List<SalaryGroup>> entry : groupKpiMap.entrySet()) {
              groupId = entry.getKey();
              beEffectiveKpis = entry.getValue();
              postKpiMap = beEffectiveKpis.stream().collect(Collectors.groupingBy(SalaryGroup::getPostId));
              for (Map.Entry<Long, List<SalaryGroup>> postKpi : postKpiMap.entrySet()) {
                  postId = postKpi.getKey();
                  kpis = postKpi.getValue();
                  shopIds = new HashSet<>();
98f0b2aa   姜超   feature(*): 添加yn判断
117
118
                  for (SalaryGroup salaryGroup : kpis) {
                      shopIds.addAll(salaryGroup.getShopIds());
1a7eecec   姜超   feature(*): 薪酬组审批...
119
120
121
                  }
                  SalaryGroupRepeatQueryDTO repeatQueryDTO = SalaryGroupRepeatQueryDTO.builder()
                          .shopIds(new ArrayList<>(shopIds))
98f0b2aa   姜超   feature(*): 添加yn判断
122
                          .status(SettingStatusEnum.EFFECTIVE)
1a7eecec   姜超   feature(*): 薪酬组审批...
123
124
                          .groupId(groupId)
                          .postId(postId)
1a7eecec   姜超   feature(*): 薪酬组审批...
125
                          .build();
98f0b2aa   姜超   feature(*): 添加yn判断
126
127
                  List<SalaryGroup> effectSalaryGroups = salaryGroupService.queryRepeatSalaryGroup(repeatQueryDTO);
                  salaryGroupDataService.modifyStatusBySalaryGroups(effectSalaryGroups, SettingStatusEnum.INEFFECTIVE);
1a7eecec   姜超   feature(*): 薪酬组审批...
128
129
130
131
              }
          }
      }
  
50eb5a99   张志伟   feature(*): 维护薪酬组人员
132
  
e990aa6f   姜超   fix(*): 绩效、薪酬定时任务...
133
134
135
      /**
       * 生成昨日薪酬组人员
       */
b05ea76a   姜超   feature(*): 定时任务日志打印
136
      @Scheduled(cron = TimeTaskConstant.SALARY_GROUP_USER)
50eb5a99   张志伟   feature(*): 维护薪酬组人员
137
      public void asyncSalaryGroupUser() {
6443c00b   张志伟   feature(*): 发放工资&...
138
139
140
          final String lockKey = ":asyncSalaryGroupUser";
          Lock lock = distributedLocker.lock(lockKey);
          if (((RLock) lock).isLocked()) {
e990aa6f   姜超   fix(*): 绩效、薪酬定时任务...
141
142
143
              log.info("薪酬组人员更新定时任务");
              StopWatch stopWatch = new StopWatch();
              stopWatch.start("薪酬组人员更新定时任务");
5f57db6b   姜超   fix(*): 薪酬编码修改、绩效...
144
145
              LocalDateTime queryTime = LocalDateTime.now().minusDays(1);
              List<SalaryGroup> list = salaryGroupService.getAllEffectGroups(DateUtil.localDateTime2Date(queryTime));
6443c00b   张志伟   feature(*): 发放工资&...
146
              for (SalaryGroup salaryGroup : list) {
53994b9e   姜超   feature(*): 调整查询工作天数
147
                  salaryGroupUserBizService.asyncSalaryGroupUser(salaryGroup, queryTime.toLocalDate());
6443c00b   张志伟   feature(*): 发放工资&...
148
              }
e990aa6f   姜超   fix(*): 绩效、薪酬定时任务...
149
150
              stopWatch.stop();
              log.info(stopWatch.prettyPrint(TimeUnit.SECONDS));
6443c00b   张志伟   feature(*): 发放工资&...
151
              lock.unlock();
50eb5a99   张志伟   feature(*): 维护薪酬组人员
152
153
154
          }
      }
  
1a7eecec   姜超   feature(*): 薪酬组审批...
155
  }