zhoushihao
2025-05-12 4ebdc83c264042962e92905562099efd285d835d
1、界面下载近10天内的的生产数据信息
4个文件已修改
2个文件已添加
154 ■■■■ 已修改文件
hangzhoumesParent/common/servicebase/src/main/java/com/mes/largenscreen/controller/LargenScreenController.java 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
hangzhoumesParent/common/servicebase/src/main/java/com/mes/largenscreen/entity/DailyProductionData.java 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
hangzhoumesParent/common/servicebase/src/main/java/com/mes/largenscreen/service/LargenScreenService.java 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
hangzhoumesParent/common/servicebase/src/main/java/com/mes/largenscreen/service/impl/LargenScreenServiceImpl.java 22 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
hangzhoumesParent/common/servicebase/src/main/java/com/mes/utils/excel/CustomCellStyleHandler.java 35 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
hangzhoumesParent/common/servicebase/src/main/java/com/mes/utils/excel/CustomCellWriteWidthConfig.java 82 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
hangzhoumesParent/common/servicebase/src/main/java/com/mes/largenscreen/controller/LargenScreenController.java
@@ -9,10 +9,7 @@
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
@@ -37,9 +34,9 @@
    }
    @ApiOperation("按照条件统计每日生产情况导出")
    @PostMapping("/exportDailyProduction")
    public void exportDailyProduction(@RequestBody @Validated DateRequest query) {
        largenScreenService.exportDailyProduction(query);
    @GetMapping("/exportDailyProduction")
    public void exportDailyProduction() {
        largenScreenService.exportDailyProduction();
    }
    @ApiOperation("按照条件统计生产情况")
hangzhoumesParent/common/servicebase/src/main/java/com/mes/largenscreen/entity/DailyProductionData.java
@@ -13,7 +13,7 @@
public class DailyProductionData {
    @ExcelProperty(value = "日期")
    @ExcelMerge(merge = true)
    @ExcelMerge(merge = true, isPrimaryKey = true)
    private String productDate;
    @ExcelProperty(value = {"切割", "开始时间"})
hangzhoumesParent/common/servicebase/src/main/java/com/mes/largenscreen/service/LargenScreenService.java
@@ -18,7 +18,7 @@
    List<DailyProductionVO> querySameDayProduction(DateRequest query);
    void exportDailyProduction(DateRequest query);
    void exportDailyProduction();
    List<DailyProductionData> queryProduction(DateRequest query);
}
hangzhoumesParent/common/servicebase/src/main/java/com/mes/largenscreen/service/impl/LargenScreenServiceImpl.java
@@ -9,6 +9,7 @@
import com.mes.largenscreen.mapper.LargenScreenMapper;
import com.mes.largenscreen.service.LargenScreenService;
import com.mes.tools.PageUtil;
import com.mes.utils.excel.CustomCellWriteWidthConfig;
import com.mes.utils.excel.ExcelMergeStrategy;
import com.mes.utils.excel.ExcelUtil;
import org.springframework.stereotype.Service;
@@ -60,21 +61,20 @@
    }
    @Override
    public void exportDailyProduction(DateRequest query) {
    public void exportDailyProduction() {
        try {
            if (StringUtils.isEmpty(query.getBeginDate())) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                Calendar calendar = Calendar.getInstance();
                calendar.add(Calendar.DAY_OF_MONTH, -6);
                query.setBeginDate(sdf.format(calendar.getTime()));
                query.setEndDate(sdf.format(new Date()));
            }
            ExcelUtil.setExcelResponseProp(response, "供应商列表-合并");
            List<DailyProductionData> providerList = largenScreenMapper.exportDailyProduction(query.getBeginDate(), query.getEndDate());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_MONTH, -10);
            ExcelUtil.setExcelResponseProp(response, "MES试生产记录" + sdf.format(new Date()));
            List<DailyProductionData> providerList = largenScreenMapper.exportDailyProduction(sdf.format(calendar.getTime()), sdf.format(new Date()));
            EasyExcel.write(response.getOutputStream())
                    .head(DailyProductionData.class)
                    .registerWriteHandler(new ExcelMergeStrategy(DailyProductionData.class))
                    .excelType(ExcelTypeEnum.XLSX).sheet().doWrite(providerList);
                    .registerWriteHandler(new CustomCellWriteWidthConfig())
                    .excelType(ExcelTypeEnum.XLSX).sheet("生产记录").doWrite(providerList);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
hangzhoumesParent/common/servicebase/src/main/java/com/mes/utils/excel/CustomCellStyleHandler.java
New file
@@ -0,0 +1,35 @@
package com.mes.utils.excel;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFFont;
/**
 * @Author : zhoush
 * @Date: 2025/5/12 20:32
 * @Description:
 */
public class CustomCellStyleHandler implements CellWriteHandler {
    @Override
    public void afterCellDispose(CellWriteHandlerContext context) {
        // 获取单元格
        Cell cell = context.getCell();
        CellStyle cellStyle = cell.getCellStyle();
        // 设置水平和垂直居中
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyle.setFont(new XSSFFont());
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);
        // 设置样式回到单元格
        cell.setCellStyle(cellStyle);
    }
}
hangzhoumesParent/common/servicebase/src/main/java/com/mes/utils/excel/CustomCellWriteWidthConfig.java
New file
@@ -0,0 +1,82 @@
package com.mes.utils.excel;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.CellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @Author : zhoush
 * @Date: 2025/5/12 20:34
 * @Description:
 */
public class CustomCellWriteWidthConfig extends AbstractColumnWidthStyleStrategy {
    private final Map<Integer, Map<Integer, Integer>> CACHE = new HashMap<>();
    @Override
    protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer integer, Boolean isHead) {
        boolean needSetWidth = isHead || !CollectionUtils.isEmpty(cellDataList);
        if (needSetWidth) {
            Map<Integer, Integer> maxColumnWidthMap = CACHE.computeIfAbsent(writeSheetHolder.getSheetNo(), k -> new HashMap<>());
            Integer columnWidth = this.dataLength(cellDataList, cell, isHead);
            if (columnWidth > 0) {
                if (columnWidth > 10) {
                    columnWidth = 20;
                } else {
                    columnWidth = 10;
                }
                Integer maxColumnWidth = maxColumnWidthMap.get(cell.getColumnIndex());
                if (maxColumnWidth == null || columnWidth > maxColumnWidth) {
                    maxColumnWidthMap.put(cell.getColumnIndex(), columnWidth);
                    Sheet sheet = writeSheetHolder.getSheet();
                    sheet.setColumnWidth(cell.getColumnIndex(), 256 * columnWidth + 184);
                }
            }
        }
    }
    /**
     * 计算长度
     *
     * @param cellDataList
     * @param cell
     * @param isHead
     * @return
     */
    private Integer dataLength(List<WriteCellData<?>> cellDataList, Cell cell, Boolean isHead) {
        if (isHead) {
            return cell.getStringCellValue().getBytes().length;
        } else {
            CellData<?> cellData = cellDataList.get(0);
            CellDataTypeEnum type = cellData.getType();
            if (type == null) {
                return -1;
            } else {
                switch (type) {
                    case STRING:
                        // 换行符(数据需要提前解析好)
                        int index = cellData.getStringValue().indexOf("\n");
                        return index != -1 ?
                                cellData.getStringValue().substring(0, index).getBytes().length + 1 : cellData.getStringValue().getBytes().length + 1;
                    case BOOLEAN:
                        return cellData.getBooleanValue().toString().getBytes().length;
                    case NUMBER:
                        return cellData.getNumberValue().toString().getBytes().length;
                    default:
                        return -1;
                }
            }
        }
    }
}