|
|
@@ -1,8 +1,11 @@
|
|
|
package com.zsElectric.boot.business.service.impl;
|
|
|
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.zsElectric.boot.business.model.form.applet.AppletGainCouponForm;
|
|
|
import com.zsElectric.boot.business.model.query.applet.AppCouponQuery;
|
|
|
import com.zsElectric.boot.business.model.vo.applet.AppCouponStatusNumVO;
|
|
|
+import com.zsElectric.boot.business.service.CouponTemplateService;
|
|
|
import com.zsElectric.boot.core.exception.CouponException;
|
|
|
import com.zsElectric.boot.business.mapper.CouponTemplateMapper;
|
|
|
import com.zsElectric.boot.business.model.entity.CouponTemplate;
|
|
|
@@ -41,7 +44,7 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> impleme
|
|
|
|
|
|
private final CouponConverter couponConverter;
|
|
|
|
|
|
- private final CouponTemplateMapper couponTemplateMapper;
|
|
|
+ private final CouponTemplateService couponTemplateService;
|
|
|
|
|
|
/**
|
|
|
* 获取优惠劵分页列表
|
|
|
@@ -122,7 +125,7 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> impleme
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
public Long takeCoupon(Long templateId, Long userId, Integer takeType) {
|
|
|
// 获取优惠券模板
|
|
|
- CouponTemplate template = couponTemplateMapper.selectById(templateId);
|
|
|
+ CouponTemplate template = couponTemplateService.getById(templateId);
|
|
|
if (template == null) {
|
|
|
throw new CouponException("优惠券模板不存在");
|
|
|
}
|
|
|
@@ -159,7 +162,7 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> impleme
|
|
|
|
|
|
// 更新模板已发放数量
|
|
|
template.setTotalCountAll(template.getTotalCountAll() != null ? template.getTotalCountAll() + 1 : 1);
|
|
|
- couponTemplateMapper.updateById(template);
|
|
|
+ couponTemplateService.updateById(template);
|
|
|
|
|
|
return coupon.getId();
|
|
|
}
|
|
|
@@ -178,7 +181,7 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> impleme
|
|
|
}
|
|
|
|
|
|
// 获取优惠券模板
|
|
|
- CouponTemplate template = couponTemplateMapper.selectById(coupon.getTemplateId());
|
|
|
+ CouponTemplate template = couponTemplateService.getById(coupon.getTemplateId());
|
|
|
if (template == null) {
|
|
|
return BigDecimal.ZERO;
|
|
|
}
|
|
|
@@ -296,7 +299,7 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> impleme
|
|
|
}
|
|
|
|
|
|
// 获取优惠券模板
|
|
|
- CouponTemplate template = couponTemplateMapper.selectById(coupon.getTemplateId());
|
|
|
+ CouponTemplate template = couponTemplateService.getById(coupon.getTemplateId());
|
|
|
if (template == null) {
|
|
|
return false;
|
|
|
}
|
|
|
@@ -432,4 +435,40 @@ public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> impleme
|
|
|
return this.baseMapper.getCouponStatusNum(userId);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public Boolean gainCoupon(AppletGainCouponForm formData) {
|
|
|
+ formData.setUserId(SecurityUtils.getUserId());
|
|
|
+ //查询优惠券模板是否存在且可用
|
|
|
+ CouponTemplate template = couponTemplateService.getOne(Wrappers.lambdaQuery(CouponTemplate.class)
|
|
|
+ .eq(CouponTemplate::getCode, formData.getCouponCode()));
|
|
|
+ if (ObjectUtil.isEmpty(template)) {
|
|
|
+ throw new CouponException("优惠券不存在");
|
|
|
+ }
|
|
|
+ if (!couponTemplateService.isValidTemplate(template.getId())) {
|
|
|
+ throw new CouponException("优惠券已失效");
|
|
|
+ }
|
|
|
+ //判断当前用户是否已经领取过该优惠券
|
|
|
+ Coupon coupon = this.getOne(Wrappers.lambdaQuery(Coupon.class)
|
|
|
+ .eq(Coupon::getUserId, formData.getUserId())
|
|
|
+ .eq(Coupon::getCouponCode, formData.getCouponCode())
|
|
|
+ );
|
|
|
+ if (ObjectUtil.isNotEmpty(coupon)) {
|
|
|
+ throw new CouponException("当前用户已经领取过该优惠券");
|
|
|
+ }
|
|
|
+ Coupon userCoupon = new Coupon();
|
|
|
+ userCoupon.setTemplateId(template.getId());
|
|
|
+ userCoupon.setName(template.getName());
|
|
|
+ userCoupon.setCouponCode(template.getCode());
|
|
|
+ userCoupon.setStatus(1);
|
|
|
+ userCoupon.setDescription(template.getDescription());
|
|
|
+ userCoupon.setUserId(formData.getUserId());
|
|
|
+ userCoupon.setTakeType(1);
|
|
|
+
|
|
|
+ LocalDateTime now = LocalDateTime.now();
|
|
|
+
|
|
|
+ userCoupon.setTakeTime(now);
|
|
|
+ userCoupon.setExpireTime(now.plusDays(template.getFailureTime()));
|
|
|
+ return this.save(userCoupon);
|
|
|
+ }
|
|
|
+
|
|
|
}
|