|
|
@@ -14,6 +14,7 @@ import io.swagger.annotations.ApiOperation;
|
|
|
import io.swagger.annotations.ApiParam;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import org.apache.poi.ss.usermodel.*;
|
|
|
+import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
|
|
import org.springblade.core.mp.support.Condition;
|
|
|
import org.springblade.core.mp.support.Query;
|
|
|
@@ -128,75 +129,117 @@ public class SalesOrderController {
|
|
|
// 存储最终导出的物料数据(物料号、名称、品牌、规格、花纹)
|
|
|
List<Map<String, String>> exportDataList = new ArrayList<>();
|
|
|
try {
|
|
|
- // 1. 获取当前登录用户ID(空值处理)
|
|
|
+ // 1. 获取当前登录用户ID(空值处理+详细日志)
|
|
|
Long userId = AuthUtil.getUserId();
|
|
|
if (userId == null) {
|
|
|
- System.err.println("未获取到当前登录用户ID");
|
|
|
+ System.err.println("【下载模板】未获取到当前登录用户ID");
|
|
|
} else {
|
|
|
- // 2. 获取用户信息(空值处理)
|
|
|
+ System.err.println("【下载模板】当前登录用户ID:" + userId);
|
|
|
+
|
|
|
+ // 2. 获取用户信息(空值处理+详细日志)
|
|
|
R<User> userInfoRes = userClient.userInfoById(userId);
|
|
|
- if (userInfoRes == null || userInfoRes.getData() == null || userInfoRes.getData().getCustomerId() == null) {
|
|
|
- System.err.println("用户信息为空或无Customer_ID");
|
|
|
+ if (userInfoRes == null) {
|
|
|
+ System.err.println("【下载模板】用户信息接口返回null,用户ID:" + userId);
|
|
|
+ } else if (!userInfoRes.isSuccess()) {
|
|
|
+ System.err.println("【下载模板】用户信息接口调用失败,msg:" + userInfoRes.getMsg());
|
|
|
+ } else if (userInfoRes.getData() == null) {
|
|
|
+ System.err.println("【下载模板】用户信息为空,用户ID:" + userId);
|
|
|
+ } else if (userInfoRes.getData().getCustomerId() == null) {
|
|
|
+ System.err.println("【下载模板】用户无关联Customer_ID,用户ID:" + userId);
|
|
|
} else {
|
|
|
// 3. 查询用户关联的客户信息
|
|
|
Long customerId = userInfoRes.getData().getCustomerId();
|
|
|
+ System.err.println("【下载模板】用户关联Customer_ID:" + customerId);
|
|
|
+
|
|
|
ViewCustomerSel viewCustomerSel = zcrmViewCustomerSelService.selectZcrmViewCustomerSelByCustomerId(customerId);
|
|
|
if (viewCustomerSel == null) {
|
|
|
- System.err.println("未查询到用户关联的客户信息,Customer_ID:" + customerId);
|
|
|
+ System.err.println("【下载模板】未查询到用户关联的客户信息,Customer_ID:" + customerId);
|
|
|
} else {
|
|
|
// 4. 获取用户关联的品牌列表
|
|
|
String pubDescSeg4Name = viewCustomerSel.getPubDescSeg4Name();
|
|
|
+ System.err.println("【下载模板】用户关联品牌原始字符串:" + pubDescSeg4Name);
|
|
|
+
|
|
|
if (pubDescSeg4Name == null || pubDescSeg4Name.isEmpty()) {
|
|
|
- System.err.println("用户关联的品牌名称为空");
|
|
|
+ System.err.println("【下载模板】用户关联的品牌名称为空");
|
|
|
} else {
|
|
|
- // 5. 拆分品牌编码数组
|
|
|
+ // 5. 拆分品牌编码数组(处理空字符串/多余分号)
|
|
|
String[] codeArray = pubDescSeg4Name.split(";");
|
|
|
- System.err.println("用户关联品牌列表:" + Arrays.toString(codeArray));
|
|
|
-
|
|
|
- // 6. 查询料品档案(获取品牌名、花纹)
|
|
|
- QueryWrapper<ViewItemSel> itemQuery = new QueryWrapper<>();
|
|
|
- itemQuery.in(codeArray.length > 0, "PubDescSeg4_Name", codeArray);
|
|
|
- List<ViewItemSel> viewItemSelList = zcrmViewItemSelService.list(itemQuery);
|
|
|
- System.err.println("料品档案查询结果数量:" + viewItemSelList.size());
|
|
|
-
|
|
|
- // 7. 构建物料编码映射表(核心:关联品牌名、花纹)
|
|
|
- Map<String, String> itemBrandMap = new HashMap<>(); // 物料编码→品牌名(PubDescSeg4_Name)
|
|
|
- Map<String, String> itemPatternMap = new HashMap<>(); // 物料编码→花纹(pattern)
|
|
|
- for (ViewItemSel item : viewItemSelList) {
|
|
|
- if (item.getItemCode() != null) {
|
|
|
- itemBrandMap.put(item.getItemCode(), item.getPubDescSeg4Name() != null ? item.getPubDescSeg4Name() : "");
|
|
|
- itemPatternMap.put(item.getItemCode(), item.getPattern() != null ? item.getPattern() : "");
|
|
|
+ // 过滤空字符串元素
|
|
|
+ codeArray = Arrays.stream(codeArray).filter(StringUtils::isNotBlank).toArray(String[]::new);
|
|
|
+ System.err.println("【下载模板】用户关联品牌列表(去空后):" + Arrays.toString(codeArray));
|
|
|
+
|
|
|
+ if (codeArray.length == 0) {
|
|
|
+ System.err.println("【下载模板】品牌列表拆分后为空");
|
|
|
+ } else {
|
|
|
+ // 6. 查询料品档案(获取品牌名、花纹)
|
|
|
+ QueryWrapper<ViewItemSel> itemQuery = new QueryWrapper<>();
|
|
|
+ itemQuery.in("PubDescSeg4_Name", codeArray);
|
|
|
+ List<ViewItemSel> viewItemSelList = zcrmViewItemSelService.list(itemQuery);
|
|
|
+ System.err.println("【下载模板】料品档案查询结果数量:" + (viewItemSelList == null ? 0 : viewItemSelList.size()));
|
|
|
+
|
|
|
+ if (CollectionUtils.isEmpty(viewItemSelList)) {
|
|
|
+ System.err.println("【下载模板】未查询到对应品牌的料品档案");
|
|
|
+ } else {
|
|
|
+ // 7. 构建物料编码映射表(核心:关联品牌名、花纹)
|
|
|
+ Map<String, String> itemBrandMap = new HashMap<>(); // 物料编码→品牌名(PubDescSeg4_Name)
|
|
|
+ Map<String, String> itemPatternMap = new HashMap<>(); // 物料编码→花纹(pattern)
|
|
|
+
|
|
|
+ int validItemCount = 0;
|
|
|
+ for (ViewItemSel item : viewItemSelList) {
|
|
|
+ if (item.getItemCode() != null) {
|
|
|
+ itemBrandMap.put(item.getItemCode(), item.getPubDescSeg4Name() != null ? item.getPubDescSeg4Name() : "");
|
|
|
+ itemPatternMap.put(item.getItemCode(), item.getPattern() != null ? item.getPattern() : "");
|
|
|
+ validItemCount++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ System.err.println("【下载模板】有效物料编码映射数量:" + validItemCount);
|
|
|
+
|
|
|
+ // 8. 查询有库存的物料(可下单=库存>0)
|
|
|
+ String[] itemCodeArray = viewItemSelList.stream()
|
|
|
+ .map(ViewItemSel::getItemCode)
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .toArray(String[]::new);
|
|
|
+ System.err.println("【下载模板】待查询库存的物料编码数量:" + itemCodeArray.length);
|
|
|
+
|
|
|
+ QueryWrapper<ViewWhqohSel> stockQuery = new QueryWrapper<>();
|
|
|
+ stockQuery.in(itemCodeArray.length > 0, "Item_Code", itemCodeArray)
|
|
|
+ .gt("StoreQty", 0); // 只查库存>0的可下单物料
|
|
|
+ List<ViewWhqohSel> viewWhqohSelList = zcrmViewWhqohSelService.list(stockQuery);
|
|
|
+ System.err.println("【下载模板】可下单库存物料数量:" + (viewWhqohSelList == null ? 0 : viewWhqohSelList.size()));
|
|
|
+
|
|
|
+ if (!CollectionUtils.isEmpty(viewWhqohSelList)) {
|
|
|
+ // 9. 拼接导出数据(库存+料品字段)
|
|
|
+ int exportCount = 0;
|
|
|
+ for (ViewWhqohSel stock : viewWhqohSelList) {
|
|
|
+ if (stock.getItemCode() == null) {
|
|
|
+ System.err.println("【下载模板】库存物料编码为空,跳过该条数据");
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, String> dataMap = new HashMap<>();
|
|
|
+ dataMap.put("itemCode", stock.getItemCode()); // 物料号
|
|
|
+ dataMap.put("itemName", stock.getItemName() != null ? stock.getItemName() : ""); // 物料名称
|
|
|
+ dataMap.put("brandName", itemBrandMap.getOrDefault(stock.getItemCode(), "")); // 品牌名
|
|
|
+ dataMap.put("itemPecs", stock.getItemPecs() != null ? stock.getItemPecs() : ""); // 规格
|
|
|
+ dataMap.put("pattern", itemPatternMap.getOrDefault(stock.getItemCode(), "")); // 花纹
|
|
|
+
|
|
|
+ exportDataList.add(dataMap);
|
|
|
+ exportCount++;
|
|
|
+ }
|
|
|
+ System.err.println("【下载模板】最终拼接的导出数据数量:" + exportCount);
|
|
|
+ } else {
|
|
|
+ System.err.println("【下载模板】无库存>0的物料");
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- // 8. 查询有库存的物料(可下单=库存>0)
|
|
|
- String[] itemCodeArray = viewItemSelList.stream()
|
|
|
- .map(ViewItemSel::getItemCode)
|
|
|
- .filter(Objects::nonNull)
|
|
|
- .toArray(String[]::new);
|
|
|
- QueryWrapper<ViewWhqohSel> stockQuery = new QueryWrapper<>();
|
|
|
- stockQuery.in(itemCodeArray.length > 0, "Item_Code", itemCodeArray)
|
|
|
- .gt("StoreQty", 0); // 只查库存>0的可下单物料
|
|
|
- List<ViewWhqohSel> viewWhqohSelList = zcrmViewWhqohSelService.list(stockQuery);
|
|
|
- System.err.println("可下单库存物料数量:" + viewWhqohSelList.size());
|
|
|
-
|
|
|
- // 9. 拼接导出数据(库存+料品字段)
|
|
|
- for (ViewWhqohSel stock : viewWhqohSelList) {
|
|
|
- Map<String, String> dataMap = new HashMap<>();
|
|
|
- dataMap.put("itemCode", stock.getItemCode() != null ? stock.getItemCode() : ""); // 物料号
|
|
|
- dataMap.put("itemName", stock.getItemName() != null ? stock.getItemName() : ""); // 物料名称
|
|
|
- dataMap.put("brandName", itemBrandMap.getOrDefault(stock.getItemCode(), "")); // 品牌名
|
|
|
- dataMap.put("itemPecs", stock.getItemPecs() != null ? stock.getItemPecs() : ""); // 规格
|
|
|
- dataMap.put("pattern", itemPatternMap.getOrDefault(stock.getItemCode(), "")); // 花纹
|
|
|
- exportDataList.add(dataMap);
|
|
|
- }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
- System.err.println("查询可下单物料数据异常:" + e.getMessage());
|
|
|
+ System.err.println("【下载模板】查询可下单物料数据异常:" + e.getMessage());
|
|
|
e.printStackTrace();
|
|
|
+ // 异常时仍继续导出,避免Excel下载失败
|
|
|
}
|
|
|
|
|
|
// 4. 填充可下单物料数据
|
|
|
@@ -204,6 +247,7 @@ public class SalesOrderController {
|
|
|
CellStyle dataStyle = workbook.createCellStyle();
|
|
|
dataStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
|
|
+ System.err.println("【下载模板】准备导出的数据数量:" + exportDataList.size());
|
|
|
if (!exportDataList.isEmpty()) {
|
|
|
for (Map<String, String> dataMap : exportDataList) {
|
|
|
Row row = sheet.createRow(rowNum++);
|
|
|
@@ -228,11 +272,10 @@ public class SalesOrderController {
|
|
|
// 无可用物料提示(增强提示信息)
|
|
|
Row emptyRow = sheet.createRow(1);
|
|
|
Cell emptyCell = emptyRow.createCell(0);
|
|
|
+ // 合并单元格,让提示更美观
|
|
|
+ sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 5));
|
|
|
emptyCell.setCellValue("暂无可用下单的物料(无库存或当前用户无关联物料)");
|
|
|
emptyCell.setCellStyle(dataStyle);
|
|
|
- // 订单数量列留空
|
|
|
- emptyRow.createCell(5).setCellValue("");
|
|
|
- emptyRow.getCell(5).setCellStyle(dataStyle);
|
|
|
}
|
|
|
|
|
|
// 5. 触发下载(解决中文乱码+规范响应头)
|
|
|
@@ -240,6 +283,9 @@ public class SalesOrderController {
|
|
|
response.setCharacterEncoding("UTF-8");
|
|
|
String fileName = URLEncoder.encode("可下单物料订单导入模板.xlsx", StandardCharsets.UTF_8.toString());
|
|
|
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
|
|
|
+ response.setHeader("Pragma", "no-cache");
|
|
|
+ response.setHeader("Cache-Control", "no-cache");
|
|
|
+ response.setDateHeader("Expires", 0);
|
|
|
|
|
|
// 6. 写出Excel(try-with-resources自动释放资源)
|
|
|
try (OutputStream outputStream = response.getOutputStream()) {
|