| | |
| | | package cn.iocoder.yudao.module.hrm.service.salary; |
| | | |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.iocoder.yudao.framework.common.exception.ServiceException; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.module.hrm.controller.admin.salary.vo.HrmLeaveTypeConfigImportExcelVO; |
| | | import cn.iocoder.yudao.module.hrm.controller.admin.salary.vo.HrmLeaveTypeConfigImportReqVO; |
| | | import cn.iocoder.yudao.module.hrm.controller.admin.salary.vo.HrmLeaveTypeConfigImportRespVO; |
| | | import cn.iocoder.yudao.module.hrm.dal.dataobject.salary.HrmLeaveTypeConfigDO; |
| | | import cn.iocoder.yudao.module.hrm.dal.mysql.salary.HrmLeaveTypeConfigMapper; |
| | | import jakarta.annotation.Resource; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.validation.annotation.Validated; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.module.hrm.enums.ErrorCodeConstants.LEAVE_TYPE_CONFIG_NOT_EXISTS; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList; |
| | | import static cn.iocoder.yudao.module.hrm.enums.ErrorCodeConstants.*; |
| | | |
| | | /** |
| | | * 请假类型扣款配置 Service 实现类 |
| | | * |
| | | * @author 芋道源码 |
| | | * @author 超级管理员 |
| | | */ |
| | | @Service |
| | | @Validated |
| | | public class HrmLeaveTypeConfigServiceImpl implements HrmLeaveTypeConfigService { |
| | | |
| | | /** |
| | | * 请假类型 -> 默认名称 |
| | | */ |
| | | private static final Map<Integer, String> LEAVE_TYPE_NAME_MAP = Map.of( |
| | | 1, "事假", |
| | | 2, "病假", |
| | | 3, "年假", |
| | | 4, "婚假", |
| | | 5, "产假", |
| | | 6, "丧假" |
| | | ); |
| | | |
| | | @Resource |
| | | private HrmLeaveTypeConfigMapper leaveTypeConfigMapper; |
| | |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public HrmLeaveTypeConfigImportRespVO importLeaveTypeConfigList( |
| | | List<HrmLeaveTypeConfigImportExcelVO> importList, HrmLeaveTypeConfigImportReqVO importReqVO) { |
| | | // 1. 校验非空 |
| | | importList = filterList(importList, item -> item.getLeaveType() != null); |
| | | if (CollUtil.isEmpty(importList)) { |
| | | throw exception(LEAVE_TYPE_CONFIG_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | // 2. 逐条处理 |
| | | HrmLeaveTypeConfigImportRespVO respVO = HrmLeaveTypeConfigImportRespVO.builder() |
| | | .createNames(new ArrayList<>()) |
| | | .updateNames(new ArrayList<>()) |
| | | .failureNames(new LinkedHashMap<>()) |
| | | .build(); |
| | | importList.forEach(importConfig -> { |
| | | try { |
| | | // 2.1 名称兜底(从请假类型推导) |
| | | String configName = StrUtil.isNotBlank(importConfig.getName()) |
| | | ? importConfig.getName() : LEAVE_TYPE_NAME_MAP.get(importConfig.getLeaveType()); |
| | | if (StrUtil.isBlank(configName)) { |
| | | respVO.getFailureNames().put(String.valueOf(importConfig.getLeaveType()), "请假类型不合法"); |
| | | return; |
| | | } |
| | | // 2.2 判断是否存在(按请假类型唯一) |
| | | HrmLeaveTypeConfigDO existConfig = leaveTypeConfigMapper.selectByLeaveType(importConfig.getLeaveType()); |
| | | if (existConfig == null) { |
| | | // 2.2.1 新增 |
| | | HrmLeaveTypeConfigDO config = BeanUtils.toBean(importConfig, HrmLeaveTypeConfigDO.class) |
| | | .setName(configName); |
| | | if (config.getStatus() == null) { |
| | | config.setStatus(0); |
| | | } |
| | | leaveTypeConfigMapper.insert(config); |
| | | respVO.getCreateNames().add(configName); |
| | | return; |
| | | } |
| | | // 2.2.2 已存在,判断是否允许更新 |
| | | if (!importReqVO.getUpdateSupport()) { |
| | | respVO.getFailureNames().put(configName, |
| | | StrUtil.format(LEAVE_TYPE_CONFIG_EXISTS.getMsg(), configName)); |
| | | return; |
| | | } |
| | | HrmLeaveTypeConfigDO updateConfig = BeanUtils.toBean(importConfig, HrmLeaveTypeConfigDO.class) |
| | | .setId(existConfig.getId()).setName(configName); |
| | | if (updateConfig.getStatus() == null) { |
| | | updateConfig.setStatus(0); |
| | | } |
| | | leaveTypeConfigMapper.updateById(updateConfig); |
| | | respVO.getUpdateNames().add(configName); |
| | | } catch (ServiceException ex) { |
| | | respVO.getFailureNames().put(String.valueOf(importConfig.getLeaveType()), ex.getMessage()); |
| | | } |
| | | }); |
| | | return respVO; |
| | | } |
| | | |
| | | } |