| | |
| | | import cn.hutool.core.date.DateTime; |
| | | import cn.hutool.core.date.DateUtil; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.time.temporal.TemporalAdjusters; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | .set(PerformanceShift::getShift, performanceShift.getShift())); |
| | | } |
| | | |
| | | // public static void main(String[] args) { |
| | | // String dateStr = "2023-09-15 12:30:45"; |
| | | // LocalDateTime localDateTime = convertToLocalDateTime(dateStr); |
| | | // System.out.println(localDateTime); |
| | | // } |
| | | // |
| | | // public static LocalDateTime convertToLocalDateTime(String dateStr) { |
| | | // DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
| | | // return LocalDateTime.parse(dateStr, formatter); |
| | | // } |
| | | @Override |
| | | public IPage<Map<String, Object>> performanceShiftPageYear(Page<Object> page, String time, String userName, String laboratory) { |
| | | IPage<Map<String, Object>> mapYearIPage = baseMapper.performanceShiftYear(page, time, userName, laboratory); |
| | | List<Enums> shiftType = enumService.selectEnumByCategory("班次类型"); |
| | | mapYearIPage.setRecords(annualAttendanceProcessing(mapYearIPage.getRecords(), shiftType)); |
| | | return mapYearIPage; |
| | | } |
| | | |
| | | // 年分页与导出共同使用 |
| | | public List<Map<String, Object>> annualAttendanceProcessing(List<Map<String, Object>> mapYearList, List<Enums> shiftType){ |
| | | for (Map<String, Object> map : mapYearList) { |
| | | Map<String, Object> resultMap = new HashMap<>(); |
| | | Map<String, Object> hashMapYear = new LinkedHashMap<>(); |
| | | int totalYearAttendance = 0; |
| | | // 一年12个月 |
| | | for (int i = 1; i < 13; i++) { |
| | | Map<String, Object> hashMapMonth = new LinkedHashMap<>(); |
| | | int totalMonthAttendance = 0; |
| | | for (Enums enums : shiftType) { |
| | | if (!hashMapYear.containsKey(enums.getLabel())) { |
| | | hashMapYear.put(enums.getLabel(), 0); |
| | | } |
| | | // 月 |
| | | if (ObjectUtils.isNotEmpty(map.get("month_str")) && map.get("work_time").equals(i)) { |
| | | String charArray = map.get("month_str").toString(); |
| | | int count = countOccurrences(charArray, i + ":" + enums.getValue()); |
| | | hashMapMonth.put(enums.getLabel(), count); |
| | | hashMapYear.put(enums.getLabel(), Integer.parseInt(hashMapYear.get(enums.getLabel()).toString()) + count ); |
| | | if (enums.getValue().equals("0") || enums.getValue().equals("1") || enums.getValue().equals("2")) { |
| | | totalMonthAttendance += count; |
| | | totalYearAttendance += count; |
| | | } |
| | | } |
| | | // 空数据 |
| | | else { |
| | | map.put("work_time", i); |
| | | hashMapMonth.put(enums.getLabel(), 0); |
| | | } |
| | | } |
| | | hashMapMonth.put("totalMonthAttendance", totalMonthAttendance); |
| | | hashMapYear.put("totalYearAttendance", totalYearAttendance); |
| | | resultMap.put(i + "", hashMapMonth); |
| | | } |
| | | map.remove("month_str"); |
| | | map.remove("year_str"); |
| | | map.put("year", hashMapYear); |
| | | map.put("month", resultMap); |
| | | } |
| | | return mapYearList; |
| | | } |
| | | |
| | | public static int countOccurrences(String str, String target) { |
| | | int count = 0; |
| | | int index = 0; |
| | | while ((index = str.indexOf(target, index))!= -1) { |
| | | count++; |
| | | index += target.length(); |
| | | } |
| | | return count; |
| | | } |
| | | |
| | | public void dataRequiredForProcessingIntoExcel(List<Map<String, Object>> list){ |
| | | Map<Object, List<Map<String, Object>>> userId = list.stream().collect(Collectors.groupingBy(i -> i.get("user_id"))); |
| | | userId.forEach((k, v) -> { |
| | | System.out.println(k); |
| | | System.out.println("============="); |
| | | System.out.println(v); |
| | | }); |
| | | } |
| | | |
| | | @Override |
| | | public Map<Object, Object> exportToExcel(String time, String userName, String laboratory) { |
| | | 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)); |
| | | List<Map<String, Object>> mapYearList = baseMapper.performanceShiftYearList(time, userName, laboratory); |
| | | annualAttendanceProcessing(mapYearList, shiftType); |
| | | dataRequiredForProcessingIntoExcel(mapYearList); |
| | | map.put("data", mapYearList); |
| | | return map; |
| | | } |
| | | |
| | | // 获取两个localDateTime的每一天 |
| | | public static List<LocalDateTime> getLocalDateTimesBetween(LocalDateTime start, LocalDateTime end) { |
| | |
| | | return "未知"; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 返回表头 |
| | | * |
| | | * 外层List代表行内层 List代表列 相同的列数据会被主动合并 |
| | | * 构造双列表头 |
| | | * @return List<List<String>> |
| | | */ |
| | | private static List<List<String>> getHeader(String month, List<Enums> enums){ |
| | | List<List<String>> line = new ArrayList<>(); |
| | | line.add(Arrays.asList("考勤汇总", "序号", "序号")); |
| | | line.add(Arrays.asList("考勤汇总", "工号", "工号")); |
| | | line.add(Arrays.asList("考勤汇总", "姓名", "姓名")); |
| | | // 年 header |
| | | for (Enums anEnum : enums) { |
| | | line.add(Arrays.asList("考勤汇总", month, anEnum.getLabel())); |
| | | } |
| | | // 月header |
| | | for (int i = 1; i < 13; i++) { |
| | | for (Enums anEnum : enums) { |
| | | line.add(Arrays.asList("出勤详情", i + " 月", anEnum.getLabel())); |
| | | } |
| | | } |
| | | return line; |
| | | } |
| | | } |