From d74c28c6f826c9d9e61612b4bc589b0774feb82a Mon Sep 17 00:00:00 2001
From: Crunchy <3114200645@qq.com>
Date: 星期五, 10 五月 2024 13:30:09 +0800
Subject: [PATCH] 2024-5-9 绩效管理-班次后端开发调整年份完成

---
 inspect-server/src/main/java/com/yuanchu/mom/controller/InsReportController.java               |    2 
 system-run/src/test/java/com/yuanchu/mom/SystemRunApplicationTest.java                         |   20 +++++
 performance-server/src/main/java/com/yuanchu/mom/mapper/PerformanceShiftMapper.java            |    4 +
 performance-server/src/main/resources/mapper/PerformanceShiftMapper.xml                        |   42 ++++++++++
 performance-server/src/main/java/com/yuanchu/mom/service/impl/PerformanceShiftServiceImpl.java |  121 +++++++++++++++++++++++++++--
 pom.xml                                                                                        |    8 ++
 performance-server/src/main/java/com/yuanchu/mom/service/PerformanceShiftService.java          |    5 +
 performance-server/src/main/java/com/yuanchu/mom/controller/PerformanceShiftController.java    |   15 +++
 8 files changed, 204 insertions(+), 13 deletions(-)

diff --git a/inspect-server/src/main/java/com/yuanchu/mom/controller/InsReportController.java b/inspect-server/src/main/java/com/yuanchu/mom/controller/InsReportController.java
index 6a40450..ab508a7 100644
--- a/inspect-server/src/main/java/com/yuanchu/mom/controller/InsReportController.java
+++ b/inspect-server/src/main/java/com/yuanchu/mom/controller/InsReportController.java
@@ -10,7 +10,6 @@
 import com.yuanchu.mom.vo.Result;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
-import lombok.AllArgsConstructor;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.web.bind.annotation.*;
 import org.springframework.web.multipart.MultipartFile;
@@ -23,7 +22,6 @@
 
 @RestController
 @RequestMapping("/insReport")
-@AllArgsConstructor
 @Api("妫�楠屾姤鍛�")
 public class InsReportController {
 
diff --git a/performance-server/src/main/java/com/yuanchu/mom/controller/PerformanceShiftController.java b/performance-server/src/main/java/com/yuanchu/mom/controller/PerformanceShiftController.java
index 65b368a..30f16ae 100644
--- a/performance-server/src/main/java/com/yuanchu/mom/controller/PerformanceShiftController.java
+++ b/performance-server/src/main/java/com/yuanchu/mom/controller/PerformanceShiftController.java
@@ -34,10 +34,16 @@
         return Result.success();
     }
 
-    @ApiOperation(value = "缁╂晥绠$悊-鐝-鍒嗛〉鏌ヨ")
+    @ApiOperation(value = "缁╂晥绠$悊-鐝-鏈堜唤鍒嗛〉鏌ヨ")
     @PostMapping("page")
     public Result<?> performanceShiftPage(Integer size, Integer current, String time, String userName, String laboratory) {
         return Result.success(performanceShiftService.performanceShiftPage(new Page<>(current, size), time, userName, laboratory));
+    }
+
+    @ApiOperation(value = "缁╂晥绠$悊-鐝-骞翠唤鍒嗛〉鏌ヨ")
+    @PostMapping("pageYear")
+    public Result<?> performanceShiftPageYear(Integer size, Integer current, String time, String userName, String laboratory) {
+        return Result.success(performanceShiftService.performanceShiftPageYear(new Page<>(current, size), time, userName, laboratory));
     }
 
     @ApiOperation(value = "缁╂晥绠$悊-鐝-鐝鐘舵�佷慨鏀�")
@@ -46,4 +52,11 @@
         performanceShiftService.performanceShiftUpdate(performanceShift);
         return Result.success();
     }
+
+    @ApiOperation(value = "缁╂晥绠$悊-鐝-瀵煎嚭")
+    @GetMapping("update")
+    public Result<?> exportToExcel(String time, String userName, String laboratory) {
+        performanceShiftService.exportToExcel(time, userName, laboratory);
+        return Result.success();
+    }
 }
