feat(pricing): 增加券码重复使用功能并优化相关数据结构

- 在 PriceVoucherUsageRecord 和 VoucherUsageRecordResp 中添加 usageSequence 字段,用于记录券码的使用序号- 更新 PriceVoucherCode 实体和相关 mapper,增加 currentUseCount 和 lastUsedTime 字段
- 修改 VoucherCodeServiceImpl 和 VoucherServiceImpl 中的券码使用逻辑,支持重复使用
- 新增VoucherBatchOverviewResp、VoucherDetailResp、VoucherUsageSummaryResp 和 VoucherValidationResp 等新的响应 DTO 类,用于提供券码批次概览、详情、使用统计和验证等功能
This commit is contained in:
2025-09-16 20:54:37 +08:00
parent 8380b02fbb
commit 1506ae95b8
10 changed files with 536 additions and 175 deletions

View File

@@ -0,0 +1,80 @@
package com.ycwl.basic.pricing.dto.resp;
import lombok.Data;
import java.math.BigDecimal;
/**
* 券码批次概览响应
*/
@Data
public class VoucherBatchOverviewResp {
/**
* 批次ID
*/
private Long batchId;
/**
* 批次名称
*/
private String batchName;
/**
* 券码总数
*/
private Integer totalCodes;
/**
* 已领取券码数
*/
private Integer claimedCodes;
/**
* 已使用券码数(至少使用过一次)
*/
private Integer usedCodes;
/**
* 已达到最大使用次数的券码数
*/
private Integer exhaustedCodes;
/**
* 总使用记录数(支持重复使用)
*/
private Integer totalUsageRecords;
/**
* 平均每个券码使用次数
*/
private BigDecimal averageUsagePerCode;
/**
* 重复使用率 (重复使用的券码数/已使用券码数 * 100)
*/
private BigDecimal repeatUsageRate;
/**
* 可重复使用设置
*/
private RepeatableSettings repeatableSettings;
@Data
public static class RepeatableSettings {
/**
* 每个券码最大使用次数
*/
private Integer maxUseCount;
/**
* 每个用户最大使用次数
*/
private Integer maxUsePerUser;
/**
* 使用间隔小时数
*/
private Integer useIntervalHours;
}
}

View File

@@ -0,0 +1,142 @@
package com.ycwl.basic.pricing.dto.resp;
import com.ycwl.basic.pricing.enums.ProductType;
import lombok.Data;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
/**
* 券码详情响应
*/
@Data
public class VoucherDetailResp {
/**
* 券码
*/
private String voucherCode;
/**
* 批次信息
*/
private BatchInfo batchInfo;
/**
* 使用信息
*/
private UsageInfo usageInfo;
/**
* 用户信息
*/
private UserInfo userInfo;
/**
* 状态信息
*/
private StatusInfo statusInfo;
@Data
public static class BatchInfo {
/**
* 批次ID
*/
private Long batchId;
/**
* 批次名称
*/
private String batchName;
/**
* 优惠类型 (0: 赠品兑换, 1: 金额抵扣, 2: 百分比折扣)
*/
private Integer discountType;
/**
* 优惠值
*/
private BigDecimal discountValue;
/**
* 适用商品类型
*/
private List<ProductType> applicableProducts;
}
@Data
public static class UsageInfo {
/**
* 最大使用次数
*/
private Integer maxUseCount;
/**
* 当前已使用次数
*/
private Integer currentUseCount;
/**
* 剩余使用次数
*/
private Integer remainingUseCount;
/**
* 每个用户最大使用次数
*/
private Integer maxUsePerUser;
/**
* 使用间隔小时数
*/
private Integer useIntervalHours;
/**
* 是否还能使用
*/
private Boolean canUseMore;
}
@Data
public static class UserInfo {
/**
* 用户人脸ID
*/
private Long faceId;
/**
* 该用户已使用此券码的次数
*/
private Integer userUsageCount;
/**
* 最后使用时间
*/
private Date lastUsedTime;
/**
* 下次可用时间
*/
private Date nextAvailableTime;
}
@Data
public static class StatusInfo {
/**
* 状态码
*/
private Integer status;
/**
* 状态名称
*/
private String statusName;
/**
* 领取时间
*/
private Date claimedTime;
}
}

View File

