Blame view

fw-morax-server/src/main/java/cn/fw/morax/server/task/SalaryPoolFundShopTask.java 6.9 KB
c574c94a   姜超   feature(*): 推送财务的...
1
2
3
4
  package cn.fw.morax.server.task;
  
  import cn.fw.common.cache.locker.DistributedLocker;
  import cn.fw.ehr.sdk.api.enums.StaffShopTypeEnum;
b05ea76a   姜超   feature(*): 定时任务日志打印
5
  import cn.fw.morax.common.constant.TimeTaskConstant;
c574c94a   姜超   feature(*): 推送财务的...
6
  import cn.fw.morax.common.utils.PublicUtil;
c574c94a   姜超   feature(*): 推送财务的...
7
  import cn.fw.morax.domain.db.salary.SalaryPool;
c574c94a   姜超   feature(*): 推送财务的...
8
9
10
  import cn.fw.morax.rpc.ehr.EhrRpcService;
  import cn.fw.morax.rpc.ehr.dto.StaffBaseInfoDTO;
  import cn.fw.morax.rpc.ehr.dto.StaffShopInfoDTO;
c574c94a   姜超   feature(*): 推送财务的...
11
  import cn.fw.morax.service.data.salary.SalaryPoolService;
c574c94a   姜超   feature(*): 推送财务的...
12
13
  import com.google.common.base.Functions;
  import com.google.common.collect.Lists;
7d59a85e   姜超   feature(*): redis...
14
  import lombok.Getter;
c574c94a   姜超   feature(*): 推送财务的...
15
16
17
  import lombok.RequiredArgsConstructor;
  import lombok.extern.slf4j.Slf4j;
  import org.redisson.api.RLock;
7d59a85e   姜超   feature(*): redis...
18
  import org.springframework.beans.factory.annotation.Value;
c574c94a   姜超   feature(*): 推送财务的...
19
20
21
22
23
  import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  import org.springframework.scheduling.annotation.Scheduled;
  import org.springframework.stereotype.Component;
  import org.springframework.transaction.annotation.Transactional;
  
c574c94a   姜超   feature(*): 推送财务的...
24
  import java.time.YearMonth;
12be9b2a   姜超   fix(rpc): 修改rpc方法
25
26
27
28
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Map;
  import java.util.Set;