diff --git a/performance-server/src/main/java/com/yuanchu/mom/mapper/PerformanceShiftMapper.java b/performance-server/src/main/java/com/yuanchu/mom/mapper/PerformanceShiftMapper.java
index f9ef663..2623b39 100644
--- a/performance-server/src/main/java/com/yuanchu/mom/mapper/PerformanceShiftMapper.java
+++ b/performance-server/src/main/java/com/yuanchu/mom/mapper/PerformanceShiftMapper.java
@@ -30,4 +30,8 @@
     List<Map<String, Object>> performanceShiftYearPage(@Param("time") String time,
                                                        @Param("userName") String userName,
                                                        @Param("laboratory") String laboratory);
+
+    IPage<Map<String, Object>> performanceShiftYear(Page<Object> page, String time, String userName, String laboratory);
+
+    List<Map<String, Object>> performanceShiftYearList(String time, String userName, String laboratory);
 }
diff --git a/performance-server/src/main/java/com/yuanchu/mom/service/PerformanceShiftService.java b/performance-server/src/main/java/com/yuanchu/mom/service/PerformanceShiftService.java
index 67ad72a..25cd719 100644
--- a/performance-server/src/main/java/com/yuanchu/mom/service/PerformanceShiftService.java
+++ b/performance-server/src/main/java/com/yuanchu/mom/service/PerformanceShiftService.java
@@ -1,5 +1,6 @@
 package com.yuanchu.mom.service;
 
+import com.baomidou.mybatisplus.core.metadata.IPage;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.yuanchu.mom.dto.PerformanceShiftAddDto;
@@ -22,4 +23,8 @@
     Map<String, Object> performanceShiftPage(Page<Object> page, String time, String userName, String laboratory);
 
     void performanceShiftUpdate(PerformanceShift performanceShift);
+
+    IPage<Map<String, Object>> performanceShiftPageYear(Page<Object> page, String time, String userName, String laboratory);
+
+    Map<Object, Object> exportToExcel(String time, String userName, String laboratory);
 }
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..b0f4c58 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;
@@ -23,6 +24,7 @@
 import java.time.format.DateTimeFormatter;
 import java.time.temporal.TemporalAdjusters;
 import java.util.*;