@@ -47,6 +47,11 @@ public class VoucherUsageRecordResp {
*/
private String batchName;
/**
* 使用序号(该券码的第几次使用)
*/
private Integer usageSequence;
/**
* 使用时间
*/

View File

@@ -0,0 +1,100 @@
package com.ycwl.basic.pricing.dto.resp;
import lombok.Data;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
/**
* 券码使用统计摘要响应
*/
@Data
public class VoucherUsageSummaryResp {
/**
* 券码
*/
private String voucherCode;
/**
* 批次ID
*/
private Long batchId;
/**
* 批次名称
*/
private String batchName;
/**
* 总使用记录数(重复使用产生多条记录)
*/
private Integer totalUsageRecords;
/**
* 使用过的独立用户数
*/
private Integer uniqueUsers;
/**
* 使用统计
*/
private UsageStatistics usageStatistics;
/**
* 使用时间轴
*/
private List<UsageTimeline> usageTimeline;
@Data
public static class UsageStatistics {
/**
* 最大可使用次数
*/
private Integer maxUseCount;
/**
* 当前已使用次数
*/
private Integer currentUseCount;
/**
* 剩余使用次数
*/
private Integer remainingUseCount;
/**
* 使用率
*/
private BigDecimal usageRate;
/**
* 是否还能使用
*/
private Boolean canUseMore;
}
@Data
public static class UsageTimeline {
/**
* 使用时间
*/
private Date useTime;
/**
* 用户人脸ID
*/
private Long faceId;
/**
* 订单ID
*/
private String orderId;
/**
* 优惠金额
*/
private BigDecimal discountAmount;
}
}

View File

@@ -0,0 +1,33 @@
package com.ycwl.basic.pricing.dto.resp;
import com.ycwl.basic.pricing.dto.VoucherInfo;
import lombok.Builder;
import lombok.Data;
/**
* 券码验证响应
*/
@Data
@Builder
public class VoucherValidationResp {
/**
* 券码
*/
private String voucherCode;
/**
* 是否可用
*/
private Boolean available;
/**
* 不可用原因
*/
private String unavailableReason;
/**
* 券码详细信息
*/
private VoucherInfo voucherInfo;
}

View File

@@ -44,6 +44,11 @@ public class PriceVoucherUsageRecord {
*/
private Long batchId;
/**
* 使用序号(该券码的第几次使用)
*/
private Integer usageSequence;
/**
* 使用时间
*/

View File

@@ -58,17 +58,6 @@ public interface PriceVoucherCodeMapper extends BaseMapper<PriceVoucherCode> {
@Param("faceId") Long faceId,
@Param("claimedTime") LocalDateTime claimedTime);
/**
* 使用券码(更新状态为已使用)
* @param code 券码
* @param usedTime 使用时间
* @param remark 使用备注
* @return 影响行数
*/
int useVoucher(@Param("code") String code,
@Param("usedTime") LocalDateTime usedTime,
@Param("remark") String remark);
/**
* 根据批次ID查询券码列表(支持分页)
* @param batchId 批次ID

View File

@@ -49,7 +49,7 @@ public class VoucherCodeServiceImpl implements VoucherCodeService {
private VoucherBatchService voucherBatchService;
@Override
public void generateVoucherCodes(Long batchId, Long scenicId, Integer count) {
public void generateVoucherCodes(Long batchId, Long scenicId, Integer count) {
List<PriceVoucherCode> codes = new ArrayList<>();
for (int i = 0; i < count; i++) {
@@ -58,6 +58,7 @@ public class VoucherCodeServiceImpl implements VoucherCodeService {
code.setScenicId(scenicId);
code.setCode(generateVoucherCode());
code.setStatus(VoucherCodeStatus.UNCLAIMED.getCode());
code.setCurrentUseCount(0); // 初始化使用次数为0
code.setCreateTime(new Date());
code.setDeleted(0);
codes.add(code);
@@ -66,11 +67,11 @@ public class VoucherCodeServiceImpl implements VoucherCodeService {
for (PriceVoucherCode code : codes) {
voucherCodeMapper.insert(code);
}
}
}
@Override
@Transactional
public VoucherCodeResp claimVoucher(VoucherClaimReq req) {
@Transactional
public VoucherCodeResp claimVoucher(VoucherClaimReq req) {
if (req.getScenicId() == null) {
throw new BizException(400, "景区ID不能为空");
}
@@ -110,13 +111,17 @@ public class VoucherCodeServiceImpl implements VoucherCodeService {
voucherCode.setFaceId(req.getFaceId());
voucherCode.setStatus(VoucherCodeStatus.CLAIMED_UNUSED.getCode());
voucherCode.setClaimedTime(new Date());
// 确保currentUseCount被初始化
if (voucherCode.getCurrentUseCount() == null) {
voucherCode.setCurrentUseCount(0);
}
voucherCodeMapper.updateById(voucherCode);
voucherBatchService.updateBatchClaimedCount(batch.getId());
return convertToResp(voucherCode, batch);
}
}
@Override
public Page<VoucherCodeResp> queryCodeList(VoucherCodeQueryReq req) {
@@ -165,8 +170,8 @@ public class VoucherCodeServiceImpl implements VoucherCodeService {
}
@Override
@Transactional
public void markCodeAsUsed(Long codeId, String remark) {
@Transactional
public void markCodeAsUsed(Long codeId, String remark) {
PriceVoucherCode code = voucherCodeMapper.selectById(codeId);
if (code == null || code.getDeleted() == 1) {
throw new BizException(404, "券码不存在");
@@ -176,6 +181,11 @@ public class VoucherCodeServiceImpl implements VoucherCodeService {
throw new BizException(400, "券码状态异常,无法使用");
}
// 更新使用计数和时间
Integer currentUseCount = code.getCurrentUseCount() != null ? code.getCurrentUseCount() : 0;
code.setCurrentUseCount(currentUseCount + 1);
code.setLastUsedTime(new Date());
code.setStatus(VoucherCodeStatus.USED.getCode());
code.setUsedTime(new Date());
code.setRemark(remark);
@@ -183,7 +193,7 @@ public class VoucherCodeServiceImpl implements VoucherCodeService {
voucherCodeMapper.updateById(code);
voucherBatchService.updateBatchUsedCount(code.getBatchId());
}
}
@Override
public boolean canClaimVoucher(Long faceId, Long scenicId) {

View File

@@ -189,8 +189,8 @@ public class VoucherServiceImpl implements IVoucherService {
* @param discountAmount 优惠金额
* @param faceId 人脸ID
*/
@Override
public void markVoucherAsUsed(String voucherCode, String remark, String orderId, BigDecimal discountAmount, Long faceId) {
@Override
public void markVoucherAsUsed(String voucherCode, String remark, String orderId, BigDecimal discountAmount, Long faceId) {
if (!StringUtils.hasText(voucherCode)) {
return;
}
@@ -209,6 +209,11 @@ public class VoucherServiceImpl implements IVoucherService {
Date now = new Date();
// 计算新的使用次数和序号
Integer currentUseCount = voucherCodeEntity.getCurrentUseCount() != null ?
voucherCodeEntity.getCurrentUseCount() : 0;
Integer newUseCount = currentUseCount + 1;
// 创建使用记录
PriceVoucherUsageRecord usageRecord = new PriceVoucherUsageRecord();
usageRecord.setVoucherCodeId(voucherCodeEntity.getId());
@@ -216,6 +221,7 @@ public class VoucherServiceImpl implements IVoucherService {
usageRecord.setFaceId(faceId != null ? faceId : voucherCodeEntity.getFaceId());
usageRecord.setScenicId(voucherCodeEntity.getScenicId());
usageRecord.setBatchId(voucherCodeEntity.getBatchId());
usageRecord.setUsageSequence(newUseCount); // 设置使用序号,表示这是该券码的第几次使用
usageRecord.setUseTime(now);
usageRecord.setOrderId(orderId);
usageRecord.setDiscountAmount(discountAmount);
@@ -226,14 +232,12 @@ public class VoucherServiceImpl implements IVoucherService {
usageRecordMapper.insert(usageRecord);
// 更新券码使用次数和状态
Integer currentUseCount = voucherCodeEntity.getCurrentUseCount() != null ?
voucherCodeEntity.getCurrentUseCount() + 1 : 1;
voucherCodeEntity.setCurrentUseCount(currentUseCount);
voucherCodeEntity.setCurrentUseCount(newUseCount);
voucherCodeEntity.setLastUsedTime(now);
// 检查是否达到最大使用次数
Integer maxUseCount = batchConfig.getMaxUseCount();
if (maxUseCount != null && currentUseCount >= maxUseCount) {
if (maxUseCount != null && newUseCount >= maxUseCount) {
if (maxUseCount == 1) {
// 兼容原有逻辑,单次使用设为USED状态
voucherCodeEntity.setStatus(VoucherCodeStatus.USED.getCode());
@@ -249,9 +253,9 @@ public class VoucherServiceImpl implements IVoucherService {
// 更新批次统计(使用记录数,不是使用券码数)
voucherBatchConfigMapper.updateUsedCount(voucherCodeEntity.getBatchId(), 1);
log.info("券码使用记录已创建: code={}, useCount={}, maxUseCount={}, faceId={}",
voucherCode, currentUseCount, maxUseCount, faceId);
}
log.info("券码使用记录已创建: code={}, useCount={}, maxUseCount={}, faceId={}, sequence={}",
voucherCode, newUseCount, maxUseCount, faceId, newUseCount);
}
@Override
public boolean canClaimVoucher(Long faceId, Long scenicId) {

View File

@@ -11,6 +11,8 @@
<result column="face_id" property="faceId" jdbcType="BIGINT"/>
<result column="claimed_time" property="claimedTime" jdbcType="TIMESTAMP"/>
<result column="used_time" property="usedTime" jdbcType="TIMESTAMP"/>
<result column="current_use_count" property="currentUseCount" jdbcType="INTEGER"/>
<result column="last_used_time" property="lastUsedTime" jdbcType="TIMESTAMP"/>
<result column="remark" property="remark" jdbcType="VARCHAR"/>
<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
<result column="update_time" property="updateTime" jdbcType="TIMESTAMP"/>
@@ -20,7 +22,7 @@
<sql id="Base_Column_List">
id, batch_id, scenic_id, code, status, face_id, claimed_time, used_time,
remark, create_time, update_time, deleted, deleted_at
current_use_count, last_used_time, remark, create_time, update_time, deleted, deleted_at
</sql>
<select id="selectByCode" resultMap="BaseResultMap">
@@ -69,16 +71,7 @@
AND deleted = 0
</update>
<update id="useVoucher">
UPDATE price_voucher_code
SET status = 2,
used_time = #{usedTime},
remark = #{remark},
update_time = NOW()
WHERE code = #{code}
AND (status = 1 OR status = 0)
AND deleted = 0
</update>
<select id="selectByBatchId" resultMap="BaseResultMap">
SELECT <include refid="Base_Column_List"/>