From d54fbc9d4f8981fa0f7f613b33a28a8cd4f764ea Mon Sep 17 00:00:00 2001
From: 云 <2163098428@qq.com>
Date: 星期日, 25 十月 2026 16:29:53 +0800
Subject: [PATCH] feat(system): 优化用户搜索功能并完善ERP和MES模块验证逻辑

---
 yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/service/attendance/HrmAttendanceRuleServiceImpl.java |  165 +++++++++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 155 insertions(+), 10 deletions(-)

diff --git a/yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/service/attendance/HrmAttendanceRuleServiceImpl.java b/yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/service/attendance/HrmAttendanceRuleServiceImpl.java
index 4fdf7ec..00a15e4 100644
--- a/yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/service/attendance/HrmAttendanceRuleServiceImpl.java
+++ b/yudao-module-hrm/src/main/java/cn/iocoder/yudao/module/hrm/service/attendance/HrmAttendanceRuleServiceImpl.java
@@ -1,25 +1,40 @@
 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.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.util.ArrayList;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
 import java.util.List;
+import java.util.Map;
+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.framework.common.util.collection.CollectionUtils.filterList;
 import static cn.iocoder.yudao.module.hrm.enums.ErrorCodeConstants.*;
 
 /**
  * 鑰冨嫟瑙勫垯閰嶇疆 Service 瀹炵幇绫�
  *
- * @author 鑺嬮亾婧愮爜
+ * @author 瓒呯骇绠$悊鍛�
  */
 @Service
 @Validated
@@ -27,9 +42,12 @@
 
     @Resource
     private HrmAttendanceRuleMapper attendanceRuleMapper;
+    @Resource
+    private DeptApi deptApi;
 
     @Override
     public Long createAttendanceRule(HrmAttendanceRuleSaveReqVO createReqVO) {
+        validateLocation(createReqVO);
         // 鏍¢獙瑙勫垯鍚嶇О鏄惁閲嶅
         validateAttendanceRuleNameUnique(null, createReqVO.getName());
         // 鏍¢獙閮ㄩ棬鏄惁宸查厤缃鍒欙紙濡傛灉鎸囧畾浜嗛儴闂級
@@ -56,7 +74,21 @@
 
     @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());
         // 鏍¢獙閮ㄩ棬鏄惁宸查厤缃鍒欙紙濡傛灉鎸囧畾浜嗛儴闂級
@@ -76,6 +108,39 @@
     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(200);
+        }
+        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 || StrUtil.isBlank(reqVO.getCoordinateSystem()))) {
+            throw exception(ATTENDANCE_LOCATION_RULE_INVALID);
         }
     }
 
@@ -121,21 +186,101 @@
 
     @Override
     public HrmAttendanceRuleDO getAttendanceRuleByDeptId(Long deptId) {
-        // 1. 鍏堟煡鎵鹃儴闂ㄤ笓灞炶鍒�
-        List<HrmAttendanceRuleDO> deptRules = attendanceRuleMapper.selectList(new cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX<HrmAttendanceRuleDO>()
-                .eq(HrmAttendanceRuleDO::getDeptId, deptId)
-                .eq(HrmAttendanceRuleDO::getStatus, 0));
-        if (!deptRules.isEmpty()) {
-            return deptRules.get(0);
+        // 1. 浠庡憳宸ユ墍鍦ㄩ儴闂ㄥ紑濮嬮�愮骇鍚戜笂鏌ユ壘锛氶厤缃湪涓婄骇閮ㄩ棬鐨勮鍒欏鍏跺瓙閮ㄩ棬鐢熸晥
+        Set<Long> visitedDeptIds = new HashSet<>();
+        Long currentDeptId = deptId;
+        while (currentDeptId != null && currentDeptId != 0 && visitedDeptIds.add(currentDeptId)) {
+            List<HrmAttendanceRuleDO> deptRules = attendanceRuleMapper.selectEnabledListByDeptId(currentDeptId);
+            if (!deptRules.isEmpty()) {
+                // 鍚屼竴閮ㄩ棬锛堝銆屾�诲叕鍙搞�嶏級鍙兘閰嶇疆浜嗗鏉″惎鐢ㄨ鍒欙紝浠ユ渶鏂板垱寤虹殑涓哄綋鍓嶇敓鏁堣鍒�
+                return deptRules.get(0);
+            }
+            DeptRespDTO dept = deptApi.getDept(currentDeptId);
+            if (dept == null) {
+                break;
+            }
+            currentDeptId = dept.getParentId();
         }
         // 2. 鏌ユ壘鍏ㄥ眬瑙勫垯锛坉eptId 涓� null锛�
         List<HrmAttendanceRuleDO> globalRules = attendanceRuleMapper.selectList(new cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX<HrmAttendanceRuleDO>()
                 .isNull(HrmAttendanceRuleDO::getDeptId)
-                .eq(HrmAttendanceRuleDO::getStatus, 0));
+                .eq(HrmAttendanceRuleDO::getStatus, 0)
+                .orderByDesc(HrmAttendanceRuleDO::getId));
         if (!globalRules.isEmpty()) {
             return globalRules.get(0);
         }
         return null;
     }
 
-}
\ No newline at end of file
+    @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 瑙f瀽閮ㄩ棬
+                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);
+                    applyDefaults(rule);
+                    attendanceRuleMapper.insert(rule);
+                    respVO.getCreateNames().add(importRule.getName());
+                    return;
+                }
+                // 3.2.2 宸插瓨鍦紝鍒ゆ柇鏄惁鍏佽鏇存柊
+                if (!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);
+                applyDefaults(updateRule);
+                attendanceRuleMapper.updateById(updateRule);
+                respVO.getUpdateNames().add(importRule.getName());
+            } catch (ServiceException ex) {
+                respVO.getFailureNames().put(importRule.getName(), ex.getMessage());
+            }
+        });
+        return respVO;
+    }
+
+    private void applyDefaults(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);
+        }
+    }
+
+}

--
Gitblit v1.9.3