| | |
| | | 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) { |
| | | validateLocation(createReqVO); |
| | | // 校验规则名称是否重复(同一部门允许多条规则,不再限制部门唯一) |
| | | validateAttendanceRuleNameUnique(null, createReqVO.getName()); |
| | | validateEffectiveDateRange(createReqVO.getEffectiveStartDate(), createReqVO.getEffectiveEndDate()); |
| | |
| | | |
| | | @Override |
| | | public void updateAttendanceRule(HrmAttendanceRuleSaveReqVO updateReqVO) { |
| | | validateAttendanceRuleExists(updateReqVO.getId()); |
| | | HrmAttendanceRuleDO currentRule = attendanceRuleMapper.selectById(updateReqVO.getId()); |
| | | if (currentRule == null) { |
| | | throw exception(ATTENDANCE_RULE_NOT_EXISTS); |
| | | } |
| | | // 兼容旧管理端:没有提交地点字段时,不清空已配置的位置限制。 |
| | | if (updateReqVO.getLocationRequired() == null) { |
| | | updateReqVO.setLocationRequired(currentRule.getLocationRequired()); |
| | | if (updateReqVO.getLocationName() == null) updateReqVO.setLocationName(currentRule.getLocationName()); |
| | | if (updateReqVO.getLocationAddress() == null) updateReqVO.setLocationAddress(currentRule.getLocationAddress()); |
| | | if (updateReqVO.getLongitude() == null) updateReqVO.setLongitude(currentRule.getLongitude()); |
| | | if (updateReqVO.getLatitude() == null) updateReqVO.setLatitude(currentRule.getLatitude()); |
| | | if (updateReqVO.getRadiusMeters() == null) updateReqVO.setRadiusMeters(currentRule.getRadiusMeters()); |
| | | if (updateReqVO.getCoordinateSystem() == null) updateReqVO.setCoordinateSystem(currentRule.getCoordinateSystem()); |
| | | } |
| | | validateLocation(updateReqVO); |
| | | // 校验规则名称是否重复 |
| | | validateAttendanceRuleNameUnique(updateReqVO.getId(), updateReqVO.getName()); |
| | | validateEffectiveDateRange(updateReqVO.getEffectiveStartDate(), updateReqVO.getEffectiveEndDate()); |
| | |
| | | private void validateAttendanceRuleExists(Long id) { |
| | | if (attendanceRuleMapper.selectById(id) == null) { |
| | | throw exception(ATTENDANCE_RULE_NOT_EXISTS); |
| | | } |
| | | } |
| | | |
| | | private void validateLocation(HrmAttendanceRuleSaveReqVO reqVO) { |
| | | if (reqVO.getLocationRequired() == null) { |
| | | reqVO.setLocationRequired(false); |
| | | } |
| | | if (reqVO.getRadiusMeters() == null) { |
| | | reqVO.setRadiusMeters(10); |
| | | } |
| | | if (StrUtil.isBlank(reqVO.getCoordinateSystem())) { |
| | | reqVO.setCoordinateSystem("WGS84"); |
| | | } else { |
| | | reqVO.setCoordinateSystem(reqVO.getCoordinateSystem().trim().toUpperCase(java.util.Locale.ROOT)); |
| | | } |
| | | if (!"WGS84".equals(reqVO.getCoordinateSystem())) { |
| | | throw exception(ATTENDANCE_LOCATION_SYSTEM_INVALID); |
| | | } |
| | | if (reqVO.getLongitude() != null && (reqVO.getLongitude().compareTo(BigDecimal.valueOf(-180)) < 0 |
| | | || reqVO.getLongitude().compareTo(BigDecimal.valueOf(180)) > 0)) { |
| | | throw exception(ATTENDANCE_LOCATION_COORDINATE_INVALID); |
| | | } |
| | | if (reqVO.getLatitude() != null && (reqVO.getLatitude().compareTo(BigDecimal.valueOf(-90)) < 0 |
| | | || reqVO.getLatitude().compareTo(BigDecimal.valueOf(90)) > 0)) { |
| | | throw exception(ATTENDANCE_LOCATION_COORDINATE_INVALID); |
| | | } |
| | | if (reqVO.getRadiusMeters() < 10 || reqVO.getRadiusMeters() > 5000) { |
| | | throw exception(ATTENDANCE_LOCATION_RULE_INVALID); |
| | | } |
| | | if (Boolean.TRUE.equals(reqVO.getLocationRequired()) |
| | | && (StrUtil.isBlank(reqVO.getLocationName()) || reqVO.getLongitude() == null |
| | | || reqVO.getLatitude() == null)) { |
| | | throw exception(ATTENDANCE_LOCATION_RULE_INVALID); |
| | | } |
| | | } |
| | | |
| | |
| | | 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); |
| | | } |
| | | } |
| | | } |