+import java.util.stream.Collectors;
 
 /**
  * <p>
@@ -171,16 +173,90 @@
                 .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) {
@@ -230,4 +306,29 @@
                 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("鑰冨嫟姹囨��", "濮撳悕", "濮撳悕"));
+        // 骞� header
+        for (Enums anEnum : enums) {
+            line.add(Arrays.asList("鑰冨嫟姹囨��", month, anEnum.getLabel()));
+        }
+        // 鏈坔eader
+        for (int i = 1; i < 13; i++) {
+            for (Enums anEnum : enums) {
+                line.add(Arrays.asList("鍑哄嫟璇︽儏", i + " 鏈�", anEnum.getLabel()));
+            }
+        }
+        return line;
+    }
 }
diff --git a/performance-server/src/main/resources/mapper/PerformanceShiftMapper.xml b/performance-server/src/main/resources/mapper/PerformanceShiftMapper.xml
index 29c9d73..0b702be 100644
--- a/performance-server/src/main/resources/mapper/PerformanceShiftMapper.xml
+++ b/performance-server/src/main/resources/mapper/PerformanceShiftMapper.xml
@@ -43,4 +43,46 @@
         <if test="laboratory != null and laboratory != ''">
         </if>
     </select>
+
+    <select id="performanceShiftYear" resultType="java.util.Map">
+        SELECT if(u.department is not null and u.department != '', CONCAT(u.name, '锛�', u.department, '锛�'), u.name) name,
+        s.user_id, u.account,
+        DATE_FORMAT(s.work_time, '%c') work_time,
+        GROUP_CONCAT(DATE_FORMAT(s.work_time, '%c'), '锛�', s.shift order by s.work_time SEPARATOR ';') month_str
+        FROM performance_shift s
+        LEFT JOIN user u on u.id = s.user_id
+        where s.shift is not NULL
+        and s.shift != ''
+        <if test="time != null and time != ''">
+            and DATE_FORMAT(s.work_time, '%Y') = DATE_FORMAT(#{time}, '%Y' )
+        </if>
+        <if test="userName != null and userName != ''">
+            and u.name like concat('%', #{userName}, '%')
+        </if>
+        <if test="laboratory != null and laboratory != ''">
+        </if>
+        GROUP BY u.id
+        order by s.work_time
+    </select>
+
+    <select id="performanceShiftYearList" resultType="map">
+        SELECT if(u.department is not null and u.department != '', CONCAT(u.name, '锛�', u.department, '锛�'), u.name) name,
+        s.user_id, u.account,
+        DATE_FORMAT(s.work_time, '%c') work_time,
+        GROUP_CONCAT(DATE_FORMAT(s.work_time, '%c'), '锛�', s.shift order by s.work_time SEPARATOR ';') month_str
+        FROM performance_shift s
+        LEFT JOIN user u on u.id = s.user_id
+        where s.shift is not NULL
+        and s.shift != ''
+        <if test="time != null and time != ''">
+            and DATE_FORMAT(s.work_time, '%Y') = DATE_FORMAT(#{time}, '%Y' )
+        </if>
+        <if test="userName != null and userName != ''">
+            and u.name like concat('%', #{userName}, '%')
+        </if>
+        <if test="laboratory != null and laboratory != ''">
+        </if>
+        GROUP BY u.id
+        order by s.work_time
+    </select>
 </mapper>
diff --git a/pom.xml b/pom.xml
index e129ed5..766c9d9 100644
--- a/pom.xml
+++ b/pom.xml
@@ -37,12 +37,20 @@
         <feign-okhttp.version>11.0</feign-okhttp.version>
         <shiro.version>1.5.3</shiro.version>
         <freemwork.version>2.3.32</freemwork.version>
+        <easyexcel.version>3.3.2</easyexcel.version>
         <!--鎻愪氦鍓�-->
         <!-- 鎵撳寘鍚庡鍑虹殑璺緞 -->
         <package.path>${project.build.directory}/BLOG</package.path>
     </properties>
 
     <dependencies>
+        <!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>easyexcel</artifactId>
+            <version>${easyexcel.version}</version>
+        </dependency>
+
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
diff --git a/system-run/src/test/java/com/yuanchu/mom/SystemRunApplicationTest.java b/system-run/src/test/java/com/yuanchu/mom/SystemRunApplicationTest.java
index a34819b..1f22691 100644
--- a/system-run/src/test/java/com/yuanchu/mom/SystemRunApplicationTest.java
+++ b/system-run/src/test/java/com/yuanchu/mom/SystemRunApplicationTest.java
@@ -1,13 +1,33 @@
 package com.yuanchu.mom;
 
+import com.yuanchu.mom.service.PerformanceShiftService;
 import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
+
+import java.util.Map;
 
 @SpringBootTest
 class SystemRunApplicationTest {
 
+    private static String fileName = "D:\\" + System.currentTimeMillis() + ".xlsx";
+
+    @Autowired
+    private PerformanceShiftService performanceShiftService;
 
     @Test
     void  contextLoads() {
+        Map<Object, Object> header = performanceShiftService.exportToExcel("2024-05-04 00:00:00", null, null);
+
+//        EasyExcel.write(fileName)
+//                .head(header)
+//                // 鍚堝苟绛栫暐锛氬悎骞剁浉鍚屾暟鎹殑琛屻�傜涓�涓弬鏁拌〃绀轰粠鍝竴琛屽紑濮嬭繘琛屽悎骞讹紝鐢变簬琛ㄥご鍗犱簡涓よ锛屽洜姝や粠绗�2琛屽紑濮嬶紙绱㈠紩浠�0寮�濮嬶級
+//                // 绗簩涓弬鏁版槸鎸囧畾鍝簺鍒楄杩涜鍚堝苟
+////                .registerWriteHandler(new MergeSameRowsStrategy(2, new int[]{0, 1, 2, 3, 8, 9}))
+//                // 娉ㄦ剰锛氶渶瑕佸厛璋冪敤registerWriteHandler()鍐嶈皟鐢╯heet()鏂规硶鎵嶈兘浣垮悎骞剁瓥鐣ョ敓鏁堬紒锛侊紒
+//                .sheet("妯℃澘")
+//                .doWrite(data());
     }
+
+
 }

--
Gitblit v1.9.3