新增产品和附加费删除标记,优化版本编辑逻辑

This commit is contained in:
wuweihua
2026-11-16 14:54:19 +08:00
parent 4503424cf0
commit 215effe036
10 changed files with 418 additions and 166 deletions
@@ -107,4 +107,7 @@ public class LblProduct extends BaseEntity
/** 邮编→分区(保存时同步到 prc_zone,不落产品子表) */
private Map<String, List<String>> zonePostcodes;
/** 编辑保存删除标记(不落库) */
private Boolean deleted;
}
@@ -59,4 +59,7 @@ public class LblSurcharge
private BigDecimal weightMax;
private Integer sortNo;
/** 编辑保存删除标记(不落库) */
private Boolean deleted;
}
@@ -57,4 +57,7 @@ public class LblWeightBand
private String extraPriceUnit;
private Integer sortNo;
/** 编辑保存删除标记(不落库) */
private Boolean deleted;
}
@@ -310,6 +310,8 @@ public final class LblBandNormalizer
private static LblWeightBand copyBand(LblWeightBand src)
{
LblWeightBand b = new LblWeightBand();
b.setBandId(src.getBandId());
b.setDeleted(src.getDeleted());
b.setProductId(src.getProductId());
b.setVersionId(src.getVersionId());
b.setSectionName(src.getSectionName());
@@ -36,6 +36,8 @@ public interface LblProductMapper
int deleteAllVersionsBySetAndNo(@Param("priceSetId") Long priceSetId, @Param("versionNo") Integer versionNo);
int deleteVersionById(@Param("versionId") Long versionId);
List<LblProductVersion> selectVersionsBySet(@Param("priceSetId") Long priceSetId);
List<LblProductVersion> selectVersionsBySetAndNo(@Param("priceSetId") Long priceSetId,
@@ -56,6 +58,8 @@ public interface LblProductMapper
int deleteWeightBandsByVersion(@Param("versionId") Long versionId);
int deleteWeightBandById(@Param("bandId") Long bandId);
List<LblWeightBand> selectWeightBandsByVersion(@Param("versionId") Long versionId);
int insertSurcharge(LblSurcharge surcharge);
@@ -66,6 +70,8 @@ public interface LblProductMapper
int deleteSurchargesByVersion(@Param("versionId") Long versionId);
int deleteSurchargeById(@Param("surchargeId") Long surchargeId);
List<LblSurcharge> selectSurchargesByVersion(@Param("versionId") Long versionId);
List<Long> selectVersionIdsBySetAndProducts(@Param("priceSetId") Long priceSetId, @Param("productIds") Long[] productIds);
@@ -215,15 +215,11 @@ public class LblProductServiceImpl implements ILblProductService
Long priceSetId = ensureLabelPriceSet(command);
report(listener, 8, "套餐 " + priceSetId + " 就绪,开始落库产品");
String operator = SecurityUtils.getUsername();
Date keepStart = null;
String keepStatus = TERM_PENDING;
int nextVersionNo;
boolean editPendingVersion = command.getVersionNo() != null;
if (editPendingVersion)
boolean editVersion = command.getVersionNo() != null;
if (editVersion)
{
VersionKeep keep = clearEditableVersion(priceSetId, command.getVersionNo(), operator);
keepStart = keep.effectiveStart;
keepStatus = keep.status == null ? TERM_PENDING : keep.status;
assertEditableVersionExists(priceSetId, command.getVersionNo());
nextVersionNo = command.getVersionNo();
}
else
@@ -234,13 +230,24 @@ public class LblProductServiceImpl implements ILblProductService
int saved = 0;
int replaced = 0;
int skipped = 0;
int removed = 0;
List<LblProduct> products = command.getProducts();
int total = products.size();
Map<String, Map<String, List<String>>> zoneSyncByCountry = new LinkedHashMap<String, Map<String, List<String>>>();
for (int i = 0; i < products.size(); i++)
{
LblProduct product = products.get(i);
if (product == null || isBlank(product.getProductName()) || isBlank(product.getSheetName())
if (product == null)
{
throw PaymentMesEnum.BMS_PAY_PROD_FIELD.exception();
}
if (editVersion && Boolean.TRUE.equals(product.getDeleted()))
{
removeEditVersionProduct(priceSetId, nextVersionNo, product);
removed++;
continue;
}
if (isBlank(product.getProductName()) || isBlank(product.getSheetName())
|| isBlank(product.getFileMd5()))
{
throw PaymentMesEnum.BMS_PAY_PROD_FIELD.exception();
@@ -248,7 +255,15 @@ public class LblProductServiceImpl implements ILblProductService
String displayName = blankTo(product.getProductName(), product.getSheetName());
int pct = 10 + (int) Math.floor((i * 75.0) / Math.max(total, 1));
report(listener, pct, "正在保存「" + displayName + "」(" + (i + 1) + "/" + total + ")");
LblBandNormalizer.normalizeProduct(product);
if (editVersion && product.getVersionId() != null)
{
// 编辑已有版本:不做拆合归一,保留 bandId/surchargeId
fillBandDefaults(product);
}
else
{
LblBandNormalizer.normalizeProduct(product);
}
String dict = blankTo(product.getDictValue(), product.getProductName());
product.setDictValue(dict);
Long existsId = lblProductMapper.selectProductIdBySetAndDict(priceSetId, dict);
@@ -257,7 +272,7 @@ public class LblProductServiceImpl implements ILblProductService
LblProduct byDict = lblProductMapper.selectProductByDict(dict);
existsId = byDict == null ? null : byDict.getProductId();
}
boolean hasPending = !editPendingVersion && existsId != null && hasPending(priceSetId, existsId);
boolean hasPending = !editVersion && existsId != null && hasPending(priceSetId, existsId);
if (hasPending && !command.isOverwrite())
{
skipped++;
@@ -270,20 +285,36 @@ public class LblProductServiceImpl implements ILblProductService
product.setCreateTime(new Date());
if (existsId != null)
{
if (editPendingVersion || command.isOverwrite())
{
lblProductMapper.closePendingBySetProduct(priceSetId, existsId, yesterday(), operator);
}
product.setProductId(existsId);
lblProductMapper.updateProduct(product);
openVersion(product, priceSetId, nextVersionNo, operator);
replaced++;
if (editVersion && product.getVersionId() != null)
{
assertVersionBelongs(product.getVersionId(), priceSetId, nextVersionNo, existsId);
upsertDetails(product, product.getVersionId());
}
else
{
if (!editVersion && command.isOverwrite())
{
lblProductMapper.closePendingBySetProduct(priceSetId, existsId, yesterday(), operator);
}
openVersion(product, priceSetId, nextVersionNo, operator);
if (editVersion)
{
alignNewVersionMeta(priceSetId, nextVersionNo, product.getVersionId(), operator);
}
replaced++;
}
}
else
{
product.setProductId(null);
lblProductMapper.insertProduct(product);
openVersion(product, priceSetId, nextVersionNo, operator);
if (editVersion)
{
alignNewVersionMeta(priceSetId, nextVersionNo, product.getVersionId(), operator);
}
}
saved++;
collectZoneSync(zoneSyncByCountry, product);
@@ -293,20 +324,9 @@ public class LblProductServiceImpl implements ILblProductService
{
prcZoneService.syncCountryZones(e.getKey(), e.getValue());
}
if (editPendingVersion && keepStart != null)
{
List<LblProductVersion> rows = lblProductMapper.selectVersionsBySetAndNo(priceSetId, nextVersionNo);
for (LblProductVersion row : rows)
{
row.setEffectiveStart(keepStart);
row.setStatus(keepStatus);
row.setUpdateBy(operator);
lblProductMapper.updateVersion(row);
}
}
report(listener, 98, "保存完成,正在收尾");
return editPendingVersion
? ("套餐" + priceSetId + "版本V" + nextVersionNo + "已更新" + saved + "个产品")
return editVersion
? ("套餐" + priceSetId + "版本V" + nextVersionNo + "已更新" + saved + "个产品,删除" + removed + "个")
: ("套餐" + priceSetId + "已保存" + saved + "个产品,新版本" + replaced + "个,跳过" + skipped + "个");
}
@@ -675,6 +695,230 @@ public class LblProductServiceImpl implements ILblProductService
}
}
private void assertEditableVersionExists(Long priceSetId, Integer versionNo)
{
List<LblProductVersion> rows = lblProductMapper.selectVersionsBySetAndNo(priceSetId, versionNo);
if (rows == null || rows.isEmpty())
{
throw new ServiceException("版本不存在");
}
for (LblProductVersion row : rows)
{
if (!TERM_PENDING.equals(row.getStatus()) && !TERM_EFFECT.equals(row.getStatus()))
{
throw new ServiceException("只能编辑待生效或已生效版本");
}
}
}
private void assertVersionBelongs(Long versionId, Long priceSetId, int versionNo, Long productId)
{
LblProductVersion ver = lblProductMapper.selectVersionById(versionId);
if (ver == null)
{
throw new ServiceException("价卡版本不存在");
}
if (!priceSetId.equals(ver.getPriceSetId()) || ver.getVersionNo() == null || ver.getVersionNo().intValue() != versionNo)
{
throw new ServiceException("版本与当前编辑版不匹配");
}
if (productId != null && ver.getProductId() != null && !productId.equals(ver.getProductId()))
{
throw new ServiceException("版本产品不匹配");
}
}
private void removeEditVersionProduct(Long priceSetId, int versionNo, LblProduct product)
{
Long versionId = product.getVersionId();
if (versionId == null && product.getProductId() != null)
{
List<LblProductVersion> rows = lblProductMapper.selectVersionsBySetAndNo(priceSetId, versionNo);
if (rows != null)
{
for (LblProductVersion row : rows)
{
if (product.getProductId().equals(row.getProductId()))
{
versionId = row.getVersionId();
break;
}
}
}
}
if (versionId == null)
{
return;
}
LblProductVersion exists = lblProductMapper.selectVersionById(versionId);
if (exists == null)
{
return;
}
assertVersionBelongs(versionId, priceSetId, versionNo, product.getProductId());
lblProductMapper.deleteWeightBandsByVersion(versionId);
lblProductMapper.deleteSurchargesByVersion(versionId);
lblProductMapper.deleteVersionById(versionId);
}
private void alignNewVersionMeta(Long priceSetId, int versionNo, Long newVersionId, String operator)
{
if (newVersionId == null)
{
return;
}
List<LblProductVersion> rows = lblProductMapper.selectVersionsBySetAndNo(priceSetId, versionNo);
if (rows == null || rows.isEmpty())
{
return;
}
LblProductVersion sample = null;
for (LblProductVersion row : rows)
{
if (row != null && row.getVersionId() != null && !newVersionId.equals(row.getVersionId()))
{
sample = row;
break;
}
}
if (sample == null)
{
return;
}
LblProductVersion neu = lblProductMapper.selectVersionById(newVersionId);
if (neu == null)
{
return;
}
neu.setEffectiveStart(sample.getEffectiveStart());
neu.setEffectiveEnd(sample.getEffectiveEnd());
neu.setStatus(sample.getStatus() == null ? TERM_PENDING : sample.getStatus());
neu.setUpdateBy(operator);
lblProductMapper.updateVersion(neu);
}
private void fillBandDefaults(LblProduct product)
{
if (product == null || product.getWeightBands() == null)
{
return;
}
int sort = 0;
for (LblWeightBand band : product.getWeightBands())
{
if (band == null || Boolean.TRUE.equals(band.getDeleted()))
{
continue;
}
if (band.getFeeType() == null || band.getFeeType().isEmpty())
{
band.setFeeType("FREIGHT");
}
if (band.getChargeMode() == null || band.getChargeMode().isEmpty())
{
band.setChargeMode("ITEM");
}
if (band.getCountryCode() == null)
{
band.setCountryCode("");
}
if (band.getZoneCode() == null)
{
band.setZoneCode("");
}
if (band.getPostcodeParam() == null)
{
band.setPostcodeParam("");
}
if (band.getCalcParam() == null)
{
band.setCalcParam("");
}
band.setSortNo(sort++);
}
}
private void upsertDetails(LblProduct product, Long versionId)
{
if (product.getWeightBands() != null)
{
int i = 0;
for (LblWeightBand band : product.getWeightBands())
{
if (band == null)
{
continue;
}
if (Boolean.TRUE.equals(band.getDeleted()))
{
if (band.getBandId() != null)
{
lblProductMapper.deleteWeightBandById(band.getBandId());
}
continue;
}
band.setProductId(product.getProductId());
band.setVersionId(versionId);
if (band.getCountryCode() == null)
{
band.setCountryCode("");
}
if (band.getZoneCode() == null)
{
band.setZoneCode("");
}
if (band.getPostcodeParam() == null)
{
band.setPostcodeParam("");
}
band.setSortNo(i++);
if (band.getBandId() != null)
{
lblProductMapper.updateWeightBand(band);
}
else
{
lblProductMapper.insertWeightBand(band);
}
}
}
if (product.getSurcharges() != null)
{
int i = 0;
for (LblSurcharge fee : product.getSurcharges())
{
if (fee == null)
{
continue;
}
if (Boolean.TRUE.equals(fee.getDeleted()))
{
if (fee.getSurchargeId() != null)
{
lblProductMapper.deleteSurchargeById(fee.getSurchargeId());
}
continue;
}
if (isBlank(fee.getFeeName()))
{
continue;
}
fee.setProductId(product.getProductId());
fee.setVersionId(versionId);
fee.setSortNo(i++);
if (fee.getSurchargeId() != null)
{
lblProductMapper.updateSurcharge(fee);
}
else
{
lblProductMapper.insertSurcharge(fee);
}
}
}
}
private VersionKeep clearEditableVersion(Long priceSetId, Integer versionNo, String operator)
{
List<LblProductVersion> rows = lblProductMapper.selectVersionsBySetAndNo(priceSetId, versionNo);
@@ -786,8 +1030,28 @@ public class LblProductServiceImpl implements ILblProductService
private void prepareCounts(LblProduct product)
{
int bands = product.getWeightBands() == null ? 0 : product.getWeightBands().size();
int fees = product.getSurcharges() == null ? 0 : product.getSurcharges().size();
int bands = 0;
if (product.getWeightBands() != null)
{
for (LblWeightBand band : product.getWeightBands())
{
if (band != null && !Boolean.TRUE.equals(band.getDeleted()))
{
bands++;
}
}
}
int fees = 0;
if (product.getSurcharges() != null)
{
for (LblSurcharge fee : product.getSurcharges())
{
if (fee != null && !Boolean.TRUE.equals(fee.getDeleted()) && !isBlank(fee.getFeeName()))
{
fees++;
}
}
}
product.setWeightCount(bands);
product.setSurchargeCount(fees);
}
@@ -117,6 +117,10 @@
where price_set_id = #{priceSetId} and version_no = #{versionNo}
</delete>
<delete id="deleteVersionById">
delete from prc_label_product_version where version_id = #{versionId}
</delete>
<select id="selectVersionsBySet" resultType="com.bms.price.domain.LblProductVersion">
select v.version_id as versionId, v.price_set_id as priceSetId, v.product_id as productId, v.dict_value as dictValue,
v.version_no as versionNo, v.effective_start as effectiveStart, v.effective_end as effectiveEnd, v.file_path as filePath,
@@ -200,6 +204,10 @@
delete from prc_label_weight_band where version_id = #{versionId}
</delete>
<delete id="deleteWeightBandById">
delete from prc_label_weight_band where band_id = #{bandId}
</delete>
<select id="selectWeightBandsByVersion" resultType="com.bms.price.domain.LblWeightBand">
select band_id as bandId, product_id as productId, version_id as versionId, fee_type as feeType,
country_name as countryName, country_code as countryCode, zone_code as zoneCode, zone_name as zoneName,
@@ -251,6 +259,10 @@
delete from prc_label_surcharge where version_id = #{versionId}
</delete>
<delete id="deleteSurchargeById">
delete from prc_label_surcharge where surcharge_id = #{surchargeId}
</delete>
<select id="selectSurchargesByVersion" resultType="com.bms.price.domain.LblSurcharge">
select surcharge_id as surchargeId, product_id as productId, version_id as versionId, fee_name as feeName, sc_id as scId,
zone_code as zoneCode, zone_name as zoneName, charge_text as chargeText, amount, currency, price_unit as priceUnit,