From 2f9f2b3edb14efdd1e2071e26420e9c9d20b1992 Mon Sep 17 00:00:00 2001 From: Crunchy <3114200645@qq.com> Date: 星期三, 22 五月 2024 16:14:30 +0800 Subject: [PATCH] 2024-5-22 班次模块调整 --- performance-server/src/main/java/com/yuanchu/mom/service/impl/PerformanceShiftServiceImpl.java | 287 +++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 files changed, 267 insertions(+), 20 deletions(-) diff --git a/performance-server/src/main/java/com/yuanchu/mom/service/impl/PerformanceShiftServiceImpl.java b/performance-server/src/main/java/com/yuanchu/mom/service/impl/PerformanceShiftServiceImpl.java index fd33f86..d4f0b04 100644 --- a/performance-server/src/main/java/com/yuanchu/mom/service/impl/PerformanceShiftServiceImpl.java +++ b/performance-server/src/main/java/com/yuanchu/mom/service/impl/PerformanceShiftServiceImpl.java @@ -3,6 +3,7 @@ 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; @@ -13,13 +14,16 @@ import com.yuanchu.mom.pojo.PerformanceShift; import com.yuanchu.mom.service.EnumService; import com.yuanchu.mom.service.PerformanceShiftService; +import com.yuanchu.mom.utils.JackSonUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.math.BigDecimal; 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.*; @@ -97,30 +101,40 @@ List<Map<String, Object>> mapYearIPage = baseMapper.performanceShiftYearPage(time, userName, laboratory); mapIPage.getRecords().forEach(i -> { String[] shiftTimes = i.getShiftTime().split(";"); - int totalAttendance = 0; - List<Object> map = new ArrayList<>(); + double totalAttendance = 0; + 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())) { 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); + BigDecimal bigDecimal = new BigDecimal(i.getMonthlyAttendance().get(enums.getLabel()).toString()); + i.getMonthlyAttendance().put(enums.getLabel(), bigDecimal.add(new BigDecimal("1"))); + } + // 鍗婏紝鍙﹀鍗婂ぉ绠楃粰鏃� + if (shiftTimeAndShift[1].equals("5") && enums.getValue().equals("0")) { + BigDecimal bigDecimal = new BigDecimal(i.getMonthlyAttendance().get(enums.getLabel()).toString()); + i.getMonthlyAttendance().put(enums.getLabel(), bigDecimal.add(new BigDecimal("0.5"))); } } - if (shiftTimeAndShift[1].equals("1") || shiftTimeAndShift[1].equals("2") || shiftTimeAndShift[1].equals("0")) { + // 鏃╋紝涓紝澶滐紝宸� + if (shiftTimeAndShift[1].equals("1") || shiftTimeAndShift[1].equals("2") || shiftTimeAndShift[1].equals("0") || shiftTimeAndShift[1].equals("6")) { i.getMonthlyAttendance().put("totalAttendance", totalAttendance += 1); + } + // 鍗� + if (shiftTimeAndShift[1].equals("5")) { + i.getMonthlyAttendance().put("totalAttendance", totalAttendance += 0.5); } hashMap.put("id", shiftTimeAndShift[2]); hashMap.put("shift", shiftTimeAndShift[1]); hashMap.put("time", shiftTimeAndShift[0]); map.add(hashMap); } - int totalYearAttendance = 0; + double totalYearAttendance = 0; Map<String, Object> hashMap = new HashMap<>(); for (Map<String, Object> record : mapYearIPage) { if (record.get("user_id").toString().equals(i.getUserId())) { @@ -129,12 +143,21 @@ hashMap.put(enums.getLabel(), 0); } if (enums.getValue().equals(record.get("shift"))) { - Integer num = (Integer) hashMap.get(enums.getLabel()); - hashMap.put(enums.getLabel(), num += 1); + BigDecimal num = new BigDecimal(hashMap.get(enums.getLabel()).toString()); + hashMap.put(enums.getLabel(), num.add(new BigDecimal("1"))); + } + // 鍗婏紝鍙﹀鍗婂ぉ绠楃粰鏃� + if (record.get("shift").equals("5") && enums.getValue().equals("0")) { + BigDecimal bigDecimal = new BigDecimal(hashMap.get(enums.getLabel()).toString()); + hashMap.put(enums.getLabel(), bigDecimal.add(new BigDecimal("0.5"))); } } - if (record.get("shift").equals("1") || record.get("shift").equals("2") || record.get("shift").equals("0")) { + if (record.get("shift").equals("1") || record.get("shift").equals("2") || record.get("shift").equals("0") || record.get("shift").equals("6")) { hashMap.put("totalAttendance", totalYearAttendance += 1); + } + // 鍗� + if (record.get("shift").equals("5")) { + hashMap.put("totalAttendance", totalYearAttendance += 0.5); } } } @@ -171,16 +194,160 @@ .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 LinkedHashMap<>(); + Map<String, Object> hashMapYear = new LinkedHashMap<>(); + double totalYearAttendance = 0; + // 涓�骞�12涓湀 + for (int i = 1; i < 13; i++) { + Map<String, Object> hashMapMonth = new LinkedHashMap<>(); + double 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") || enums.getValue().equals("6")) { + totalMonthAttendance += count; + totalYearAttendance += count; + } + // 鍗婏紝鍙﹀鍗婂ぉ绠楃粰鏃� + if (enums.getValue().equals("5")) { + BigDecimal multiply = new BigDecimal("0.5").multiply(new BigDecimal(count)).setScale(1, BigDecimal.ROUND_CEILING); + hashMapMonth.put(shiftType.get(0).getLabel(), new BigDecimal(hashMapMonth.get(shiftType.get(0).getLabel()).toString()).add(multiply)); + hashMapYear.put(shiftType.get(0).getLabel(), new BigDecimal(hashMapYear.get(shiftType.get(0).getLabel()).toString()).add(multiply)); + totalMonthAttendance += multiply.doubleValue(); + totalYearAttendance += multiply.doubleValue(); + } + } + // 绌烘暟鎹� + 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 List<List<Object>> dataRequiredForProcessingIntoExcel(List<Map<String, Object>> list, List<Enums> enums) throws Exception { + 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).get("account")); + excelRowList.add(list.get(i).get("name")); + Map<String, Object> year = JackSonUtil.unmarshal(JackSonUtil.marshal(list.get(i).get("year")), Map.class); + excelRowList.add(year.get("totalYearAttendance")); + enums.forEach(j -> { + if (!j.getValue().equals("5")) { + excelRowList.add(year.get(j.getLabel())); + } + }); + Map<String, Map<String, Object>> month = JackSonUtil.unmarshal(JackSonUtil.marshal(list.get(i).get("month")), Map.class); + for (int j = 1; j < 13; j++) { + Object totalMonthAttendance = month.get(j + "").get("totalMonthAttendance"); + excelRowList.add(totalMonthAttendance); + for (Enums anEnum : enums) { + if (!anEnum.getValue().equals("5")) { + excelRowList.add(month.get(j + "").get(anEnum.getLabel())); + } + } + } + data.add(excelRowList); + } + return data; + } + + @Override + 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", 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; + } // 鑾峰彇涓や釜localDateTime鐨勬瘡涓�澶� public static List<LocalDateTime> getLocalDateTimesBetween(LocalDateTime start, LocalDateTime end) { @@ -230,4 +397,84 @@ return "鏈煡"; } } + + /** + * 杩斿洖琛ㄥご + * + * 澶栧眰List浠h〃琛屽唴灞� List浠h〃鍒� 鐩稿悓鐨勫垪鏁版嵁浼氳涓诲姩鍚堝苟 + * 鏋勯�犲弻鍒楄〃澶� + * @return List<List<String>> + */ + 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("鍑哄嫟璇︽儏", year, "鍑哄嫟")); + // 骞� header + for (Enums anEnum : enums) { + line.add(Arrays.asList("鑰冨嫟姹囨��", year, anEnum.getLabel())); + } + // 鏈坔eader + for (int i = 1; i < 13; i++) { + line.add(Arrays.asList("鍑哄嫟璇︽儏", i + " 鏈�", "鍑哄嫟")); + for (Enums anEnum : enums) { + line.add(Arrays.asList("鍑哄嫟璇︽儏", i + " 鏈�", anEnum.getLabel())); + } + } + 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; + } } -- Gitblit v1.9.3