Browse Source

盲票创建编辑增加券和券包的关联

cup 2 years ago
parent
commit
0f1e6c986e

+ 71 - 15
mp-service/src/main/java/com/qs/mp/admin/service/impl/TicketBoxServiceImpl.java

@@ -37,6 +37,7 @@ import ma.glasnost.orika.MapperFacade;
 import org.apache.pulsar.client.api.PulsarClientException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -140,18 +141,45 @@ public class TicketBoxServiceImpl extends ServiceImpl<TicketBoxMapper, TicketBox
 
         // 关联商品
         if (CollectionUtils.isNotEmpty(param.getGoodsList())) {
+            List<TicketBoxGoods> ticketBoxGoodsList = new ArrayList<>();
             // 设置boxId和商品信息
             for (TicketBoxGoods ticketBoxGoods : param.getGoodsList()) {
-                ticketBoxGoods.setId(null);
-                ticketBoxGoods.setBoxId(ticketBox.getBoxId());
-                Goods goods = goodsService.getById(ticketBoxGoods.getRefId());
-                if (goods != null) {
-                    ticketBoxGoods.setTitle(goods.getTitle());
-                    ticketBoxGoods.setPicUrl(goods.getPicUrl());
-                    ticketBoxGoods.setValue(goods.getValue());
+                TicketBoxGoods boxGoods = new TicketBoxGoods();
+                BeanUtils.copyProperties(ticketBoxGoods, boxGoods);
+                boxGoods.setBoxId(ticketBox.getBoxId());
+                if (TicketBoxGoodsTypeEnum.COUPON.equals(boxGoods.getType())) {
+                    Coupon coupon = couponService.getById(boxGoods.getRefId());
+                    if (coupon == null) {
+                        LogUtil.error(logger,"关联优惠券信息不存在,boxId:{0}", ticketBox.getBoxId());
+                        throw new ServiceException("关联优惠券信息不存在");
+                    }
+                    boxGoods.setTitle(coupon.getTitle());
+                    boxGoods.setPicUrl(coupon.getPicUrl());
+                    boxGoods.setValue(coupon.getDiscount());
+                } else if (TicketBoxGoodsTypeEnum.COUPON_PKG.equals(boxGoods.getType())) {
+                    CouponPkg couponPkg = couponPkgService.getById(boxGoods.getRefId());
+                    if (couponPkg == null) {
+                        LogUtil.error(logger,"关联券包信息不存在,boxId:{0}", ticketBox.getBoxId());
+                        throw new ServiceException("关联券包信息不存在");
+                    }
+                    boxGoods.setTitle(couponPkg.getTitle());
+                    boxGoods.setPicUrl(couponPkg.getPicUrl());
+                    boxGoods.setValue(couponPkg.getFacePrice());
+                } else {
+                    Goods goods = goodsService.getById(boxGoods.getRefId());
+                    if (goods == null) {
+                        LogUtil.error(logger,"关联商品信息不存在,boxId:{0}", ticketBox.getBoxId());
+                        throw new ServiceException("关联商品信息不存在");
+                    }
+                    boxGoods.setTitle(goods.getTitle());
+                    boxGoods.setPicUrl(goods.getPicUrl());
+                    boxGoods.setValue(goods.getValue());
                 }
+                ticketBoxGoodsList.add(boxGoods);
+            }
+            if (CollectionUtils.isNotEmpty(ticketBoxGoodsList)) {
+                ticketBoxGoodsService.saveBatch(ticketBoxGoodsList);
             }
-            ticketBoxGoodsService.saveBatch(param.getGoodsList());
         }
 
         // 清除指定渠道内容
@@ -364,17 +392,45 @@ public class TicketBoxServiceImpl extends ServiceImpl<TicketBoxMapper, TicketBox
 
         // 关联商品
         if (CollectionUtils.isNotEmpty(param.getGoodsList())) {
+            List<TicketBoxGoods> ticketBoxGoodsList = new ArrayList<>();
             // 设置boxId和商品信息
             for (TicketBoxGoods ticketBoxGoods : param.getGoodsList()) {
-                ticketBoxGoods.setBoxId(ticketBox.getBoxId());
-                Goods goods = goodsService.getById(ticketBoxGoods.getRefId());
-                if (goods != null) {
-                    ticketBoxGoods.setTitle(goods.getTitle());
-                    ticketBoxGoods.setPicUrl(goods.getPicUrl());
-                    ticketBoxGoods.setValue(goods.getValue());
+                TicketBoxGoods boxGoods = new TicketBoxGoods();
+                BeanUtils.copyProperties(ticketBoxGoods, boxGoods);
+                boxGoods.setBoxId(ticketBox.getBoxId());
+                if (TicketBoxGoodsTypeEnum.COUPON.equals(boxGoods.getType())) {
+                    Coupon coupon = couponService.getById(boxGoods.getRefId());
+                    if (coupon == null) {
+                        LogUtil.error(logger,"关联优惠券信息不存在,boxId:{0}", ticketBox.getBoxId());
+                        throw new ServiceException("关联优惠券信息不存在");
+                    }
+                    boxGoods.setTitle(coupon.getTitle());
+                    boxGoods.setPicUrl(coupon.getPicUrl());
+                    boxGoods.setValue(coupon.getDiscount());
+                } else if (TicketBoxGoodsTypeEnum.COUPON_PKG.equals(boxGoods.getType())) {
+                    CouponPkg couponPkg = couponPkgService.getById(boxGoods.getRefId());
+                    if (couponPkg == null) {
+                        LogUtil.error(logger,"关联券包信息不存在,boxId:{0}", ticketBox.getBoxId());
+                        throw new ServiceException("关联券包信息不存在");
+                    }
+                    boxGoods.setTitle(couponPkg.getTitle());
+                    boxGoods.setPicUrl(couponPkg.getPicUrl());
+                    boxGoods.setValue(couponPkg.getFacePrice());
+                } else {
+                    Goods goods = goodsService.getById(boxGoods.getRefId());
+                    if (goods == null) {
+                        LogUtil.error(logger,"关联商品信息不存在,boxId:{0}", ticketBox.getBoxId());
+                        throw new ServiceException("关联商品信息不存在");
+                    }
+                    boxGoods.setTitle(goods.getTitle());
+                    boxGoods.setPicUrl(goods.getPicUrl());
+                    boxGoods.setValue(goods.getValue());
                 }
+                ticketBoxGoodsList.add(boxGoods);
+            }
+            if (CollectionUtils.isNotEmpty(ticketBoxGoodsList)) {
+                ticketBoxGoodsService.saveBatch(ticketBoxGoodsList);
             }
-            ticketBoxGoodsService.saveBatch(param.getGoodsList());
         }
 
         // 指定渠道