opt:优化成品导出

This commit is contained in:
zhangzq
2025-04-29 09:40:20 +08:00
parent 44d55dae35
commit 3379e32512
7 changed files with 203 additions and 23 deletions

View File

@@ -1,5 +1,6 @@
package org.nl.b_lms.pdm.subpackagerelation.dao.mapper;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
@@ -31,4 +32,6 @@ public interface PdmBiSubpackagerelationMapper extends BaseMapper<PdmBiSubpackag
List<PdmBiSubpackagerelationDto> queryContainerNameBySaleOrder(@Param("sale_order_name") String sale_order_name, @Param("container_name") String container_name);
List<JSONObject> recordQuery(List<String> pcns);
}

View File

@@ -110,6 +110,14 @@
</if>
ORDER BY box_group
</select>
<select id="recordQuery" resultType="com.alibaba.fastjson.JSONObject">
select sale_order_name,container_name,insert_time from pdm_bi_subpackagerelationrecord where container_name in
<foreach collection="pcns" item="item" open="(" close=")" separator=",">
#{item}
</foreach>
AND io_type = '0'
</select>
</mapper>

View File

@@ -114,6 +114,7 @@ public interface IpdmBiSubpackagerelationService extends IService<PdmBiSubpackag
void createSubTest(JSONObject jo);
List<JSONObject> recordQuery(List<String> pcsn);
}

View File

@@ -691,6 +691,12 @@ public class PdmBiSubpackagerelationServiceImpl extends ServiceImpl<PdmBiSubpack
}
@Override
public List<JSONObject> recordQuery(List<String> pcsn) {
if (CollectionUtils.isEmpty(pcsn)){
return new ArrayList<>();
}
return this.baseMapper.recordQuery(pcsn);
}
}

View File

@@ -19,11 +19,15 @@ import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.poi.excel.BigExcelWriter;
import cn.hutool.poi.excel.ExcelUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.nl.modules.common.exception.BadRequestException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.nl.modules.common.utils.enums.CellTypeEnum;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
@@ -33,6 +37,7 @@ import java.io.*;
import java.security.MessageDigest;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
@@ -43,9 +48,10 @@ import java.util.Map;
* @author Zheng Jie
* @date 2018-12-27
*/
@Slf4j
public class FileUtil extends cn.hutool.core.io.FileUtil {
private static final Logger log = LoggerFactory.getLogger(FileUtil.class);
private Integer batchWriteExcelRowAmount = 500;
/**
* 系统临时目录
@@ -234,6 +240,88 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
}
/**
* 流导出:后续改成导出文件
* @param list
* @param response
* @throws IOException
*/
public void downloadExcelIO(List<Map<String,String>> list, String[] columnExcelNameArr, HttpServletResponse response) {
List<Map<String,String>> lastRestDataList = list;
List<List<Map<String,String>>> dataPiece = Lists.partition(list, 5000);
int blockNum = 0;
int blockNumMax = dataPiece.size();
List<String> allFileLocations = new ArrayList<>();
ServletOutputStream out = null;
try {
boolean allFinish = false;
while (!allFinish) {
int thisFileRowNumber = 1;
SXSSFWorkbook wb = null;
try {
// if (blockNum<blockNumMax){
// lastRestDataList = dataPiece.get(blockNum);
// blockNum++;
// }
// if (blockNum==blockNumMax){
// allFinish = true;
// }
wb = new SXSSFWorkbook(this.batchWriteExcelRowAmount);// 流式写入EXCEL只保留少数行(比如50行)数据在内存,防止内存溢出
Sheet sh = wb.createSheet();
Row titleRow = sh.createRow(0);
for (int cellNum = 0; cellNum < columnExcelNameArr.length; cellNum++) {
Cell cell = titleRow.createCell(cellNum);
cell.setCellValue(columnExcelNameArr[cellNum]);
}
if (!CollectionUtils.isEmpty(lastRestDataList)) {
for (int i = 0; i < lastRestDataList.size(); i++) {
Row dataRow = sh.createRow(thisFileRowNumber);
Map<String, String> columnMap = lastRestDataList.get(i);
for (int cellNum = 0; cellNum < columnExcelNameArr.length; cellNum++) {
Cell cell = dataRow.createCell(cellNum);
String valueStr = columnMap.get(columnExcelNameArr[cellNum]);
cell.setCellValue(valueStr);
}
thisFileRowNumber++;
}
}
String localFilePath = SYS_TEM_DIR + IdUtil.fastSimpleUUID()+ blockNum + ".xlsx";
allFileLocations.add(localFilePath);
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename="+localFilePath);
out = response.getOutputStream();
wb.write(out);
// 必须清理流式写入Excel生成的临时文件
wb.dispose();
allFinish = true;
}catch (Exception ex){
log.warn(ex.getMessage());
throw new BadRequestException(ex.getMessage());
} finally {
if (out != null) {
try {
out.flush();
out.close();
} catch (IOException e) {log.warn(e.getMessage());}
}
if (wb != null) {
try {
wb.dispose();
} catch (Exception e) {log.warn(e.getMessage());}
}
}
}
}finally {
if (out != null) {
try {
out.flush();
out.close();
} catch (IOException e) {log.warn(e.getMessage());}
}
}
}
public static String getFileType(String type) {
String documents = "txt doc pdf ppt pps xlsx xls docx";
String music = "mp3 wav wma mpa ram ra aac aif m4a";
@@ -351,4 +439,19 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
return getMd5(getByte(file));
}
private CellStyle createCellStyle(SXSSFWorkbook wb){
try{
CellStyle cellStyle = wb.createCellStyle();
DataFormat dataFormat = wb.createDataFormat();
String[] formats = CellTypeEnum.get_formats();
short format = dataFormat.getFormat(formats[3]);
cellStyle.setDataFormat(format);
return cellStyle;
}catch (Exception ee){
return null;
}
}
}

