优惠卷
复读
This commit is contained in:
@@ -3,7 +3,6 @@ package com.peanut.modules.common.service.impl;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.github.yulichang.wrapper.MPJLambdaWrapper;
|
||||
import com.peanut.common.utils.DateUtil;
|
||||
import com.peanut.common.utils.DateUtils;
|
||||
import com.peanut.common.utils.R;
|
||||
import com.peanut.common.utils.ShiroUtils;
|
||||
@@ -11,14 +10,13 @@ import com.peanut.config.DelayQueueConfig;
|
||||
import com.peanut.modules.common.dao.*;
|
||||
import com.peanut.modules.common.entity.*;
|
||||
import com.peanut.modules.common.service.CouponService;
|
||||
import io.swagger.models.auth.In;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.springframework.amqp.core.MessagePostProcessor;
|
||||
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.*;
|
||||
|
||||
@Slf4j
|
||||
@@ -36,9 +34,11 @@ public class CouponServiceImpl extends ServiceImpl<CouponDao, CouponEntity> impl
|
||||
@Autowired
|
||||
private RabbitTemplate rabbitTemplate;
|
||||
@Autowired
|
||||
private ShopProductDao shopProductDao;
|
||||
@Autowired
|
||||
private ShopProductCourseDao shopProductCourseDao;
|
||||
@Autowired
|
||||
private CouponToProductDao couponToProductDao;
|
||||
@Autowired
|
||||
private ShopProductDao shopProductDao;
|
||||
|
||||
@Override
|
||||
public CouponEntity getByIdSetRange(int id) {
|
||||
@@ -95,19 +95,21 @@ public class CouponServiceImpl extends ServiceImpl<CouponDao, CouponEntity> impl
|
||||
couponHistory.setEndTime(couponEntity.getExpireTime());
|
||||
}
|
||||
couponHistoryDao.insert(couponHistory);
|
||||
//发放完优惠卷设置过期
|
||||
rabbitTemplate.convertAndSend(
|
||||
DelayQueueConfig.COMMON_EXCHANGE,
|
||||
DelayQueueConfig.COMMON_ROUTING_KEY,
|
||||
"couponExpire"+","+couponHistory.getId(),
|
||||
messagePostProcessor(couponHistory.getEndTime().getTime()-new Date().getTime())
|
||||
);
|
||||
if (couponEntity.getEffectType()!=0){
|
||||
//发放完优惠卷设置过期
|
||||
rabbitTemplate.convertAndSend(
|
||||
DelayQueueConfig.COMMON_EXCHANGE,
|
||||
DelayQueueConfig.COMMON_ROUTING_KEY,
|
||||
"couponExpire"+","+couponHistory.getId(),
|
||||
messagePostProcessor(couponHistory.getEndTime().getTime()-new Date().getTime())
|
||||
);
|
||||
}
|
||||
return R.ok();
|
||||
}else {
|
||||
return R.error("每人限领"+couponEntity.getLimitedCollar()+"张");
|
||||
}
|
||||
}else {
|
||||
return R.error("优惠卷已放完");
|
||||
return R.error("优惠券已放完");
|
||||
}
|
||||
}
|
||||
private MessagePostProcessor messagePostProcessor(long date) {
|
||||
@@ -125,16 +127,33 @@ public class CouponServiceImpl extends ServiceImpl<CouponDao, CouponEntity> impl
|
||||
.eq(CouponHistory::getStatus,0));
|
||||
if (couponHistoryList.size() > 0) {
|
||||
for (CouponHistory couponHistory : couponHistoryList) {
|
||||
if (couponHistory.getEffectType()==0){
|
||||
res.add(couponHistory);
|
||||
}else {
|
||||
List<ShopProduct> shopProductList = shopProductDao.selectList(new MPJLambdaWrapper<ShopProduct>()
|
||||
.leftJoin(BuyOrderProduct.class,BuyOrderProduct::getProductId,ShopProduct::getProductId)
|
||||
.eq(BuyOrderProduct::getOrderId,params.get("orderId").toString()));
|
||||
for (ShopProduct shopProduct : shopProductList) {
|
||||
Set<Integer> set = getShopProductByCoupon(couponHistory.getCouponId());
|
||||
if(!set.add(shopProduct.getProductId())){
|
||||
res.add(couponHistory);
|
||||
CouponEntity couponEntity = this.baseMapper.selectById(couponHistory.getCouponId());
|
||||
if (couponEntity != null) {
|
||||
//校验优惠卷使用时间
|
||||
couponHistory.setCanUse(1);
|
||||
couponHistory.setCanUseReason("");
|
||||
if (couponHistory.getEffectType()!=0){
|
||||
if (couponHistory.getStartTime().getTime() < new Date().getTime() &&
|
||||
couponHistory.getEndTime().getTime() > new Date().getTime()) {
|
||||
}else {
|
||||
couponHistory.setCanUse(0);
|
||||
couponHistory.setCanUseReason("优惠券使用时间未到");
|
||||
}
|
||||
}
|
||||
couponHistory.setCouponEntity(couponEntity);
|
||||
if (couponEntity.getCouponRange()==0){
|
||||
//无限制
|
||||
res.add(couponHistory);
|
||||
}else {
|
||||
String shopProductIds = params.get("shopProductIds").toString();
|
||||
String[] ids = shopProductIds.split(",");
|
||||
for (String shopProductId : ids) {
|
||||
//通过优惠卷获取商品
|
||||
Set<Integer> set = getShopProductByCoupon(couponEntity);
|
||||
//比对用户优惠卷是否有此商品
|
||||
if(!set.add(Integer.parseInt(shopProductId))){
|
||||
res.add(couponHistory);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -143,32 +162,30 @@ public class CouponServiceImpl extends ServiceImpl<CouponDao, CouponEntity> impl
|
||||
return res;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Integer> getShopProductByCoupon(int couponId) {
|
||||
public Set<Integer> getShopProductByCoupon(CouponEntity couponEntity) {
|
||||
Set<Integer> set = new HashSet<>();
|
||||
CouponEntity couponEntity = couponDao.selectById(couponId);
|
||||
if (couponEntity!=null){
|
||||
String[] strs = couponEntity.getRangeInfo().split(",");
|
||||
if (couponEntity.getCouponRange()==1) {
|
||||
for (String courseId : strs) {
|
||||
List<ShopProductCourseEntity> shopProductCourseList = shopProductCourseDao.selectList(new LambdaQueryWrapper<ShopProductCourseEntity>()
|
||||
.eq(ShopProductCourseEntity::getCourseId,courseId));
|
||||
for (ShopProductCourseEntity shopProductCourseEntity : shopProductCourseList) {
|
||||
set.add(shopProductCourseEntity.getProductId());
|
||||
}
|
||||
String[] strs = couponEntity.getRangeInfo().split(",");
|
||||
if (couponEntity.getCouponRange()==1) {
|
||||
//1课程卷
|
||||
for (String courseId : strs) {
|
||||
List<ShopProductCourseEntity> shopProductCourseList = shopProductCourseDao.selectList(new LambdaQueryWrapper<ShopProductCourseEntity>()
|
||||
.eq(ShopProductCourseEntity::getCourseId,courseId));
|
||||
for (ShopProductCourseEntity shopProductCourseEntity : shopProductCourseList) {
|
||||
set.add(shopProductCourseEntity.getProductId());
|
||||
}
|
||||
}else if (couponEntity.getCouponRange()==2) {
|
||||
for (String courseMedical : strs) {
|
||||
MPJLambdaWrapper<ShopProductCourseEntity> wrapper = new MPJLambdaWrapper<>();
|
||||
wrapper.leftJoin(CourseToMedicine.class,CourseToMedicine::getCourseId,ShopProductCourseEntity::getCourseId);
|
||||
List l = new ArrayList();
|
||||
getCourseMedicalIds(Integer.parseInt(courseMedical),l);
|
||||
wrapper.in(CourseToMedicine::getMedicalId,l);
|
||||
wrapper.selectAll(ShopProductCourseEntity.class);
|
||||
List<ShopProductCourseEntity> shopProductCourseList = shopProductCourseDao.selectList(wrapper);
|
||||
for (ShopProductCourseEntity shopProductCourseEntity : shopProductCourseList) {
|
||||
set.add(shopProductCourseEntity.getProductId());
|
||||
}
|
||||
}
|
||||
}else if (couponEntity.getCouponRange()==2) {
|
||||
//2课程品类卷
|
||||
for (String courseMedical : strs) {
|
||||
MPJLambdaWrapper<ShopProductCourseEntity> wrapper = new MPJLambdaWrapper<>();
|
||||
wrapper.leftJoin(CourseToMedicine.class,CourseToMedicine::getCourseId,ShopProductCourseEntity::getCourseId);
|
||||
List l = new ArrayList();
|
||||
getCourseMedicalIds(Integer.parseInt(courseMedical),l);
|
||||
wrapper.in(CourseToMedicine::getMedicalId,l);
|
||||
wrapper.selectAll(ShopProductCourseEntity.class);
|
||||
List<ShopProductCourseEntity> shopProductCourseList = shopProductCourseDao.selectList(wrapper);
|
||||
for (ShopProductCourseEntity shopProductCourseEntity : shopProductCourseList) {
|
||||
set.add(shopProductCourseEntity.getProductId());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -188,5 +205,36 @@ public class CouponServiceImpl extends ServiceImpl<CouponDao, CouponEntity> impl
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void useCouponAmount(CouponHistory couponHistory) {
|
||||
couponHistory.setStatus(1);//使用状态 0 未使用 1 已使用 2 已过期
|
||||
couponHistory.setUseTime(new Date());
|
||||
couponHistoryDao.updateById(couponHistory);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void rollbackCoupon(int couponHistoryId) {
|
||||
CouponHistory couponHistory = couponHistoryDao.selectById(couponHistoryId);
|
||||
if (couponHistory != null) {
|
||||
couponHistory.setOrderId(0);
|
||||
couponHistory.setStatus(0);
|
||||
couponHistory.setUseTime(null);
|
||||
couponHistoryDao.updateById(couponHistory);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void insertCouponHistoryByProductId(List<Integer> productIds) {
|
||||
List<CouponToProduct> couponToProductList = couponToProductDao.selectList(new LambdaQueryWrapper<CouponToProduct>()
|
||||
.in(CouponToProduct::getProductId,productIds));
|
||||
for (CouponToProduct couponToProduct : couponToProductList) {
|
||||
ShopProduct shopProduct = shopProductDao.selectById(couponToProduct.getProductId());
|
||||
insertCouponHistory(couponToProduct.getCouponId(),
|
||||
ShiroUtils.getUId(), 1,"购买商品"+shopProduct.getProductName()+"赠送");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.peanut.modules.common.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.peanut.modules.common.dao.CouponToProductDao;
|
||||
import com.peanut.modules.common.entity.CouponToProduct;
|
||||
import com.peanut.modules.common.service.CouponToProductService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Slf4j
|
||||
@Service("commonCouponToProductService")
|
||||
public class CouponToProductServiceImpl extends ServiceImpl<CouponToProductDao, CouponToProduct> implements CouponToProductService {
|
||||
}
|
||||
Reference in New Issue
Block a user