From 3e144b2eb8fc9447d2d092edb71db533d12abc52 Mon Sep 17 00:00:00 2001
From: liu <2021943741@qq.com>
Date: 星期四, 24 九月 2026 19:06:39 +0800
Subject: [PATCH] feat(生产核算): 新增大类「中提琴」并打通工资/核算折算

---
 src/main/java/com/ruoyi/staff/service/impl/StaffSalaryMainServiceImpl.java |  557 ++++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 445 insertions(+), 112 deletions(-)

diff --git a/src/main/java/com/ruoyi/staff/service/impl/StaffSalaryMainServiceImpl.java b/src/main/java/com/ruoyi/staff/service/impl/StaffSalaryMainServiceImpl.java
index f9a5d21..367913c 100644
--- a/src/main/java/com/ruoyi/staff/service/impl/StaffSalaryMainServiceImpl.java
+++ b/src/main/java/com/ruoyi/staff/service/impl/StaffSalaryMainServiceImpl.java
@@ -5,6 +5,8 @@
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.ruoyi.common.utils.StringUtils;
 import com.ruoyi.common.utils.poi.ExcelUtil;
+import com.ruoyi.basic.mapper.ProductMapper;
+import com.ruoyi.basic.pojo.Product;
 import com.ruoyi.framework.web.domain.AjaxResult;
 import com.ruoyi.project.system.domain.SysDept;
 import com.ruoyi.project.system.mapper.SysDeptMapper;
@@ -13,6 +15,7 @@
 import com.ruoyi.production.mapper.ProductionProductMainMapper;
 import com.ruoyi.production.bean.dto.ProductionAccountDto;
 import com.ruoyi.production.bean.dto.ProductionProductMainDto;
+import com.ruoyi.production.enums.MajorCategoryEnum;
 import com.ruoyi.production.pojo.ProductionAccount;
 import com.ruoyi.technology.mapper.TechnologyOperationMapper;
 import com.ruoyi.technology.pojo.TechnologyOperation;
@@ -49,6 +52,7 @@
 import java.time.YearMonth;
 import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.ss.util.CellRangeAddress;
+import org.apache.poi.ss.util.RegionUtil;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import java.io.IOException;
 import java.text.SimpleDateFormat;
@@ -73,6 +77,7 @@
     private final StaffLeaveMapper staffLeaveMapper;
     private final StaffContractMapper staffContractMapper;
     private final SysDeptMapper sysDeptMapper;
+    private final ProductMapper productMapper;
     private final ProductionAccountMapper productionAccountMapper;
     private final ProductionProductMainMapper productionProductMainMapper;
     private final TechnologyOperationMapper technologyOperationMapper;
@@ -308,14 +313,35 @@
                 exportData.setSalaryMonth(main.getSalaryMonth());
                 exportData.setSalaryTitle(main.getSalaryTitle());
                 exportData.setStaffName(detail.getStaffName());
+                if (detail.getEntryDate() != null) {
+                    exportData.setEntryDate(detail.getEntryDate());
+                } else if (detail.getStaffOnJobId() != null) {
+                    StaffContract staffContract = staffContractMapper.selectOne(new LambdaQueryWrapper<StaffContract>()
+                            .eq(StaffContract::getStaffOnJobId, detail.getStaffOnJobId())
+                            .orderByAsc(StaffContract::getContractStartTime)
+                            .orderByAsc(StaffContract::getId)
+                            .last("limit 1"));
+                    if (staffContract != null && staffContract.getContractStartTime() != null) {
+                        exportData.setEntryDate(formatEntryDate(staffContract.getContractStartTime()));
+                    }
+                }
                 exportData.setDeptName(detail.getDeptName());
                 exportData.setBasicSalary(detail.getBasicSalary());
                 exportData.setAttendanceBonus(detail.getAttendanceBonus());
-                exportData.setPieceSalary(detail.getPieceSalary());
-                exportData.setHourlySalary(detail.getHourlySalary());
+                exportData.setDailySalary(detail.getBasicSalary() != null && detail.getWorkDays() != null && detail.getWorkDays() > 0
+                        ? detail.getBasicSalary().divide(new BigDecimal(detail.getWorkDays()), 3, java.math.RoundingMode.HALF_UP)
+                        : null);
+                exportData.setWorkDays(detail.getWorkDays());
+                exportData.setConversionRatio(detail.getConversionRatio() == null
+                        ? null
+                        : detail.getConversionRatio().setScale(4, java.math.RoundingMode.HALF_UP));
                 exportData.setMonthTimeTotal(detail.getMonthTimeTotal());
-                exportData.setConversionRatio(detail.getConversionRatio());
                 exportData.setOtherIncome(detail.getOtherIncome());
+                exportData.setPersonalLeave(detail.getPersonalLeave());
+                exportData.setSickLeave(detail.getSickLeave());
+                exportData.setBereavementLeave(detail.getBereavementLeave());
+                exportData.setMarriageLeave(detail.getMarriageLeave());
+                exportData.setAllowance(detail.getAllowance());
                 exportData.setSocialPersonal(detail.getSocialPersonal());
                 exportData.setFundPersonal(detail.getFundPersonal());
                 exportData.setOtherDeduct(detail.getOtherDeduct());
@@ -336,7 +362,7 @@
     /**
      * 姹囨�诲憳宸ュ綋鏈堟湀鏃堕棿鍚堣銆�
      * 瑙勫垯锛�
-     * 1. 灏忔彁鐞� / 澶ф彁鐞� / 璐濇柉锛氭湀浜ф姌鍚堝皬鎻愮惔鏁伴噺 / 宸ュ簭鏍囧噯鏁伴噺 脳 8 + 淇ˉ鐞嗗伐鏃�
+     * 1. 灏忔彁鐞� / 涓彁鐞� / 澶ф彁鐞� / 璐濇柉锛氭湀浜ф姌鍚堝皬鎻愮惔鏁伴噺 / 宸ュ簭鏍囧噯鏁伴噺 脳 8 + 淇ˉ鐞嗗伐鏃�
      * 2. 鍏朵粬浜у搧锛氬綋鏈堝疄闄呬骇閲� / 瀵瑰簲浜у搧鏍囧噯鏁伴噺 脳 8 + 淇ˉ鐞嗗伐鏃�
      * 鏈�缁堢粨鏋滃氨鏄伐璧勮〃閲岀殑鏈堟椂闂村悎璁°��
      */
