diff --git a/src/main/java/com/ycwl/basic/pricing/entity/PriceVoucherBatchConfig.java b/src/main/java/com/ycwl/basic/pricing/entity/PriceVoucherBatchConfig.java index 368cfb8..2539e9b 100644 --- a/src/main/java/com/ycwl/basic/pricing/entity/PriceVoucherBatchConfig.java +++ b/src/main/java/com/ycwl/basic/pricing/entity/PriceVoucherBatchConfig.java @@ -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 applicableProducts; /** @@ -87,4 +99,73 @@ public class PriceVoucherBatchConfig { private Integer deleted; private Date deletedAt; + + /** + * 获取适用商品类型列表 + */ + public List getApplicableProducts() { + if (this.applicableProducts == null && this.applicableProductsJson != null) { + this.applicableProducts = parseApplicableProductsFromJson(this.applicableProductsJson); + } + return this.applicableProducts; + } + + /** + * 设置适用商品类型列表 + */ + public void setApplicableProducts(List applicableProducts) { + this.applicableProducts = applicableProducts; + this.applicableProductsJson = convertApplicableProductsToJson(applicableProducts); + } + + /** + * 从JSON字符串解析商品类型列表 + */ + private List parseApplicableProductsFromJson(String json) { + if (json == null || json.trim().isEmpty()) { + return null; + } + + try { + TypeReference> typeReference = new TypeReference>() {}; + List 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 products) { + if (products == null || products.isEmpty()) { + return null; + } + + try { + List 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; + } + } } \ No newline at end of file diff --git a/src/main/java/com/ycwl/basic/pricing/service/impl/VoucherServiceImpl.java b/src/main/java/com/ycwl/basic/pricing/service/impl/VoucherServiceImpl.java index 049d51d..cc3903f 100644 --- a/src/main/java/com/ycwl/basic/pricing/service/impl/VoucherServiceImpl.java +++ b/src/main/java/com/ycwl/basic/pricing/service/impl/VoucherServiceImpl.java @@ -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; diff --git a/src/main/resources/mapper/PriceVoucherBatchConfigMapper.xml b/src/main/resources/mapper/PriceVoucherBatchConfigMapper.xml index 4575abd..406657d 100644 --- a/src/main/resources/mapper/PriceVoucherBatchConfigMapper.xml +++ b/src/main/resources/mapper/PriceVoucherBatchConfigMapper.xml @@ -10,6 +10,7 @@ + @@ -24,7 +25,7 @@ - id, batch_name, scenic_id, broker_id, discount_type, discount_value, + id, batch_name, scenic_id, broker_id, discount_type, discount_value, applicable_products, total_count, used_count, claimed_count, status, create_time, update_time, create_by, update_by, deleted, deleted_at @@ -71,7 +72,7 @@