feat(voucher): 增加券码适用商品类型功能

- 在 VoucherBatchCreateReq、VoucherBatchResp 和 VoucherInfo 中添加适用商品类型列表字段
- 在 PriceVoucherBatchConfig 中添加适用商品类型列表字段,并使用 ProductTypeListTypeHandler 进行 JSON 序列化和反序列化
- 实现 ProductTypeListTypeHandler 以处理商品类型列表的 JSON 序列化和反序列化
- 更新 VoucherBatchServiceImpl 和 VoucherServiceImpl 以支持适用商品类型的筛选和计算
This commit is contained in:
2025-08-30 15:31:35 +08:00
parent 57b087a4fb
commit 966568156c
7 changed files with 182 additions and 7 deletions

View File

@@ -0,0 +1,94 @@
package com.ycwl.basic.pricing.handler;
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 lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* 商品类型列表类型处理器
* 用于处理券码适用商品类型的JSON序列化和反序列化
*/
@Slf4j
public class ProductTypeListTypeHandler extends BaseTypeHandler<List<ProductType>> {
private final ObjectMapper objectMapper = new ObjectMapper();
private final TypeReference<List<String>> typeReference = new TypeReference<List<String>>() {};
@Override
public void setNonNullParameter(PreparedStatement ps, int i, List<ProductType> parameter, JdbcType jdbcType) throws SQLException {
try {
// 将ProductType枚举转换为字符串列表
List<String> codeList = parameter.stream()
.map(ProductType::getCode)
.toList();
String json = objectMapper.writeValueAsString(codeList);
ps.setString(i, json);
log.debug("序列化商品类型列表: {}", json);
} catch (JsonProcessingException e) {
log.error("序列化商品类型列表失败", e);
throw new SQLException("序列化商品类型列表失败", e);
}
}
@Override
public List<ProductType> getNullableResult(ResultSet rs, String columnName) throws SQLException {
String json = rs.getString(columnName);
return parseJson(json, columnName);
}
@Override
public List<ProductType> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
String json = rs.getString(columnIndex);
return parseJson(json, "columnIndex:" + columnIndex);
}
@Override
public List<ProductType> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
String json = cs.getString(columnIndex);
return parseJson(json, "columnIndex:" + columnIndex);
}
private List<ProductType> parseJson(String json, String source) {
if (json == null || json.trim().isEmpty()) {
log.debug("从{}获取的JSON为空,返回null表示适用所有商品类型", source);
return null; // null表示适用所有商品类型
}
try {
List<String> codeList = objectMapper.readValue(json, typeReference);
if (codeList == null || codeList.isEmpty()) {
log.debug("从{}反序列化得到空列表,返回null表示适用所有商品类型", source);
return null;
}
List<ProductType> result = new ArrayList<>();
for (String code : codeList) {
try {
ProductType productType = ProductType.fromCode(code);
result.add(productType);
} catch (IllegalArgumentException e) {
log.warn("从{}反序列化时发现未知商品类型代码: {}", source, code);
// 忽略未知的商品类型代码,继续处理其他类型
}
}
log.debug("从{}反序列化商品类型列表成功,数量: {}", source, result.size());
return result.isEmpty() ? null : result;
} catch (JsonProcessingException e) {
log.error("从{}反序列化商品类型列表失败,JSON: {}", source, json, e);
// 解析失败时返回null,表示适用所有商品类型
return null;
}
}
}