|
|
@@ -95,7 +95,7 @@ public class SalesOrderController {
|
|
|
private final GeneratedNumberUitls generatedNumberUitls;
|
|
|
|
|
|
|
|
|
- // ====================== 功能1:下载可下单物料模板 ======================
|
|
|
+ // ====================== 功能1:下载可下单物料模板(重构版) ======================
|
|
|
@GetMapping("/downloadTemplate")
|
|
|
@ApiOperation(value = "下载订单导入模板", notes = "模板包含所有可下单物料,字段:物料号、物料名称、品牌名、规格、花纹、订单数量")
|
|
|
public void downloadTemplate(HttpServletResponse response) throws IOException {
|
|
|
@@ -124,34 +124,100 @@ public class SalesOrderController {
|
|
|
sheet.setColumnWidth(i, sheet.getColumnWidth(i) + 2048); // 加宽列宽
|
|
|
}
|
|
|
|
|
|
- // 3. 查询所有可下单物料(核心:可下单=有库存+当前用户可访问)
|
|
|
- Map<String, Object> res = getUserLinkGoodsPro();
|
|
|
- // 修复1:Gson转换(兼容原有写法,避免类型强转报错)
|
|
|
- List<PjpfStockDesc> pjpfStockDescList = new ArrayList<>();
|
|
|
- if (res.get("pjpfStockDescList") != null && res.get("pjpfStockDescList") instanceof List) {
|
|
|
- // 直接强转,保留你原有逻辑,避免Gson转换复杂问题
|
|
|
- pjpfStockDescList = (List<PjpfStockDesc>) res.get("pjpfStockDescList");
|
|
|
+ // ========== 核心重构:直接使用原始实体拼接数据,无需中间实体 ==========
|
|
|
+ // 存储最终导出的物料数据(物料号、名称、品牌、规格、花纹)
|
|
|
+ List<Map<String, String>> exportDataList = new ArrayList<>();
|
|
|
+ try {
|
|
|
+ // 1. 获取当前登录用户ID(空值处理)
|
|
|
+ Long userId = AuthUtil.getUserId();
|
|
|
+ if (userId == null) {
|
|
|
+ System.err.println("未获取到当前登录用户ID");
|
|
|
+ } else {
|
|
|
+ // 2. 获取用户信息(空值处理)
|
|
|
+ R<User> userInfoRes = userClient.userInfoById(userId);
|
|
|
+ if (userInfoRes == null || userInfoRes.getData() == null || userInfoRes.getData().getCustomerId() == null) {
|
|
|
+ System.err.println("用户信息为空或无Customer_ID");
|
|
|
+ } else {
|
|
|
+ // 3. 查询用户关联的客户信息
|
|
|
+ Long customerId = userInfoRes.getData().getCustomerId();
|
|
|
+ ViewCustomerSel viewCustomerSel = zcrmViewCustomerSelService.selectZcrmViewCustomerSelByCustomerId(customerId);
|
|
|
+ if (viewCustomerSel == null) {
|
|
|
+ System.err.println("未查询到用户关联的客户信息,Customer_ID:" + customerId);
|
|
|
+ } else {
|
|
|
+ // 4. 获取用户关联的品牌列表
|
|
|
+ String pubDescSeg4Name = viewCustomerSel.getPubDescSeg4Name();
|
|
|
+ if (pubDescSeg4Name == null || pubDescSeg4Name.isEmpty()) {
|
|
|
+ System.err.println("用户关联的品牌名称为空");
|
|
|
+ } else {
|
|
|
+ // 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() : "");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 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());
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
|
|
|
- // 4. 填充可下单物料数据(订单数量留空)
|
|
|
+ // 4. 填充可下单物料数据
|
|
|
int rowNum = 1;
|
|
|
CellStyle dataStyle = workbook.createCellStyle();
|
|
|
dataStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
|
|
|
- if (pjpfStockDescList != null && !pjpfStockDescList.isEmpty()) {
|
|
|
- for (PjpfStockDesc stock : pjpfStockDescList) {
|
|
|
+ if (!exportDataList.isEmpty()) {
|
|
|
+ for (Map<String, String> dataMap : exportDataList) {
|
|
|
Row row = sheet.createRow(rowNum++);
|
|
|
// 物料号
|
|
|
- row.createCell(0).setCellValue(stock.getCode() == null ? "" : stock.getCode());
|
|
|
+ row.createCell(0).setCellValue(dataMap.get("itemCode"));
|
|
|
// 物料名称
|
|
|
- row.createCell(1).setCellValue(stock.getCname() == null ? "" : stock.getCname());
|
|
|
- // 品牌名
|
|
|
- row.createCell(2).setCellValue(stock.getBrandName() == null ? "" : stock.getBrandName());
|
|
|
+ row.createCell(1).setCellValue(dataMap.get("itemName"));
|
|
|
+ // 品牌名(直接从映射表获取)
|
|
|
+ row.createCell(2).setCellValue(dataMap.get("brandName"));
|
|
|
// 规格
|
|
|
- row.createCell(3).setCellValue(stock.getTypeNo() == null ? "" : stock.getTypeNo());
|
|
|
- // 花纹
|
|
|
- row.createCell(4).setCellValue(stock.getBrandItem() == null ? "" : stock.getBrandItem());
|
|
|
- // 订单数量(留空,用户填写)
|
|
|
+ row.createCell(3).setCellValue(dataMap.get("itemPecs"));
|
|
|
+ // 花纹(直接从映射表获取)
|
|
|
+ row.createCell(4).setCellValue(dataMap.get("pattern"));
|
|
|
+ // 订单数量(默认留空,也可填0,根据你的需求调整)
|
|
|
row.createCell(5).setCellValue("");
|
|
|
// 统一设置样式
|
|
|
for (int i = 0; i < 6; i++) {
|
|
|
@@ -159,20 +225,23 @@ public class SalesOrderController {
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
- // 无可用物料提示
|
|
|
+ // 无可用物料提示(增强提示信息)
|
|
|
Row emptyRow = sheet.createRow(1);
|
|
|
Cell emptyCell = emptyRow.createCell(0);
|
|
|
- emptyCell.setCellValue("暂无可用下单的物料");
|
|
|
+ emptyCell.setCellValue("暂无可用下单的物料(无库存或当前用户无关联物料)");
|
|
|
emptyCell.setCellStyle(dataStyle);
|
|
|
+ // 订单数量列留空
|
|
|
+ emptyRow.createCell(5).setCellValue("");
|
|
|
+ emptyRow.getCell(5).setCellStyle(dataStyle);
|
|
|
}
|
|
|
|
|
|
- // 5. 触发下载
|
|
|
+ // 5. 触发下载(解决中文乱码+规范响应头)
|
|
|
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
|
|
response.setCharacterEncoding("UTF-8");
|
|
|
String fileName = URLEncoder.encode("可下单物料订单导入模板.xlsx", StandardCharsets.UTF_8.toString());
|
|
|
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
|
|
|
|
|
|
- // 6. 写出Excel
|
|
|
+ // 6. 写出Excel(try-with-resources自动释放资源)
|
|
|
try (OutputStream outputStream = response.getOutputStream()) {
|
|
|
workbook.write(outputStream);
|
|
|
outputStream.flush();
|
|
|
@@ -183,6 +252,94 @@ public class SalesOrderController {
|
|
|
|
|
|
|
|
|
|
|
|
+// // ====================== 功能1:下载可下单物料模板 ======================
|
|
|
+// @GetMapping("/downloadTemplate")
|
|
|
+// @ApiOperation(value = "下载订单导入模板", notes = "模板包含所有可下单物料,字段:物料号、物料名称、品牌名、规格、花纹、订单数量")
|
|
|
+// public void downloadTemplate(HttpServletResponse response) throws IOException {
|
|
|
+// // 1. 创建Excel工作簿
|
|
|
+// Workbook workbook = new XSSFWorkbook();
|
|
|
+// Sheet sheet = workbook.createSheet("可下单物料订单模板");
|
|
|
+//
|
|
|
+// // 2. 构建表头
|
|
|
+// Row headerRow = sheet.createRow(0);
|
|
|
+// String[] headers = {"物料号", "物料名称", "品牌名", "规格", "花纹", "订单数量"};
|
|
|
+// // 表头样式
|
|
|
+// CellStyle headerStyle = workbook.createCellStyle();
|
|
|
+// Font font = workbook.createFont();
|
|
|
+// font.setBold(true);
|
|
|
+// font.setFontName("微软雅黑");
|
|
|
+// font.setFontHeightInPoints((short) 12);
|
|
|
+// headerStyle.setFont(font);
|
|
|
+// headerStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+//
|
|
|
+// // 填充表头
|
|
|
+// for (int i = 0; i < headers.length; i++) {
|
|
|
+// Cell cell = headerRow.createCell(i);
|
|
|
+// cell.setCellValue(headers[i]);
|
|
|
+// cell.setCellStyle(headerStyle);
|
|
|
+// sheet.autoSizeColumn(i);
|
|
|
+// sheet.setColumnWidth(i, sheet.getColumnWidth(i) + 2048); // 加宽列宽
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 3. 查询所有可下单物料(核心:可下单=有库存+当前用户可访问)
|
|
|
+// Map<String, Object> res = getUserLinkGoodsPro();
|
|
|
+// // 修复1:Gson转换(兼容原有写法,避免类型强转报错)
|
|
|
+// List<PjpfStockDesc> pjpfStockDescList = new ArrayList<>();
|
|
|
+// if (res.get("pjpfStockDescList") != null && res.get("pjpfStockDescList") instanceof List) {
|
|
|
+// // 直接强转,保留你原有逻辑,避免Gson转换复杂问题
|
|
|
+// pjpfStockDescList = (List<PjpfStockDesc>) res.get("pjpfStockDescList");
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 4. 填充可下单物料数据(订单数量留空)
|
|
|
+// int rowNum = 1;
|
|
|
+// CellStyle dataStyle = workbook.createCellStyle();
|
|
|
+// dataStyle.setAlignment(HorizontalAlignment.CENTER);
|
|
|
+//
|
|
|
+// if (pjpfStockDescList != null && !pjpfStockDescList.isEmpty()) {
|
|
|
+// for (PjpfStockDesc stock : pjpfStockDescList) {
|
|
|
+// Row row = sheet.createRow(rowNum++);
|
|
|
+// // 物料号
|
|
|
+// row.createCell(0).setCellValue(stock.getCode() == null ? "" : stock.getCode());
|
|
|
+// // 物料名称
|
|
|
+// row.createCell(1).setCellValue(stock.getCname() == null ? "" : stock.getCname());
|
|
|
+// // 品牌名
|
|
|
+// row.createCell(2).setCellValue(stock.getBrandName() == null ? "" : stock.getBrandName());
|
|
|
+// // 规格
|
|
|
+// row.createCell(3).setCellValue(stock.getTypeNo() == null ? "" : stock.getTypeNo());
|
|
|
+// // 花纹
|
|
|
+// row.createCell(4).setCellValue(stock.getBrandItem() == null ? "" : stock.getBrandItem());
|
|
|
+// // 订单数量(留空,用户填写)
|
|
|
+// row.createCell(5).setCellValue("");
|
|
|
+// // 统一设置样式
|
|
|
+// for (int i = 0; i < 6; i++) {
|
|
|
+// row.getCell(i).setCellStyle(dataStyle);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// } else {
|
|
|
+// // 无可用物料提示
|
|
|
+// Row emptyRow = sheet.createRow(1);
|
|
|
+// Cell emptyCell = emptyRow.createCell(0);
|
|
|
+// emptyCell.setCellValue("暂无可用下单的物料");
|
|
|
+// emptyCell.setCellStyle(dataStyle);
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 5. 触发下载
|
|
|
+// response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
|
|
+// response.setCharacterEncoding("UTF-8");
|
|
|
+// String fileName = URLEncoder.encode("可下单物料订单导入模板.xlsx", StandardCharsets.UTF_8.toString());
|
|
|
+// response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
|
|
|
+//
|
|
|
+// // 6. 写出Excel
|
|
|
+// try (OutputStream outputStream = response.getOutputStream()) {
|
|
|
+// workbook.write(outputStream);
|
|
|
+// outputStream.flush();
|
|
|
+// } finally {
|
|
|
+// workbook.close();
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* 用户关联品牌和商品业务接口
|