From d8eddeba2456ad1c89df68979112ae98d82c7948 Mon Sep 17 00:00:00 2001
From: BAOLH <baolh@yhzhg.cn>
Date: Tue, 02 Dec 2025 08:21:25 +0000
Subject: [PATCH] 因外出及调休记录可跨天产生的修改

---
 eladmin-master/eladmin-system/src/main/java/me/zhengjie/modules/quartz/service/impl/WeComServiceImpl.java |   87 ++++++++++++++++++++++++++++---------------
 1 files changed, 57 insertions(+), 30 deletions(-)

diff --git a/eladmin-master/eladmin-system/src/main/java/me/zhengjie/modules/quartz/service/impl/WeComServiceImpl.java b/eladmin-master/eladmin-system/src/main/java/me/zhengjie/modules/quartz/service/impl/WeComServiceImpl.java
index 0674f90..89e0e1c 100644
--- a/eladmin-master/eladmin-system/src/main/java/me/zhengjie/modules/quartz/service/impl/WeComServiceImpl.java
+++ b/eladmin-master/eladmin-system/src/main/java/me/zhengjie/modules/quartz/service/impl/WeComServiceImpl.java
@@ -40,6 +40,7 @@
 import me.zhengjie.modules.treatment.domain.UserLevelWage;
 import me.zhengjie.modules.treatment.repository.UserInsuranceRepository;
 import me.zhengjie.modules.treatment.repository.UserLevelWageRepository;
+import me.zhengjie.utils.DateUtil;
 import me.zhengjie.utils.QueryHelp;
 import me.zhengjie.utils.StringUtils;
 import me.zhengjie.modules.weixin.cp.api.WxCpOaWeDocSmartsheetService;
@@ -519,7 +520,7 @@
             }
         }
         // 查询外出表
