|
|
@@ -1,5 +1,9 @@
|
|
|
package com.x1ongzhu.wisFactory.service;
|
|
|
|
|
|
+import com.alibaba.excel.EasyExcel;
|
|
|
+import com.alibaba.excel.ExcelWriter;
|
|
|
+import com.alibaba.excel.write.metadata.WriteSheet;
|
|
|
+import com.alibaba.excel.write.style.row.SimpleRowHeightStyleStrategy;
|
|
|
import com.x1ongzhu.wisFactory.config.Constants;
|
|
|
import com.x1ongzhu.wisFactory.domain.*;
|
|
|
import com.x1ongzhu.wisFactory.dto.StaffAttendanceBrief;
|
|
|
@@ -9,13 +13,19 @@ import com.x1ongzhu.wisFactory.enums.HolidayType;
|
|
|
import com.x1ongzhu.wisFactory.exception.BusinessException;
|
|
|
import com.x1ongzhu.wisFactory.repo.*;
|
|
|
import com.x1ongzhu.wisFactory.utils.DateTimeUtils;
|
|
|
+import com.x1ongzhu.wisFactory.utils.excel.*;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import lombok.Data;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.Range;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.apache.poi.ss.usermodel.*;
|
|
|
+import org.apache.poi.xssf.usermodel.XSSFCellStyle;
|
|
|
import org.springframework.data.jpa.domain.Specification;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.OutputStream;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.math.RoundingMode;
|
|
|
import java.time.LocalDate;
|
|
|
@@ -24,7 +34,9 @@ import java.time.LocalTime;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
import java.time.temporal.ChronoUnit;
|
|
|
import java.util.*;
|
|
|
+import java.util.function.IntUnaryOperator;
|
|
|
import java.util.stream.Collectors;
|
|
|
+import java.util.stream.IntStream;
|
|
|
import java.util.stream.Stream;
|
|
|
|
|
|
@Service
|
|
|
@@ -454,7 +466,7 @@ public class AttendanceService {
|
|
|
.name(staffInfo.getName())
|
|
|
.department(staffInfo.getDepartment())
|
|
|
.date(date)
|
|
|
- .weekDay(DateTimeUtils.format(date, "E"))
|
|
|
+ .weekDay(DateTimeUtils.weekDay(date))
|
|
|
.absent(true)
|
|
|
.build();
|
|
|
}
|
|
|
@@ -466,7 +478,7 @@ public class AttendanceService {
|
|
|
.name(staffInfo.getName())
|
|
|
.department(staffInfo.getDepartment())
|
|
|
.date(date)
|
|
|
- .weekDay(DateTimeUtils.format(date, "E"))
|
|
|
+ .weekDay(DateTimeUtils.weekDay(date))
|
|
|
.start(timeBucket.start)
|
|
|
.end(timeBucket.end)
|
|
|
.remark(timeBucket.subList.stream()
|
|
|
@@ -588,4 +600,149 @@ public class AttendanceService {
|
|
|
this.subList = subList;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ public void exportRaw(LocalDate start, LocalDate end, List<String> departmentNames, OutputStream outputStream) throws IOException {
|
|
|
+ ExcelUtil excelUtil = ExcelUtil.Create();
|
|
|
+ List<String> dates = new ArrayList<>();
|
|
|
+ List<String> weeks = new ArrayList<>();
|
|
|
+
|
|
|
+ Font cellStyleFont = excelUtil.getWorkbook().createFont();
|
|
|
+ cellStyleFont.setFontHeightInPoints((short) 18);
|
|
|
+ cellStyleFont.setColor(IndexedColors.BLACK.getIndex());
|
|
|
+ cellStyleFont.setBold(true);
|
|
|
+ cellStyleFont.setFontName("微软雅黑");
|
|
|
+
|
|
|
+ CellStyle cellStyle = excelUtil.getWorkbook().createCellStyle();
|
|
|
+ cellStyle.setAlignment(HorizontalAlignment.CENTER); // 居中设置
|
|
|
+ cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+ cellStyle.setWrapText(true);
|
|
|
+ cellStyle.setBorderRight(BorderStyle.THIN);
|
|
|
+ cellStyle.setBorderLeft(BorderStyle.THIN);
|
|
|
+ cellStyle.setBorderTop(BorderStyle.THIN);
|
|
|
+ cellStyle.setBorderBottom(BorderStyle.THIN);
|
|
|
+ cellStyle.setFont(cellStyleFont);
|
|
|
+
|
|
|
+ Font nameCellStyleFont = excelUtil.getWorkbook().createFont();
|
|
|
+ cellStyleFont.setFontHeightInPoints((short) 12);
|
|
|
+ cellStyleFont.setColor(IndexedColors.BLACK.getIndex());
|
|
|
+ cellStyleFont.setFontName("微软雅黑");
|
|
|
+
|
|
|
+ CellStyle nameCellStyle1 = nameStyle1(excelUtil.getWorkbook());
|
|
|
+ CellStyle nameCellStyle2 = nameStyle2(excelUtil.getWorkbook());
|
|
|
+
|
|
|
+ int totalDays = (int) (ChronoUnit.DAYS.between(start, end) + 1);
|
|
|
+ for (int i = 0; i < totalDays; i++) {
|
|
|
+ dates.add(DateTimeUtils.format(start.plusDays(i), "dd"));
|
|
|
+ weeks.add(DateTimeUtils.weekDay(start.plusDays(i)));
|
|
|
+ }
|
|
|
+ for (String departmentName : departmentNames) {
|
|
|
+ String[] arr = departmentName.split("/");
|
|
|
+ String sheetName = arr[arr.length - 1];
|
|
|
+ excelUtil.createSheet(sheetName);
|
|
|
+ excelUtil.writeRow(RowModel.Builder()
|
|
|
+ .addCell(CellModel.builder()
|
|
|
+ .startColumn(0)
|
|
|
+ .endColumn(totalDays - 1)
|
|
|
+ .style(cellStyle)
|
|
|
+ .value("考勤记录表")
|
|
|
+ .build())
|
|
|
+ .build(), (short) (32 * 20));
|
|
|
+ excelUtil.writeRow(RowModel.Builder()
|
|
|
+ .addCell(CellModel.builder()
|
|
|
+ .startColumn(0)
|
|
|
+ .endColumn(totalDays - 1)
|
|
|
+ .value("部门" + departmentName + " 记录时间:" + DateTimeUtils
|
|
|
+ .format(start, "yyyy-MM-dd") + " ~ "
|
|
|
+ + DateTimeUtils.format(end, "yyyy-MM-dd"))
|
|
|
+ .build())
|
|
|
+ .build(), (short) (25 * 20));
|
|
|
+ excelUtil.writeRow(RowModel.Builder()
|
|
|
+ .addCells(dates.toArray(new String[0]))
|
|
|
+ .build());
|
|
|
+ excelUtil.writeRow(RowModel.Builder()
|
|
|
+ .addCells(weeks.toArray(new String[0]))
|
|
|
+ .build());
|
|
|
+ for (StaffInfo staffInfo : staffInfoRepo.findByDepartment(departmentName)) {
|
|
|
+ if (totalDays > 10) {
|
|
|
+ excelUtil.writeRow(RowModel.Builder()
|
|
|
+ .addCell(CellModel.builder()
|
|
|
+ .startColumn(0)
|
|
|
+ .endColumn((int) Math.floor(totalDays / 2.0) - 1)
|
|
|
+ .value("姓名:" + staffInfo.getName())
|
|
|
+ .style(nameCellStyle1)
|
|
|
+ .build())
|
|
|
+ .addCell(CellModel.builder()
|
|
|
+ .startColumn((int) Math.floor(totalDays / 2.0))
|
|
|
+ .endColumn(totalDays - 1)
|
|
|
+ .value("部门:" + departmentName)
|
|
|
+ .style(nameCellStyle2)
|
|
|
+ .build())
|
|
|
+ .build(), (short) (25 * 20));
|
|
|
+ } else {
|
|
|
+ excelUtil.writeRow(RowModel.Builder()
|
|
|
+ .addCell(CellModel.builder()
|
|
|
+ .startColumn(0)
|
|
|
+ .endColumn(totalDays - 1)
|
|
|
+ .value("姓名:" + staffInfo.getName() + " 部门:" + departmentName)
|
|
|
+ .build())
|
|
|
+ .build(), (short) (25 * 20));
|
|
|
+ }
|
|
|
+ List<StaffAccess> staffAccessList = staffAccessRepo
|
|
|
+ .findByEmployNoAndAccessTimeBetweenOrderByAccessTimeAsc(staffInfo.getEmployNo(), start
|
|
|
+ .atStartOfDay(), end.atTime(Constants.TIME_MAX));
|
|
|
+ excelUtil.writeRow(RowModel.Builder()
|
|
|
+ .addCells(IntStream.range(0, (int) ChronoUnit.DAYS.between(start, end) + 1)
|
|
|
+ .mapToObj(i -> staffAccessList.stream()
|
|
|
+ .filter(staffAccess -> {
|
|
|
+ LocalDate date = start.plusDays(i);
|
|
|
+ return staffAccess.getAccessTime().isAfter(date.atStartOfDay())
|
|
|
+ && staffAccess.getAccessTime()
|
|
|
+ .isBefore(date.atTime(Constants.TIME_MAX));
|
|
|
+ })
|
|
|
+ .sorted(Comparator.comparing(StaffAccess::getAccessTime))
|
|
|
+ .map(staffAccess -> DateTimeUtils.format(staffAccess.getAccessTime(), "HH:mm"))
|
|
|
+ .distinct()
|
|
|
+ .collect(Collectors.joining("\r\n"))).toArray(String[]::new))
|
|
|
+ .build());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ excelUtil.to(outputStream);
|
|
|
+ }
|
|
|
+
|
|
|
+ private CellStyle nameStyle1(Workbook workbook) {
|
|
|
+ Font nameCellStyleFont = workbook.createFont();
|
|
|
+ nameCellStyleFont.setFontHeightInPoints((short) 12);
|
|
|
+ nameCellStyleFont.setColor(IndexedColors.BLACK.getIndex());
|
|
|
+ nameCellStyleFont.setFontName("微软雅黑");
|
|
|
+
|
|
|
+ CellStyle nameCellStyle = workbook.createCellStyle();
|
|
|
+ nameCellStyle.setAlignment(HorizontalAlignment.CENTER); // 居中设置
|
|
|
+ nameCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+ nameCellStyle.setWrapText(true);
|
|
|
+ nameCellStyle.setBorderRight(BorderStyle.NONE);
|
|
|
+ nameCellStyle.setBorderLeft(BorderStyle.THIN);
|
|
|
+ nameCellStyle.setBorderTop(BorderStyle.THIN);
|
|
|
+ nameCellStyle.setBorderBottom(BorderStyle.THIN);
|
|
|
+ nameCellStyle.setFont(nameCellStyleFont);
|
|
|
+ return nameCellStyle;
|
|
|
+ }
|
|
|
+
|
|
|
+ private CellStyle nameStyle2(Workbook workbook) {
|
|
|
+ Font nameCellStyleFont = workbook.createFont();
|
|
|
+ nameCellStyleFont.setFontHeightInPoints((short) 12);
|
|
|
+ nameCellStyleFont.setColor(IndexedColors.BLACK.getIndex());
|
|
|
+ nameCellStyleFont.setFontName("微软雅黑");
|
|
|
+
|
|
|
+ CellStyle nameCellStyle = workbook.createCellStyle();
|
|
|
+ nameCellStyle.setAlignment(HorizontalAlignment.CENTER); // 居中设置
|
|
|
+ nameCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|
|
+ nameCellStyle.setWrapText(true);
|
|
|
+ nameCellStyle.setBorderRight(BorderStyle.THIN);
|
|
|
+ nameCellStyle.setBorderLeft(BorderStyle.NONE);
|
|
|
+ nameCellStyle.setBorderTop(BorderStyle.THIN);
|
|
|
+ nameCellStyle.setBorderBottom(BorderStyle.THIN);
|
|
|
+ nameCellStyle.setFont(nameCellStyleFont);
|
|
|
+ return nameCellStyle;
|
|
|
+ }
|
|
|
}
|