|
@@ -0,0 +1,141 @@
|
|
|
+package com.qs.mp.admin.service.impl;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
+import com.qs.mp.admin.domain.*;
|
|
|
+import com.qs.mp.admin.domain.param.*;
|
|
|
+import com.qs.mp.admin.mapper.MarketingMapper;
|
|
|
+import com.qs.mp.admin.service.*;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.qs.mp.common.enums.TicketPrizeTypeEnum;
|
|
|
+import com.qs.mp.common.exception.ServiceException;
|
|
|
+import com.qs.mp.common.utils.bean.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+/**
|
|
|
+ * <p>
|
|
|
+ * 营销活动表 服务实现类
|
|
|
+ * </p>
|
|
|
+ *
|
|
|
+ * @author quanshu
|
|
|
+ * @since 2022-05-16
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class MarketingServiceImpl extends ServiceImpl<MarketingMapper, Marketing> implements IMarketingService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IGoodsService goodsService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ICouponService couponService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ICouponPkgService couponPkgService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IMarketingAwardsService marketingAwardsService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IMarketingAwardsPrizeService marketingAwardsPrizeService;
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void createMarketing(MarketingCreateParam marketingCreateParam) {
|
|
|
+ // 保存营销活动
|
|
|
+ Marketing marketing = new Marketing();
|
|
|
+ BeanUtils.copyProperties(marketingCreateParam, marketing);
|
|
|
+ this.save(marketing);
|
|
|
+
|
|
|
+ // 创建奖级和奖品信息
|
|
|
+ createAwardsAndPrize(marketingCreateParam.getAwardsList(), marketing);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void updateMarketing(MarketingUpdateParam marketingUpdateParam) {
|
|
|
+ Marketing marketing = this.getById(marketingUpdateParam.getId());
|
|
|
+ if (Objects.isNull(marketing)) {
|
|
|
+ throw new ServiceException("营销活动不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新营销活动
|
|
|
+ BeanUtils.copyProperties(marketingUpdateParam, marketing);
|
|
|
+ this.updateById(marketing);
|
|
|
+
|
|
|
+ // 删除奖级和奖品信息
|
|
|
+ marketingAwardsService.remove(new LambdaUpdateWrapper<MarketingAwards>().eq(MarketingAwards::getMarketingId, marketing.getId()));
|
|
|
+ marketingAwardsPrizeService.remove(new LambdaUpdateWrapper<MarketingAwardsPrize>().eq(MarketingAwardsPrize::getMarketingId, marketing.getId()));
|
|
|
+
|
|
|
+ // 创建奖级和奖品信息
|
|
|
+ createAwardsAndPrize(marketingUpdateParam.getAwardsList(), marketing);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建奖级和奖品信息
|
|
|
+ * @param awardsList
|
|
|
+ * @param marketing
|
|
|
+ */
|
|
|
+ private void createAwardsAndPrize(List<MarketingAwardsParam> awardsList, Marketing marketing) {
|
|
|
+ // 创建奖级
|
|
|
+ List<MarketingAwardsPrize> marketingAwardsPrizeList = new ArrayList<>();
|
|
|
+ for (MarketingAwardsParam awardsParam : awardsList) {
|
|
|
+ if (awardsParam.getInsideNum() > awardsParam.getQuantity()) {
|
|
|
+ throw new ServiceException("内定数量不能大于奖品数量");
|
|
|
+ }
|
|
|
+
|
|
|
+ MarketingAwards marketingAwards = new MarketingAwards();
|
|
|
+ BeanUtils.copyProperties(awardsParam, marketingAwards);
|
|
|
+ marketingAwards.setId(null);
|
|
|
+ // 设置活动id
|
|
|
+ marketingAwards.setMarketingId(marketing.getId());
|
|
|
+ marketingAwardsService.save(marketingAwards);
|
|
|
+
|
|
|
+ if (awardsParam.getPrizeList().size() > 1) {
|
|
|
+ throw new ServiceException("奖品目前暂时不能超过1个");
|
|
|
+ }
|
|
|
+
|
|
|
+ for (MarketingAwardsPrizeParam prizeParam : awardsParam.getPrizeList()) {
|
|
|
+ MarketingAwardsPrize awardsPrize = new MarketingAwardsPrize();
|
|
|
+ BeanUtils.copyProperties(prizeParam, awardsPrize);
|
|
|
+ awardsPrize.setId(null);
|
|
|
+ awardsPrize.setMarketingId(marketing.getId());
|
|
|
+ awardsPrize.setAwardsId(marketingAwards.getId());
|
|
|
+ if (null == awardsPrize.getQuantity() || 0 == awardsPrize.getQuantity()) {
|
|
|
+ // 页面没设置奖品的具体数量,则默认为整个奖项的数量
|
|
|
+ awardsPrize.setQuantity(marketingAwards.getQuantity());
|
|
|
+ }
|
|
|
+ if (prizeParam.getPrizeType() == TicketPrizeTypeEnum.GOODS) {
|
|
|
+ Goods goods = goodsService.getById(awardsPrize.getRefId());
|
|
|
+ awardsPrize.setTitle(goods.getTitle());
|
|
|
+ awardsPrize.setPicUrl(goods.getPicUrl());
|
|
|
+ awardsPrize.setValue(goods.getValue());
|
|
|
+ } else if (prizeParam.getPrizeType() == TicketPrizeTypeEnum.COUPON) {
|
|
|
+ Coupon coupon = couponService.getById(awardsPrize.getRefId());
|
|
|
+ awardsPrize.setTitle(coupon.getTitle());
|
|
|
+ awardsPrize.setPicUrl(coupon.getPicUrl());
|
|
|
+ awardsPrize.setValue(coupon.getDiscount());
|
|
|
+ } else if (prizeParam.getPrizeType() == TicketPrizeTypeEnum.COUPON_PKG) {
|
|
|
+ CouponPkg couponPkg = couponPkgService.getById(awardsPrize.getRefId());
|
|
|
+ awardsPrize.setTitle(couponPkg.getTitle());
|
|
|
+ awardsPrize.setValue(couponPkg.getFacePrice());
|
|
|
+ awardsPrize.setPicUrl(couponPkg.getPicUrl());
|
|
|
+ } else {
|
|
|
+ awardsPrize.setTitle("盲豆");
|
|
|
+ awardsPrize.setPicUrl("md.jpeg");
|
|
|
+ }
|
|
|
+ marketingAwardsPrizeList.add(awardsPrize);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ marketingAwardsPrizeService.saveBatch(marketingAwardsPrizeList);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|