-        List<AttendanceGoOut> attendanceGoOutList = attendanceGoOutRepository.findByUserAndDate(user.getId(), attendance.getRosteringWorkStartTime().toLocalDateTime(), attendance.getRosteringWorkEndTime().toLocalDateTime());
+        List<AttendanceGoOut> attendanceGoOutList = attendanceGoOutRepository.findByUserAndDate(user.getId(), DateUtil.parseAttStartTime(attendance.getRosteringWorkStartTime().toLocalDateTime()), DateUtil.parseAttEndTime(attendance.getRosteringWorkEndTime().toLocalDateTime()));
         for (AttendanceGoOut attendanceGoOut : attendanceGoOutList) {
             // 上午/下午
             if (attendanceGoOut.getOutStartTime().toLocalDateTime().isBefore(attendance.getRosteringLunchStartTime().toLocalDateTime()) && attendanceGoOut.getOutEndTime().toLocalDateTime().compareTo(attendance.getRosteringLunchStartTime().toLocalDateTime()) <= 0) {
@@ -546,46 +547,77 @@
         // 查询考勤请假表
         List<AttendanceLeave> attendanceLeaveList = attendanceLeaveRepository.findByUserIdAndDate(user.getId(), startDate.toLocalDateTime(), endDate.toLocalDateTime());
         // 查询考勤出差表
-        List<AttendanceBusinessTravel> attendanceBusinessTravelList = attendanceBusinessTravelRepository.findByUserAndSEDate(user.getId(), attendance.getAttendanceDate().toLocalDateTime().withHour(0).withMinute(0).withSecond(0).withNano(0), attendance.getAttendanceDate().toLocalDateTime().withHour(23).withMinute(59).withSecond(59).withNano(999));
+        List<AttendanceBusinessTravel> attendanceBusinessTravelList = attendanceBusinessTravelRepository.findByUserAndSEDate(user.getId(), attendance.getAttendanceDate().toLocalDateTime().withHour(0).withMinute(0).withSecond(0).withNano(0), attendance.getAttendanceDate().toLocalDateTime().withHour(23).withMinute(59).withSecond(59).withNano(0));
         // 查询考勤外出表
-        List<AttendanceGoOut> attendanceGoOutList = attendanceGoOutRepository.findByUserAndDate(user.getId(), attendance.getRosteringWorkStartTime().toLocalDateTime(), attendance.getRosteringWorkEndTime().toLocalDateTime());
+        List<AttendanceGoOut> attendanceGoOutList = attendanceGoOutRepository.findByUserAndDate(user.getId(), DateUtil.parseAttStartTime(attendance.getAttendanceDate().toLocalDateTime()), DateUtil.parseAttEndTime(attendance.getAttendanceDate().toLocalDateTime()));
+
+        Double outStartDuration = new Double(0.00); // 上午时长
+        Double outEndDuration = new Double(0.00); // 下午时长
+
+        Double bussStartDuration = new Double(0.00); // 上午时长
+        Double bussEndDuration = new Double(0.00); // 下午时长
+
+        Double leaveStartDuration = new Double(0.00); // 上午时长
+        Double leaveEndDuration = new Double(0.00); // 下午时长
+
+        Map<Timestamp, String> startTimeMap = new HashMap<>();
+        Map<Timestamp, String> endTimeMap = new HashMap<>();
 
         // TODO:统计规则请假优先于出差优先于外出
         if (attendanceGoOutList.size() > 0) {
-            // 统计外出
-            Double outStartDuration = new Double(0.00); // 上午时长
-            Double outEndDuration = new Double(0.00); // 下午时长
             for (AttendanceGoOut attendanceGoOut : attendanceGoOutList) {
                 // 上午时长、下午时长
                 outStartDuration += getStartDuration(attendanceGoOut.getOutStartTime(), attendanceGoOut.getOutEndTime(), attendance);
                 outEndDuration += getEndDuration(attendanceGoOut.getOutStartTime(), attendanceGoOut.getOutEndTime(), attendance);
+
+                startTimeMap.put(attendanceGoOut.getOutStartTime(), Constant.ATTENDANCE_STATE_GOOUT);
+                endTimeMap.put(attendanceGoOut.getOutEndTime(), Constant.ATTENDANCE_STATE_GOOUT);
             }
-            // 调用方法修改考勤打卡状态
-            updateAttendanceRecord(null, attendance, Constant.ATTENDANCE_STATE_GOOUT, day, outStartDuration, outEndDuration);
         }
         if (attendanceBusinessTravelList.size() > 0) {
-            // 统计出差
-            Double bussStartDuration = new Double(0.00); // 上午时长
-            Double bussEndDuration = new Double(0.00); // 下午时长
             for (AttendanceBusinessTravel attendanceBusinessTravel : attendanceBusinessTravelList) {
                 // 上午时长、下午时长
                 bussStartDuration += getStartDuration(attendanceBusinessTravel.getTravelStartTime(), attendanceBusinessTravel.getTravelEndTime(), attendance);
                 bussEndDuration += getEndDuration(attendanceBusinessTravel.getTravelStartTime(), attendanceBusinessTravel.getTravelEndTime(), attendance);
+
+                startTimeMap.put(attendanceBusinessTravel.getTravelStartTime(), Constant.ATTENDANCE_STATE_BUSINESS);
+                endTimeMap.put(attendanceBusinessTravel.getTravelEndTime(), Constant.ATTENDANCE_STATE_BUSINESS);
             }
-            // 调用方法修改考勤打卡状态
-            updateAttendanceRecord(null, attendance, Constant.ATTENDANCE_STATE_BUSINESS, day, bussStartDuration, bussEndDuration);
         }
         if (attendanceLeaveList.size() > 0) {
-            // 统计请假
-            Double leaveStartDuration = new Double(0.00); // 上午时长
-            Double leaveEndDuration = new Double(0.00); // 下午时长
             for (AttendanceLeave attendanceLeave : attendanceLeaveList) {
                 // 上午总时长、下午总时长
                 leaveStartDuration += getStartDuration(attendanceLeave.getLeaveStartTime(), attendanceLeave.getLeaveEndTime(), attendance);
                 leaveEndDuration += getEndDuration(attendanceLeave.getLeaveStartTime(), attendanceLeave.getLeaveEndTime(), attendance);
+
+                startTimeMap.put(attendanceLeave.getLeaveStartTime(), Constant.ATTENDANCE_STATE_LEAVE);
+                endTimeMap.put(attendanceLeave.getLeaveEndTime(), Constant.ATTENDANCE_STATE_LEAVE);
             }
-            //调用方法修改考勤打卡状态
-            updateAttendanceRecord(null, attendance, Constant.ATTENDANCE_STATE_LEAVE, day, leaveStartDuration, leaveEndDuration);
+        }
+        // 选出最早的开始时间, 更新考勤记录
+        if (!startTimeMap.isEmpty()) {
+            Timestamp startTime = startTimeMap.keySet().stream().sorted(Comparator.comparing(Timestamp::toLocalDateTime)).findFirst().get();
+
+            if (startTimeMap.get(startTime).equals(Constant.ATTENDANCE_STATE_GOOUT)) {
+                updateAttendanceRecord(null, attendance, Constant.ATTENDANCE_STATE_GOOUT, day, outStartDuration + bussStartDuration + leaveStartDuration, 0.00);
+            } else if (startTimeMap.get(startTime).equals(Constant.ATTENDANCE_STATE_BUSINESS)) {
+                updateAttendanceRecord(null, attendance, Constant.ATTENDANCE_STATE_BUSINESS, day, outStartDuration + bussStartDuration + leaveStartDuration, 0.00);
+            } else if (startTimeMap.get(startTime).equals(Constant.ATTENDANCE_STATE_LEAVE)) {
+                updateAttendanceRecord(null, attendance, Constant.ATTENDANCE_STATE_LEAVE, day, outStartDuration + bussStartDuration + leaveStartDuration, 0.00);
+            }
+        }
+
+        // 选出最晚的结束时间, 更新考勤记录
+        if (!endTimeMap.isEmpty()) {
+            Timestamp endTime = endTimeMap.keySet().stream().sorted(Comparator.comparing(Timestamp::toLocalDateTime).reversed()).findFirst().get();
+
+            if (endTimeMap.get(endTime).equals(Constant.ATTENDANCE_STATE_GOOUT)) {
+                updateAttendanceRecord(null, attendance, Constant.ATTENDANCE_STATE_GOOUT, day, 0.00, outEndDuration + bussEndDuration + leaveEndDuration);
+            } else if (endTimeMap.get(endTime).equals(Constant.ATTENDANCE_STATE_BUSINESS)) {
+                updateAttendanceRecord(null, attendance, Constant.ATTENDANCE_STATE_BUSINESS, day, 0.00, outEndDuration + bussEndDuration + leaveEndDuration);
+            } else if (endTimeMap.get(endTime).equals(Constant.ATTENDANCE_STATE_LEAVE)) {
+                updateAttendanceRecord(null, attendance, Constant.ATTENDANCE_STATE_LEAVE, day, 0.00, outEndDuration + bussEndDuration + leaveEndDuration);
+            }
         }
 
         // TODO:20250813修改 增加打卡时间判断,打卡时间需要闭环
@@ -1003,7 +1035,7 @@
     public Integer calculateTimes(LocalDateTime start, LocalDateTime end) {
         Integer time = 0;
         Duration duration = null;
-        duration = Duration.between(start, start.toLocalDate().atTime(end.toLocalTime()));
+        duration = Duration.between(start, end);
         Long StartSeconds = duration.getSeconds();
         time += StartSeconds.intValue();
         return time;
@@ -1167,10 +1199,8 @@
             Map<String, String> robotParamMap = dictDetailRepository.findByDictName(Constant.WECOM_ROBOT_PARAM).stream().collect(Collectors.toMap(DictDetail::getLabel, DictDetail::getValue));
             if (robotParamMap.containsKey(Constant.REMIND_REPORT_WEBHOOK_KEY) && robotParamMap.containsKey(Constant.REMIND_REPORT_MSG)) {
                 // 发送消息
-                WxCpDefaultConfigImpl wxCpDefaultConfig = new WxCpDefaultConfigImpl();
-                wxCpDefaultConfig.setWebhookKey(robotParamMap.get(Constant.REMIND_REPORT_WEBHOOK_KEY));
-                wxCpService.setWxCpConfigStorage(wxCpDefaultConfig);
-                wxCpService.getGroupRobotService().sendText(robotParamMap.get(Constant.REMIND_REPORT_MSG), Arrays.asList("@all"), Arrays.asList());
+                String webhookUrl = wxCpService.getWxCpConfigStorage().getApiUrl("/cgi-bin/webhook/send?key=") + robotParamMap.get(Constant.REMIND_REPORT_WEBHOOK_KEY);
+                wxCpService.getGroupRobotService().sendText(webhookUrl, robotParamMap.get(Constant.REMIND_REPORT_MSG), Arrays.asList("@all"), Arrays.asList());
             }
         } catch (WxErrorException wxErrorException) {
             wxErrorException.printStackTrace();
@@ -1223,10 +1253,8 @@
             Map<String, String> robotParamMap = dictDetailRepository.findByDictName(Constant.WECOM_ROBOT_PARAM).stream().collect(Collectors.toMap(DictDetail::getLabel, DictDetail::getValue));
             if (robotParamMap.containsKey(Constant.REMIND_REPORT_WEBHOOK_KEY) && robotParamMap.containsKey(Constant.REMIND_REPORT_SUCCESS_MSG)) {
                 // 发送周报生成成功消息
-                WxCpDefaultConfigImpl wxCpDefaultConfig = new WxCpDefaultConfigImpl();
-                wxCpDefaultConfig.setWebhookKey(robotParamMap.get(Constant.REMIND_REPORT_WEBHOOK_KEY));
-                wxCpService.setWxCpConfigStorage(wxCpDefaultConfig);
-                wxCpService.getGroupRobotService().sendText(robotParamMap.get(Constant.REMIND_REPORT_SUCCESS_MSG), Arrays.asList("@all"), Arrays.asList());
+                String webhookUrl = wxCpService.getWxCpConfigStorage().getApiUrl("/cgi-bin/webhook/send?key=") + robotParamMap.get(Constant.REMIND_REPORT_WEBHOOK_KEY);
+                wxCpService.getGroupRobotService().sendText(webhookUrl, robotParamMap.get(Constant.REMIND_REPORT_SUCCESS_MSG), Arrays.asList("@all"), Arrays.asList());
             }
         } catch (WxErrorException wxErrorException) {
             wxErrorException.printStackTrace();
@@ -1381,15 +1409,12 @@
                         sort.setFieldTitle("提交日期");
                         sort.setDesc(true);
                         recordQueryRequest.setSort(Arrays.asList(sort));
-                        // 待删除记录
-                        List<Record> recordDeleteList = new ArrayList<>();
                         WxCpRecordInfo recordInfo = wxCpOaWeDocSmartsheetService.recordInfo(recordQueryRequest);
 
                         // 制作项目周报数据
                         Map<String, List<Record>> filterRecordMap = daliyMongoDtoList.stream().map(item -> {
                             Record record = recordInfo.getRecords().stream().filter(r -> {
                                 if (ObjectUtils.isEmpty(r.getValues().get("项目名称")) || StringUtils.isBlank(r.getValues().get("项目名称").toString())) {
-                                    recordDeleteList.add(r);
                                     return false;
                                 }
                                 LocalDate createDate = Instant.ofEpochMilli(Long.parseLong(r.getValues().get("提交日期").toString())).atZone(ZoneId.systemDefault()).toLocalDate();
@@ -1436,6 +1461,8 @@
                             record.setValues(values);
                             return record;
                         }).collect(Collectors.groupingBy(record -> StringUtils.isNotBlank(record.getRecordId()) ? "EDIT" : "CREATE"));
+                        // 待删除记录
+                        List<Record> recordDeleteList = recordInfo.getRecords().stream().filter(r -> ObjectUtils.isEmpty(r.getValues().get("项目名称")) || StringUtils.isBlank(r.getValues().get("项目名称").toString())).collect(Collectors.toList());
 
                         // 创建新纪录
                         List<Record> recordCreateList = filterRecordMap.getOrDefault("CREATE", Collections.emptyList());

--
Gitblit v1.9.3