@@ -355,11 +381,13 @@
             java.math.BigDecimal productStandardQuantity = toDecimal(item.get("productStandardQuantity"));
             java.math.BigDecimal violinFactor = toDecimal(item.get("violinFactor"));
             java.math.BigDecimal violaFactor = toDecimal(item.get("violaFactor"));
+            java.math.BigDecimal midViolinFactor = toDecimal(item.get("midViolinFactor"));
             java.math.BigDecimal bassFactor = toDecimal(item.get("bassFactor"));
-            String productName = item.get("productName") == null ? null : String.valueOf(item.get("productName"));
+            // 褰掔被渚濇嵁鐢ㄦ姤宸ユ椂宸插綊闆嗗ソ鐨勫ぇ绫伙紝涓嶅啀鎷夸骇鍝佸悕绉板仛瀛楃涓叉瘮杈�
+            String majorCategory = item.get("majorCategory") == null ? null : String.valueOf(item.get("majorCategory"));
             java.math.BigDecimal rowTotal = calculateOperationTime(
                     operationType,
-                    productName,
+                    majorCategory,
                     finishedNum,
                     workHour,
                     repairWorkHour,
@@ -367,6 +395,7 @@
                     productStandardQuantity,
                     violinFactor,
                     violaFactor,
+                    midViolinFactor,
                     bassFactor
             );
             total = total.add(rowTotal).setScale(2, java.math.RoundingMode.HALF_UP);
