feat(pricing): 添加优惠券管理功能

- 新增 CouponManagementController 控制器,实现优惠券配置和领取记录的管理
- 新增 ICouponManagementService 接口和 CouponManagementServiceImpl 实现类,提供优惠券管理服务
- 在 PricingConfigController 中添加获取所有优惠券配置和领取记录的接口
- 新增 BundleProductListTypeHandler 类,用于处理一口价商品列表的序列化和反序列化
- 更新 PriceCouponClaimRecordMapper 和 PriceCouponConfigMapper,添加管理端所需的查询接口
This commit is contained in:
2025-08-18 04:33:58 +08:00
parent 16e07ee9ef
commit 4787efd328
7 changed files with 813 additions and 0 deletions

View File

@@ -0,0 +1,107 @@
package com.ycwl.basic.pricing.service;
import com.github.pagehelper.PageInfo;
import com.ycwl.basic.pricing.entity.PriceCouponClaimRecord;
import com.ycwl.basic.pricing.entity.PriceCouponConfig;
import com.ycwl.basic.pricing.enums.CouponStatus;
import java.util.List;
import java.util.Map;
/**
* 优惠券管理服务接口(管理端)
*/
public interface ICouponManagementService {
// ==================== 优惠券配置管理 ====================
/**
* 创建优惠券配置
*/
Long createCouponConfig(PriceCouponConfig config);
/**
* 更新优惠券配置
*/
boolean updateCouponConfig(PriceCouponConfig config);
/**
* 删除优惠券配置
*/
boolean deleteCouponConfig(Long id);
/**
* 启用/禁用优惠券配置
*/
boolean updateCouponConfigStatus(Long id, Boolean isActive);
/**
* 查询所有优惠券配置(包含禁用的)
*/
List<PriceCouponConfig> getAllCouponConfigs();
/**
* 分页查询优惠券配置
*/
PageInfo<PriceCouponConfig> getCouponConfigsPage(Integer pageNum, Integer pageSize,
Boolean isActive, String couponName);
/**
* 根据状态查询优惠券配置
*/
List<PriceCouponConfig> getCouponConfigsByStatus(Boolean isActive);
/**
* 根据ID查询优惠券配置
*/
PriceCouponConfig getCouponConfigById(Long id);
// ==================== 优惠券领取记录查询 ====================
/**
* 查询所有优惠券领取记录
*/
List<PriceCouponClaimRecord> getAllClaimRecords();
/**
* 分页查询优惠券领取记录
*/
PageInfo<PriceCouponClaimRecord> getClaimRecordsPage(Integer pageNum, Integer pageSize,
Long userId, Long couponId, CouponStatus status,
String startTime, String endTime);
/**
* 根据用户ID查询优惠券领取记录
*/
List<PriceCouponClaimRecord> getClaimRecordsByUserId(Long userId);
/**
* 根据优惠券ID查询领取记录
*/
List<PriceCouponClaimRecord> getClaimRecordsByCouponId(Long couponId);
/**
* 根据状态查询领取记录
*/
List<PriceCouponClaimRecord> getClaimRecordsByStatus(CouponStatus status);
/**
* 查询优惠券使用统计
*/
Map<String, Object> getCouponUsageStats(Long couponId);
/**
* 查询优惠券配置详细统计
*/
Map<String, Object> getCouponDetailStats(Long couponId);
/**
* 查询时间范围内的统计数据
*/
Map<String, Object> getPeriodStats(String startDate, String endDate);
/**
* 查询所有优惠券的使用统计概览
*/
List<Map<String, Object>> getAllCouponUsageOverview();
}

View File