c574c94a   姜超   feature(*): 推送财务的...
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  import java.util.concurrent.locks.Lock;
  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 SalaryPoolFundShopTask {
  
c574c94a   姜超   feature(*): 推送财务的...
44
45
46
47
      private final DistributedLocker distributedLocker;
      private final SalaryPoolService salaryPoolService;
      private final EhrRpcService ehrRpcService;
  
7d59a85e   姜超   feature(*): redis...
48
49
50
      @Value("${spring.cache.custom.global-prefix}:salary:pool:fund")
      @Getter
      private String salaryPoolFundKey;
c574c94a   姜超   feature(*): 推送财务的...
51
52
  
      /**
a3dd2862   姜超   feature(*): corn修改
53
       * 每月第一天凌晨执行,薪酬池写入社保公积金门店
c574c94a   姜超   feature(*): 推送财务的...
54
55
56
       * 1. 处理员工有多条绩效池的数据
       * 2. 处理员工只有一条绩效池的数据
       */
b05ea76a   姜超   feature(*): 定时任务日志打印
57
      @Scheduled(cron = TimeTaskConstant.SALARY_POOL_FUND_SHOP)
c574c94a   姜超   feature(*): 推送财务的...
58
59
      @Transactional(rollbackFor = Exception.class)
      public void dealSalaryPoolFundShop() {
7d59a85e   姜超   feature(*): redis...
60
          Lock lock = distributedLocker.lock(getSalaryPoolFundKey());
c574c94a   姜超   feature(*): 推送财务的...
61
62
          if (((RLock) lock).isLocked()) {
              try {
b05ea76a   姜超   feature(*): 定时任务日志打印
63
                  log.info("定时任务【酬池写入社保公积金门店】开始执行");
c574c94a   姜超   feature(*): 推送财务的...
64
65
66
67
68
69
                  //查找待生效数据
                  YearMonth yearMonth = YearMonth.now();
                  List<SalaryPool> updatePools = Lists.newArrayListWithCapacity(1000);
                  this.dealStaffMultiPools(updatePools, yearMonth);
                  this.dealStaffPools(updatePools, yearMonth);
                  salaryPoolService.updateBatchById(updatePools);
a3dd2862   姜超   feature(*): corn修改
70
                  log.info("薪酬池写入社保公积金门店定时任务结束");
c574c94a   姜超   feature(*): 推送财务的...
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
              } catch (Exception e){
                  log.error(e.getMessage(), e);
              } finally {
                  lock.unlock();
              }
          }
      }
  
      /**
       * 处理一个员工多条绩效池的数据
       *
       * @param updatePools
       * @param yearMonth
       */
      @Transactional(rollbackFor = Exception.class)
      public void dealStaffMultiPools(List<SalaryPool> updatePools, YearMonth yearMonth) {
          List<SalaryPool> staffMultiPools = salaryPoolService.queryStaffMultiPools(yearMonth);
          if (PublicUtil.isEmpty(staffMultiPools)) {
              return;
          }
          Set<Long> staffMultiIds = staffMultiPools.stream().map(SalaryPool::getUserId).collect(Collectors.toSet());
          List<Long> staffIds = new ArrayList<>(staffMultiIds);
          Map<Long, List<SalaryPool>> staffMultiPoolMap = staffMultiPools.stream().collect(Collectors.groupingBy(SalaryPool::getUserId));
          List<StaffBaseInfoDTO> multiStaffBaseInfoDTOS = ehrRpcService.queryStaffBaseInfo(staffIds);
          Integer fundShopType = StaffShopTypeEnum.FUND_SHOP.getValue();
  
          for (StaffBaseInfoDTO staff : multiStaffBaseInfoDTOS) {
              List<SalaryPool> pools = staffMultiPoolMap.get(staff.getId());
              if (PublicUtil.isEmpty(pools)) {
                  continue;
              }
              List<StaffShopInfoDTO> staffShops = staff.getStaffShopList();
              StaffShopInfoDTO fundShop = staffShops.stream()
                      .filter(shop -> fundShopType.equals(shop.getType()))
                      .findFirst()
                      .orElse(null);
              if (PublicUtil.isEmpty(fundShop)) {
                  continue;
              }
              for (SalaryPool pool : pools) {
                  pool.setFundShopId(fundShop.getShopId());
                  pool.setFundShopName(fundShop.getShopName());
                  updatePools.add(pool);
              }
          }
      }
  
      /**
       * 处理一个员工一条绩效池的数据
       *
       * @param updatePools
       * @param yearMonth
       */
      @Transactional(rollbackFor = Exception.class)
      public void dealStaffPools(List<SalaryPool> updatePools, YearMonth yearMonth) {
a3dd2862   姜超   feature(*): corn修改
126
          List<SalaryPool> staffPools = salaryPoolService.queryStaffPools(yearMonth);
c574c94a   姜超   feature(*): 推送财务的...
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
          if (PublicUtil.isEmpty(staffPools)) {
              return;
          }
          List<Long> staffIds = staffPools.stream().map(SalaryPool::getUserId).collect(Collectors.toList());
          Map<Long, SalaryPool> staffPoolMap = staffPools.stream()
                  .collect(Collectors.toMap(SalaryPool::getUserId, Functions.identity(), (v1, v2) -> {
                      log.error("出现员工多条绩效池 " + v1.getUserId());
                      return v2;
                  }));
          List<StaffBaseInfoDTO> multiStaffBaseInfoDTOS = ehrRpcService.queryStaffBaseInfo(staffIds);
          Integer fundShopType = StaffShopTypeEnum.FUND_SHOP.getValue();
  
          for (StaffBaseInfoDTO staff : multiStaffBaseInfoDTOS) {
              SalaryPool pool = staffPoolMap.get(staff.getId());
              if (PublicUtil.isEmpty(pool)) {
                  continue;
              }
              List<StaffShopInfoDTO> staffShops = staff.getStaffShopList();
              StaffShopInfoDTO fundShop = staffShops.stream()
                      .filter(shop -> fundShopType.equals(shop.getType()))
                      .findFirst()
                      .orElse(null);
              if (PublicUtil.isEmpty(fundShop)) {
                  continue;
              }
              pool.setFundShopId(fundShop.getShopId());
              pool.setFundShopName(fundShop.getShopName());
              updatePools.add(pool);
          }
      }
  
      /**
       * 截取list
       *
       * @param list
       * @param skip
       * @param pageSize
       * @param <T>
       * @return
       */
      public static <T> List<T> getSubListPage(List<T> list, int skip, int pageSize) {
          if (list == null || list.isEmpty()) {
              return null;
          }
          int startIndex = skip;
          int endIndex = skip + pageSize;
          if (startIndex > endIndex || startIndex > list.size()) {
              return null;
          }
          if (endIndex > list.size()) {
              endIndex = list.size();
          }
          return list.subList(startIndex, endIndex);
      }
  
c574c94a   姜超   feature(*): 推送财务的...
182
  }