@@ -377,11 +406,11 @@
     /**
      * 璁$畻鍗曟潯宸ュ簭鐨勬姌绠楀伐鏃躲��
      * 杩欓噷鏄湀鏃堕棿鍚堣鐨勬牳蹇冨垎鏀細
-     * - 灏忔彁鐞� / 澶ф彁鐞� / 璐濇柉锛氭湀浜ф姌鍚堝皬鎻愮惔鏁伴噺 / 宸ュ簭鏍囧噯鏁伴噺 脳 8 + 淇ˉ鐞嗗伐鏃讹紱
-     * - 鍏朵粬浜у搧锛氬綋鏈堝疄闄呬骇閲� / 瀵瑰簲浜у搧鏍囧噯鏁伴噺 脳 8 + 淇ˉ鐞嗗伐鏃躲��
+     * - 灏忔彁鐞� / 涓彁鐞� / 澶ф彁鐞� / 璐濇柉锛氭湀浜ф姌鍚堝皬鎻愮惔鏁伴噺 / 宸ュ簭鏍囧噯鏁伴噺 脳 8 + 淇ˉ鐞嗗伐鏃讹紱
+     * - 鍏朵粬浜у搧锛堝惈鏈綊绫绘姤宸ワ級锛氬綋鏈堝疄闄呬骇閲� / 瀵瑰簲浜у搧鏍囧噯鏁伴噺 脳 8 + 淇ˉ鐞嗗伐鏃躲��
      */
     private java.math.BigDecimal calculateOperationTime(Integer operationType,
-                                                        String productName,
+                                                        String majorCategory,
                                                         java.math.BigDecimal finishedNum,
                                                         java.math.BigDecimal workHour,
                                                         java.math.BigDecimal repairWorkHour,
@@ -389,12 +418,12 @@
                                                         java.math.BigDecimal productStandardQuantity,
                                                         java.math.BigDecimal violinFactor,
                                                         java.math.BigDecimal violaFactor,
+                                                        java.math.BigDecimal midViolinFactor,
                                                         java.math.BigDecimal bassFactor) {
-        if (isInstrumentProduct(productName)) {
-            java.math.BigDecimal foldedQuantity = calculateFoldedQuantity(
-                    finishedNum,
-                    resolveInstrumentFactor(productName, violinFactor, violaFactor, bassFactor)
-            );
+        java.math.BigDecimal instrumentFactor = resolveInstrumentFactor(
+                majorCategory, violinFactor, violaFactor, midViolinFactor, bassFactor);
+        if (instrumentFactor != null) {
+            java.math.BigDecimal foldedQuantity = calculateFoldedQuantity(finishedNum, instrumentFactor);
             if (standardQuantity.compareTo(java.math.BigDecimal.ZERO) <= 0) {
                 return java.math.BigDecimal.ZERO.setScale(2, java.math.RoundingMode.HALF_UP);
             }
@@ -415,9 +444,8 @@
     }
 
     /**
-     * 鎸変骇鍝佸悕绉板拰閰嶇疆绯绘暟鎶樼畻浜ч噺銆�
-     * 鍙灏忔彁鐞淬�佸ぇ鎻愮惔銆佽礉鏂笁绫讳骇鍝佺敓鏁堬紝
-     * 杩斿洖鐨勬槸鍙敤浜庡悗缁伐搴忔爣鍑嗘暟閲忔崲绠楃殑缁熶竴鎶樺悎浜ч噺銆�
+     * 鎸変骇閲忎笌鎶樼畻鐜囨姌绠椼�傛槸鍚﹀睘浜庝箰鍣ㄧ敱璋冪敤鏂圭敤 {@link MajorCategoryEnum} 鍒ゅ畾锛�
+     * 杩欓噷鍙仛涔樻硶锛岃繑鍥炵殑鏄彲鐢ㄤ簬鍚庣画宸ュ簭鏍囧噯鏁伴噺鎹㈢畻鐨勭粺涓�鎶樺悎浜ч噺銆�
      */
     private java.math.BigDecimal calculateFoldedQuantity(java.math.BigDecimal quantity,
                                                          java.math.BigDecimal factor) {
@@ -427,26 +455,41 @@
         return quantity.multiply(factor).setScale(2, java.math.RoundingMode.HALF_UP);
     }
 
-    private boolean isInstrumentProduct(String productName) {
-        return "灏忔彁鐞�".equals(productName)
-                || "澶ф彁鐞�".equals(productName)
-                || "璐濇柉".equals(productName);
+    /**
+     * 鍒ゆ柇鎶ュ伐澶х被鏄惁灞炰簬涔愬櫒銆傛湭褰掔被锛坽@code major_category} 涓� null锛�
+     * 濡傞浂浠�/鍘熸枡绫诲伐搴忎骇鍑猴級杩斿洖 false锛岀敱璋冪敤鏂硅蛋鈥滃叾浠栦骇鍝佲�濆垎鏀��
+     */
+    private boolean isInstrumentCategory(String majorCategory) {
+        return MajorCategoryEnum.isInstrument(majorCategory);
     }
 
-    private java.math.BigDecimal resolveInstrumentFactor(String productName,
+    /**
+     * 鎸夋姤宸ュぇ绫诲彇瀵瑰簲鎶樼畻鐜囥��
+     *
+     * @return {@code null} 琛ㄧず涓嶆槸涔愬櫒澶х被锛堝惈鏈綊绫伙級锛岃皟鐢ㄦ柟鎹璧扳�滃叾浠栦骇鍝佲�濆垎鏀�
+     */
+    private java.math.BigDecimal resolveInstrumentFactor(String majorCategory,
                                                          java.math.BigDecimal violinFactor,
                                                          java.math.BigDecimal violaFactor,
+                                                         java.math.BigDecimal midViolinFactor,
                                                          java.math.BigDecimal bassFactor) {
-        if ("灏忔彁鐞�".equals(productName)) {
-            return violinFactor;
+        MajorCategoryEnum category = MajorCategoryEnum.of(majorCategory);
+        if (category == null) {
+            return null;
         }
-        if ("澶ф彁鐞�".equals(productName)) {
-            return violaFactor;
+        switch (category) {
+            case VIOLIN:
+                return violinFactor;
+            case VIOLA:
+                return midViolinFactor;
+            case CELLO:
+                // violaFactor 鐨勫垪鍚嶆部鐢ㄤ簡 viola锛堜腑鎻愮惔锛夛紝鍘嗗彶璇箟瀹炰负銆愬ぇ鎻愮惔銆戞姌绠楃巼锛屽嬁鏀�
+                return violaFactor;
+            case BASS:
+                return bassFactor;
+            default:
+                return null;
         }
-        if ("璐濇柉".equals(productName)) {
-            return bassFactor;
-        }
-        return java.math.BigDecimal.ZERO.setScale(2, java.math.RoundingMode.HALF_UP);
     }
 
     private String formatEntryDate(java.util.Date entryDate) {
@@ -475,85 +518,32 @@
 
     @Override
     public void exportWorkHours(HttpServletResponse response, String salaryMonth) {
-        List<ProductionProductMainDto> accountList = queryProductionDetails(salaryMonth);
-        List<String> operationNames = accountList.stream()
-                .map(ProductionProductMainDto::getProcess)
-                .filter(StringUtils::isNotEmpty)
-                .distinct()
-                .sorted()
-                .collect(Collectors.toList());
         Workbook workbook = new XSSFWorkbook();
         CellStyle titleStyle = createStyle(workbook, true, HorizontalAlignment.CENTER, 14, false);
-        CellStyle headerStyle = createStyle(workbook, false, HorizontalAlignment.CENTER, 11, false);
+        CellStyle headerStyle = createStyle(workbook, false, HorizontalAlignment.CENTER, 11, true);
         CellStyle labelStyle = createStyle(workbook, false, HorizontalAlignment.CENTER, 10, false);
         CellStyle numStyle = createStyle(workbook, false, HorizontalAlignment.CENTER, 10, false);
         DataFormat dataFormat = workbook.createDataFormat();
         numStyle.setDataFormat(dataFormat.getFormat("0.00"));
 
-        if (CollectionUtils.isEmpty(operationNames)) {
-            operationNames = new ArrayList<>();
+        List<String> monthList = new ArrayList<>();
+        if (StringUtils.isNotEmpty(salaryMonth)) {
+            monthList.add(salaryMonth);
+        } else {
+            monthList = productionAccountMapper.selectAccountingMonths();
         }
-
-        int sheetIndex = 1;
-        for (String operationName : operationNames) {
-            TechnologyOperation operation = technologyOperationMapper.selectOne(
-                    new LambdaQueryWrapper<TechnologyOperation>().eq(TechnologyOperation::getName, operationName).last("limit 1"));
-            String sheetName = operationName;
-            if (sheetName.length() > 31) {
-                sheetName = sheetName.substring(0, 31);
-            }
-            Sheet sheet = workbook.createSheet(sheetName);
-            int rowIdx = 0;
-            Row titleRow = sheet.createRow(rowIdx++);
-            titleRow.setHeightInPoints(52.5f);
-            titleRow.createCell(0).setCellValue(operationName + "宸ユ椂缁熻");
-            titleRow.getCell(0).setCellStyle(titleStyle);
-            sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 3));
-
-            Row workDayRow = sheet.createRow(rowIdx++);
-            workDayRow.createCell(0).setCellValue("宸ヤ綔鏃�");
-            workDayRow.getCell(0).setCellStyle(headerStyle);
-            Integer monthWorkDays = calculateWorkDays(salaryMonth);
-            createNumberCell(workDayRow, 2, monthWorkDays == null ? null : new java.math.BigDecimal(monthWorkDays), headerStyle);
-            workDayRow.createCell(3).setCellValue("澶�");
-            workDayRow.getCell(3).setCellStyle(headerStyle);
-            createNumberCell(workDayRow, 4, monthWorkDays == null ? null : new java.math.BigDecimal(monthWorkDays * 8), headerStyle);
-            workDayRow.createCell(5).setCellValue("灏忔椂");
-            workDayRow.getCell(5).setCellStyle(headerStyle);
-
-            Row headRow = sheet.createRow(rowIdx++);
-            headRow.createCell(0).setCellValue("浜哄憳");
-            headRow.getCell(0).setCellStyle(headerStyle);
-            headRow.createCell(1).setCellValue("鏈堜骇閲�");
-            headRow.getCell(1).setCellStyle(headerStyle);
-            headRow.createCell(2).setCellValue("鎶樼畻鏃堕暱");
-            headRow.getCell(2).setCellStyle(headerStyle);
-            headRow.createCell(3).setCellValue("宸ヨ祫");
-            headRow.getCell(3).setCellStyle(headerStyle);
-
-            Map<String, java.math.BigDecimal> standardMap = buildStandardQuantityMap(List.of(operationName));
-            Map<String, java.math.BigDecimal> opQtyMap = buildOperationQuantityMap(accountList, operationName, operation);
-            Set<String> employeeNames = accountList.stream()
-                    .filter(item -> operationName.equals(item.getProcess()))
-                    .map(ProductionProductMainDto::getSchedulingUserName)
-                    .filter(StringUtils::isNotEmpty)
-                    .collect(Collectors.toCollection(LinkedHashSet::new));
-            for (String staffName : employeeNames) {
-                Row row = sheet.createRow(rowIdx++);
-                row.createCell(0).setCellValue(staffName);
-                row.getCell(0).setCellStyle(labelStyle);
-                java.math.BigDecimal qty = opQtyMap.getOrDefault(staffName, java.math.BigDecimal.ZERO);
-                createNumberCell(row, 1, qty, numStyle);
-                java.math.BigDecimal standardQty = standardMap.getOrDefault(operationName, java.math.BigDecimal.ONE);
-                createNumberCell(row, 2, standardQty.compareTo(java.math.BigDecimal.ZERO) == 0 ? java.math.BigDecimal.ZERO : qty.divide(standardQty, 2, java.math.RoundingMode.HALF_UP).multiply(new java.math.BigDecimal("8")), numStyle);
-                java.math.BigDecimal wage = calculateSheetWage(operation, operationName, qty, standardQty);
-                createNumberCell(row, 3, wage, numStyle);
-            }
-
-            for (int i = 0; i <= 3; i++) {
-                sheet.autoSizeColumn(i);
-            }
-            sheetIndex++;
+        if (CollectionUtils.isEmpty(monthList)) {
+            throw new IllegalArgumentException("娌℃湁鍙鍑虹殑鐢熶骇鏍哥畻鏈堜唤");
+        }
+        Sheet nonInstrumentSheet = workbook.createSheet("鐞寸洅閮�");
+        Sheet instrumentSheet = workbook.createSheet("鎴愬搧閮�");
+        int nonInstrumentRowIdx = 0;
+        int instrumentRowIdx = 0;
+        for (String month : monthList) {
+            List<ProductionProductMainDto> accountList = queryProductionDetails(month);
+            Integer monthWorkDays = calculateWorkDays(month);
+            nonInstrumentRowIdx = appendWorkHoursSection(nonInstrumentSheet, nonInstrumentRowIdx, month, false, accountList, monthWorkDays, titleStyle, headerStyle, labelStyle, numStyle, nonInstrumentRowIdx > 0);
+            instrumentRowIdx = appendWorkHoursSection(instrumentSheet, instrumentRowIdx, month, true, accountList, monthWorkDays, titleStyle, headerStyle, labelStyle, numStyle, instrumentRowIdx > 0);
         }
 
         try {
@@ -569,11 +559,319 @@
     }
 
     public List<Map<String, Object>> setSchemeApplicableStaffUserInfo(List<Long> ids) {
-        if(CollectionUtils.isEmpty(ids)){
+        if (CollectionUtils.isEmpty(ids)) {
             return new ArrayList<>();
         }
         // 閫氳繃閮ㄩ棬鑾峰彇浜哄憳id
         return sysUserDeptMapper.setSchemeApplicableStaffUserInfo(ids);
+    }
+
+    private int appendWorkHoursSection(Sheet sheet,
+                                       int startRowIdx,
+                                       String salaryMonth,
+                                       boolean instrumentSheet,
+                                       List<ProductionProductMainDto> accountList,
+                                       Integer monthWorkDays,
+                                       CellStyle titleStyle,
+                                       CellStyle headerStyle,
+                                       CellStyle labelStyle,
+                                       CellStyle numStyle,
+                                       boolean addSectionTitle) {
+        int rowIdx = startRowIdx;
+        List<Map<String, Object>> columns = buildWorkHourColumns(instrumentSheet);
+        int lastCol = 1 + columns.size() + 2;
+        Row titleRow = sheet.createRow(rowIdx++);
+        titleRow.setHeightInPoints(56f);
+        String sectionTitle = (StringUtils.isEmpty(salaryMonth) ? "鏈煡鏈堜唤" : salaryMonth) + "宸ユ椂缁熻";
+        titleRow.createCell(0).setCellValue(sectionTitle);
+        titleRow.getCell(0).setCellStyle(titleStyle);
+        sheet.addMergedRegion(new CellRangeAddress(titleRow.getRowNum(), titleRow.getRowNum(), 0, lastCol));
+
+        Row workDayRow = sheet.createRow(rowIdx++);
+        createTextCell(workDayRow, 0, "宸ヤ綔鏃�", numStyle);
+        createDisplayNumberCell(workDayRow, 1, monthWorkDays == null ? null : new java.math.BigDecimal(monthWorkDays), numStyle);
+        createTextCell(workDayRow, 2, "澶�", numStyle);
+        createDisplayNumberCell(workDayRow, 3, monthWorkDays == null ? null : new java.math.BigDecimal(monthWorkDays * 8), numStyle);
+        createTextCell(workDayRow, 4, "灏忔椂", numStyle);
+
+        Row headRow = sheet.createRow(rowIdx++);
+        Row standardRow = sheet.createRow(rowIdx++);
+        createTextCell(headRow, 0, "浜哄憳", numStyle);
+        createTextCell(headRow, 1, "鏍囧噯鏁伴噺", numStyle);
+        CellRangeAddress personRegion = new CellRangeAddress(headRow.getRowNum(), standardRow.getRowNum(), 0, 0);
+        CellRangeAddress standardRegion = new CellRangeAddress(headRow.getRowNum(), standardRow.getRowNum(), 1, 1);
+        sheet.addMergedRegion(personRegion);
+        sheet.addMergedRegion(standardRegion);
+        RegionUtil.setBorderTop(BorderStyle.THIN, personRegion, sheet);
+        RegionUtil.setBorderBottom(BorderStyle.THIN, personRegion, sheet);
+        RegionUtil.setBorderLeft(BorderStyle.THIN, personRegion, sheet);
+        RegionUtil.setBorderRight(BorderStyle.THIN, personRegion, sheet);
+        RegionUtil.setBorderTop(BorderStyle.THIN, standardRegion, sheet);
+        RegionUtil.setBorderBottom(BorderStyle.THIN, standardRegion, sheet);
+        RegionUtil.setBorderLeft(BorderStyle.THIN, standardRegion, sheet);
+        RegionUtil.setBorderRight(BorderStyle.THIN, standardRegion, sheet);
+        int col = 2;
+        for (Map<String, Object> column : columns) {
+            createTextCell(headRow, col, String.valueOf(column.get("label")), numStyle);
+            createDisplayNumberCell(standardRow, col, (java.math.BigDecimal) column.get("standardQty"), numStyle);
+            col++;
+        }
+        createTextCell(headRow, col, "鏈堟椂闂村悎璁�", numStyle);
+        createTextCell(standardRow, col, "", numStyle);
+        col++;
+        createTextCell(headRow, col, "鎶樺悎鍊嶆暟", numStyle);
+        createTextCell(standardRow, col, "", numStyle);
+
+        Map<String, List<ProductionProductMainDto>> grouped = accountList.stream()
+                .filter(item -> item != null && item.getSchedulingUserName() != null)
+                .filter(item -> instrumentSheet == isInstrumentCategory(item.getMajorCategory()))
+                .sorted(java.util.Comparator
+                        .comparing(ProductionProductMainDto::getSchedulingUserName, java.util.Comparator.nullsLast(String::compareTo))
+                        .thenComparing(item -> StringUtils.isEmpty(item.getProductName()) ? "" : item.getProductName())
+                        .thenComparing(item -> StringUtils.isEmpty(item.getProcess()) ? "" : item.getProcess()))
+                .collect(Collectors.groupingBy(
+                        ProductionProductMainDto::getSchedulingUserName,
+                        LinkedHashMap::new,
+                        Collectors.toList()
+                ));
+
+        for (Map.Entry<String, List<ProductionProductMainDto>> entry : grouped.entrySet()) {
+            String staffName = entry.getKey();
+            List<ProductionProductMainDto> staffItems = entry.getValue();
+            java.math.BigDecimal staffMonthTimeTotal = java.math.BigDecimal.ZERO.setScale(2, java.math.RoundingMode.HALF_UP);
+            Map<String, java.math.BigDecimal> quantityMap = new LinkedHashMap<>();
+            Map<String, java.math.BigDecimal> convertedMap = new LinkedHashMap<>();
+            Map<String, java.math.BigDecimal> foldedMap = new LinkedHashMap<>();
+            Map<String, java.math.BigDecimal> violinQuantityMap = new LinkedHashMap<>();
+            // 鍙橀噺鍚嶆寜瀹為檯涔愬櫒鍛藉悕锛歮idViolin=涓彁鐞淬�乧ello=澶ф彁鐞达紙娉ㄦ剰鍕夸笌鍘嗗彶鍒楀悕 viola_factor 娣锋穯锛�
+            Map<String, java.math.BigDecimal> midViolinQuantityMap = new LinkedHashMap<>();
+            Map<String, java.math.BigDecimal> celloQuantityMap = new LinkedHashMap<>();
+            Map<String, java.math.BigDecimal> bassQuantityMap = new LinkedHashMap<>();
+            for (Map<String, Object> column : columns) {
+                String key = String.valueOf(column.get("key"));
+                quantityMap.put(key, java.math.BigDecimal.ZERO.setScale(2, java.math.RoundingMode.HALF_UP));
+                convertedMap.put(key, java.math.BigDecimal.ZERO.setScale(2, java.math.RoundingMode.HALF_UP));
+                foldedMap.put(key, java.math.BigDecimal.ZERO.setScale(2, java.math.RoundingMode.HALF_UP));
+                violinQuantityMap.put(key, java.math.BigDecimal.ZERO.setScale(2, java.math.RoundingMode.HALF_UP));
+                midViolinQuantityMap.put(key, java.math.BigDecimal.ZERO.setScale(2, java.math.RoundingMode.HALF_UP));
+                celloQuantityMap.put(key, java.math.BigDecimal.ZERO.setScale(2, java.math.RoundingMode.HALF_UP));
+                bassQuantityMap.put(key, java.math.BigDecimal.ZERO.setScale(2, java.math.RoundingMode.HALF_UP));
+            }
+            for (ProductionProductMainDto item : staffItems) {
+                String key = instrumentSheet ? item.getProcess() : item.getProductName();
+                if (!quantityMap.containsKey(key)) {
+                    continue;
+                }
+                java.math.BigDecimal quantity = item.getQuantity() == null ? java.math.BigDecimal.ZERO : item.getQuantity();
+                TechnologyOperation operation = technologyOperationMapper.selectOne(
+                        new LambdaQueryWrapper<TechnologyOperation>()
+                                .eq(TechnologyOperation::getName, item.getProcess())
+                                .last("limit 1"));
+                java.math.BigDecimal standardQty = instrumentSheet
+                        ? (operation == null || operation.getStandardQuantity() == null ? java.math.BigDecimal.ZERO : operation.getStandardQuantity())
+                        : (item.getStandardQuantity() == null ? java.math.BigDecimal.ZERO : item.getStandardQuantity());
+                java.math.BigDecimal foldedQty = instrumentSheet ? calculateFoldedQuantity(item, operation) : quantity;
+                java.math.BigDecimal convertedHours = calculateExportConvertedHours(foldedQty, standardQty, item.getRepairWorkHour());
+                quantityMap.merge(key, quantity, java.math.BigDecimal::add);
+                foldedMap.merge(key, foldedQty, java.math.BigDecimal::add);
+                convertedMap.merge(key, convertedHours, java.math.BigDecimal::add);
+                if (instrumentSheet) {
+                    MajorCategoryEnum category = MajorCategoryEnum.of(item.getMajorCategory());
+                    if (category == MajorCategoryEnum.VIOLIN) {
+                        violinQuantityMap.merge(key, quantity, java.math.BigDecimal::add);
+                    } else if (category == MajorCategoryEnum.VIOLA) {
+                        midViolinQuantityMap.merge(key, quantity, java.math.BigDecimal::add);
+                    } else if (category == MajorCategoryEnum.CELLO) {
+                        celloQuantityMap.merge(key, quantity, java.math.BigDecimal::add);
+                    } else if (category == MajorCategoryEnum.BASS) {
+                        bassQuantityMap.merge(key, quantity, java.math.BigDecimal::add);
+                    }
+                }
+                staffMonthTimeTotal = staffMonthTimeTotal.add(convertedHours).setScale(2, java.math.RoundingMode.HALF_UP);
+            }
+
+            if (!staffItems.isEmpty()) {
+                Row qtyRow = sheet.createRow(rowIdx++);
+                qtyRow.setHeightInPoints(24f);
+                createEdgeCell(sheet.getWorkbook(), qtyRow, 0, staffName, numStyle, true, false, true, true);
+                createTextCell(qtyRow, 1, instrumentSheet ? "鏈堜骇灏�" : "鏈堜骇閲�", numStyle);
+                int dataCol = 2;
+                for (Map<String, Object> column : columns) {
+                    String key = String.valueOf(column.get("key"));
+                    createDisplayNumberCell(qtyRow, dataCol++, instrumentSheet ? violinQuantityMap.getOrDefault(key, java.math.BigDecimal.ZERO) : quantityMap.getOrDefault(key, java.math.BigDecimal.ZERO), numStyle);
+                }
+                createDisplayNumberCell(qtyRow, dataCol++, null, numStyle);
+                createDisplayNumberCell(qtyRow, dataCol, null, numStyle);
+
+                if (instrumentSheet) {
+                    Row midViolinRow = sheet.createRow(rowIdx++);
+                    midViolinRow.setHeightInPoints(24f);
+                    createEdgeCell(sheet.getWorkbook(), midViolinRow, 0, "", numStyle, false, false, true, true);
+                    createTextCell(midViolinRow, 1, "鏈堜骇涓�", numStyle);
+                    dataCol = 2;
+                    for (Map<String, Object> column : columns) {
+                        String key = String.valueOf(column.get("key"));
+                        createDisplayNumberCell(midViolinRow, dataCol++, midViolinQuantityMap.getOrDefault(key, java.math.BigDecimal.ZERO), numStyle);
+                    }
+                    createDisplayNumberCell(midViolinRow, dataCol++, null, numStyle);
+                    createDisplayNumberCell(midViolinRow, dataCol, null, numStyle);
+
+                    Row celloRow = sheet.createRow(rowIdx++);
+                    celloRow.setHeightInPoints(24f);
+                    createEdgeCell(sheet.getWorkbook(), celloRow, 0, "", numStyle, false, false, true, true);
+                    createTextCell(celloRow, 1, "鏈堜骇澶�", numStyle);
+                    dataCol = 2;
+                    for (Map<String, Object> column : columns) {
+                        String key = String.valueOf(column.get("key"));
+                        createDisplayNumberCell(celloRow, dataCol++, celloQuantityMap.getOrDefault(key, java.math.BigDecimal.ZERO), numStyle);
+                    }
+                    createDisplayNumberCell(celloRow, dataCol++, null, numStyle);
+                    createDisplayNumberCell(celloRow, dataCol, null, numStyle);
+
+                    Row bassRow = sheet.createRow(rowIdx++);
+                    bassRow.setHeightInPoints(24f);
+                    createEdgeCell(sheet.getWorkbook(), bassRow, 0, "", numStyle, false, false, true, true);
+                    createTextCell(bassRow, 1, "鏈堜骇璐�", numStyle);
+                    dataCol = 2;
+                    for (Map<String, Object> column : columns) {
+                        String key = String.valueOf(column.get("key"));
+                        createDisplayNumberCell(bassRow, dataCol++, bassQuantityMap.getOrDefault(key, java.math.BigDecimal.ZERO), numStyle);
+                    }
+                    createDisplayNumberCell(bassRow, dataCol++, null, numStyle);
+                    createDisplayNumberCell(bassRow, dataCol, null, numStyle);
+
+                    Row foldedQtyRow = sheet.createRow(rowIdx++);
+                    foldedQtyRow.setHeightInPoints(24f);
+                    createEdgeCell(sheet.getWorkbook(), foldedQtyRow, 0, "", numStyle, false, false, true, true);
+                    createTextCell(foldedQtyRow, 1, "鏈堜骇鎶樺悎灏忔彁", numStyle);
+                    dataCol = 2;
+                    for (Map<String, Object> column : columns) {
+                        String key = String.valueOf(column.get("key"));
+                        createDisplayNumberCell(foldedQtyRow, dataCol++, foldedMap.getOrDefault(key, java.math.BigDecimal.ZERO), numStyle);
+                    }
+                    createDisplayNumberCell(foldedQtyRow, dataCol++, null, numStyle);
+                    createDisplayNumberCell(foldedQtyRow, dataCol, null, numStyle);
+
+                    Row hoursRow = sheet.createRow(rowIdx++);
+                    hoursRow.setHeightInPoints(24f);
+                    createEdgeCell(sheet.getWorkbook(), hoursRow, 0, "", headerStyle, false, true, true, true);
+                    createTextCell(hoursRow, 1, "鎶樼畻鏃堕暱", headerStyle);
+                    dataCol = 2;
+                    for (Map<String, Object> column : columns) {
+                        String key = String.valueOf(column.get("key"));
+                        createDisplayNumberCell(hoursRow, dataCol++, convertedMap.getOrDefault(key, java.math.BigDecimal.ZERO), headerStyle);
+                    }
+                    createDisplayNumberCell(hoursRow, dataCol++, staffMonthTimeTotal, headerStyle);
+                    createDisplayNumberCell(hoursRow, dataCol, monthWorkDays == null || monthWorkDays <= 0
+                            ? null
+                            : staffMonthTimeTotal.divide(new java.math.BigDecimal(monthWorkDays * 8), 8, java.math.RoundingMode.HALF_UP).setScale(4, java.math.RoundingMode.HALF_UP), headerStyle);
+                    CellRangeAddress staffRegion = new CellRangeAddress(qtyRow.getRowNum(), hoursRow.getRowNum(), 0, 0);
+                    sheet.addMergedRegion(staffRegion);
+                    RegionUtil.setBorderTop(BorderStyle.THIN, staffRegion, sheet);
+                    RegionUtil.setBorderBottom(BorderStyle.THIN, staffRegion, sheet);
+                    RegionUtil.setBorderLeft(BorderStyle.THIN, staffRegion, sheet);
+                    RegionUtil.setBorderRight(BorderStyle.THIN, staffRegion, sheet);
+                } else {
+                    Row hoursRow = sheet.createRow(rowIdx++);
+                    hoursRow.setHeightInPoints(24f);
+                    createEdgeCell(sheet.getWorkbook(), hoursRow, 0, "", headerStyle, false, true, true, true);
+                    createTextCell(hoursRow, 1, "鎶樼畻鏃堕暱", headerStyle);
+                    dataCol = 2;
+                    for (Map<String, Object> column : columns) {
+                        String key = String.valueOf(column.get("key"));
+                        createDisplayNumberCell(hoursRow, dataCol++, convertedMap.getOrDefault(key, java.math.BigDecimal.ZERO), headerStyle);
+                    }
+                    createDisplayNumberCell(hoursRow, dataCol++, staffMonthTimeTotal, headerStyle);
+                    createDisplayNumberCell(hoursRow, dataCol, monthWorkDays == null || monthWorkDays <= 0
+                            ? null
+                            : staffMonthTimeTotal.divide(new java.math.BigDecimal(monthWorkDays * 8), 8, java.math.RoundingMode.HALF_UP).setScale(4, java.math.RoundingMode.HALF_UP), headerStyle);
+                    CellRangeAddress staffRegion = new CellRangeAddress(qtyRow.getRowNum(), hoursRow.getRowNum(), 0, 0);
+                    sheet.addMergedRegion(staffRegion);
+                    RegionUtil.setBorderTop(BorderStyle.THIN, staffRegion, sheet);
+                    RegionUtil.setBorderBottom(BorderStyle.THIN, staffRegion, sheet);
+                    RegionUtil.setBorderLeft(BorderStyle.THIN, staffRegion, sheet);
+                    RegionUtil.setBorderRight(BorderStyle.THIN, staffRegion, sheet);
+                }
+            }
+        }
+
+        int[] widths = buildWorkHourColumnWidths(columns.size(), instrumentSheet);
+        for (int i = 0; i < widths.length; i++) {
+            sheet.setColumnWidth(i, widths[i] * 256);
+        }
+        return rowIdx;
+    }
+
+    private List<Map<String, Object>> buildWorkHourColumns(boolean instrumentSheet) {
+        Map<String, Map<String, Object>> columnMap = new LinkedHashMap<>();
+        if (instrumentSheet) {
+            List<TechnologyOperation> operations = technologyOperationMapper.selectList(
+                    new LambdaQueryWrapper<TechnologyOperation>()
+                            .orderByAsc(TechnologyOperation::getId));
+            for (TechnologyOperation operation : operations) {
+                if (operation == null || StringUtils.isEmpty(operation.getName())) {
+                    continue;
+                }
+                String key = operation.getName();
+                if (columnMap.containsKey(key)) {
+                    continue;
+                }
+                Map<String, Object> column = new LinkedHashMap<>();
+                column.put("key", key);
+                column.put("label", operation.getName());
+                column.put("standardQty", operation.getStandardQuantity() == null ? java.math.BigDecimal.ZERO : operation.getStandardQuantity());
+                columnMap.put(key, column);
+            }
+        } else {
+            List<Product> products = productMapper.selectList(
+                    new LambdaQueryWrapper<Product>()
+                            .isNotNull(Product::getParentId)
+                            .orderByAsc(Product::getId));
+            for (Product product : products) {
+                if (product == null || StringUtils.isEmpty(product.getProductName())) {
+                    continue;
+                }
+                // 涔愬櫒绫讳骇鍝佸綊鍒扳�滄垚鍝侀儴闂ㄢ�漵heet锛屼笉鍦ㄨ繖閲屼綔涓虹惔鐩掗儴绛夌殑鍒�
+                if (MajorCategoryEnum.match(product.getProductName()) != null) {
+                    continue;
+                }
+                String key = product.getProductName();
+                if (columnMap.containsKey(key)) {
+                    continue;
+                }
+                Map<String, Object> column = new LinkedHashMap<>();
+                column.put("key", key);
+                column.put("label", product.getProductName());
+                column.put("standardQty", product.getStandardQuantity() == null ? java.math.BigDecimal.ZERO : product.getStandardQuantity());
+                columnMap.put(key, column);
+            }
+        }
+        return new ArrayList<>(columnMap.values());
+    }
+
+    private int[] buildWorkHourColumnWidths(int columnCount, boolean instrumentSheet) {
+        int baseSize = 2;
+        int total = baseSize + columnCount;
+        int[] widths = new int[total];
+        widths[0] = 16;
+        widths[1] = 14;
+        for (int i = 2; i < total; i++) {
+            widths[i] = 11;
+        }
+        return widths;
+    }
+
+    private java.math.BigDecimal calculateExportConvertedHours(java.math.BigDecimal foldedQty,
+                                                               java.math.BigDecimal standardQty,
+                                                               java.math.BigDecimal repairWorkHour) {
+        if (foldedQty == null || standardQty == null || standardQty.compareTo(java.math.BigDecimal.ZERO) <= 0) {
+            return repairWorkHour == null ? java.math.BigDecimal.ZERO.setScale(2, java.math.RoundingMode.HALF_UP)
+                    : repairWorkHour.setScale(2, java.math.RoundingMode.HALF_UP);
+        }
+        return foldedQty.divide(standardQty, 8, java.math.RoundingMode.HALF_UP)
+                .multiply(new java.math.BigDecimal("8"))
+                .add(repairWorkHour == null ? java.math.BigDecimal.ZERO.setScale(2, java.math.RoundingMode.HALF_UP) : repairWorkHour.setScale(2, java.math.RoundingMode.HALF_UP))
+                .setScale(2, java.math.RoundingMode.HALF_UP);
     }
 
     @Override
@@ -670,10 +968,40 @@
         cell.setCellStyle(style);
     }
 
+    private void createEdgeCell(Workbook workbook,
+                                Row row,
+                                int colIndex,
+                                String value,
+                                CellStyle baseStyle,
+                                boolean topBorder,
+                                boolean bottomBorder,
+                                boolean leftBorder,
+                                boolean rightBorder) {
+        CellStyle edgeStyle = workbook.createCellStyle();
+        edgeStyle.cloneStyleFrom(baseStyle);
+        edgeStyle.setBorderTop(topBorder ? BorderStyle.THIN : BorderStyle.NONE);
+        edgeStyle.setBorderBottom(bottomBorder ? BorderStyle.THIN : BorderStyle.NONE);
+        edgeStyle.setBorderLeft(leftBorder ? BorderStyle.THIN : BorderStyle.NONE);
+        edgeStyle.setBorderRight(rightBorder ? BorderStyle.THIN : BorderStyle.NONE);
+        Cell cell = row.createCell(colIndex);
+        cell.setCellValue(value == null ? "" : value);
+        cell.setCellStyle(edgeStyle);
+    }
+
     private void createNumberCell(Row row, int colIndex, java.math.BigDecimal value, CellStyle style) {
         Cell cell = row.createCell(colIndex);
         if (value != null) {
             cell.setCellValue(value.doubleValue());
+        }
+        cell.setCellStyle(style);
+    }
+
+    private void createDisplayNumberCell(Row row, int colIndex, java.math.BigDecimal value, CellStyle style) {
+        Cell cell = row.createCell(colIndex);
+        if (value != null && value.compareTo(java.math.BigDecimal.ZERO) != 0) {
+            cell.setCellValue(value.doubleValue());
+        } else {
+            cell.setCellValue("");
         }
         cell.setCellStyle(style);
     }
@@ -710,7 +1038,6 @@
         dto.setEntryDateStart(start);
         dto.setEntryDateEnd(end);
         dto.setAuditStatus(1);
-        dto.setReportType(0);
         Page<ProductionAccountDto> page = new Page<>(1, 100000);
         return productionProductMainMapper.listProductionDetails(dto, page).getRecords();
     }
@@ -747,17 +1074,23 @@
             return java.math.BigDecimal.ZERO;
         }
         java.math.BigDecimal quantity = item.getQuantity() == null ? java.math.BigDecimal.ZERO : item.getQuantity();
-        String productName = item.getProductName();
-        if ("灏忔彁鐞�".equals(productName)) {
-            return operation.getViolinFactor() == null ? java.math.BigDecimal.ZERO : quantity.multiply(operation.getViolinFactor());
+        MajorCategoryEnum category = MajorCategoryEnum.of(item.getMajorCategory());
+        if (category == null) {
+            return java.math.BigDecimal.ZERO;
         }
-        if ("澶ф彁鐞�".equals(productName)) {
-            return operation.getViolaFactor() == null ? java.math.BigDecimal.ZERO : quantity.multiply(operation.getViolaFactor());
+        switch (category) {
+            case VIOLIN:
+                return operation.getViolinFactor() == null ? java.math.BigDecimal.ZERO : quantity.multiply(operation.getViolinFactor());
+            case VIOLA:
+                return operation.getMidViolinFactor() == null ? java.math.BigDecimal.ZERO : quantity.multiply(operation.getMidViolinFactor());
+            case CELLO:
+                // violaFactor 鐨勫垪鍚嶆部鐢ㄤ簡 viola锛堜腑鎻愮惔锛夛紝鍘嗗彶璇箟瀹炰负銆愬ぇ鎻愮惔銆戠郴鏁帮紝鍕挎敼
+                return operation.getViolaFactor() == null ? java.math.BigDecimal.ZERO : quantity.multiply(operation.getViolaFactor());
+            case BASS:
+                return operation.getBassFactor() == null ? java.math.BigDecimal.ZERO : quantity.multiply(operation.getBassFactor());
+            default:
+                return java.math.BigDecimal.ZERO;
         }
-        if ("璐濇柉".equals(productName)) {
-            return operation.getBassFactor() == null ? java.math.BigDecimal.ZERO : quantity.multiply(operation.getBassFactor());
-        }
-        return java.math.BigDecimal.ZERO;
     }
 
     private Map<String, java.math.BigDecimal> buildStandardQuantityMap(List<String> operationNames) {

--
Gitblit v1.9.3