mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-08-19 04:31:53 +08:00
【代码优化】商城: 满减送活动优惠券相关
This commit is contained in:
@@ -11,7 +11,7 @@ import jakarta.annotation.Resource;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 优惠劵 API 实现类
|
||||
@@ -43,8 +43,13 @@ public class CouponApiImpl implements CouponApi {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void takeCouponsByAdmin(List<Long> templateIds, List<Integer> counts, Long userId) {
|
||||
couponService.takeCouponsByAdmin(templateIds, counts, userId);
|
||||
public void takeCouponsByAdmin(Map<Long, Integer> giveCouponsMap, Long userId) {
|
||||
couponService.takeCouponsByAdmin(giveCouponsMap, userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void takeBackCouponsByAdmin(Map<Long, Integer> giveCouponsMap, Long userId) {
|
||||
couponService.takeBackCouponsByAdmin(giveCouponsMap, userId);
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -16,6 +16,7 @@ import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
@@ -88,16 +89,7 @@ public class RewardActivityBaseVO {
|
||||
private Boolean giveCoupon;
|
||||
|
||||
@Schema(description = "赠送的优惠劵编号的数组", example = "1,2,3")
|
||||
private List<Long> couponIds;
|
||||
|
||||
@Schema(description = "赠送的优惠券数量的数组", example = "1,2,3")
|
||||
private List<Integer> couponCounts;
|
||||
|
||||
@AssertTrue(message = "优惠劵和数量必须一一对应")
|
||||
@JsonIgnore
|
||||
public boolean isCouponCountsValid() {
|
||||
return BooleanUtil.isFalse(giveCoupon) || CollUtil.size(couponIds) == CollUtil.size(couponCounts);
|
||||
}
|
||||
private Map<Long, Integer> giveCouponsMap;
|
||||
|
||||
@AssertTrue(message = "赠送的积分不能小于 1")
|
||||
@JsonIgnore
|
||||
|
@@ -50,7 +50,6 @@ public class CouponDO extends BaseDO {
|
||||
*
|
||||
* 枚举 {@link CouponStatusEnum}
|
||||
*/
|
||||
// TODO 芋艿:已作废?
|
||||
private Integer status;
|
||||
|
||||
// TODO 芋艿:发放 adminid?
|
||||
|
@@ -16,6 +16,7 @@ import lombok.EqualsAndHashCode;
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 满减送活动 DO
|
||||
@@ -114,13 +115,11 @@ public class RewardActivityDO extends BaseDO {
|
||||
*/
|
||||
private Boolean giveCoupon;
|
||||
/**
|
||||
* 赠送的优惠劵编号的数组
|
||||
* 赠送的优惠劵
|
||||
*
|
||||
* key: 优惠劵编号,value:对应的优惠券数量
|
||||
*/
|
||||
private List<Long> couponIds;
|
||||
/**
|
||||
* 赠送的优惠券数量的数组
|
||||
*/
|
||||
private List<Integer> couponCounts;
|
||||
private Map<Long, Integer> giveCouponsMap;
|
||||
|
||||
}
|
||||
|
||||
|
@@ -72,6 +72,15 @@ public interface CouponMapper extends BaseMapperX<CouponDO> {
|
||||
);
|
||||
}
|
||||
|
||||
default List<CouponDO> selectListByTemplateIdAndUserIdAndTakeType(Long templateId, Collection<Long> userIds,
|
||||
Integer takeType) {
|
||||
return selectList(new LambdaQueryWrapperX<CouponDO>()
|
||||
.eq(CouponDO::getTemplateId, templateId)
|
||||
.eq(CouponDO::getTakeType, takeType)
|
||||
.in(CouponDO::getUserId, userIds)
|
||||
);
|
||||
}
|
||||
|
||||
default Map<Long, Integer> selectCountByUserIdAndTemplateIdIn(Long userId, Collection<Long> templateIds) {
|
||||
String templateIdAlias = "templateId";
|
||||
String countAlias = "count";
|
||||
|
@@ -108,11 +108,18 @@ public interface CouponService {
|
||||
/**
|
||||
* 【管理员】给指定用户批量发送优惠券
|
||||
*
|
||||
* @param templateIds 优惠劵编号的数组
|
||||
* @param counts 优惠券数量的数组
|
||||
* @param giveCouponsMap key: 优惠劵编号,value:对应的优惠券数量
|
||||
* @param userId 用户编号
|
||||
*/
|
||||
void takeCouponsByAdmin(List<Long> templateIds, List<Integer> counts, Long userId);
|
||||
void takeCouponsByAdmin(Map<Long, Integer> giveCouponsMap, Long userId);
|
||||
|
||||
/**
|
||||
* 【管理员】收回给指定用户批量发送优惠券
|
||||
*
|
||||
* @param giveCouponsMap key: 优惠劵编号,value:对应的优惠券数量
|
||||
* @param userId 用户编号
|
||||
*/
|
||||
void takeBackCouponsByAdmin(Map<Long, Integer> giveCouponsMap, Long userId);
|
||||
|
||||
/**
|
||||
* 【会员】领取优惠券
|
||||
|
@@ -31,7 +31,6 @@ import java.util.stream.Collectors;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*;
|
||||
import static cn.iocoder.yudao.framework.common.util.collection.MapUtils.findAndThen;
|
||||
import static cn.iocoder.yudao.module.promotion.enums.ErrorCodeConstants.*;
|
||||
import static java.util.Arrays.asList;
|
||||
|
||||
@@ -165,6 +164,7 @@ public class CouponServiceImpl implements CouponService {
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void takeCoupon(Long templateId, Set<Long> userIds, CouponTakeTypeEnum takeType) {
|
||||
CouponTemplateDO template = couponTemplateService.getCouponTemplate(templateId);
|
||||
// 1. 过滤掉达到领取限制的用户
|
||||
@@ -180,28 +180,66 @@ public class CouponServiceImpl implements CouponService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void takeCouponsByAdmin(List<Long> templateIds, List<Integer> counts, Long userId) {
|
||||
// TODO @puhui999:要不要循环调用上面的 takeCoupon 方法?按道理说,赠送也不会很多张。如果某次发卷失败,可以打个 error log;
|
||||
// 1. 获得优惠券模版
|
||||
List<CouponTemplateDO> templateList = couponTemplateService.getCouponTemplateList(templateIds);
|
||||
if (CollUtil.isEmpty(templateList)) {
|
||||
public void takeCouponsByAdmin(Map<Long, Integer> giveCouponsMap, Long userId) {
|
||||
if (CollUtil.isEmpty(giveCouponsMap)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Map<Long, CouponTemplateDO> templateMap = convertMap(templateList, CouponTemplateDO::getId);
|
||||
// 2.1 批量构建优惠券
|
||||
List<CouponDO> couponList = new ArrayList<>();
|
||||
for (int i = 0; i < templateIds.size(); i++) {
|
||||
int finalI = i;
|
||||
findAndThen(templateMap, templateIds.get(i), template -> {
|
||||
for (int j = 0; j < counts.get(finalI); j++) {
|
||||
couponList.add(CouponConvert.INSTANCE.convert(template, userId)
|
||||
.setTakeType(CouponTakeTypeEnum.ADMIN.getValue()));
|
||||
// 循环发放
|
||||
for (Map.Entry<Long, Integer> entry : giveCouponsMap.entrySet()) {
|
||||
try {
|
||||
for (int i = 0; i < entry.getValue(); i++) {
|
||||
getSelf().takeCoupon(entry.getKey(), CollUtil.newHashSet(userId), CouponTakeTypeEnum.ADMIN);
|
||||
}
|
||||
});
|
||||
} catch (Exception e) {
|
||||
log.error("[takeCouponsByAdmin][coupon({}) 优惠券发放失败]", entry, e);
|
||||
}
|
||||
}
|
||||
// 2.2 批量保存优惠券
|
||||
couponMapper.insertBatch(couponList);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void takeBackCouponsByAdmin(Map<Long, Integer> giveCouponsMap, Long userId) {
|
||||
// 循环收回
|
||||
for (Map.Entry<Long, Integer> entry : giveCouponsMap.entrySet()) {
|
||||
try {
|
||||
for (int i = 0; i < entry.getValue(); i++) {
|
||||
getSelf().takeBackCoupon(entry.getKey(), CollUtil.newHashSet(userId), CouponTakeTypeEnum.ADMIN);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("[takeBackCouponsByAdmin][coupon({}) 收回优惠券失败]", entry, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 【管理员】收回优惠券
|
||||
*
|
||||
* @param templateId 模版编号
|
||||
* @param userIds 用户编号列表
|
||||
* @param takeType 领取方式
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void takeBackCoupon(Long templateId, Set<Long> userIds, CouponTakeTypeEnum takeType) {
|
||||
CouponTemplateDO couponTemplate = couponTemplateService.getCouponTemplate(templateId);
|
||||
// 1.1 校验模板
|
||||
if (couponTemplate == null) {
|
||||
throw exception(COUPON_TEMPLATE_NOT_EXISTS);
|
||||
}
|
||||
// 1.2 校验领取方式
|
||||
if (ObjectUtil.notEqual(couponTemplate.getTakeType(), takeType.getValue())) {
|
||||
throw exception(COUPON_TEMPLATE_CANNOT_TAKE);
|
||||
}
|
||||
|
||||
// 2.1 过滤出还未使用的赠送的优惠券
|
||||
List<CouponDO> couponList = couponMapper.selectListByTemplateIdAndUserIdAndTakeType(templateId, userIds,
|
||||
takeType.getValue());
|
||||
List<CouponDO> unUsedCouponList = filterList(couponList, item -> !CouponStatusEnum.USED.getStatus().equals(item.getStatus()));
|
||||
// 2.2 减少优惠劵模板的领取数量
|
||||
couponTemplateService.updateCouponTemplateTakeCount(templateId, unUsedCouponList.size() * -1);
|
||||
// 2.3 批量更新优惠劵状态
|
||||
couponMapper.updateById(convertList(unUsedCouponList, item -> new CouponDO().setId(item.getId())
|
||||
.setStatus(CouponStatusEnum.INVALID.getStatus())));
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Reference in New Issue
Block a user