5 小时以前 4d15fa8051884869c5b612d0641508874cc71811
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
package cn.iocoder.yudao.module.hrm.service.handover;
 
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.hrm.dal.dataobject.handover.HrmUserHandoverDO;
import cn.iocoder.yudao.module.hrm.dal.mysql.handover.HrmUserHandoverMapper;
import cn.iocoder.yudao.module.hrm.enums.HrmHandoverStatusEnum;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
 
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
 
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.hrm.enums.ErrorCodeConstants.*;
 
/**
 * 用户数据交接 Service 实现
 *
 * @author 芋道源码
 */
@Service
@Validated
@Slf4j
public class HrmUserHandoverServiceImpl implements HrmUserHandoverService {
 
    @Resource
    private HrmUserHandoverMapper userHandoverMapper;
    @Resource
    private AdminUserApi adminUserApi;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createHandover(Long fromUserId, Long toUserId, Long resignationId) {
        // 校验源用户存在
        AdminUserRespDTO fromUser = adminUserApi.getUser(fromUserId);
        if (fromUser == null) {
            throw exception(USER_HANDOVER_FROM_USER_NOT_EXISTS);
        }
        // 校验目标用户存在
        AdminUserRespDTO toUser = adminUserApi.getUser(toUserId);
        if (toUser == null) {
            throw exception(USER_HANDOVER_TO_USER_NOT_EXISTS);
        }
        // 检查是否已存在
        HrmUserHandoverDO existing = userHandoverMapper.selectByResignationId(resignationId);
        if (existing != null) {
            throw exception(USER_HANDOVER_ALREADY_EXISTS);
        }
        HrmUserHandoverDO record = HrmUserHandoverDO.builder()
                .fromUserId(fromUserId)
                .toUserId(toUserId)
                .resignationId(resignationId)
                .handoverStatus(HrmHandoverStatusEnum.COMPLETED.getStatus())
                .handoverTime(LocalDateTime.now())
                .build();
        userHandoverMapper.insert(record);
        log.info("[createHandover] 创建交接映射: fromUserId={}, toUserId={}, resignationId={}", fromUserId, toUserId, resignationId);
    }
 
    @Override
    public Set<Long> expandUserIds(Set<Long> userIds) {
        if (CollUtil.isEmpty(userIds)) {
            return Collections.emptySet();
        }
        List<HrmUserHandoverDO> handoverList = userHandoverMapper.selectListByToUserIds(userIds);
        if (CollUtil.isEmpty(handoverList)) {
            return new HashSet<>(userIds);
        }
        Set<Long> fromUserIds = handoverList.stream()
                .map(HrmUserHandoverDO::getFromUserId)
                .collect(Collectors.toSet());
        Set<Long> result = new HashSet<>(userIds);
        result.addAll(fromUserIds);
        return result;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHandover(Long id) {
        HrmUserHandoverDO record = userHandoverMapper.selectById(id);
        if (record == null) {
            throw exception(USER_HANDOVER_NOT_EXISTS);
        }
        userHandoverMapper.deleteById(id);
        log.info("[deleteHandover] 删除交接映射: id={}, fromUserId={}, toUserId={}", id, record.getFromUserId(), record.getToUserId());
    }
 
}