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());
|
}
|
|
}
|