From 7c0e6e12143be500d35209b323239c215f62aa10 Mon Sep 17 00:00:00 2001 From: luluyuyu <3305167204@qq.com> Date: Sun, 15 Dec 2024 11:04:16 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=9A=E4=BF=AE=E6=94=B9=E7=BB=93?= =?UTF-8?q?=E6=9D=9F=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/CouponUseServiceImpl.java | 41 +++++++++++++------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/skyeye-shop/shop-store/src/main/java/com/skyeye/coupon/service/impl/CouponUseServiceImpl.java b/skyeye-shop/shop-store/src/main/java/com/skyeye/coupon/service/impl/CouponUseServiceImpl.java index 69a4d2c8..78252b8f 100644 --- a/skyeye-shop/shop-store/src/main/java/com/skyeye/coupon/service/impl/CouponUseServiceImpl.java +++ b/skyeye-shop/shop-store/src/main/java/com/skyeye/coupon/service/impl/CouponUseServiceImpl.java @@ -5,6 +5,7 @@ package com.skyeye.coupon.service.impl; import cn.hutool.core.collection.CollectionUtil; +import cn.hutool.core.util.ObjUtil; import cn.hutool.core.util.ObjectUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.json.JSONUtil; @@ -42,10 +43,7 @@ import org.springframework.stereotype.Service; import java.text.DateFormat; import java.text.SimpleDateFormat; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; +import java.util.*; import java.util.stream.Collectors; /** @@ -122,8 +120,16 @@ public class CouponUseServiceImpl extends SkyeyeBusinessServiceImpl list = list(queryWrapper); couponService.setDataMation(list, CouponUse::getCouponId); List collect = list.stream().map(item -> { - item.setUsageCount(item.getCouponMation().getUseCount()); + if (item.getCouponMation() != null) { + item.setUsageCount(item.getCouponMation().getUseCount()); + } return item; }).collect(Collectors.toList()); return JSONUtil.toList(JSONUtil.toJsonStr(collect), null); @@ -234,7 +242,7 @@ public class CouponUseServiceImpl extends SkyeyeBusinessServiceImpl> mapList = listMaps(queryWrapper); return CollectionUtil.isEmpty(mapList) ? new HashMap<>() - : mapList.stream().collect(Collectors.toMap(map -> map.get("coupon_id").toString(), map -> Integer.parseInt(map.get("total").toString()))); + : mapList.stream().collect(Collectors.toMap(map -> map.get("coupon_id").toString(), map -> Integer.parseInt(map.get("total").toString()))); } /** @@ -262,15 +270,24 @@ public class CouponUseServiceImpl extends SkyeyeBusinessServiceImpl updateWrapper = new UpdateWrapper<>(); - updateWrapper.eq(CommonConstants.ID, couponUseId); - updateWrapper.set(MybatisPlusUtil.toColumns(CouponUse::getState), CouponUseState.USED.getKey()); - update(updateWrapper); + CouponUse couponUse = selectById(couponUseId); + if (ObjUtil.isEmpty(couponUse)) { + throw new CustomException("优惠券使用记录不存在"); + } + if (couponUse.getUsedCount() == couponUse.getUsageCount()) { + UpdateWrapper updateWrapper = new UpdateWrapper<>(); + updateWrapper.eq(CommonConstants.ID, couponUseId); + updateWrapper.set(MybatisPlusUtil.toColumns(CouponUse::getState), CouponUseState.USED.getKey()); + update(updateWrapper); + } } @Override public void UpdateUsedCount(String couponUseId) { CouponUse couponUse = selectById(couponUseId); + if (ObjUtil.isEmpty(couponUse)) { + throw new CustomException("优惠券使用记录不存在"); + } Integer usedCount = couponUse.getUsedCount(); if (couponUse.getUsedCount() < couponUse.getUsageCount()) { UpdateWrapper updateWrapper = new UpdateWrapper<>();