package com.ycwl.basic.pricing.entity; 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 lombok.Data; import lombok.extern.slf4j.Slf4j; import java.math.BigDecimal; import java.util.Date; 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; /** * 券码批次名称 */ private String batchName; /** * 景区ID */ private Long scenicId; /** * 推客ID */ private Long brokerId; /** * 优惠类型:0=全场免费,1=商品降价,2=商品打折 */ private Integer discountType; /** * 优惠值(降价金额或折扣百分比) */ private BigDecimal discountValue; /** * 适用商品类型列表(JSON字符串,数据库字段) */ @TableField("applicable_products") private String applicableProductsJson; /** * 适用商品类型列表(业务字段) * null表示适用所有商品类型 */ @TableField(exist = false) private List applicableProducts; /** * 总券码数量 */ private Integer totalCount; /** * 已使用数量 */ private Integer usedCount; /** * 已领取数量 */ private Integer claimedCount; /** * 状态:0=禁用,1=启用 */ private Integer status; @TableField("create_time") private Date createTime; @TableField("update_time") private Date updateTime; private Long createBy; private Long updateBy; /** * 每个券码最大使用次数(NULL表示无限次,1表示单次使用兼容原有逻辑) */ private Integer maxUseCount; /** * 每个用户最大使用次数(NULL表示无限次) */ private Integer maxUsePerUser; /** * 两次使用间隔小时数(NULL表示无间隔限制) */ private Integer useIntervalHours; private Integer deleted; private Date deletedAt; /** * 有效期开始时间(NULL表示无开始时间限制) */ @TableField("valid_start_time") private Date validStartTime; /** * 有效期结束时间(NULL表示无结束时间限制) */ @TableField("valid_end_time") private Date validEndTime; /** * 检查当前时间是否在有效期内 * @return true-在有效期内,false-不在有效期内 */ public boolean isWithinValidTimeRange() { return isWithinValidTimeRange(new Date()); } /** * 检查指定时间是否在有效期内 * @param checkTime 待检查的时间 * @return true-在有效期内,false-不在有效期内 */ public boolean isWithinValidTimeRange(Date checkTime) { if (checkTime == null) { return false; } // 检查开始时间 if (validStartTime != null && checkTime.before(validStartTime)) { return false; } // 检查结束时间 if (validEndTime != null && checkTime.after(validEndTime)) { return false; } return true; } /** * 获取适用商品类型列表 */ 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; } } }