Crunchy
2024-05-13 35da9191297752452f7ab60ff9bc3eed515dfeec
performance-server/src/main/java/com/yuanchu/mom/service/impl/PerformanceShiftServiceImpl.java
@@ -22,6 +22,7 @@
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
@@ -100,10 +101,10 @@
        mapIPage.getRecords().forEach(i -> {
            String[] shiftTimes = i.getShiftTime().split(";");
            int totalAttendance = 0;
            List<Object> map = new ArrayList<>();
            List<Map<String, Object>> map = new ArrayList<>();
            // 分割日期
            for (String shiftTime : shiftTimes) {
                Map<Object, Object> hashMap = new HashMap<>();
                Map<String, Object> hashMap = new HashMap<>();
                String[] shiftTimeAndShift = shiftTime.split(":");
                for (Enums enums : shiftType) {
                    if (!i.getMonthlyAttendance().containsKey(enums.getLabel())) {
@@ -260,16 +261,58 @@
    }
    @Override
    public Map<Object, Object> exportToExcel(String time, String userName, String laboratory) throws Exception {
    public Map<Object, Object> exportToYearExcel(String time, String userName, String laboratory) throws Exception {
        Map<Object, Object> map = new HashMap<>();
        List<Enums> shiftType = enumService.selectEnumByCategory("班次类型");
        DateTimeFormatter formatters = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 将字符串时间转换为 LocalDateTime 类型时间
        LocalDateTime localDateTime = LocalDateTime.parse(time, formatters);
        map.put("header", getHeader(localDateTime.getYear() + " 年", shiftType));
        map.put("header", getYearHeader(localDateTime.getYear() + " 年", shiftType));
        List<Map<String, Object>> mapYearList = baseMapper.performanceShiftYearList(time, userName, laboratory);
        annualAttendanceProcessing(mapYearList, shiftType);
        List<List<Object>> lists = dataRequiredForProcessingIntoExcel(mapYearList, shiftType);
        map.put("data", lists);
        return map;
    }
    @Override
    public Map<Object, Object> exportToMonthExcel(String time, String userName, String laboratory) {
        List<Enums> shiftType = enumService.selectEnumByCategory("班次类型");
        List<PerformanceShiftMapDto> mapIPage = baseMapper.performanceShiftList(time, userName, laboratory);
        mapIPage.forEach(i -> {
            String[] shiftTimes = i.getShiftTime().split(";");
            int totalAttendance = 0;
            List<Map<String, Object>> map = new ArrayList<>();
            // 分割日期
            for (String shiftTime : shiftTimes) {
                Map<String, Object> hashMap = new HashMap<>();
                String[] shiftTimeAndShift = shiftTime.split(":");
                for (Enums enums : shiftType) {
                    if (!i.getMonthlyAttendance().containsKey(enums.getLabel())) {
                        i.getMonthlyAttendance().put(enums.getLabel(), 0);
                    }
                    if (enums.getValue().equals(shiftTimeAndShift[1])) {
                        Integer num = (Integer) i.getMonthlyAttendance().get(enums.getLabel());
                        i.getMonthlyAttendance().put(enums.getLabel(), num += 1);
                    }
                }
                if (shiftTimeAndShift[1].equals("1") || shiftTimeAndShift[1].equals("2") || shiftTimeAndShift[1].equals("0")) {
                    i.getMonthlyAttendance().put("totalAttendance", totalAttendance += 1);
                }
                hashMap.put("id", shiftTimeAndShift[2]);
                hashMap.put("shift", shiftTimeAndShift[1]);
                hashMap.put("time", shiftTimeAndShift[0]);
                map.add(hashMap);
            }
            i.setList(map);
            i.setShiftTime(null);
        });
        Map<Object, Object> map = new HashMap<>();
        DateTimeFormatter formatters = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 将字符串时间转换为 LocalDateTime 类型时间
        LocalDateTime localDateTime = LocalDateTime.parse(time, formatters);
        map.put("header", getMonthHeader(localDateTime, shiftType));
        List<List<Object>> lists = dataRequiredForProcessingIntoExcelMonth(mapIPage, shiftType);
        map.put("data", lists);
        return map;
    }
@@ -330,15 +373,15 @@
     * 构造双列表头
     * @return List<List<String>>
     */
    private static List<List<String>> getHeader(String month, List<Enums> enums){
    private static List<List<String>> getYearHeader(String year, List<Enums> enums){
        List<List<String>> line = new ArrayList<>();
        line.add(Arrays.asList("考勤汇总", "序号", "序号"));
        line.add(Arrays.asList("考勤汇总", "工号", "工号"));
        line.add(Arrays.asList("考勤汇总", "姓名", "姓名"));
        line.add(Arrays.asList("出勤详情", month, "出勤"));
        line.add(Arrays.asList("出勤详情", year, "出勤"));
        // 年 header
        for (Enums anEnum : enums) {
            line.add(Arrays.asList("考勤汇总", month, anEnum.getLabel()));
            line.add(Arrays.asList("考勤汇总", year, anEnum.getLabel()));
        }
        // 月header
        for (int i = 1; i < 13; i++) {
@@ -349,4 +392,57 @@
        }
        return line;
    }
    private static List<List<String>> getMonthHeader(LocalDateTime localDateTimeYear, List<Enums> enums){
        String year = localDateTimeYear.getYear() + " 年人员班次";
        List<List<String>> line = new ArrayList<>();
        line.add(Arrays.asList(year, "序号", "序号", "序号"));
        line.add(Arrays.asList(year, "姓名", "姓名", "姓名"));
        line.add(Arrays.asList(year, "实验室", "实验室", "实验室"));
        line.add(Arrays.asList(year, localDateTimeYear.getYear() + "", localDateTimeYear.getYear() + "", "出勤"));
        line.add(Arrays.asList(year, localDateTimeYear.getYear() + "", localDateTimeYear.getYear() + "", enums.get(3).getLabel()));
        line.add(Arrays.asList(year, "年", "年", enums.get(4).getLabel()));
        line.add(Arrays.asList(year, localDateTimeYear.getMonthValue() + "", localDateTimeYear.getMonthValue() + "", enums.get(0).getLabel()));
        line.add(Arrays.asList(year, "月", "月", enums.get(1).getLabel()));
        line.add(Arrays.asList(year, "", "", enums.get(2).getLabel()));
        line.add(Arrays.asList(year, "周次", "星期", "出差"));
        LocalDate firstDayOfMonth = localDateTimeYear.toLocalDate().withDayOfMonth(1);
        LocalDate lastDayOfMonth = localDateTimeYear.toLocalDate().with(TemporalAdjusters.lastDayOfMonth());
        List<LocalDateTime> timeList = getLocalDateTimesBetween(firstDayOfMonth.atStartOfDay(), lastDayOfMonth.atStartOfDay());
        timeList.forEach(i -> {
            int dayOfYear = i.getDayOfMonth();
            Date from = Date.from(i.atZone(ZoneId.systemDefault()).toInstant());
            String weekDay = getWeekDay(i.getDayOfWeek().getValue());
            line.add(Arrays.asList(year, DateUtil.weekOfYear(DateUtil.offsetDay(from, 1)) + "", weekDay, dayOfYear + ""));
        });
        return line;
    }
    public List<List<Object>> dataRequiredForProcessingIntoExcelMonth(List<PerformanceShiftMapDto> list, List<Enums> enums) {
        List<List<Object>> data = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            List<Object> excelRowList = new ArrayList<>();
            excelRowList.add(i + 1);
            excelRowList.add(list.get(i).getName());
            excelRowList.add(list.get(i).getDepartment());
            excelRowList.add(list.get(i).getMonthlyAttendance().get("totalAttendance"));
            excelRowList.add(list.get(i).getMonthlyAttendance().get(enums.get(3).getLabel()));
            excelRowList.add(list.get(i).getMonthlyAttendance().get(enums.get(4).getLabel()));
            excelRowList.add(list.get(i).getMonthlyAttendance().get(enums.get(0).getLabel()));
            excelRowList.add(list.get(i).getMonthlyAttendance().get(enums.get(1).getLabel()));
            excelRowList.add(list.get(i).getMonthlyAttendance().get(enums.get(2).getLabel()));
            excelRowList.add(0); // 出差
            for (Map<String, Object> o : list.get(i).getList()) {
                String enumLabel = "";
                for (Enums anEnum : enums) {
                    if (anEnum.getValue().equals(o.get("shift"))) {
                        enumLabel = anEnum.getLabel();
                    }
                }
                excelRowList.add(ObjectUtils.isEmpty(enumLabel) ? "无" : enumLabel);
            }
            data.add(excelRowList);
        }
        return data;
    }
}