@@ -0,0 +1,234 @@
package com.ycwl.basic.pricing.service.impl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ycwl.basic.pricing.entity.PriceCouponClaimRecord;
import com.ycwl.basic.pricing.entity.PriceCouponConfig;
import com.ycwl.basic.pricing.enums.CouponStatus;
import com.ycwl.basic.pricing.mapper.PriceCouponClaimRecordMapper;
import com.ycwl.basic.pricing.mapper.PriceCouponConfigMapper;
import com.ycwl.basic.pricing.service.ICouponManagementService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 优惠券管理服务实现(管理端)
*/
@Slf4j
@Service
@RequiredArgsConstructor
public class CouponManagementServiceImpl implements ICouponManagementService {
private final PriceCouponConfigMapper couponConfigMapper;
private final PriceCouponClaimRecordMapper claimRecordMapper;
// ==================== 优惠券配置管理 ====================
@Override
@Transactional
public Long createCouponConfig(PriceCouponConfig config) {
log.info("创建优惠券配置: {}", config.getCouponName());
// 设置默认值
if (config.getUsedQuantity() == null) {
config.setUsedQuantity(0);
}
if (config.getIsActive() == null) {
config.setIsActive(true);
}
int result = couponConfigMapper.insertCoupon(config);
if (result > 0) {
log.info("优惠券配置创建成功,ID: {}", config.getId());
return config.getId();
} else {
log.error("优惠券配置创建失败");
return null;
}
}
@Override
@Transactional
public boolean updateCouponConfig(PriceCouponConfig config) {
log.info("更新优惠券配置,ID: {}", config.getId());
PriceCouponConfig existing = couponConfigMapper.selectById(config.getId());
if (existing == null) {
log.error("优惠券配置不存在,ID: {}", config.getId());
return false;
}
int result = couponConfigMapper.updateCoupon(config);
if (result > 0) {
log.info("优惠券配置更新成功,ID: {}", config.getId());
return true;
} else {
log.error("优惠券配置更新失败,ID: {}", config.getId());
return false;
}
}
@Override
@Transactional
public boolean deleteCouponConfig(Long id) {
log.info("删除优惠券配置,ID: {}", id);
PriceCouponConfig existing = couponConfigMapper.selectById(id);
if (existing == null) {
log.error("优惠券配置不存在,ID: {}", id);
return false;
}
int result = couponConfigMapper.deleteCoupon(id);
if (result > 0) {
log.info("优惠券配置删除成功,ID: {}", id);
return true;
} else {
log.error("优惠券配置删除失败,ID: {}", id);
return false;
}
}
@Override
@Transactional
public boolean updateCouponConfigStatus(Long id, Boolean isActive) {
log.info("更新优惠券配置状态,ID: {}, 状态: {}", id, isActive);
PriceCouponConfig existing = couponConfigMapper.selectById(id);
if (existing == null) {
log.error("优惠券配置不存在,ID: {}", id);
return false;
}
int result = couponConfigMapper.updateCouponStatus(id, isActive);
if (result > 0) {
log.info("优惠券配置状态更新成功,ID: {}", id);
return true;
} else {
log.error("优惠券配置状态更新失败,ID: {}", id);
return false;
}
}
@Override
public List<PriceCouponConfig> getAllCouponConfigs() {
log.info("查询所有优惠券配置");
return couponConfigMapper.selectAllForAdmin();
}
@Override
public PageInfo<PriceCouponConfig> getCouponConfigsPage(Integer pageNum, Integer pageSize,
Boolean isActive, String couponName) {
log.info("分页查询优惠券配置,页码: {}, 页大小: {}, 状态: {}, 名称: {}",
pageNum, pageSize, isActive, couponName);
PageHelper.startPage(pageNum, pageSize);
List<PriceCouponConfig> configs = couponConfigMapper.selectByConditionsForAdmin(isActive, couponName);
return new PageInfo<>(configs);
}
@Override
public List<PriceCouponConfig> getCouponConfigsByStatus(Boolean isActive) {
log.info("根据状态查询优惠券配置,状态: {}", isActive);
return couponConfigMapper.selectByStatusForAdmin(isActive);
}
@Override
public PriceCouponConfig getCouponConfigById(Long id) {
log.info("根据ID查询优惠券配置,ID: {}", id);
return couponConfigMapper.selectById(id);
}
// ==================== 优惠券领取记录查询 ====================
@Override
public List<PriceCouponClaimRecord> getAllClaimRecords() {
log.info("查询所有优惠券领取记录");
return claimRecordMapper.selectAllForAdmin();
}
@Override
public PageInfo<PriceCouponClaimRecord> getClaimRecordsPage(Integer pageNum, Integer pageSize,
Long userId, Long couponId, CouponStatus status,
String startTime, String endTime) {
log.info("分页查询优惠券领取记录,页码: {}, 页大小: {}, 用户ID: {}, 优惠券ID: {}, 状态: {}, 开始时间: {}, 结束时间: {}",
pageNum, pageSize, userId, couponId, status, startTime, endTime);
PageHelper.startPage(pageNum, pageSize);
List<PriceCouponClaimRecord> records = claimRecordMapper.selectByConditionsForAdmin(userId, couponId, status, startTime, endTime);
return new PageInfo<>(records);
}
@Override
public List<PriceCouponClaimRecord> getClaimRecordsByUserId(Long userId) {
log.info("根据用户ID查询优惠券领取记录,用户ID: {}", userId);
return claimRecordMapper.selectByUserIdForAdmin(userId);
}
@Override
public List<PriceCouponClaimRecord> getClaimRecordsByCouponId(Long couponId) {
log.info("根据优惠券ID查询领取记录,优惠券ID: {}", couponId);
return claimRecordMapper.selectByCouponIdForAdmin(couponId);
}
@Override
public List<PriceCouponClaimRecord> getClaimRecordsByStatus(CouponStatus status) {
log.info("根据状态查询领取记录,状态: {}", status);
return claimRecordMapper.selectByStatusForAdmin(status);
}
@Override
public Map<String, Object> getCouponUsageStats(Long couponId) {
log.info("查询优惠券使用统计,优惠券ID: {}", couponId);
return claimRecordMapper.selectCouponUsageStats(couponId);
}
@Override
public Map<String, Object> getCouponDetailStats(Long couponId) {
log.info("查询优惠券详细统计,优惠券ID: {}", couponId);
return claimRecordMapper.selectCouponDetailStats(couponId);
}
@Override
public Map<String, Object> getPeriodStats(String startDate, String endDate) {
log.info("查询时间范围统计,开始日期: {}, 结束日期: {}", startDate, endDate);
return claimRecordMapper.selectPeriodStats(startDate, endDate);
}
@Override
public List<Map<String, Object>> getAllCouponUsageOverview() {
log.info("查询所有优惠券使用统计概览");
List<PriceCouponConfig> allCoupons = couponConfigMapper.selectAllForAdmin();
List<Map<String, Object>> overview = new ArrayList<>();
for (PriceCouponConfig coupon : allCoupons) {
Map<String, Object> stats = new HashMap<>();
stats.put("couponId", coupon.getId());
stats.put("couponName", coupon.getCouponName());
stats.put("couponType", coupon.getCouponType());
stats.put("totalQuantity", coupon.getTotalQuantity());
stats.put("usedQuantity", coupon.getUsedQuantity());
stats.put("remainingQuantity", coupon.getTotalQuantity() - coupon.getUsedQuantity());
stats.put("isActive", coupon.getIsActive());
stats.put("validFrom", coupon.getValidFrom());
stats.put("validUntil", coupon.getValidUntil());
// 获取详细统计
Map<String, Object> usageStats = claimRecordMapper.selectCouponUsageStats(coupon.getId());
stats.putAll(usageStats);
overview.add(stats);
}
return overview;
}
}