| | |
| | | package cn.iocoder.yudao.module.hrm.service.attendance; |
| | | |
| | | 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.pojo.PageResult; |
| | | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| | | import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; |
| | | import cn.iocoder.yudao.module.hrm.controller.admin.attendance.vo.HrmAttendanceRuleImportExcelVO; |
| | | import cn.iocoder.yudao.module.hrm.controller.admin.attendance.vo.HrmAttendanceRuleImportReqVO; |
| | | import cn.iocoder.yudao.module.hrm.controller.admin.attendance.vo.HrmAttendanceRuleImportRespVO; |
| | | import cn.iocoder.yudao.module.hrm.controller.admin.attendance.vo.HrmAttendanceRulePageReqVO; |
| | | import cn.iocoder.yudao.module.hrm.controller.admin.attendance.vo.HrmAttendanceRuleSaveReqVO; |
| | | import cn.iocoder.yudao.module.hrm.dal.dataobject.attendance.HrmAttendanceRuleDO; |
| | | import cn.iocoder.yudao.module.hrm.dal.mysql.attendance.HrmAttendanceRuleMapper; |
| | | import cn.iocoder.yudao.module.system.api.dept.DeptApi; |
| | | import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO; |
| | | import jakarta.annotation.Resource; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.validation.annotation.Validated; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.time.LocalDate; |
| | | import java.util.ArrayList; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.stream.Collectors; |
| | | |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList; |
| | | import static cn.iocoder.yudao.module.hrm.enums.ErrorCodeConstants.*; |
| | | |
| | | /** |
| | |
| | | |
| | | @Resource |
| | | private HrmAttendanceRuleMapper attendanceRuleMapper; |
| | | |
| | | @Resource |
| | | private DeptApi deptApi; |
| | | |
| | | @Override |
| | | public Long createAttendanceRule(HrmAttendanceRuleSaveReqVO createReqVO) { |
| | |
| | | return attendanceRuleMapper.selectList(wrapper); |
| | | } |
| | | |
| | | /** |
| | | * 导入考勤规则(移植自新高电力) |
| | | * |
| | | * 说明:导入按「规则名称」判重;已存在时需 updateSupport=true 才覆盖。 |
| | | * 部门按名称匹配(deptName -> deptId),部门为空表示全局规则。 |
| | | */ |
| | | @Override |
| | | public HrmAttendanceRuleImportRespVO importAttendanceRuleList( |
| | | List<HrmAttendanceRuleImportExcelVO> importList, HrmAttendanceRuleImportReqVO importReqVO) { |
| | | // 1. 校验非空 |
| | | importList = filterList(importList, item -> StrUtil.isNotBlank(item.getName())); |
| | | if (CollUtil.isEmpty(importList)) { |
| | | throw exception(ATTENDANCE_RULE_IMPORT_LIST_IS_EMPTY); |
| | | } |
| | | // 2. 预加载部门名称 -> 部门ID |
| | | Map<String, Long> deptMap = deptApi.getDeptList().stream() |
| | | .filter(dept -> StrUtil.isNotBlank(dept.getName())) |
| | | .collect(Collectors.toMap(DeptRespDTO::getName, DeptRespDTO::getId, (a, b) -> a)); |
| | | // 3. 逐条处理 |
| | | HrmAttendanceRuleImportRespVO respVO = HrmAttendanceRuleImportRespVO.builder() |
| | | .createNames(new ArrayList<>()) |
| | | .updateNames(new ArrayList<>()) |
| | | .failureNames(new LinkedHashMap<>()) |
| | | .build(); |
| | | importList.forEach(importRule -> { |
| | | try { |
| | | // 3.1 解析部门 |
| | | Long deptId = null; |
| | | if (StrUtil.isNotBlank(importRule.getDeptName())) { |
| | | deptId = deptMap.get(importRule.getDeptName()); |
| | | if (deptId == null) { |
| | | throw exception(ATTENDANCE_RULE_DEPT_NOT_EXISTS, importRule.getDeptName()); |
| | | } |
| | | } |
| | | // 3.2 判断是否存在(按名称唯一) |
| | | HrmAttendanceRuleDO existRule = attendanceRuleMapper.selectOne(HrmAttendanceRuleDO::getName, importRule.getName()); |
| | | if (existRule == null) { |
| | | // 3.2.1 新增 |
| | | HrmAttendanceRuleDO rule = BeanUtils.toBean(importRule, HrmAttendanceRuleDO.class).setDeptId(deptId); |
| | | applyImportDefaults(rule); |
| | | attendanceRuleMapper.insert(rule); |
| | | respVO.getCreateNames().add(importRule.getName()); |
| | | return; |
| | | } |
| | | // 3.2.2 已存在,判断是否允许更新 |
| | | if (!Boolean.TRUE.equals(importReqVO.getUpdateSupport())) { |
| | | respVO.getFailureNames().put(importRule.getName(), |
| | | StrUtil.format(ATTENDANCE_RULE_EXISTS.getMsg(), importRule.getName())); |
| | | return; |
| | | } |
| | | HrmAttendanceRuleDO updateRule = BeanUtils.toBean(importRule, HrmAttendanceRuleDO.class) |
| | | .setId(existRule.getId()).setDeptId(deptId); |
| | | applyImportDefaults(updateRule); |
| | | attendanceRuleMapper.updateById(updateRule); |
| | | respVO.getUpdateNames().add(importRule.getName()); |
| | | } catch (ServiceException ex) { |
| | | respVO.getFailureNames().put(importRule.getName(), ex.getMessage()); |
| | | } |
| | | }); |
| | | return respVO; |
| | | } |
| | | |
| | | /** |
| | | * 导入时的默认值补全(与页面新增保持一致的默认口径;不覆盖地点字段,避免导入清空已配置的地点) |
| | | */ |
| | | private void applyImportDefaults(HrmAttendanceRuleDO rule) { |
| | | if (rule.getLateAllowMinutes() == null) { |
| | | rule.setLateAllowMinutes(0); |
| | | } |
| | | if (rule.getEarlyAllowMinutes() == null) { |
| | | rule.setEarlyAllowMinutes(0); |
| | | } |
| | | if (rule.getOvertimeThreshold() == null) { |
| | | rule.setOvertimeThreshold(BigDecimal.ONE); |
| | | } |
| | | if (rule.getStatus() == null) { |
| | | rule.setStatus(0); |
| | | } |
| | | } |
| | | } |