You've already forked FrameTour-BE
refactor(pricing): 重构适用商品类型处理逻辑
- 移除 ProductTypeListTypeHandler,直接在实体类中处理 JSON转换 - 为 PriceVoucherBatchConfig 添加 ObjectMapper 静态实例和日志记录 - 实现 JSON 字符串与 ProductType 列表之间的转换方法- 更新数据库映射,将 applicableProducts 映射为 JSON 字符串 - 优化 VoucherServiceImpl 中的产品适用性检查逻辑
This commit is contained in:
@@ -4,9 +4,12 @@ import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableField;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.ycwl.basic.pricing.enums.ProductType;
|
||||
import com.ycwl.basic.pricing.handler.ProductTypeListTypeHandler;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Date;
|
||||
@@ -16,9 +19,12 @@ import java.util.List;
|
||||
* 券码批次配置实体
|
||||
*/
|
||||
@Data
|
||||
@Slf4j
|
||||
@TableName("price_voucher_batch_config")
|
||||
public class PriceVoucherBatchConfig {
|
||||
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
||||
@TableId(value = "id", type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
@@ -48,10 +54,16 @@ public class PriceVoucherBatchConfig {
|
||||
private BigDecimal discountValue;
|
||||
|
||||
/**
|
||||
* 适用商品类型列表(JSON数组)
|
||||
* 适用商品类型列表(JSON字符串,数据库字段)
|
||||
*/
|
||||
@TableField("applicable_products")
|
||||
private String applicableProductsJson;
|
||||
|
||||
/**
|
||||
* 适用商品类型列表(业务字段)
|
||||
* null表示适用所有商品类型
|
||||
*/
|
||||
@TableField(typeHandler = ProductTypeListTypeHandler.class)
|
||||
@TableField(exist = false)
|
||||
private List<ProductType> applicableProducts;
|
||||
|
||||
/**
|
||||
@@ -87,4 +99,73 @@ public class PriceVoucherBatchConfig {
|
||||
private Integer deleted;
|
||||
|
||||
private Date deletedAt;
|
||||
|
||||
/**
|
||||
* 获取适用商品类型列表
|
||||
*/
|
||||
public List<ProductType> getApplicableProducts() {
|
||||
if (this.applicableProducts == null && this.applicableProductsJson != null) {
|
||||
this.applicableProducts = parseApplicableProductsFromJson(this.applicableProductsJson);
|
||||
}
|
||||
return this.applicableProducts;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置适用商品类型列表
|
||||
*/
|
||||
public void setApplicableProducts(List<ProductType> applicableProducts) {
|
||||
this.applicableProducts = applicableProducts;
|
||||
this.applicableProductsJson = convertApplicableProductsToJson(applicableProducts);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从JSON字符串解析商品类型列表
|
||||
*/
|
||||
private List<ProductType> parseApplicableProductsFromJson(String json) {
|
||||
if (json == null || json.trim().isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
TypeReference<List<String>> typeReference = new TypeReference<List<String>>() {};
|
||||
List<String> codeList = objectMapper.readValue(json, typeReference);
|
||||
if (codeList == null || codeList.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return codeList.stream()
|
||||
.map(code -> {
|
||||
try {
|
||||
return ProductType.fromCode(code);
|
||||
} catch (IllegalArgumentException e) {
|
||||
log.warn("未知的商品类型代码: {}", code);
|
||||
return null;
|
||||
}
|
||||
})
|
||||
.filter(type -> type != null)
|
||||
.collect(java.util.stream.Collectors.toList());
|
||||
} catch (JsonProcessingException e) {
|
||||
log.error("解析适用商品类型JSON失败,JSON: {}", json, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将商品类型列表转换为JSON字符串
|
||||
*/
|
||||
private String convertApplicableProductsToJson(List<ProductType> products) {
|
||||
if (products == null || products.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
List<String> codeList = products.stream()
|
||||
.map(ProductType::getCode)
|
||||
.collect(java.util.stream.Collectors.toList());
|
||||
return objectMapper.writeValueAsString(codeList);
|
||||
} catch (JsonProcessingException e) {
|
||||
log.error("转换适用商品类型为JSON失败", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@@ -293,8 +293,7 @@ public class VoucherServiceImpl implements IVoucherService {
|
||||
return products.stream()
|
||||
.filter(product -> {
|
||||
try {
|
||||
ProductType productType = ProductType.fromCode(product.getProductType());
|
||||
return applicableProducts.contains(productType);
|
||||
return applicableProducts.contains(product.getProductType());
|
||||
} catch (IllegalArgumentException e) {
|
||||
log.warn("未知的商品类型: {}", product.getProductType());
|
||||
return false;
|
||||
|
Reference in New Issue
Block a user