package com.engine.kq.job;
import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson.JSON;
import com.engine.kq.biz.KQShiftManagementComInfo;
import com.engine.kq.biz.KQShiftRestTimeSectionComInfo;
import com.engine.kq.biz.KQWorkTime;
import com.engine.kq.entity.WorkTimeEntity;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
/**
* @author:dxfeng
* @createTime: 2024/03/29
* @version: 1.0
*/
public class UpdateEffectiveDuration {
/**
* 日期时间格式化
*/
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
/**
* 有效时长计算逻辑:
* 1、最早、最晚打卡时间,与开始时间、结束时间没有交集=》有效时长=共计时长
* 2、最早、最晚打卡时间,与开始时间、结束时间有交集=》
* (1)比较最早打卡时间与开始时间,如果最早打卡时间晚于开始时间,统计晚打卡的分钟数
* (2)比较最晚打卡时间与结束时间,如果最晚打卡时间早于结束时间,统计早打卡的分钟数
* (3)统计相差的分钟数,按半小时起算,有效时长=共计时长-相差时长
*
* @param mainId
* @param userId
* @param gzrq
* @param earlyStartTime
* @param afterEndTime
* @param startTime
* @param endTime
*/
public static void execute(String mainId, String userId, String gzrq, String earlyStartTime, String afterEndTime, String startTime, String endTime) {
RecordSet recordSet = new RecordSet();
try {
LocalDateTime startSignTime = LocalDateTime.parse(earlyStartTime, DATE_TIME_FORMATTER);
LocalDateTime endSignTime = LocalDateTime.parse(afterEndTime, DATE_TIME_FORMATTER);
LocalDateTime startWorkTime = LocalDateTime.parse(startTime, DATE_TIME_FORMATTER);
LocalDateTime endWorkTime = LocalDateTime.parse(endTime, DATE_TIME_FORMATTER);
// 最早、最晚打卡时间,与开始时间、结束时间,是否存在交集
boolean hasIntersection = startSignTime.isBefore(endWorkTime) && endSignTime.isAfter(startWorkTime);
recordSet.executeQuery("select gjsc from uf_jbtz where id = ?", mainId);
double gjscHours = 0.0;
if (recordSet.next()) {
gjscHours = recordSet.getDouble("gjsc");
}
if (gjscHours < 0) {
throw new Exception("mainId===" + mainId + ",共计时长小于0");
}
if (hasIntersection) {
int totalMinutes = 0;
if (startSignTime.isAfter(startWorkTime)) {
int startDifference = (int) (startWorkTime.until(startSignTime, ChronoUnit.MINUTES));
recordSet.execute("mainId=" + mainId + ",userId=" + userId + ",gzrq=" + gzrq + ",startDifference=" + startDifference);
totalMinutes += startDifference;
}
if (endSignTime.isBefore(endWorkTime)) {
int endDifference = (int) (endSignTime.until(endWorkTime, ChronoUnit.MINUTES));
recordSet.execute("mainId=" + mainId + ",userId=" + userId + ",gzrq=" + gzrq + ",endDifference=" + endDifference);
totalMinutes += endDifference;
}
// 总计相差时间,按照半小时,向上取整
double totalHours = (double) totalMinutes / 60;
double timeDifference = gjscHours - totalHours;
// 将小时数按照0.5小时的单位向下取整
double roundedHours = (int) Math.floor(timeDifference / 0.5) * 0.5;
recordSet.writeLog("userId==" + userId + ",totalMinutes===" + totalMinutes + ",gjscHours===" + gjscHours + ",totalHours==" + totalHours + ",roundedHours==" + roundedHours);
recordSet.executeUpdate("update uf_jbtz set yxsc=? where id=? ", roundedHours, mainId);
} else {
// 最早、最晚打卡时间,与开始时间、结束时间没有交集=》有效时长=共计时长
recordSet.executeUpdate("update uf_jbtz set yxsc=? where id=? ", gjscHours, mainId);
}
} catch (Exception e) {
recordSet.writeLog(e);
}
}
/**
* 更新加班台账表,有效时长字段
*
* 导入的加班数据增加根据班次的最早最晚打卡时间取交集,按照系统的加班单和打卡数据取交集处理
*
* @param mainId 当前数据ID
* @param userId 加班人ID
* @param gzrq 归属日志
* @param earlyStartTime 最早打卡时间
* @param afterEndTime 最晚打卡时间
*/
public static void execute1(String mainId, String userId, String gzrq, String earlyStartTime, String afterEndTime, String startTime, String endTime) {
RecordSet recordSet = new RecordSet();
try {
KQWorkTime kqWorkTime = new KQWorkTime();
WorkTimeEntity workTimeEntity = kqWorkTime.getWorkTime(userId, gzrq);
String serialId = workTimeEntity.getSerialId();
recordSet.writeLog("serialId===" + serialId);
if (StringUtils.isBlank(serialId) || "-1".equals(serialId)) {
kqWorkTime.setIsFormat(true);
workTimeEntity = kqWorkTime.getWorkTime(userId, gzrq);
serialId = workTimeEntity.getSerialId();
}
recordSet.writeLog("userId==" + userId + ",serialId===" + JSON.toJSONString(serialId) + ",workTimeEntity===" + JSON.toJSONString(workTimeEntity));
// 转换所有的工作时间
List