有效时长计算

dev_dxf
dxfeng 1 year ago
parent 7bf24fcaec
commit 30a401dc01

@ -1,11 +1,22 @@
package com.engine.kq.job;
import com.alibaba.fastjson.JSON;
import com.engine.kq.biz.KQShiftOnOffWorkSectionComInfo;
import com.engine.kq.biz.KQShiftRestTimeSectionComInfo;
import com.engine.kq.biz.KQWorkTime;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.interfaces.schedule.BaseCronJob;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.*;
/**
@ -16,6 +27,11 @@ import java.util.*;
* @Description
*/
public class SynClockInTimeJob extends BaseCronJob {
/**
*
*/
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
@Override
public void execute() {
@ -49,6 +65,11 @@ public class SynClockInTimeJob extends BaseCronJob {
boolean bool = rs1.executeUpdate("update uf_jbtz set zzdksj=?,zwdkrq=? where id=?",earlystarttime,afterendtime,mainid);
if(bool){
rs.writeLog(userid+ " ------------ "+ kssj +" ------------ "+ jsrq +" ---------- "+ bool);
// 计算有效时长
rs.writeLog("计算有效时长开始");
String gzrq = Util.null2String(rs.getString("gzrq"));
updateEffectiveDuration(mainid, userid, gzrq, earlystarttime, afterendtime);
rs.writeLog("计算有效时长结束");
}
}
}catch(Exception e){
@ -58,6 +79,172 @@ public class SynClockInTimeJob extends BaseCronJob {
rs.writeLog("---------- end to syn overtime work card time ----------");
}
/**
*
* <p/>
* <p></p>
*
* @param mainId ID
* @param userId ID
* @param gzrq
* @param earlyStartTime
* @param afterEndTime
*/
private void updateEffectiveDuration(String mainId, String userId, String gzrq, String earlyStartTime, String afterEndTime) {
RecordSet recordSet = new RecordSet();
try {
KQWorkTime kqWorkTime = new KQWorkTime();
Map<String, Object> serialInfo = kqWorkTime.getSerialInfo(userId, gzrq, false, true);
recordSet.writeLog("userId==" + userId + ",serialInfo===" + JSON.toJSONString(serialInfo));
String serialId = Util.null2String(serialInfo.get(gzrq));
List<Object> workSectionTimes = new KQShiftOnOffWorkSectionComInfo().getWorkSectionTimes(serialId);
recordSet.writeLog("userId==" + userId + ",workSectionTimes===" + JSON.toJSONString(workSectionTimes));
// 转换所有的工作时间
List<Map<String, String>> workTimeList = new ArrayList<>();
if (CollectionUtils.isNotEmpty(workSectionTimes)) {
convertDateTime(gzrq, workSectionTimes, workTimeList);
recordSet.writeLog("userId==" + userId + ",workTimeList===" + JSON.toJSONString(workTimeList));
}
// 转换所有的休息时间
List<Object> restSectionTimes = new KQShiftRestTimeSectionComInfo().getRestSectionTimes(serialId);
recordSet.writeLog("userId==" + userId + ",restSectionTimes===" + JSON.toJSONString(restSectionTimes));
List<Map<String, String>> restTimeList = new ArrayList<>();
if (CollectionUtils.isNotEmpty(restSectionTimes)) {
convertDateTime(gzrq, restSectionTimes, restTimeList);
recordSet.writeLog("userId==" + userId + ",restTimeList===" + JSON.toJSONString(restTimeList));
}
// 所有的有效时间区间
List<String> effectiveTime = getEffectiveTime(earlyStartTime, afterEndTime, workTimeList, restTimeList);
recordSet.writeLog("userId==" + userId + ",effectiveTime===" + JSON.toJSONString(effectiveTime));
// 有效区间总计分钟数
long totalMinutes = effectiveTime.stream()
.mapToInt(interval -> {
String[] parts = interval.split(",");
LocalDateTime start = LocalDateTime.parse(parts[0], DATE_TIME_FORMATTER);
LocalDateTime end = LocalDateTime.parse(parts[1], DATE_TIME_FORMATTER);
return (int) (start.until(end, ChronoUnit.MINUTES));
})
.sum();
double totalHours = (double) totalMinutes / 60;
// 将小时数按照0.5小时的单位向下取整
double roundedHours = (int) Math.floor(totalHours / 0.5) * 0.5;
recordSet.writeLog("userId==" + userId + ",totalMinutes===" + totalMinutes + ",totalHours==" + totalHours + ",roundedHours==" + roundedHours);
// 更新当前数据的有效时长
recordSet.executeUpdate("update uf_jbtz set yxsc=? where id=? ", roundedHours, mainId);
} catch (Exception e) {
recordSet.writeLog(e);
}
}
/**
*
*
* @param earlyStartTime
* @param afterEndTime
* @param workTimeList
* @param restTimeList
* @return
*/
private List<String> getEffectiveTime(String earlyStartTime, String afterEndTime, List<Map<String, String>> workTimeList, List<Map<String, String>> restTimeList) {
// 打卡时间区间,去除所有的休息时间
List<String> effectiveTimeList = new ArrayList<>();
LocalDateTime startSignTime = LocalDateTime.parse(earlyStartTime, DATE_TIME_FORMATTER);
LocalDateTime endSignTime = LocalDateTime.parse(afterEndTime, DATE_TIME_FORMATTER);
for (Map<String, String> workTimeMap : workTimeList) {
LocalDateTime startWorkTime = LocalDateTime.parse(workTimeMap.get("startTimes"), DATE_TIME_FORMATTER);
LocalDateTime endWorkTime = LocalDateTime.parse(workTimeMap.get("endTimes"), DATE_TIME_FORMATTER);
// 计算交集
LocalDateTime intersectionStart = startSignTime.isAfter(startWorkTime) ? startSignTime : startWorkTime;
LocalDateTime intersectionEnd = endSignTime.isBefore(endWorkTime) ? endSignTime : endWorkTime;
if (intersectionStart.isBefore(intersectionEnd)) {
// 去除休息时间
for (Map<String, String> restTimeMap : restTimeList) {
LocalDateTime startRestTime = LocalDateTime.parse(restTimeMap.get("startTimes"), DATE_TIME_FORMATTER);
LocalDateTime endRestTime = LocalDateTime.parse(restTimeMap.get("endTimes"), DATE_TIME_FORMATTER);
if (!intersectionEnd.isBefore(startRestTime) && !intersectionStart.isAfter(endRestTime)) {
if (intersectionStart.isBefore(startRestTime)) {
// 交集在休息时间之前
intersectionEnd = intersectionEnd.isBefore(startRestTime) ? intersectionEnd : startRestTime;
} else if (intersectionEnd.isAfter(endRestTime)) {
// 交集在休息时间之后
intersectionStart = intersectionStart.isAfter(endRestTime) ? intersectionStart : endRestTime;
} else {
// 休息时间在交集中间
intersectionStart = startRestTime;
intersectionEnd = endRestTime;
}
}
}
effectiveTimeList.add(intersectionStart.format(DATE_TIME_FORMATTER) + "," + intersectionEnd.format(DATE_TIME_FORMATTER));
}
}
return effectiveTimeList;
}
/**
*
*
* @param gzrq
* @param sectionTimes
* @param timeList
*/
private void convertDateTime(String gzrq, List<Object> sectionTimes, List<Map<String, String>> timeList) {
String currentDate = gzrq;
String preTime = "";
for (Object sectionTime : sectionTimes) {
List<Map<String, String>> sectionTime1 = (List) sectionTime;
if (sectionTime1.size() != 2) {
break;
}
// 获取当前的打卡时间
Map<String, String> startMap = sectionTime1.get(0);
String startTimes = startMap.get("times");
Map<String, String> map = new HashMap<>();
currentDate = getDateStr(currentDate, preTime, startTimes);
map.put("startTimes", currentDate + " " + startTimes);
Map<String, String> endMap = sectionTime1.get(1);
String endTimes = endMap.get("times");
currentDate = getDateStr(currentDate, startTimes, endTimes);
map.put("endTimes", currentDate + " " + endTimes);
preTime = endTimes;
timeList.add(map);
}
}
/**
*
*
* @param currentDate
* @param preTime
* @param currentTime
* @return
*/
private String getDateStr(String currentDate, String preTime, String currentTime) {
// 前一个时间为空,返回当前日期+当前时间
if (StringUtils.isBlank(preTime)) {
return currentDate;
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm");
LocalTime localPreTime = LocalTime.parse(preTime, formatter);
LocalTime localCurrentTime = LocalTime.parse(currentTime, formatter);
if (localPreTime.isAfter(localCurrentTime)) {
return DateUtil.addDate(currentDate, 1);
} else {
return currentDate;
}
}
/**
*
* @param beforetwohours_starttime

Loading…
Cancel
Save