优惠券 使用数量

This commit is contained in:
wangw 2025-03-19 11:14:46 +08:00
parent b6c4ffdee6
commit c614ebd3e5
1 changed files with 21 additions and 0 deletions

View File

@ -28,6 +28,7 @@ import com.github.pagehelper.PageInfo;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
@ -35,6 +36,7 @@ import java.math.BigDecimal;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
/**
* 优惠券 服务层实现
@ -42,6 +44,7 @@ import java.util.*;
* @author ww
* @since 2025-02-17
*/
@Slf4j
@DubboService
public class ShopCouponServiceImpl extends ServiceImpl<ShopCouponMapper, ShopCoupon> implements ShopCouponService {
@ -144,6 +147,24 @@ public class ShopCouponServiceImpl extends ServiceImpl<ShopCouponMapper, ShopCou
@Override
public Boolean use(List<Long> ids, Long shopUserId, Long orderId) {
List<ShopActivateCouponRecord> records = couponRecordService.listByIds(ids);
if (records.isEmpty()) {
log.error("优惠券使用失败订单Id:{}", orderId);
return false;
}
// 使用流来统计 couponId 出现的次数
Map<Long, Long> couponIdCountMap = records.stream()
.collect(Collectors.groupingBy(ShopActivateCouponRecord::getCouponId,
Collectors.counting()
));
couponIdCountMap.forEach((couponId, count) -> {
ShopCoupon tbShopCoupon = getById(couponId);
tbShopCoupon.setUseNumber(tbShopCoupon.getUseNumber() + count.intValue());
ShopCoupon coupon1 = new ShopCoupon();
coupon1.setId(couponId);
coupon1.setUseNumber(tbShopCoupon.getUseNumber());
updateById(coupon1);
});
return couponRecordService.updateChain()
.set(ShopActivateCouponRecord::getStatus, 1)
.set(ShopActivateCouponRecord::getTargetId, orderId)