10 天以前 8c14b50773a1e582b652c03f5a63bb2233d069b8
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
package cn.iocoder.yudao.module.hrm.service.salary;
 
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.hrm.controller.admin.salary.vo.HrmSalaryPaymentDetailPageReqVO;
import cn.iocoder.yudao.module.hrm.controller.admin.salary.vo.HrmSalaryPaymentPageReqVO;
import cn.iocoder.yudao.module.hrm.dal.dataobject.salary.HrmSalaryCalculationDO;
import cn.iocoder.yudao.module.hrm.dal.dataobject.salary.HrmSalaryPaymentDetailDO;
import cn.iocoder.yudao.module.hrm.dal.dataobject.salary.HrmSalaryPaymentDO;
import cn.iocoder.yudao.module.hrm.dal.mysql.salary.HrmSalaryCalculationMapper;
import cn.iocoder.yudao.module.hrm.dal.mysql.salary.HrmSalaryPaymentDetailMapper;
import cn.iocoder.yudao.module.hrm.dal.mysql.salary.HrmSalaryPaymentMapper;
import cn.iocoder.yudao.module.hrm.dal.redis.HrmNoRedisDAO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
 
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.hrm.enums.ErrorCodeConstants.*;
 
/**
 * 薪资发放 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class HrmSalaryPaymentServiceImpl implements HrmSalaryPaymentService {
 
    @Resource
    private HrmSalaryPaymentMapper salaryPaymentMapper;
    @Resource
    private HrmSalaryPaymentDetailMapper salaryPaymentDetailMapper;
    @Resource
    private HrmSalaryCalculationMapper salaryCalculationMapper;
    @Resource
    private HrmNoRedisDAO noRedisDAO;
 
    @Override
    public PageResult<HrmSalaryPaymentDO> getSalaryPaymentPage(HrmSalaryPaymentPageReqVO pageReqVO) {
        return salaryPaymentMapper.selectPage(pageReqVO);
    }
 
    @Override
    public HrmSalaryPaymentDO getSalaryPayment(Long id) {
        return salaryPaymentMapper.selectById(id);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long generatePayment(String period) {
        // 1. 检查是否已存在该周期的发放台账
        HrmSalaryPaymentDO existPayment = salaryPaymentMapper.selectByPeriod(period);
        if (existPayment != null) {
            throw exception(SALARY_PAYMENT_EXISTS);
        }
 
        // 2. 查询该周期已确认的薪酬核算记录(状态=10)
        List<HrmSalaryCalculationDO> calculationList = salaryCalculationMapper.selectListByPeriodAndStatus(period, 10);
        if (CollUtil.isEmpty(calculationList)) {
            throw exception(SALARY_CALCULATION_NO_CONFIRMED);
        }
 
        // 3. 生成发放台账
        String no = generatePaymentNo();
        BigDecimal totalAmount = calculationList.stream()
                .map(c -> c.getActualSalary() != null ? c.getActualSalary() : BigDecimal.ZERO)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        Integer totalCount = calculationList.size();
 
        HrmSalaryPaymentDO payment = HrmSalaryPaymentDO.builder()
                .no(no)
                .period(period)
                .totalAmount(totalAmount)
                .totalCount(totalCount)
                .status(0) // 待发放
                .build();
        salaryPaymentMapper.insert(payment);
 
        // 4. 生成发放明细(不更新核算状态)
        for (HrmSalaryCalculationDO calculation : calculationList) {
            HrmSalaryPaymentDetailDO detail = HrmSalaryPaymentDetailDO.builder()
                    .paymentId(payment.getId())
                    .calculationId(calculation.getId())
                    .userId(calculation.getUserId())
                    .deptId(calculation.getDeptId())
                    .actualSalary(calculation.getActualSalary())
                    .status(0) // 待发放
                    .build();
            salaryPaymentDetailMapper.insert(detail);
        }
 
        return payment.getId();
    }
 
    private String generatePaymentNo() {
        String no = noRedisDAO.generateSalaryPaymentNo();
        while (salaryPaymentMapper.selectByNo(no) != null) {
            no = noRedisDAO.generateSalaryPaymentNo();
        }
        return no;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void paySalary(Long id) {
        // 1. 校验发放台账存在
        HrmSalaryPaymentDO payment = validateSalaryPaymentExists(id);
        if (payment.getStatus() != 0) {
            throw exception(SALARY_PAYMENT_ALREADY_PAY);
        }
 
        // 2. 查询待发放的明细
        List<HrmSalaryPaymentDetailDO> detailList = salaryPaymentDetailMapper.selectListByPaymentIdAndStatus(id, 0);
        if (CollUtil.isEmpty(detailList)) {
            throw exception(SALARY_PAYMENT_DETAIL_NO_EXISTS);
        }
 
        // 3. 批量发放明细,并更新核算状态
        LocalDateTime paymentTime = LocalDateTime.now();
        for (HrmSalaryPaymentDetailDO detail : detailList) {
            detail.setStatus(10); // 已发放
            detail.setPaymentTime(paymentTime);
            salaryPaymentDetailMapper.updateById(detail);
 
            // 更新薪酬核算状态为已发放(20)
            HrmSalaryCalculationDO calculation = salaryCalculationMapper.selectById(detail.getCalculationId());
            if (calculation != null) {
                calculation.setStatus(20);
                calculation.setPaymentTime(paymentTime);
                salaryCalculationMapper.updateById(calculation);
            }
        }
 
        // 4. 更新发放台账状态
        payment.setStatus(10); // 已发放
        payment.setPaymentTime(paymentTime);
        salaryPaymentMapper.updateById(payment);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void paySalaryDetail(List<Long> detailIds) {
        if (CollUtil.isEmpty(detailIds)) {
            return;
        }
        LocalDateTime paymentTime = LocalDateTime.now();
        Long paymentId = null;
        for (Long detailId : detailIds) {
            HrmSalaryPaymentDetailDO detail = salaryPaymentDetailMapper.selectById(detailId);
            if (detail == null) {
                throw exception(SALARY_PAYMENT_DETAIL_NOT_EXISTS);
            }
            if (detail.getStatus() != 0) {
                throw exception(SALARY_PAYMENT_DETAIL_ALREADY_PAY);
            }
            detail.setStatus(10); // 已发放
            detail.setPaymentTime(paymentTime);
            salaryPaymentDetailMapper.updateById(detail);
            paymentId = detail.getPaymentId();
 
            // 更新薪酬核算状态为已发放(20)
            HrmSalaryCalculationDO calculation = salaryCalculationMapper.selectById(detail.getCalculationId());
            if (calculation != null) {
                calculation.setStatus(20);
                calculation.setPaymentTime(paymentTime);
                salaryCalculationMapper.updateById(calculation);
            }
        }
 
        // 检查该台账下是否所有明细都已发放,如果是则更新台账状态
        if (paymentId != null) {
            updatePaymentStatusIfAllPaid(paymentId);
        }
    }
 
    /**
     * 检查该台账下是否所有明细都已发放,如果是则更新台账状态为已发放
     */
    private void updatePaymentStatusIfAllPaid(Long paymentId) {
        // 查询该台账下所有明细
        List<HrmSalaryPaymentDetailDO> allDetails = salaryPaymentDetailMapper.selectListByPaymentId(paymentId);
        if (CollUtil.isEmpty(allDetails)) {
            return;
        }
        // 检查是否全部已发放
        boolean allPaid = allDetails.stream().allMatch(d -> d.getStatus() == 10);
        if (!allPaid) {
            return;
        }
        // 更新台账状态为已发放
        HrmSalaryPaymentDO payment = salaryPaymentMapper.selectById(paymentId);
        if (payment != null && payment.getStatus() == 0) {
            payment.setStatus(10);
            payment.setPaymentTime(LocalDateTime.now());
            salaryPaymentMapper.updateById(payment);
        }
    }
 
    private HrmSalaryPaymentDO validateSalaryPaymentExists(Long id) {
        HrmSalaryPaymentDO payment = salaryPaymentMapper.selectById(id);
        if (payment == null) {
            throw exception(SALARY_PAYMENT_NOT_EXISTS);
        }
        return payment;
    }
 
    @Override
    public PageResult<HrmSalaryPaymentDetailDO> getSalaryPaymentDetailPage(HrmSalaryPaymentDetailPageReqVO pageReqVO) {
        return salaryPaymentDetailMapper.selectPage(pageReqVO);
    }
 
    @Override
    public List<HrmSalaryPaymentDetailDO> getSalaryPaymentDetailListByPaymentId(Long paymentId) {
        return salaryPaymentDetailMapper.selectListByPaymentId(paymentId);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void revokeSalaryPaymentDetail(List<Long> detailIds) {
        if (CollUtil.isEmpty(detailIds)) {
            return;
        }
        Long paymentId = null;
        for (Long detailId : detailIds) {
            HrmSalaryPaymentDetailDO detail = salaryPaymentDetailMapper.selectById(detailId);
            if (detail == null) {
                throw exception(SALARY_PAYMENT_DETAIL_NOT_EXISTS);
            }
            // 校验状态必须是已发放(10)
            if (detail.getStatus() != 10) {
                throw exception(SALARY_PAYMENT_REVOKE_FAIL);
            }
            // 恢复明细状态为待发放
            detail.setStatus(0);
            detail.setPaymentTime(null);
            salaryPaymentDetailMapper.updateById(detail);
            paymentId = detail.getPaymentId();
 
            // 恢复薪酬核算状态为已确认(10)
            HrmSalaryCalculationDO calculation = salaryCalculationMapper.selectById(detail.getCalculationId());
            if (calculation != null) {
                calculation.setStatus(10);
                calculation.setPaymentTime(null);
                salaryCalculationMapper.updateById(calculation);
            }
        }
 
        // 更新发放台账状态为待发放(如果有明细被撤销)
        if (paymentId != null) {
            HrmSalaryPaymentDO payment = salaryPaymentMapper.selectById(paymentId);
            if (payment != null && payment.getStatus() == 10) {
                payment.setStatus(0);
                payment.setPaymentTime(null);
                salaryPaymentMapper.updateById(payment);
            }
        }
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSalaryPayment(Long id) {
        HrmSalaryPaymentDO payment = validateSalaryPaymentExists(id);
        // 仅待发放状态可删除
        if (payment.getStatus() != 0) {
            throw exception(SALARY_PAYMENT_DELETE_FAIL);
        }
        // 检查是否存在已发放明细
        List<HrmSalaryPaymentDetailDO> details = salaryPaymentDetailMapper.selectListByPaymentId(id);
        boolean hasPaidDetail = details.stream().anyMatch(d -> d.getStatus() == 10);
        if (hasPaidDetail) {
            throw exception(SALARY_PAYMENT_HAS_PAID_DETAIL);
        }
        // 删除所有明细
        for (HrmSalaryPaymentDetailDO detail : details) {
            salaryPaymentDetailMapper.deleteById(detail.getId());
        }
        // 删除发放台账
        salaryPaymentMapper.deleteById(id);
    }
 
}