@@ -45,11 +45,9 @@ import java.io.IOException;
import java.net.URLEncoder ;
import java.nio.charset.StandardCharsets ;
import java.text.SimpleDateFormat ;
import javax.servlet.ServletOutputStream ;
import javax.servlet.http.HttpServletResponse ;
import org.apache.poi.ss.usermodel.* ;
import org.apache.poi.ss.util.CellRangeAddress ;
import org.apache.poi.xssf.usermodel.XSSFWorkbook ;
import com.alibaba.excel.EasyExcel ;
/**
* 钢卷物料表Service业务层处理
@@ -5333,7 +5331,7 @@ public class WmsMaterialCoilServiceImpl implements IWmsMaterialCoilService {
" GROUP BY coil_id " +
" ) t2 ON t1.coil_id = t2.coil_id AND t1.create_time = t2.max_time " +
" WHERE t1.del_flag = '0' AND t1.rejudge_reason IS NOT NULL " ;
List < Map < String , Object > > results = wmsCoilQualityRejudgeMapper . selectMapsBySql ( sql ) ;
if ( results ! = null ) {
for ( Map < String , Object > result : results ) {
@@ -5346,212 +5344,129 @@ public class WmsMaterialCoilServiceImpl implements IWmsMaterialCoilService {
}
}
// 5. 构建导出数据(扁平化:一个钢卷+一个异常 = 一行)
List < WmsCoilAbnormalExportRow > exportData = new ArrayList < > ( ) ;
// 5. 构建EasyExcel导出数据 - O(n)复杂度
List < WmsCoilAbnormalExportVo > exportData = new ArrayList < > ( ) ;
SimpleDateFormat sdf = new SimpleDateFormat ( " yyyy-MM-dd HH:mm:ss " ) ;
for ( WmsMaterialCoilVo coil : coilList ) {
Long coilId = coil . getCoilId ( ) ;
List < WmsCoilAbnormal > abnormalList = abnormalMap . getOrDefault ( coilId , new ArrayList < > ( ) ) ;
String rejudgeReason = rejudgeReasonMap . get ( coilId ) ;
if ( abnormalList . isEmpty ( ) ) {
WmsCoilAbnormalExportRow row = new WmsCoilAbnormalExportRow ( ) ;
row . setCoil ( coil ) ;
row . setRejudgeReason ( rejudgeReason ) ;
row . setAbnormal ( null ) ;
exportData . add ( row ) ;
// 无异常信息的钢卷,创建一行空异常数据
WmsCoilAbnormalExportVo dto = createExportVo ( coil , rejudgeReason , null , sdf ) ;
dto . setCoilId ( coilId ) ;
exportData . add ( dto ) ;
} else {
// 有异常信息的钢卷,每个异常创建一行
for ( WmsCoilAbnormal abnormal : abnormalList ) {
WmsCoilAbnormalExportRow row = new WmsCoilAbnormalExportRow ( ) ;
row . setCoil ( coil ) ;
row . setRejudgeReason ( rejudgeReason ) ;
row . setAbnormal ( abnormal ) ;
exportData . add ( row ) ;
WmsCoilAbnormalExportVo dto = createExportVo ( coil , rejudgeReason , abnormal , sdf ) ;
dto . setCoilId ( coilId ) ;
exportData . add ( dto ) ;
}
}
}
// 6. 导出Excel
try ( Workbook wb = new XSSFWorkbook ( ) ) {
Sheet sheet = wb . createSheet ( " 异常报表 " ) ;
int r = 0 ;
// 标题行
Row titleRow = sheet . createRow ( r + + ) ;
titleRow . setHeightInPoints ( 36f ) ;
Cell titleCell = titleRow . createCell ( 0 ) ;
titleCell . setCellValue ( " 钢卷异常报表 " ) ;
CellStyle titleStyle = wb . createCellStyle ( ) ;
titleStyle . setAlignment ( HorizontalAlignment . CENTER ) ;
titleStyle . setVerticalAlignment ( VerticalAlignment . CENTER ) ;
Font titleFont = wb . createFont ( ) ;
titleFont . setBold ( true ) ;
titleFont . setFontHeightInPoints ( ( short ) 15 ) ;
titleStyle . setFont ( titleFont ) ;
CellRangeAddress titleRegion = new CellRangeAddress ( 0 , 0 , 0 , 41 ) ;
sheet . addMergedRegion ( titleRegion ) ;
for ( int i = titleRegion . getFirstColumn ( ) ; i < = titleRegion . getLastColumn ( ) ; i + + ) {
Cell cell = titleRow . getCell ( i ) ;
if ( cell = = null ) cell = titleRow . createCell ( i ) ;
cell . setCellStyle ( titleStyle ) ;
}
// 表头
String [ ] headers = {
" 类型 " , " 逻辑库区 " , " 实际库区 " , " 入场卷号 " , " 厂家卷号 " , " 成品卷号 " , " 日期 " ,
" 重量 " , " 用途 " , " 切边要求 " , " 包装种类 " , " 产品质量 " , " 原料材质 " ,
" 库存状态 " , " 备注 " , " 名称 " , " 规格 " , " 长度 " , " 材质 " , " 厂家 " ,
" 表面处理 " , " 锌层 " , " 物品ID " , " 操作完成时间 " , " 调拨类型 " ,
" 改判原因 " ,
" 产线 " , " 位置 " , " 长度坐标 " , " 缺陷开始位置 " , " 缺陷结束位置 " ,
" 缺陷代码 " , " 缺陷类型 " , " 缺陷率 " , " 缺陷重量 " , " 程度 " , " 判级 " ,
" 判级人 " , " 判级时间 " , " 主标记 " , " 整卷标记 " , " 异常备注 " , " 板面 "
} ;
CellStyle headStyle = wb . createCellStyle ( ) ;
Font headFont = wb . createFont ( ) ;
headFont . setBold ( true ) ;
headStyle . setFont ( headFont ) ;
headStyle . setAlignment ( HorizontalAlignment . CENTER ) ;
headStyle . setVerticalAlignment ( VerticalAlignment . CENTER ) ;
headStyle . setFillPattern ( FillPatternType . SOLID_FOREGROUND ) ;
headStyle . setFillForegroundColor ( IndexedColors . PALE_BLUE . getIndex ( ) ) ;
headStyle . setWrapText ( true ) ;
Row headRow = sheet . createRow ( r + + ) ;
headRow . setHeightInPoints ( 24 ) ;
for ( int i = 0 ; i < headers . length ; i + + ) {
Cell cell = headRow . createCell ( i ) ;
cell . setCellValue ( headers [ i ] ) ;
cell . setCellStyle ( headStyle ) ;
}
// 数据行样式
CellStyle centerStyle = wb . createCellStyle ( ) ;
centerStyle . setAlignment ( HorizontalAlignment . CENTER ) ;
centerStyle . setVerticalAlignment ( VerticalAlignment . CENTER ) ;
// 填充数据
int dataStartRow = r ;
SimpleDateFormat sdf = new SimpleDateFormat ( " yyyy-MM-dd HH:mm:ss " ) ;
for ( WmsCoilAbnormalExportRow rowData : exportData ) {
WmsMaterialCoilVo coil = rowData . getCoil ( ) ;
WmsCoilAbnormal abnormal = rowData . getAbnormal ( ) ;
String rejudgeReason = rowData . getRejudgeReason ( ) ;
Row row = sheet . createRow ( r + + ) ;
int cc = 0 ;
// 钢卷信息( 前25列)
row . createCell ( cc + + ) . setCellValue ( coil . getItemType ( ) ! = null ? coil . getItemType ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( coil . getWarehouseName ( ) ! = null ? coil . getWarehouseName ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( coil . getActualWarehouseName ( ) ! = null ? coil . getActualWarehouseName ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( coil . getEnterCoilNo ( ) ! = null ? coil . getEnterCoilNo ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( coil . getSupplierCoilNo ( ) ! = null ? coil . getSupplierCoilNo ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( coil . getCurrentCoilNo ( ) ! = null ? coil . getCurrentCoilNo ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( coil . getCreateTime ( ) ! = null ? sdf . format ( coil . getCreateTime ( ) ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( coil . getNetWeight ( ) ! = null ? coil . getNetWeight ( ) . toString ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( coil . getBusinessPurpose ( ) ! = null ? coil . getBusinessPurpose ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( coil . getTrimmingRequirement ( ) ! = null ? coil . getTrimmingRequirement ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( coil . getPackagingRequirement ( ) ! = null ? coil . getPackagingRequirement ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( coil . getQualityStatus ( ) ! = null ? coil . getQualityStatus ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( coil . getPackingStatus ( ) ! = null ? coil . getPackingStatus ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( coil . getStatus ( ) ! = null ? coil . getStatus ( ) . toString ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( coil . getRemark ( ) ! = null ? coil . getRemark ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( coil . getItemName ( ) ! = null ? coil . getItemName ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( coil . getSpecification ( ) ! = null ? coil . getSpecification ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( coil . getLength ( ) ! = null ? coil . getLength ( ) . toString ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( coil . getMaterial ( ) ! = null ? coil . getMaterial ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( coil . getManufacturer ( ) ! = null ? coil . getManufacturer ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( coil . getSurfaceTreatmentDesc ( ) ! = null ? coil . getSurfaceTreatmentDesc ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( coil . getZincLayer ( ) ! = null ? coil . getZincLayer ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( coil . getItemId ( ) ! = null ? coil . getItemId ( ) . toString ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( coil . getActionCompleteTime ( ) ! = null ? sdf . format ( coil . getActionCompleteTime ( ) ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( coil . getTransferType ( ) ! = null ? coil . getTransferType ( ) : " " ) ;
// 改判原因
row . createCell ( cc + + ) . setCellValue ( rejudgeReason ! = null ? rejudgeReason : " " ) ;
// 异常信息
if ( abnormal ! = null ) {
row . createCell ( cc + + ) . setCellValue ( abnormal . getProductionLine ( ) ! = null ? abnormal . getProductionLine ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( abnormal . getPosition ( ) ! = null ? abnormal . getPosition ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( abnormal . getLength ( ) ! = null ? abnormal . getLength ( ) . toString ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( abnormal . getStartPosition ( ) ! = null ? abnormal . getStartPosition ( ) . toString ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( abnormal . getEndPosition ( ) ! = null ? abnormal . getEndPosition ( ) . toString ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( abnormal . getDefectCode ( ) ! = null ? abnormal . getDefectCode ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( abnormal . getDefectType ( ) ! = null ? abnormal . getDefectType ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( abnormal . getDefectRate ( ) ! = null ? abnormal . getDefectRate ( ) . toString ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( abnormal . getDefectWeight ( ) ! = null ? abnormal . getDefectWeight ( ) . toString ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( abnormal . getDegree ( ) ! = null ? abnormal . getDegree ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( abnormal . getJudgeLevel ( ) ! = null ? abnormal . getJudgeLevel ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( abnormal . getJudgeBy ( ) ! = null ? abnormal . getJudgeBy ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( abnormal . getJudgeTime ( ) ! = null ? sdf . format ( abnormal . getJudgeTime ( ) ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( abnormal . getMainMark ( ) ! = null ? abnormal . getMainMark ( ) . toString ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( abnormal . getWholeCoilMark ( ) ! = null ? abnormal . getWholeCoilMark ( ) . toString ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( abnormal . getRemark ( ) ! = null ? abnormal . getRemark ( ) : " " ) ;
row . createCell ( cc + + ) . setCellValue ( abnormal . getPlateSurface ( ) ! = null ? abnormal . getPlateSurface ( ) : " " ) ;
} else {
for ( int j = 0 ; j < 17 ; j + + ) row . createCell ( cc + + ) . setCellValue ( " " ) ;
}
}
// 合并钢卷信息列( 前26列: 25列钢卷信息 + 1列改判原因)
int currentRow = dataStartRow ;
while ( currentRow < r ) {
Long currentCoilId = exportData . get ( currentRow - dataStartRow ) . getCoil ( ) . getCoilId ( ) ;
int startRow = currentRow ;
int endRow = currentRow ;
while ( endRow < r & & exportData . get ( endRow - dataStartRow ) . getCoil ( ) . getCoilId ( ) . equals ( currentCoilId ) ) {
endRow + + ;
}
if ( endRow - startRow > 1 ) {
for ( int col = 0 ; col < 26 ; col + + ) {
sheet . addMergedRegion ( new CellRangeAddress ( startRow , endRow - 1 , col , col ) ) ;
}
}
// 设置居中样式
for ( int rowIdx = startRow ; rowIdx < endRow ; rowIdx + + ) {
Row row = sheet . getRow ( rowIdx ) ;
if ( row ! = null ) {
for ( int col = 0 ; col < 26 ; col + + ) {
Cell cell = row . getCell ( col ) ;
if ( cell ! = null ) cell . setCellStyle ( centerStyle ) ;
}
}
}
currentRow = endRow ;
}
// 自适应列宽
for ( int i = 0 ; i < headers . length ; i + + ) {
sheet . autoSizeColumn ( i , true ) ;
int w = sheet . getColumnWidth ( i ) ;
sheet . setColumnWidth ( i , Math . min ( Math . max ( w , 3000 ) , 12000 ) ) ;
}
// 输出
// 6. 使用EasyExcel导出 - O(n)复杂度,瞬间完成
try {
// 设置响应头
String filename = " abnormal_report_ " + System . currentTimeMillis ( ) + " .xlsx " ;
String encoded = URLEncoder . encode ( filename , StandardCharsets . UTF_8 . name ( ) ) ;
response . setCharacterEncoding ( StandardCharsets . UTF_8 . name ( ) ) ;
response . setContentType ( " application/vnd.openxmlformats-officedocument.spreadsheetml.sheet " ) ;
response . setHeader ( " Content-Disposition " , " attachment; filename*=UTF-8'' " + encoded ) ;
try ( ServletOutputStream os = response . getOutputStream ( ) ) {
wb . write ( os ) ;
os . flush ( ) ;
}
// 使用EasyExcel写入, 正常导出
EasyExcel . write ( response . getOutputStream ( ) , WmsCoilAbnormalExportVo . class )
. sheet ( " 异常报表 " )
. doWrite ( exportData ) ;
} catch ( IOException e ) {
throw new RuntimeException ( " 导出失败: " + e . getMessage ( ) ) ;
}
}
/*
* 创建导出DTO对象
*/
private WmsCoilAbnormalExportVo createExportVo ( WmsMaterialCoilVo coil , String rejudgeReason ,
WmsCoilAbnormal abnormal , SimpleDateFormat sdf ) {
WmsCoilAbnormalExportVo dto = new WmsCoilAbnormalExportVo ( ) ;
// 钢卷基本信息( 前25列)
dto . setItemType ( coil . getItemType ( ) ! = null ? coil . getItemType ( ) : " " ) ;
dto . setWarehouseName ( coil . getWarehouseName ( ) ! = null ? coil . getWarehouseName ( ) : " " ) ;
dto . setActualWarehouseName ( coil . getActualWarehouseName ( ) ! = null ? coil . getActualWarehouseName ( ) : " " ) ;
dto . setEnterCoilNo ( coil . getEnterCoilNo ( ) ! = null ? coil . getEnterCoilNo ( ) : " " ) ;
dto . setSupplierCoilNo ( coil . getSupplierCoilNo ( ) ! = null ? coil . getSupplierCoilNo ( ) : " " ) ;
dto . setCurrentCoilNo ( coil . getCurrentCoilNo ( ) ! = null ? coil . getCurrentCoilNo ( ) : " " ) ;
dto . setCreateTime ( coil . getCreateTime ( ) ! = null ? sdf . format ( coil . getCreateTime ( ) ) : " " ) ;
dto . setNetWeight ( coil . getNetWeight ( ) ! = null ? coil . getNetWeight ( ) . toString ( ) : " " ) ;
dto . setBusinessPurpose ( coil . getBusinessPurpose ( ) ! = null ? coil . getBusinessPurpose ( ) : " " ) ;
dto . setTrimmingRequirement ( coil . getTrimmingRequirement ( ) ! = null ? coil . getTrimmingRequirement ( ) : " " ) ;
dto . setPackagingRequirement ( coil . getPackagingRequirement ( ) ! = null ? coil . getPackagingRequirement ( ) : " " ) ;
dto . setQualityStatus ( coil . getQualityStatus ( ) ! = null ? coil . getQualityStatus ( ) : " " ) ;
dto . setPackingStatus ( coil . getPackingStatus ( ) ! = null ? coil . getPackingStatus ( ) : " " ) ;
dto . setStatus ( coil . getStatus ( ) ! = null ? coil . getStatus ( ) . toString ( ) : " " ) ;
dto . setRemark ( coil . getRemark ( ) ! = null ? coil . getRemark ( ) : " " ) ;
dto . setItemName ( coil . getItemName ( ) ! = null ? coil . getItemName ( ) : " " ) ;
dto . setSpecification ( coil . getSpecification ( ) ! = null ? coil . getSpecification ( ) : " " ) ;
dto . setLength ( coil . getLength ( ) ! = null ? coil . getLength ( ) . toString ( ) : " " ) ;
dto . setMaterial ( coil . getMaterial ( ) ! = null ? coil . getMaterial ( ) : " " ) ;
dto . setManufacturer ( coil . getManufacturer ( ) ! = null ? coil . getManufacturer ( ) : " " ) ;
dto . setSurfaceTreatmentDesc ( coil . getSurfaceTreatmentDesc ( ) ! = null ? coil . getSurfaceTreatmentDesc ( ) : " " ) ;
dto . setZincLayer ( coil . getZincLayer ( ) ! = null ? coil . getZincLayer ( ) : " " ) ;
dto . setItemId ( coil . getItemId ( ) ! = null ? coil . getItemId ( ) . toString ( ) : " " ) ;
dto . setActionCompleteTime ( coil . getActionCompleteTime ( ) ! = null ? sdf . format ( coil . getActionCompleteTime ( ) ) : " " ) ;
dto . setTransferType ( coil . getTransferType ( ) ! = null ? coil . getTransferType ( ) : " " ) ;
// 改判原因( 第26列)
dto . setRejudgeReason ( rejudgeReason ! = null ? rejudgeReason : " " ) ;
// 异常信息( 后17列)
if ( abnormal ! = null ) {
dto . setProductionLine ( abnormal . getProductionLine ( ) ! = null ? abnormal . getProductionLine ( ) : " " ) ;
dto . setPosition ( abnormal . getPosition ( ) ! = null ? abnormal . getPosition ( ) : " " ) ;
dto . setAbnormalLength ( abnormal . getLength ( ) ! = null ? abnormal . getLength ( ) . toString ( ) : " " ) ;
dto . setStartPosition ( abnormal . getStartPosition ( ) ! = null ? abnormal . getStartPosition ( ) . toString ( ) : " " ) ;
dto . setEndPosition ( abnormal . getEndPosition ( ) ! = null ? abnormal . getEndPosition ( ) . toString ( ) : " " ) ;
dto . setDefectCode ( abnormal . getDefectCode ( ) ! = null ? abnormal . getDefectCode ( ) : " " ) ;
dto . setDefectType ( abnormal . getDefectType ( ) ! = null ? abnormal . getDefectType ( ) : " " ) ;
dto . setDefectRate ( abnormal . getDefectRate ( ) ! = null ? abnormal . getDefectRate ( ) . toString ( ) : " " ) ;
dto . setDefectWeight ( abnormal . getDefectWeight ( ) ! = null ? abnormal . getDefectWeight ( ) . toString ( ) : " " ) ;
dto . setDegree ( abnormal . getDegree ( ) ! = null ? abnormal . getDegree ( ) : " " ) ;
dto . setJudgeLevel ( abnormal . getJudgeLevel ( ) ! = null ? abnormal . getJudgeLevel ( ) : " " ) ;
dto . setJudgeBy ( abnormal . getJudgeBy ( ) ! = null ? abnormal . getJudgeBy ( ) : " " ) ;
dto . setJudgeTime ( abnormal . getJudgeTime ( ) ! = null ? sdf . format ( abnormal . getJudgeTime ( ) ) : " " ) ;
dto . setMainMark ( abnormal . getMainMark ( ) ! = null ? abnormal . getMainMark ( ) . toString ( ) : " " ) ;
dto . setWholeCoilMark ( abnormal . getWholeCoilMark ( ) ! = null ? abnormal . getWholeCoilMark ( ) . toString ( ) : " " ) ;
dto . setAbnormalRemark ( abnormal . getRemark ( ) ! = null ? abnormal . getRemark ( ) : " " ) ;
dto . setPlateSurface ( abnormal . getPlateSurface ( ) ! = null ? abnormal . getPlateSurface ( ) : " " ) ;
} else {
// 空异常信息
dto . setProductionLine ( " " ) ;
dto . setPosition ( " " ) ;
dto . setAbnormalLength ( " " ) ;
dto . setStartPosition ( " " ) ;
dto . setEndPosition ( " " ) ;
dto . setDefectCode ( " " ) ;
dto . setDefectType ( " " ) ;
dto . setDefectRate ( " " ) ;
dto . setDefectWeight ( " " ) ;
dto . setDegree ( " " ) ;
dto . setJudgeLevel ( " " ) ;
dto . setJudgeBy ( " " ) ;
dto . setJudgeTime ( " " ) ;
dto . setMainMark ( " " ) ;
dto . setWholeCoilMark ( " " ) ;
dto . setAbnormalRemark ( " " ) ;
dto . setPlateSurface ( " " ) ;
}
return dto ;
}
}