From 443a9510ba9c032d83139048f73201706a1d9c45 Mon Sep 17 00:00:00 2001
From: value <z1292839451@163.com>
Date: 星期五, 10 五月 2024 22:19:33 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'

---
 performance-server/src/main/java/com/yuanchu/mom/service/impl/PerformanceShiftServiceImpl.java |  255 ++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 233 insertions(+), 22 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 fcd6b5c..dde83c3 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
@@ -1,21 +1,29 @@
 package com.yuanchu.mom.service.impl;
 
+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 com.yuanchu.mom.dto.PerformanceShiftAddDto;
+import com.yuanchu.mom.dto.PerformanceShiftMapDto;
 import com.yuanchu.mom.mapper.PerformanceShiftMapper;
 import com.yuanchu.mom.pojo.Enums;
 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.text.SimpleDateFormat;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.time.temporal.TemporalAdjusters;
 import java.util.*;
 
 /**
@@ -32,27 +40,65 @@
     @Autowired
     private EnumService enumService;
 
+    public List<PerformanceShift> list = new ArrayList<>();
+
+    @Transactional(rollbackFor = Exception.class)
     @Override
     public void performanceShiftAdd(PerformanceShiftAddDto performanceShiftAddDto) {
+        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
+        String formattedDateTime = performanceShiftAddDto.getStartWeek().format(formatter);
+        String[] splitUserId = performanceShiftAddDto.getUserId().split(",");
+        for (String userId : splitUserId) {
+            boolean exists = baseMapper.exists(Wrappers.<PerformanceShift>lambdaQuery()
+                    .eq(PerformanceShift::getWorkTime, formattedDateTime)
+                    .eq(PerformanceShift::getUserId, userId));
+            // 濡傛灉涓嶅瓨鍦ㄦ坊鍔犳暟鎹�
+            if (!exists) {
+                LocalDate firstDayOfMonth = performanceShiftAddDto.getEndWeek().toLocalDate().withDayOfMonth(1);
+                LocalDate lastDayOfMonth = performanceShiftAddDto.getEndWeek().toLocalDate().with(TemporalAdjusters.lastDayOfMonth());
+                List<LocalDateTime> localDateTimesBetween = getLocalDateTimesBetween(firstDayOfMonth.atStartOfDay(), lastDayOfMonth.atStartOfDay());
+                localDateTimesBetween.forEach(i -> {
+                    for (String s : splitUserId) {
+                        PerformanceShift performanceShift = new PerformanceShift();
+                        performanceShift.setUserId(Integer.valueOf(s));
+                        performanceShift.setWorkTime(i);
+                        performanceShift.setShift("");
+                        list.add(performanceShift);
+                    }
+                    if (list.size() >= 1000) {
+                        baseMapper.insertBatchSomeColumn(list);
+                        list.clear();
+                    }
+                });
+                if (!list.isEmpty()) {
+                    baseMapper.insertBatchSomeColumn(list);
+                }
+            }
+        }
+        // 鍐嶆鏇存柊
         List<LocalDateTime> datesBetween = getLocalDateTimesBetween(performanceShiftAddDto.getStartWeek(), performanceShiftAddDto.getEndWeek());
         for (LocalDateTime date : datesBetween) {
-            String[] splitUserId = performanceShiftAddDto.getUserId().split(",");
             for (String s : splitUserId) {
                 PerformanceShift performanceShift = new PerformanceShift();
                 performanceShift.setShift(performanceShiftAddDto.getShift());
                 performanceShift.setUserId(Integer.valueOf(s));
                 performanceShift.setWorkTime(date);
-                baseMapper.insert(performanceShift);
+                String formatterDateTime = date.format(formatter);
+                baseMapper.update(new PerformanceShift(), Wrappers.<PerformanceShift>lambdaUpdate()
+                        .set(PerformanceShift::getShift, performanceShiftAddDto.getShift())
+                        .eq(PerformanceShift::getUserId, s)
+                        .eq(PerformanceShift::getWorkTime, formatterDateTime));
             }
         }
     }
 
     @Override
-    public IPage<Map<String, Object>> performanceShiftPage(Page<Object> page, String time, String userName, String laboratory) {
-        IPage<Map<String, Object>> mapIPage = baseMapper.performanceShiftPage(page, time, userName, laboratory);
+    public Map<String, Object> performanceShiftPage(Page<Object> page, String time, String userName, String laboratory) {
+        IPage<PerformanceShiftMapDto> mapIPage = baseMapper.performanceShiftPage(page, time, userName, laboratory);
         List<Enums> shiftType = enumService.selectEnumByCategory("鐝绫诲瀷");
-        for (Map<String, Object> i : mapIPage.getRecords()) {
-            String[] shiftTimes = i.get("shiftTime").toString().split(";");
+        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<>();
             // 鍒嗗壊鏃ユ湡
@@ -60,34 +106,172 @@
                 Map<Object, Object> hashMap = new HashMap<>();
                 String[] shiftTimeAndShift = shiftTime.split("锛�");
                 for (Enums enums : shiftType) {
-                    int num = 0;
+                    if (!i.getMonthlyAttendance().containsKey(enums.getLabel())) {
+                        i.getMonthlyAttendance().put(enums.getLabel(), 0);
+                    }
                     if (enums.getValue().equals(shiftTimeAndShift[1])) {
-                        num++;
+                        Integer num = (Integer) i.getMonthlyAttendance().get(enums.getLabel());
+                        i.getMonthlyAttendance().put(enums.getLabel(), num += 1);
                     }
-                    i.put(enums.getLabel(), num);
-                    if (!enums.getValue().equals("3") && !enums.getValue().equals("4")) {
-                        i.put("totalAttendance", totalAttendance += num);
-                    }
+                }
+                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("weekly", DateUtil.weekOfYear(DateUtil.parse(shiftTimeAndShift[0])));
                 hashMap.put("time", shiftTimeAndShift[0]);
-                hashMap.put("headerTime", getWeek(shiftTimeAndShift[0]));
                 map.add(hashMap);
             }
-            i.put("list", map);
-            i.remove("shiftTime");
+            int totalYearAttendance = 0;
+            Map<String, Object> hashMap = new HashMap<>();
+            for (Map<String, Object> record : mapYearIPage) {
+                if (record.get("user_id").toString().equals(i.getUserId())) {
+                    for (Enums enums : shiftType) {
+                        if (!hashMap.containsKey(enums.getLabel())) {
+                            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);
+                        }
+                    }
+                    if (record.get("shift").equals("1") || record.get("shift").equals("2") || record.get("shift").equals("0")) {
+                        hashMap.put("totalAttendance", totalYearAttendance += 1);
+                    }
+                }
+            }
+            i.setSidebarAnnualAttendance(hashMap);
+            i.setList(map);
+            i.setShiftTime(null);
+        });
+        // 鑾峰彇header鏃堕棿
+        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
+        DateTimeFormatter formatters = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
+        // 灏嗗瓧绗︿覆鏃堕棿杞崲涓� LocalDateTime 绫诲瀷鏃堕棿
+        LocalDateTime localDateTime = LocalDateTime.parse(time, formatters);
+        LocalDate firstDayOfMonth = localDateTime.toLocalDate().withDayOfMonth(1);
+        LocalDate lastDayOfMonth = localDateTime.toLocalDate().with(TemporalAdjusters.lastDayOfMonth());
+        List<LocalDateTime> localDateTimesBetween = getLocalDateTimesBetween(firstDayOfMonth.atStartOfDay(), lastDayOfMonth.atStartOfDay());
+        List<Object> list1 = new ArrayList<>();
+        for (LocalDateTime dateTime : localDateTimesBetween) {
+            Map<Object, Object> hashMap = new HashMap<>();
+            DateTime parse = DateUtil.parse(dateTime.format(formatter));
+            hashMap.put("weekly", DateUtil.weekOfYear(DateUtil.offsetDay(parse, 1)));
+            hashMap.put("headerTime", getWeek(dateTime.format(formatters)));
+            list1.add(hashMap);
         }
-        return mapIPage;
+        Map<String, Object> resultMap = new HashMap<>();
+        resultMap.put("page", mapIPage);
+        resultMap.put("headerList", list1);
+        return resultMap;
     }
 
     @Override
-    public void performanceShiftUpdate(String shift, Integer id) {
-        PerformanceShift performanceShift = new PerformanceShift();
-        performanceShift.setId(id);
-        performanceShift.setShift(shift);
-        baseMapper.updateById(performanceShift);
+    public void performanceShiftUpdate(PerformanceShift performanceShift) {
+        baseMapper.update(new PerformanceShift(), Wrappers.<PerformanceShift>lambdaUpdate()
+                .eq(PerformanceShift::getId, performanceShift.getId())
+                .set(PerformanceShift::getShift, performanceShift.getShift()));
+    }
+
+    @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<>();
+            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 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 -> {
+                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) {
+                    excelRowList.add(month.get(j + "").get(anEnum.getLabel()));
+                }
+            }
+            data.add(excelRowList);
+        }
+        return data;
+    }
+
+    @Override
+    public Map<Object, Object> exportToExcel(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));
+        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;
     }
 
     // 鑾峰彇涓や釜localDateTime鐨勬瘡涓�澶�
@@ -138,4 +322,31 @@
                 return "鏈煡";
         }
     }
+
+    /**
+     * 杩斿洖琛ㄥご
+     *
+     * 澶栧眰List浠h〃琛屽唴灞� List浠h〃鍒�  鐩稿悓鐨勫垪鏁版嵁浼氳涓诲姩鍚堝苟
+     * 鏋勯�犲弻鍒楄〃澶�
+     * @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("鑰冨嫟姹囨��", "濮撳悕", "濮撳悕"));
+        line.add(Arrays.asList("鍑哄嫟璇︽儏", month, "鍑哄嫟"));
+        // 骞� header
+        for (Enums anEnum : enums) {
+            line.add(Arrays.asList("鑰冨嫟姹囨��", month, 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;
+    }
 }

--
Gitblit v1.9.3