|
@@ -9,6 +9,7 @@ import org.springframework.util.StringUtils;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
+import java.math.BigDecimal;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.time.LocalDate;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
@@ -18,7 +19,7 @@ import java.util.List;
|
|
|
@Service
|
|
|
public class ExcelParserService {
|
|
|
|
|
|
- public List<UserCorpwxTime> parseAttendanceExcel(MultipartFile file) throws IOException {
|
|
|
+ public List<UserCorpwxTime> parseAttendanceExcel(MultipartFile file, boolean onlyReduceRestTime) throws IOException {
|
|
|
List<UserCorpwxTime> attendanceList = new ArrayList<>();
|
|
|
|
|
|
try (Workbook workbook = new XSSFWorkbook(file.getInputStream())) {
|
|
@@ -115,7 +116,7 @@ public class ExcelParserService {
|
|
|
// 计算工作时长(这里简化处理,实际需要更精确的计算)
|
|
|
if (startTime != null && endTime != null) {
|
|
|
try {
|
|
|
- double workHours = calculateZhengBeiWorkHours(startTime, endTime);
|
|
|
+ double workHours = calculateZhengBeiWorkHours(startTime, endTime, false);
|
|
|
attendance.setWorkHours(workHours);
|
|
|
} catch (Exception e) {
|
|
|
// 时间格式不正确时忽略
|
|
@@ -163,21 +164,23 @@ public class ExcelParserService {
|
|
|
}
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
- String startTime = "08:25";
|
|
|
+ String startTime = "08:20";
|
|
|
String endTime = "12:30";
|
|
|
- double workHours = calculateZhengBeiWorkHours(startTime, endTime);
|
|
|
+ double workHours = calculateZhengBeiWorkHours(startTime, endTime, true);
|
|
|
|
|
|
System.out.println("工作时长:" + workHours + "小时");
|
|
|
}
|
|
|
|
|
|
- public static double calculateZhengBeiWorkHours(String startTime, String endTime) {
|
|
|
+
|
|
|
+
|
|
|
+ public static double calculateZhengBeiWorkHours(String startTime, String endTime, boolean onlyReduceRestTime) {
|
|
|
// 简单计算工作时长(小时)
|
|
|
if (startTime.compareTo("08:30") < 0 && endTime.compareTo("08:30") < 0) {
|
|
|
//上下班都是八点半之前,忽略掉
|
|
|
return 0;
|
|
|
}
|
|
|
// 实际应用中需要更精确的计算,考虑午休时间等
|
|
|
- if (startTime.compareTo("08:30") < 0) {
|
|
|
+ if (!onlyReduceRestTime && startTime.compareTo("08:30") < 0) {
|
|
|
startTime = "08:30";
|
|
|
}
|
|
|
// 12:00-13:00为午休,中间来的得从下午上班时间开始算
|
|
@@ -216,14 +219,20 @@ public class ExcelParserService {
|
|
|
}
|
|
|
double minPart = hours - (int)hours;
|
|
|
|
|
|
- if (minPart > 0 && minPart < 0.5) {
|
|
|
- minPart = 0;
|
|
|
- } else if (minPart > 0.5) {
|
|
|
- minPart = 0.5;
|
|
|
+ //按0.5小时对齐
|
|
|
+ if (!onlyReduceRestTime) {
|
|
|
+ if (minPart > 0 && minPart < 0.5) {
|
|
|
+ minPart = 0;
|
|
|
+ } else if (minPart > 0.5) {
|
|
|
+ minPart = 0.5;
|
|
|
+ }
|
|
|
}
|
|
|
- hours = (int)hours + minPart;
|
|
|
|
|
|
- return hours;
|
|
|
+ hours = (int)hours + minPart;
|
|
|
+ //四舍五入到小数点后一位
|
|
|
+ BigDecimal bigDecimal = new BigDecimal(hours);
|
|
|
+ bigDecimal = bigDecimal.setScale(1, BigDecimal.ROUND_HALF_UP);
|
|
|
+ return bigDecimal.doubleValue();
|
|
|
}
|
|
|
|
|
|
private int convertWeekDay(String weekDayTxt) {
|