Browse Source

价格表导入

master
常熟吴彦祖 7 days ago
parent
commit
7952356e75
  1. 84
      src/main/java/com/xujie/modules/sys/service/impl/SysInitialImportServiceImpl.java

84
src/main/java/com/xujie/modules/sys/service/impl/SysInitialImportServiceImpl.java

@ -83,6 +83,8 @@ public class SysInitialImportServiceImpl implements SysInitialImportService {
private static final int MAX_IMPORT_ROWS = 1000; private static final int MAX_IMPORT_ROWS = 1000;
private static final int MAX_CONSECUTIVE_EMPTY_ROWS = 50; private static final int MAX_CONSECUTIVE_EMPTY_ROWS = 50;
private static final String SHEET_NAME = "price list"; private static final String SHEET_NAME = "price list";
// rqrq - 导入阶梯价的最后一档统一截止到 1000000(含)。
private static final BigDecimal TIER_QTY_MAX = new BigDecimal("1000000");
private static final String PART_SKU_SHEET_NAME = "Part SKU"; private static final String PART_SKU_SHEET_NAME = "Part SKU";
private static final List<String> PART_SKU_HEADERS = Arrays.asList("RAW CODE", "SKU"); private static final List<String> PART_SKU_HEADERS = Arrays.asList("RAW CODE", "SKU");
/** /**
@ -386,6 +388,10 @@ public class SysInitialImportServiceImpl implements SysInitialImportService {
DataFormatter formatter, FormulaEvaluator evaluator) { DataFormatter formatter, FormulaEvaluator evaluator) {
boolean empty = true; boolean empty = true;
for (Map.Entry<String, Integer> column : columns.entrySet()) { for (Map.Entry<String, Integer> column : columns.entrySet()) {
// rqrq - 第一档数量只是模板占位值,不参与空行判断或公式计算。
if ("TIER_QTY_1".equals(column.getKey())) {
continue;
}
if (StringUtils.hasText(readPriceV2CellText(row.getCell(column.getValue()), column.getKey(), if (StringUtils.hasText(readPriceV2CellText(row.getCell(column.getValue()), column.getKey(),
formatter, evaluator))) { formatter, evaluator))) {
empty = false; empty = false;
@ -426,8 +432,10 @@ public class SysInitialImportServiceImpl implements SysInitialImportService {
org.springframework.beans.BeanWrapper wrapper = new org.springframework.beans.BeanWrapperImpl(row); org.springframework.beans.BeanWrapper wrapper = new org.springframework.beans.BeanWrapperImpl(row);
for (int tier = 1; tier <= 8; tier++) { for (int tier = 1; tier <= 8; tier++) {
// rqrq - 显式属性名由固定的 1..8 构成,不接受用户输入属性路径。 // rqrq - 显式属性名由固定的 1..8 构成,不接受用户输入属性路径。
wrapper.setPropertyValue("tierQty" + tier, readDecimal(priceV2Cell(source, columns, "Tier_QTY_" + tier),
row.getExcelRowNo(), "Tier_QTY_" + tier, formatter, evaluator));
// rqrq - Tier_QTY_1 无论 Excel 填写什么,导入预览均显示 0。
wrapper.setPropertyValue("tierQty" + tier, tier == 1 ? BigDecimal.ZERO
: readDecimal(priceV2Cell(source, columns, "Tier_QTY_" + tier),
row.getExcelRowNo(), "Tier_QTY_" + tier, formatter, evaluator));
wrapper.setPropertyValue("priceTier" + tier, readDecimal(priceV2Cell(source, columns, "Price_Tier_" + tier), wrapper.setPropertyValue("priceTier" + tier, readDecimal(priceV2Cell(source, columns, "Price_Tier_" + tier),
row.getExcelRowNo(), "Price_Tier_" + tier, formatter, evaluator)); row.getExcelRowNo(), "Price_Tier_" + tier, formatter, evaluator));
} }
@ -551,6 +559,8 @@ public class SysInitialImportServiceImpl implements SysInitialImportService {
addPriceV2Error(row, parseError); addPriceV2Error(row, parseError);
} }
row.setRawCode(trimToEmpty(row.getRawCode())); row.setRawCode(trimToEmpty(row.getRawCode()));
// rqrq - 保存时重新规范数量1,不能依赖前端传回的占位值。
row.setTierQty1(BigDecimal.ZERO);
row.setFullName(trimToEmpty(row.getFullName())); row.setFullName(trimToEmpty(row.getFullName()));
row.setStartDate(emptyToNull(row.getStartDate())); row.setStartDate(emptyToNull(row.getStartDate()));
row.setSupplier(emptyToNull(row.getSupplier())); row.setSupplier(emptyToNull(row.getSupplier()));
@ -1301,10 +1311,18 @@ public class SysInitialImportServiceImpl implements SysInitialImportService {
boolean remarkTooLong = isOptionalTextTooLong(row.getRemarkColumnPresent(), row.getRemark()); boolean remarkTooLong = isOptionalTextTooLong(row.getRemarkColumnPresent(), row.getRemark());
boolean latestChangesTooLong = isOptionalTextTooLong( boolean latestChangesTooLong = isOptionalTextTooLong(
row.getLatestChangesColumnPresent(), row.getLatestChanges()); row.getLatestChangesColumnPresent(), row.getLatestChanges());
String tierError = null;
try {
buildTierPriceList(row);
} catch (RuntimeException ex) {
tierError = ex.getMessage();
}
row.setValid(itemExists && supplierExists && !supplierDuplicate row.setValid(itemExists && supplierExists && !supplierDuplicate
&& !remarkTooLong && !latestChangesTooLong);
row.setValidationMessage(buildValidationMessage(
itemExists, supplierExists, supplierDuplicate, remarkTooLong, latestChangesTooLong));
&& !remarkTooLong && !latestChangesTooLong && tierError == null);
String message = buildValidationMessage(
itemExists, supplierExists, supplierDuplicate, remarkTooLong, latestChangesTooLong);
row.setValidationMessage(tierError == null ? message
: (StringUtils.hasText(message) ? message + ";" : "") + tierError);
} }
} }
@ -1414,47 +1432,72 @@ public class SysInitialImportServiceImpl implements SysInitialImportService {
} }
List<SupplierPartTierPriceItemVo> buildTierPriceList(SupplierPriceInitialImportRowVo row) { List<SupplierPartTierPriceItemVo> buildTierPriceList(SupplierPriceInitialImportRowVo row) {
BigDecimal[] quantities = {
row.getTierQty1(), row.getTierQty2(), row.getTierQty3(), row.getTierQty4(),
row.getTierQty5(), row.getTierQty6(), row.getTierQty7(), row.getTierQty8()
// rqrq - Tier_QTY_1 是模板占位值,第一档始终从 0 开始;后续数量是新档起点。
BigDecimal[] nextStarts = {
row.getTierQty2(), row.getTierQty3(), row.getTierQty4(), row.getTierQty5(),
row.getTierQty6(), row.getTierQty7(), row.getTierQty8()
}; };
BigDecimal[] prices = { BigDecimal[] prices = {
row.getPriceTier1(), row.getPriceTier2(), row.getPriceTier3(), row.getPriceTier4(), row.getPriceTier1(), row.getPriceTier2(), row.getPriceTier3(), row.getPriceTier4(),
row.getPriceTier5(), row.getPriceTier6(), row.getPriceTier7(), row.getPriceTier8() row.getPriceTier5(), row.getPriceTier6(), row.getPriceTier7(), row.getPriceTier8()
}; };
List<SupplierPartTierPriceItemVo> result = new ArrayList<>();
BigDecimal quantityStart = BigDecimal.ZERO;
if (prices[0] == null) {
// rqrq - 没有第一档价格且后续档位全空时,不生成价格数据。
for (int index = 1; index < prices.length; index++) {
if (nextStarts[index - 1] != null || prices[index] != null) {
throw new RuntimeException("填写第" + (index + 1) + "档前必须填写第1档Price_Tier");
}
}
return new ArrayList<>();
}
if (prices[0].compareTo(BigDecimal.ZERO) < 0) {
throw new RuntimeException("第1档价格不能小于0");
}
List<BigDecimal> starts = new ArrayList<>();
List<BigDecimal> activePrices = new ArrayList<>();
starts.add(BigDecimal.ZERO);
activePrices.add(prices[0]);
boolean tierEnded = false; boolean tierEnded = false;
for (int index = 0; index < quantities.length; index++) {
BigDecimal quantityEnd = quantities[index];
for (int index = 1; index < prices.length; index++) {
BigDecimal quantityStart = nextStarts[index - 1];
BigDecimal unitPrice = prices[index]; BigDecimal unitPrice = prices[index];
if (quantityEnd == null && unitPrice == null) {
if (quantityStart == null && unitPrice == null) {
tierEnded = true; tierEnded = true;
continue; continue;
} }
if (tierEnded) { if (tierEnded) {
throw new RuntimeException("阶梯数量和价格不能在空档后继续填写"); throw new RuntimeException("阶梯数量和价格不能在空档后继续填写");
} }
if (quantityEnd == null || unitPrice == null) {
if (quantityStart == null || unitPrice == null) {
throw new RuntimeException("第" + (index + 1) + "档的Tier_QTY和Price_Tier必须同时填写"); throw new RuntimeException("第" + (index + 1) + "档的Tier_QTY和Price_Tier必须同时填写");
} }
if (quantityEnd.compareTo(quantityStart) < 0) {
throw new RuntimeException("第" + (index + 1) + "档数量上限必须大于上一档数量上限");
if (quantityStart.stripTrailingZeros().scale() > 0
|| quantityStart.compareTo(starts.get(starts.size() - 1)) <= 0) {
throw new RuntimeException("第" + (index + 1) + "档数量起点必须是大于上一档的整数");
}
if (quantityStart.compareTo(TIER_QTY_MAX) > 0) {
throw new RuntimeException("第" + (index + 1) + "档数量起点不能超过1000000");
} }
if (unitPrice.compareTo(BigDecimal.ZERO) < 0) { if (unitPrice.compareTo(BigDecimal.ZERO) < 0) {
throw new RuntimeException("第" + (index + 1) + "档价格不能小于0"); throw new RuntimeException("第" + (index + 1) + "档价格不能小于0");
} }
starts.add(quantityStart);
activePrices.add(unitPrice);
}
List<SupplierPartTierPriceItemVo> result = new ArrayList<>();
for (int index = 0; index < starts.size(); index++) {
BigDecimal quantityStart = starts.get(index);
BigDecimal quantityEnd = index + 1 < starts.size()
? starts.get(index + 1).subtract(BigDecimal.ONE) : TIER_QTY_MAX;
SupplierPartTierPriceItemVo tierPrice = new SupplierPartTierPriceItemVo(); SupplierPartTierPriceItemVo tierPrice = new SupplierPartTierPriceItemVo();
// Keep every row at the target SQL Server scale. Mixed BigDecimal scales in a // Keep every row at the target SQL Server scale. Mixed BigDecimal scales in a
// multi-row VALUES statement can otherwise be inferred as the lowest scale. // multi-row VALUES statement can otherwise be inferred as the lowest scale.
tierPrice.setQtyStart(quantityStart.setScale(4, RoundingMode.HALF_UP)); tierPrice.setQtyStart(quantityStart.setScale(4, RoundingMode.HALF_UP));
tierPrice.setQtyEnd(quantityEnd.setScale(4, RoundingMode.HALF_UP)); tierPrice.setQtyEnd(quantityEnd.setScale(4, RoundingMode.HALF_UP));
tierPrice.setUnitPrice(unitPrice.setScale(6, RoundingMode.HALF_UP));
tierPrice.setUnitPrice(activePrices.get(index).setScale(6, RoundingMode.HALF_UP));
tierPrice.setRemark(""); tierPrice.setRemark("");
result.add(tierPrice); result.add(tierPrice);
quantityStart = quantityEnd.add(BigDecimal.ONE);
} }
return result; return result;
} }
@ -1603,7 +1646,8 @@ public class SysInitialImportServiceImpl implements SysInitialImportService {
value.setQtyPo(readDecimal(row.getCell(3), excelRowNo, HEADERS.get(3), formatter, evaluator)); value.setQtyPo(readDecimal(row.getCell(3), excelRowNo, HEADERS.get(3), formatter, evaluator));
value.setQtyCarton(readDecimal(row.getCell(4), excelRowNo, HEADERS.get(4), formatter, evaluator)); value.setQtyCarton(readDecimal(row.getCell(4), excelRowNo, HEADERS.get(4), formatter, evaluator));
value.setStartDate(readDate(row.getCell(5), excelRowNo, formatter, evaluator)); value.setStartDate(readDate(row.getCell(5), excelRowNo, formatter, evaluator));
value.setTierQty1(readDecimal(row.getCell(6), excelRowNo, HEADERS.get(6), formatter, evaluator));
// rqrq - 旧模板的 Tier_QTY_1 同样只作占位,导入时固定为 0。
value.setTierQty1(BigDecimal.ZERO);
value.setTierQty2(readDecimal(row.getCell(7), excelRowNo, HEADERS.get(7), formatter, evaluator)); value.setTierQty2(readDecimal(row.getCell(7), excelRowNo, HEADERS.get(7), formatter, evaluator));
value.setTierQty3(readDecimal(row.getCell(8), excelRowNo, HEADERS.get(8), formatter, evaluator)); value.setTierQty3(readDecimal(row.getCell(8), excelRowNo, HEADERS.get(8), formatter, evaluator));
value.setTierQty4(readDecimal(row.getCell(9), excelRowNo, HEADERS.get(9), formatter, evaluator)); value.setTierQty4(readDecimal(row.getCell(9), excelRowNo, HEADERS.get(9), formatter, evaluator));

Loading…
Cancel
Save