View File

@@ -0,0 +1,45 @@
package org.nl.modules.common.utils.enums;
/**
* 单元格格式类型新增
*/
public enum CellTypeEnum {
/**
* 数字类型
*/
NUMERIC("numeric"),
/**
* 字符串类型
*/
STRING("string"),
/**
* 自定义类型:数字三位分割
*/
NUMERIC_DIV("division");
private final static String[] _formats = {
"General",//0
"0",//1
"0.00",//2
"#,##0",//3
"#,##0.00"//4
};
private String code;
CellTypeEnum(String code){
this.code = code;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public static String[] get_formats() {
return _formats;
}
}

View File

@@ -8,8 +8,10 @@ import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nl.b_lms.pdm.subpackagerelation.service.IpdmBiSubpackagerelationService;
import org.nl.common.utils.SecurityUtils;
import org.nl.modules.common.exception.BadRequestException;
import org.nl.modules.common.utils.FileUtil;
@@ -18,6 +20,7 @@ import org.nl.modules.wql.core.bean.WQLObject;
import org.nl.modules.wql.util.WqlUtil;
import org.nl.wms.sch.service.dto.PointDto;
import org.nl.wms.stat.service.OutBillQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -25,6 +28,9 @@ import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
@@ -37,6 +43,8 @@ import java.util.stream.Collectors;
@Slf4j
public class OutBillQueryServiceImpl implements OutBillQueryService {
@Autowired
private IpdmBiSubpackagerelationService ipdmBiSubpackagerelationService;
@Override
public Map<String, Object> queryAll(Map whereJson, Pageable page, String[] bill_types) {
@@ -271,7 +279,6 @@ public class OutBillQueryServiceImpl implements OutBillQueryService {
@Override
public void download2(Map map, HttpServletResponse response, String[] bill_types) throws IOException {
String stor_id = MapUtil.getStr(map, "stor_id");
String bill_type = MapUtil.getStr(map, "bill_type");
String with = MapUtil.getStr(map, "with"); // 厚度*幅宽
String begin_time = MapUtil.getStr(map, "begin_time");
@@ -312,39 +319,39 @@ public class OutBillQueryServiceImpl implements OutBillQueryService {
if (ObjectUtil.isNotEmpty(begin_time) && ObjectUtil.isNotEmpty(end_time)) {
String begin_time_today = begin_time.substring(0, 10);
String end_time_today = end_time.substring(0, 10);
// 开始时间
String today_begin_time = begin_time_today + " 08:00:00";
// 结束时间19:59:59
DateTime parse = DateUtil.parse(end_time_today);
String substring = DateUtil.offsetDay(parse, 1).toString().substring(0, 10);
String today_end_time = substring + " 07:59:59";
map.put("begin_time", today_begin_time);
map.put("end_time", today_end_time);
}
JSONArray resultJSONArray = WQL.getWO("ST_IVT_OUTBILLQUERY").addParamMap(map).process().getResultJSONArray(0);
List<JSONObject> content = resultJSONArray.toJavaList(JSONObject.class);
String pcsn_in = content.stream()
List<String> pcsns = content.stream()
.map(row -> row.getString("pcsn"))
.collect(Collectors.joining("','"));
.collect(Collectors.toList());
List<JSONObject> subList = WQLObject.getWQLObject("pdm_bi_subpackagerelationrecord")
.query("container_name IN ('" + pcsn_in + "') AND io_type = '0' ORDER BY insert_time")
.getResultJSONArray(0).toJavaList(JSONObject.class);
List<Map<String, Object>> list = new ArrayList<>();
ConcurrentLinkedDeque<List<JSONObject>> deque = new ConcurrentLinkedDeque<>();
Lists.partition(pcsns,500).stream()
.map((Function<List<String>, CompletableFuture>) o->CompletableFuture.runAsync(() -> {
List<JSONObject> subList = ipdmBiSubpackagerelationService.recordQuery(o);
deque.add(subList);
})).parallel().forEach(CompletableFuture::join);
System.out.println("导出数据:"+deque.size());
List<JSONObject> datas = new ArrayList<>();
for (List<JSONObject> jsonObjects : deque) {
datas.addAll(jsonObjects);
}
List<Map<String, String>> list = new ArrayList<>();
for (int i = 0; i < resultJSONArray.size(); i++) {
JSONObject json = resultJSONArray.getJSONObject(i);
Map<String, Object> mp = new LinkedHashMap<>();
Map<String, String> mp = new LinkedHashMap<>();
// 查询第一次入库的源销售订单
JSONObject jsonSub = subList.stream()
JSONObject jsonSub = datas.stream()
.filter(row -> row.getString("container_name").equals(json.getString("pcsn")))
.min(Comparator.comparing(row -> row.getString("insert_time")))
.orElse(null);
@@ -455,7 +462,14 @@ public class OutBillQueryServiceImpl implements OutBillQueryService {
mp.put("备注", json.getString("remark"));
list.add(mp);
}
FileUtil.downloadExcel(list, response);
Set<String> set = list.get(0).keySet();
int colIndex = 0;
String[] col = new String[set.size()];
for (String s : set) {
col[colIndex]=s;
colIndex++;
}
new FileUtil().downloadExcelIO(list,col,response);
// FileUtil.downloadExcel(list, response);
}
}