You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
weaver-develop/src/com/engine/xmgsecond/util/KqCalulateUtil.java

99 lines
3.5 KiB
Java

package com.engine.xmgsecond.util;
import com.engine.xmgsecond.entity.KqFormatTotal;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @Author liang.cheng
* @Date 2025/5/14 16:16
* @Description:
* @Version 1.0
*/
public class KqCalulateUtil {
/**
* @Description:
* @Author: liang.cheng
* @Date: 2025/5/14 16:22
* @param: [resourceId, fromDate, toDate]
* @return: java.util.List<com.engine.xmgsecond.entity.KqFormatTotal>
*/
public static List<KqFormatTotal> abnormalAttendance(String resourceId,String fromDate,String toDate) {
RecordSet rs = new RecordSet();
List<KqFormatTotal> kqFormatTotalList = new ArrayList<>();
rs.executeQuery("select resourceid, forgotCheck, absenteeism, beLateMins, gravebeLateMins, leaveEearly, " +
" graveLeaveEarly from kq_format_total where resourceid = ? and kqdate >= '"+fromDate+"' and kqdate <= '"+toDate+"'",resourceId);
while (rs.next()) {
boolean isAbsence;
int absenteeism = Util.getIntValue(rs.getString("absenteeism"));
int forgotCheck = Util.getIntValue(rs.getString("forgotCheck"));
int beLateMins = Util.getIntValue(rs.getString("beLateMins"),0);
int gravebeLateMins = Util.getIntValue(rs.getString("gravebeLateMins"),0);
int leaveEearly = Util.getIntValue(rs.getString("leaveEearly"));
int graveLeaveEarly = Util.getIntValue(rs.getString("graveLeaveEarly"));
//规则1
isAbsence = isPositive(absenteeism) ||
isPositive(forgotCheck) ||
sumPositive(leaveEearly, graveLeaveEarly) > 0;
kqFormatTotalList.add(KqFormatTotal.builder()
.resourceId(Util.getIntValue(rs.getString("resourceid")))
.beLateMins(beLateMins)
.gravebeLateMins(gravebeLateMins)
.isAbsence(isAbsence)
.absenceDays(isAbsence ? 1 : 0)
.build());
}
//规则2
AtomicInteger graceCounter = new AtomicInteger(0);
kqFormatTotalList.forEach(record -> {
if (!record.isAbsence()) {
int lateMinutes = sumNullSafe(record.getBeLateMins(), record.getGravebeLateMins());
record.setAbsenceDays(calculateAbsenceDays(lateMinutes, graceCounter));
}
});
rs.writeLog("kqFormatTotalList2:"+kqFormatTotalList.toString());
return kqFormatTotalList;
}
public static boolean isPositive(Integer num) {
return Optional.ofNullable(num).orElse(0) > 0;
}
// 辅助方法:安全求和(处理 null 情况)
public static int sumPositive(Integer a, Integer b) {
return Optional.ofNullable(a).orElse(0) + Optional.ofNullable(b).orElse(0);
}
private static double calculateAbsenceDays(int lateMinutes, AtomicInteger graceCounter) {
if (lateMinutes <= 0) {
return 0;
}
if (graceCounter.get() < 3 && lateMinutes > 0 && lateMinutes <= 15) {
graceCounter.incrementAndGet();
return 0;
}
return (lateMinutes > 0 && lateMinutes <= 240) ? 0.5 : 1;
}
private static int sumNullSafe(Integer a, Integer b) {
return Optional.ofNullable(a).orElse(0) + Optional.ofNullable(b).orElse(0);
}
}