MkShopCouponRecordService.receiveCoupon 发放券 统一方法

This commit is contained in:
wangw 2025-09-16 11:07:32 +08:00
parent d006a2dce3
commit ec1e94fdaf
4 changed files with 156 additions and 26 deletions

View File

@ -0,0 +1,50 @@
package com.czg.market.dto;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
/**
* 发放优惠券实体
* @author ww
* @description
*/
@Data
public class MkShopCouponGiftDTO implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
/**
* 店铺id
*/
private Long shopId;
/**
* 店铺用户id
*/
private Long shopUserId;
/**
* 卷Id (校验是否可用)
*/
private Long couponId;
/**
* 来源活动id
*/
private Long sourceId;
/**
* 来源记录Id
*/
private Long sourceFlowId;
/**
* 来源描述
*/
private String source;
}

View File

@ -3,6 +3,7 @@ package com.czg.market.service;
import com.czg.account.dto.QueryReceiveDto; import com.czg.account.dto.QueryReceiveDto;
import com.czg.account.vo.CouponReceiveVo; import com.czg.account.vo.CouponReceiveVo;
import com.czg.account.vo.UserCouponVo; import com.czg.account.vo.UserCouponVo;
import com.czg.market.dto.MkShopCouponGiftDTO;
import com.czg.market.dto.MkShopCouponRecordDTO; import com.czg.market.dto.MkShopCouponRecordDTO;
import com.mybatisflex.core.paginate.Page; import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.service.IService; import com.mybatisflex.core.service.IService;
@ -35,10 +36,11 @@ public interface MkShopCouponRecordService extends IService<MkShopCouponRecord>
/** /**
* 发放券 统一方法 * 发放券 统一方法
* *
* @param mkShopCouponRecordDTO 赠送信息 除Id外 几乎全填 * @param giftDTO sourceFlowId 全必填 如果有 也填
* @param number 发放数量 * @param number 发放数量
* @param isLimit 是否校验限领活动与管理员添加 不计限领
*/ */
void receiveCoupon(MkShopCouponRecordDTO mkShopCouponRecordDTO, Integer number); void receiveCoupon(MkShopCouponGiftDTO giftDTO, Integer number, boolean isLimit);
/** /**
* 用户优惠券 失效/删除 * 用户优惠券 失效/删除

View File

@ -8,6 +8,7 @@ import cn.hutool.core.util.StrUtil;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects;
/** /**
* 校验工具类 * 校验工具类
@ -17,6 +18,19 @@ import java.util.Map;
*/ */
public class AssertUtil { public class AssertUtil {
/**
* 判断两个对象是否不相等如果不相等则抛出异常
* @param a 实际值
* @param b 期望值
* @param message 异常消息
*/
public static void isNotEqual(Object a, Object b, String message) {
// 如果实际值与期望值不相等则抛出异常
if (!Objects.equals(a, b)) {
throw new ValidateException(message);
}
}
/** /**
* 检查字符串是否不为空或空白字符如果为空或空白字符则抛出异常 * 检查字符串是否不为空或空白字符如果为空或空白字符则抛出异常
* *

View File

@ -1,26 +1,38 @@
package com.czg.service.market.service.impl; package com.czg.service.market.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSONObject;
import com.czg.account.dto.QueryReceiveDto; import com.czg.account.dto.QueryReceiveDto;
import com.czg.account.entity.ShopInfo;
import com.czg.account.entity.ShopUser;
import com.czg.account.entity.UserInfo; import com.czg.account.entity.UserInfo;
import com.czg.account.service.ShopInfoService;
import com.czg.account.service.ShopUserService;
import com.czg.account.service.UserInfoService; import com.czg.account.service.UserInfoService;
import com.czg.account.vo.CouponReceiveVo; import com.czg.account.vo.CouponReceiveVo;
import com.czg.account.vo.UserCouponVo; import com.czg.account.vo.UserCouponVo;
import com.czg.exception.CzgException; import com.czg.exception.CzgException;
import com.czg.market.dto.MkShopCouponGiftDTO;
import com.czg.market.dto.MkShopCouponRecordDTO; import com.czg.market.dto.MkShopCouponRecordDTO;
import com.czg.market.entity.MkShopCouponRecord; import com.czg.market.entity.MkShopCouponRecord;
import com.czg.market.entity.ShopCoupon;
import com.czg.market.service.MkShopCouponRecordService; import com.czg.market.service.MkShopCouponRecordService;
import com.czg.market.service.ShopCouponService;
import com.czg.service.market.mapper.MkShopCouponRecordMapper; import com.czg.service.market.mapper.MkShopCouponRecordMapper;
import com.czg.utils.AssertUtil;
import com.czg.utils.PageUtil; import com.czg.utils.PageUtil;
import com.mybatisflex.core.paginate.Page; import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper; import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl; import com.mybatisflex.spring.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.apache.dubbo.config.annotation.DubboReference; import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService; import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.ChronoUnit;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -38,6 +50,13 @@ public class MkShopCouponRecordServiceImpl extends ServiceImpl<MkShopCouponRecor
@DubboReference @DubboReference
private UserInfoService userInfoService; private UserInfoService userInfoService;
@DubboReference
private ShopInfoService shopInfoService;
@DubboReference
private ShopUserService shopUserService;
@Resource
private ShopCouponService couponService;
@Override @Override
public List<CouponReceiveVo> queryReceive(QueryReceiveDto param) { public List<CouponReceiveVo> queryReceive(QueryReceiveDto param) {
@ -100,36 +119,81 @@ public class MkShopCouponRecordServiceImpl extends ServiceImpl<MkShopCouponRecor
} }
@Override @Override
public void receiveCoupon(MkShopCouponRecordDTO mkShopCouponRecordDTO, Integer number) { public void receiveCoupon(MkShopCouponGiftDTO giftDTO, Integer number, boolean isLimit) {
if (number == null || number <= 0) { if (number == null || number <= 0) {
throw new CzgException("发放数量不能小于0"); throw new CzgException("发放数量不能小于0");
} }
MkShopCouponRecord bean = BeanUtil.toBean(mkShopCouponRecordDTO, MkShopCouponRecord.class); AssertUtil.isNull(giftDTO.getShopId(), "店铺id不能为空");
if (bean.getType() == 1) { AssertUtil.isNull(giftDTO.getShopUserId(), "店铺用户id不能为空");
if (bean.getFullAmount() == null) { AssertUtil.isNull(giftDTO.getCouponId(), "优惠券id不能为空");
throw new CzgException("满减券,满减金额不能为空"); AssertUtil.isNull(giftDTO.getSourceId(), "来源活动id不能为空");
} AssertUtil.isBlank(giftDTO.getSource(), "来源描述不能为空");
if (bean.getDiscountAmount() == null) { ShopInfo shopInfo = shopInfoService.getById(giftDTO.getShopId());
throw new CzgException("满减券,折扣金额不能为空"); AssertUtil.isNull(shopInfo, "店铺不存在");
} ShopUser shopUser = shopUserService.getById(giftDTO.getShopUserId());
} else if (bean.getType() == 2) { AssertUtil.isNull(shopUser, "店铺用户不存在");
// 类型2fullAmount不能为空 ShopCoupon coupon = couponService.getById(giftDTO.getCouponId());
if (bean.getFullAmount() == null) { AssertUtil.isNull(coupon, "优惠券不存在");
throw new CzgException("商品兑换券,门槛金额不能为空"); AssertUtil.isNotEqual(coupon.getIsDel(), 0, "优惠券状态异常");
} AssertUtil.isNull(coupon.getShopId(), "优惠券店铺不存在");
} else if (bean.getType() == 3) { AssertUtil.isNotEqual(coupon.getShopId(), giftDTO.getShopId(), "优惠券店铺与发放店铺不一致");
// 类型3discountRate不能为空 AssertUtil.isNotEqual(coupon.getStatus(), 1, "优惠券已失效");
if (bean.getDiscountRate() == null) { if (coupon.getGiveNum() >= 0 && coupon.getLeftNum() < number) {
throw new CzgException("折扣券,折扣率不能为空"); throw new CzgException("优惠券库存不足");
}
if (isLimit) {
//查询已领取张数
long count = this.queryChain()
.eq(MkShopCouponRecord::getShopUserId, shopUser.getId())
.eq(MkShopCouponRecord::getCouponId, giftDTO.getCouponId()).count();
if (coupon.getGetLimit() >= 0 && count >= coupon.getGetLimit()) {
throw new CzgException("已达到领取限制不可领取7");
} }
} }
String couponJson = JSONObject.toJSONString(coupon);
LocalDateTime start = LocalDateTime.now().with(LocalTime.MIN);
LocalDateTime end = null;
if ("fixed".equals(coupon.getValidType())) {
//固定时间
if (coupon.getDaysToTakeEffect() != null && coupon.getDaysToTakeEffect() > 0) {
start = LocalDateTimeUtil.offset(start, coupon.getDaysToTakeEffect(), ChronoUnit.DAYS).with(LocalTime.MIN);
}
end = LocalDateTimeUtil.offset(start, coupon.getValidDays(), ChronoUnit.DAYS).with(LocalTime.MAX);
} else if ("custom".equals(coupon.getValidType())) {
//自定义时间
start = coupon.getValidStartTime();
end = coupon.getValidEndTime();
}
for (int i = 0; i < number; i++) { for (int i = 0; i < number; i++) {
bean.setId(null); MkShopCouponRecord record = new MkShopCouponRecord();
save(bean); record.setShopId(giftDTO.getShopId());
record.setShopUserId(giftDTO.getShopUserId());
record.setUserId(shopUser.getUserId());
record.setCouponId(giftDTO.getCouponId());
record.setSourceId(giftDTO.getSourceId());
record.setSourceFlowId(giftDTO.getSourceFlowId());
record.setSource(giftDTO.getSource());
record.setStatus(0);
record.setType(coupon.getCouponType());
record.setFullAmount(coupon.getFullAmount());
record.setDiscountAmount(coupon.getDiscountAmount());
record.setDiscountRate(coupon.getDiscountRate());
record.setCreateTime(LocalDateTime.now());
record.setIsDel(0);
record.setCouponJson(couponJson);
record.setUseStartTime(start);
record.setUseEndTime(end);
save(record);
} }
coupon.setGiftNum(coupon.getGiftNum() + number);
if (coupon.getGiveNum() != -10086) {
coupon.setLeftNum(coupon.getLeftNum() - number);
}
couponService.updateById(coupon);
} }
@Override @Override
public void deleteRecord(Long id) { public void deleteRecord(Long id) {
updateById(new MkShopCouponRecord().setId(id).setIsDel(1), true); updateById(new MkShopCouponRecord().setId(id).setIsDel(1), true);