gaoluyang
8 小时以前 ef3cbc1c6882786e8218b08be3e52ea44587f03b
yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/service/approval/HrmLeaveApplicationServiceImpl.java
@@ -2,6 +2,8 @@
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
@@ -10,11 +12,15 @@
import cn.iocoder.yudao.module.bpm.dal.dataobject.definition.BpmProcessDefinitionInfoDO;
import cn.iocoder.yudao.module.bpm.service.definition.BpmCategoryService;
import cn.iocoder.yudao.module.bpm.service.definition.BpmProcessDefinitionService;
import cn.iocoder.yudao.module.hrm.controller.admin.approval.vo.HrmLeaveApplicationImportExcelVO;
import cn.iocoder.yudao.module.hrm.controller.admin.approval.vo.HrmLeaveApplicationImportRespVO;
import cn.iocoder.yudao.module.hrm.controller.admin.approval.vo.HrmLeaveApplicationPageReqVO;
import cn.iocoder.yudao.module.hrm.controller.admin.approval.vo.HrmLeaveApplicationRespVO;
import cn.iocoder.yudao.module.hrm.controller.admin.approval.vo.HrmLeaveApplicationSaveReqVO;
import cn.iocoder.yudao.module.hrm.dal.dataobject.approval.HrmLeaveApplicationDO;
import cn.iocoder.yudao.module.hrm.dal.dataobject.employee.HrmEmployeeDO;
import cn.iocoder.yudao.module.hrm.dal.mysql.approval.HrmLeaveApplicationMapper;
import cn.iocoder.yudao.module.hrm.dal.mysql.employee.HrmEmployeeMapper;
import cn.iocoder.yudao.module.hrm.dal.redis.HrmNoRedisDAO;
import cn.iocoder.yudao.module.hrm.enums.HrmAuditStatusEnum;
import cn.iocoder.yudao.module.hrm.enums.HrmLeaveTypeEnum;
@@ -31,8 +37,10 @@
import org.springframework.validation.annotation.Validated;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@@ -41,6 +49,7 @@
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList;
import static cn.iocoder.yudao.module.hrm.enums.ErrorCodeConstants.*;
/**
@@ -69,6 +78,8 @@
    private StorageAttachmentApi storageAttachmentApi;
    @Resource
    private BpmProcessDefinitionService bpmProcessDefinitionService;
    @Resource
    private HrmEmployeeMapper employeeMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
@@ -332,4 +343,89 @@
        return leaveApplicationMapper.selectByProcessInstanceId(processInstanceId);
    }
    @Override
    public HrmLeaveApplicationImportRespVO importLeaveApplicationList(List<HrmLeaveApplicationImportExcelVO> importList) {
        // 1. 过滤空行(手机号为空的 Excel 行视为空行)
        importList = filterList(importList, item -> StrUtil.isNotBlank(item.getPhone()));
        if (CollUtil.isEmpty(importList)) {
            throw exception(LEAVE_APPLICATION_IMPORT_LIST_IS_EMPTY);
        }
        // 2. 逐行导入,单行失败不影响其他行
        HrmLeaveApplicationImportRespVO respVO = HrmLeaveApplicationImportRespVO.builder()
                .createNames(new ArrayList<>())
                .failureNames(new LinkedHashMap<>())
                .build();
        for (int i = 0; i < importList.size(); i++) {
            HrmLeaveApplicationImportExcelVO row = importList.get(i);
            // Excel 第 1 行是表头,数据从第 2 行开始
            String rowKey = "第" + (i + 2) + "行";
            try {
                HrmLeaveApplicationSaveReqVO saveReqVO = buildImportSaveReqVO(row);
                createLeaveApplication(saveReqVO);
                respVO.getCreateNames().add(row.getPhone() + " " + row.getStartTime().toLocalDate());
            } catch (ServiceException ex) {
                respVO.getFailureNames().put(rowKey, ex.getMessage());
            } catch (Exception ex) {
                respVO.getFailureNames().put(rowKey, "导入失败:" + ex.getMessage());
            }
        }
        return respVO;
    }
    private HrmLeaveApplicationSaveReqVO buildImportSaveReqVO(HrmLeaveApplicationImportExcelVO row) {
        String phone = row.getPhone().trim();
        HrmEmployeeDO employee = employeeMapper.selectByPhone(phone);
        if (employee == null) {
            throw exception(LEAVE_APPLICATION_IMPORT_EMPLOYEE_NOT_EXISTS, phone);
        }
        if (employee.getUserId() == null) {
            throw exception(LEAVE_APPLICATION_IMPORT_EMPLOYEE_NO_USER, employee.getName());
        }
        // 请假类型:按名称匹配(兼容直接填字典值),匹配不到时列出可填值
        List<String> leaveTypeNames = Arrays.stream(HrmLeaveTypeEnum.values())
                .map(HrmLeaveTypeEnum::getName).toList();
        String leaveTypeText = StrUtil.trim(row.getLeaveType());
        if (StrUtil.isBlank(leaveTypeText)) {
            throw exception(LEAVE_APPLICATION_IMPORT_FIELD_REQUIRED,
                    "请假类型(可选:" + String.join("、", leaveTypeNames) + ")");
        }
        Integer leaveType = Arrays.stream(HrmLeaveTypeEnum.values())
                .filter(item -> item.getName().equals(leaveTypeText)
                        || String.valueOf(item.getType()).equals(leaveTypeText))
                .map(HrmLeaveTypeEnum::getType)
                .findFirst().orElse(null);
        if (leaveType == null) {
            throw exception(LEAVE_APPLICATION_IMPORT_FIELD_INVALID,
                    "请假类型", leaveTypeText, String.join("、", leaveTypeNames));
        }
        if (row.getStartTime() == null) {
            throw exception(LEAVE_APPLICATION_IMPORT_FIELD_REQUIRED, "开始时间");
        }
        if (row.getEndTime() == null) {
            throw exception(LEAVE_APPLICATION_IMPORT_FIELD_REQUIRED, "结束时间");
        }
        if (!row.getStartTime().isBefore(row.getEndTime())) {
            throw exception(LEAVE_APPLICATION_IMPORT_TIME_INVALID,
                    row.getStartTime(), row.getEndTime());
        }
        if (row.getDuration() == null || row.getDuration().signum() <= 0) {
            throw exception(LEAVE_APPLICATION_IMPORT_FIELD_REQUIRED, "请假时长(天,必须大于 0)");
        }
        if (StrUtil.isBlank(row.getReason())) {
            throw exception(LEAVE_APPLICATION_IMPORT_FIELD_REQUIRED, "请假原因");
        }
        HrmLeaveApplicationSaveReqVO saveReqVO = new HrmLeaveApplicationSaveReqVO();
        saveReqVO.setUserId(employee.getUserId());
        saveReqVO.setDeptId(employee.getDeptId());
        saveReqVO.setLeaveType(leaveType);
        saveReqVO.setStartTime(row.getStartTime());
        saveReqVO.setEndTime(row.getEndTime());
        saveReqVO.setDuration(row.getDuration());
        saveReqVO.setReason(row.getReason().trim());
        saveReqVO.setRemark(row.getRemark());
        return saveReqVO;
    }
}