Browse Source

修改优惠券相关功能

hgw 3 years ago
parent
commit
d9252aac06

+ 32 - 2
mec-biz/src/main/java/com/ym/mec/biz/dal/enums/CouponDetailTypeEnum.java

@@ -1,7 +1,13 @@
 package com.ym.mec.biz.dal.enums;
 
+import com.ym.mec.common.page.WrapperUtil;
+import org.apache.commons.lang3.StringUtils;
+
+import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.List;
 import java.util.Objects;
+import java.util.stream.Collectors;
 
 /**
  * 优惠券明细类型
@@ -59,8 +65,18 @@ public enum CouponDetailTypeEnum {
         this.msg = msg;
     }
 
-    //根据传入code找到对应的券类型,默认全类型
+    //根据传入code找到对应的券类型,如果不存在则返回 全类型
     public static String of(String code) {
+        String check = check(code);
+        return StringUtils.isBlank(check) ? FULLCOUPON.getCode() : check;
+    }
+
+    //如果不存在则返回 null
+    public static String ofNull(String code) {
+        return check(code);
+    }
+
+    private static String check(String code) {
         CouponDetailTypeEnum detailTypeEnum = Arrays.stream(CouponDetailTypeEnum.values())
                 .filter(e -> e.code.equalsIgnoreCase(code))
                 .findFirst()
@@ -69,6 +85,8 @@ public enum CouponDetailTypeEnum {
             return detailTypeEnum.getCode();
         }
         switch (code) {
+            case "INSTRUMENT":
+                return MUSICAL.getCode();
             case "TRAINING_SINGLE":
                 return SINGLE.getCode();
             case "THEORY_COURSE":
@@ -79,9 +97,21 @@ public enum CouponDetailTypeEnum {
             case "CLOUD_TEACHER_PLUS":
                 return MEMBER.getCode();
             default:
-                return FULLCOUPON.getCode();
+                return null;
         }
+    }
 
+    public static String[] getAllowType(CouponDetailTypeEnum... type){
+        List<String> resultList = new ArrayList<>();
+        resultList.add(FULLCOUPON.getCode());
+        if(Objects.nonNull(type)){
+            List<String> collect = Arrays.stream(type)
+                    .map(CouponDetailTypeEnum::getCode)
+                    .distinct()
+                    .collect(Collectors.toList());
+            resultList.addAll(collect);
+        }
+        return resultList.toArray(new String[0]);
     }
 
 }

+ 0 - 9
mec-biz/src/main/java/com/ym/mec/biz/service/MusicGroupService.java

@@ -71,15 +71,6 @@ public interface MusicGroupService extends BaseService<String, MusicGroup> {
     HttpResponseResult pay(RegisterPayDto registerPayDto) throws Exception;
 
     /**
-     * 获取优惠券相关参数-校验优惠券和传入的支付金额是否合法
-     *
-     * @param couponIdList 优惠券集合
-     * @param total        本次订单所有商品的总数量
-     * @param payAmount    页面传入的本次支付的金额
-     */
-    CouponPayParam getCouponPayParam(List<Integer> couponIdList, AtomicInteger total, BigDecimal payAmount);
-
-    /**
      * 继续缴费
      * @param registerPayDto
      * @return

+ 24 - 1
mec-biz/src/main/java/com/ym/mec/biz/service/SysCouponCodeService.java

@@ -2,6 +2,7 @@ package com.ym.mec.biz.service;
 
 import com.ym.mec.biz.dal.dto.HorseRaceLampDto;
 import com.ym.mec.biz.dal.dto.SysCouponCodeDto;
+import com.ym.mec.biz.dal.entity.CouponPayParam;
 import com.ym.mec.biz.dal.entity.StudentPaymentOrder;
 import com.ym.mec.biz.dal.entity.SysCouponCode;
 import com.ym.mec.biz.dal.page.SysCouponCodeQueryInfo;
@@ -11,6 +12,7 @@ import com.ym.mec.common.service.BaseService;
 
 import java.math.BigDecimal;
 import java.util.List;
+import java.util.concurrent.atomic.AtomicInteger;
 
 public interface SysCouponCodeService extends BaseService<Long, SysCouponCode> {
 
@@ -55,7 +57,28 @@ public interface SysCouponCodeService extends BaseService<Long, SysCouponCode> {
      */
     StudentPaymentOrder use(List<Integer> couponIdList, BigDecimal amount, Boolean useFlag);
 
-    List<SysCouponCodeDto> checkCoupon(List<Integer> couponIdList);
+    StudentPaymentOrder use(List<Integer> couponIdList, BigDecimal amount, Boolean useFlag, String... allowType);
+
+    List<SysCouponCodeDto> checkCoupon(List<Integer> couponIdList, String... allowType);
+
+    /**
+     * 获取优惠券相关参数-校验优惠券和传入的支付金额是否合法
+     *
+     * @param couponIdList 优惠券集合
+     * @param total        本次订单所有商品的总数量
+     * @param payAmount    页面传入的本次支付的金额
+     * @param allowType   本次允许使用的优惠券类型,null则不验证
+     */
+    CouponPayParam getCouponPayParam(List<Integer> couponIdList, Integer total, BigDecimal payAmount, String... allowType);
+
+    /**
+     * 获取优惠券相关参数-校验优惠券和传入的支付金额是否合法
+     *
+     * @param couponIdList 优惠券集合
+     * @param total        本次订单所有商品的总数量
+     * @param payAmount    页面传入的本次支付的金额
+     */
+    CouponPayParam getCouponPayParam(List<Integer> couponIdList, Integer total, BigDecimal payAmount);
 
     List<SysCouponCodeDto> findByIdList(List<Integer> couponIdList);
 

+ 3 - 1
mec-biz/src/main/java/com/ym/mec/biz/service/impl/EduPracticeGroupServiceImpl.java

@@ -733,8 +733,10 @@ public class EduPracticeGroupServiceImpl implements EduPracticeGroupService{
         
         BigDecimal studentSingleCourseOriginalCost = price.divide(new BigDecimal(practiceCourses.size()), CommonConstants.DECIMAL_PLACE, BigDecimal.ROUND_DOWN);
 
+        //校验优惠券
+        String[] checkCoupon = CouponDetailTypeEnum.getAllowType(CouponDetailTypeEnum.PRACTICE);
         //使用优惠券
-        StudentPaymentOrder studentPaymentOrder = sysCouponCodeService.use(practiceGroupBuyParams.getCouponIdList(), price, true);
+        StudentPaymentOrder studentPaymentOrder = sysCouponCodeService.use(practiceGroupBuyParams.getCouponIdList(), price, true,checkCoupon);
         //实际支付金额,去除优惠券
         BigDecimal actualPrice = studentPaymentOrder.getExpectAmount();
         BigDecimal divide = actualPrice.divide(new BigDecimal(practiceCourses.size()), ROUND_DOWN);

+ 4 - 59
mec-biz/src/main/java/com/ym/mec/biz/service/impl/MusicGroupServiceImpl.java

@@ -531,7 +531,9 @@ public class MusicGroupServiceImpl extends BaseServiceImpl<String, MusicGroup> i
         Integer userId = sporadicPayDto.getUserId();
         String orderNo = idGeneratorService.generatorId("payment") + "";
         String channelType = "";
-        StudentPaymentOrder studentPaymentOrder = sysCouponCodeService.use(sporadicPayDto.getCouponIdList(), amount, true);
+        StudentPaymentOrder studentPaymentOrder = sysCouponCodeService.use(sporadicPayDto.getCouponIdList(), amount,
+                true,
+                CouponDetailTypeEnum.getAllowType());
         amount = studentPaymentOrder.getActualAmount();
         if (!(amount.compareTo(sporadicPayDto.getAmount()) == 0)) {
             throw new BizException("订单金额异常");
@@ -861,7 +863,7 @@ public class MusicGroupServiceImpl extends BaseServiceImpl<String, MusicGroup> i
             //获取本次商品的总数
             AtomicInteger total = getTotalNum(registerPayDto);
             //校验优惠券的使用
-            couponPayParam = getCouponPayParam(registerPayDto.getCouponIdList(), total, registerPayDto.getAmount());
+            couponPayParam = sysCouponCodeService.getCouponPayParam(registerPayDto.getCouponIdList(), total.get(), registerPayDto.getAmount());
             studentPaymentOrder.setCouponCodeId(StringUtils.join(registerPayDto.getCouponIdList(), ","));
             studentPaymentOrder.setCouponRemitFee(couponPayParam.getCouponRemitTotal());
         }
@@ -870,63 +872,6 @@ public class MusicGroupServiceImpl extends BaseServiceImpl<String, MusicGroup> i
         return couponPayParam;
     }
 
-    /**
-     * 获取优惠券相关参数-校验优惠券和传入的支付金额是否合法
-     *
-     * @param couponIdList 优惠券集合
-     * @param total        本次订单所有商品的总数量
-     * @param payAmount    页面传入的本次支付的金额
-     */
-    public CouponPayParam getCouponPayParam(List<Integer> couponIdList, AtomicInteger total, BigDecimal payAmount) {
-        //查询本次付款使用的优惠券
-        List<SysCouponCodeDto> sysCouponCodeDtoList = sysCouponCodeService.checkCoupon(couponIdList);
-        //全类型优惠券的总额度
-        BigDecimal fullTypeTotal = new BigDecimal(0);
-        //非全类型优惠券的总额度
-        BigDecimal notFullTypeTotal = new BigDecimal(0);
-        //非全类型优惠券的总数量
-        AtomicInteger notFullTypeTotalNum = new AtomicInteger(0);
-        //优惠券减免的总额度
-        BigDecimal couponRemitTotal = new BigDecimal(0);
-        //交易阈值 最后付款的金额大于该值就不对
-        BigDecimal threshold;
-
-        for (SysCouponCodeDto d : sysCouponCodeDtoList) {
-            if (d.getTypeDetail().equals(CouponDetailTypeEnum.FULLCOUPON.getCode())) {
-                fullTypeTotal = fullTypeTotal.add(d.getFullAmount());
-            } else {
-                notFullTypeTotal = notFullTypeTotal.add(d.getFullAmount());
-                notFullTypeTotalNum.set(notFullTypeTotalNum.incrementAndGet());
-            }
-            couponRemitTotal = couponRemitTotal.add(d.getFaceValue());
-        }
-        //阈值 = (全品类券总面值 ÷ 总商品数量) + (非全品类券总面值 ÷ 非全品类券的数量)
-        threshold = fullTypeTotal.divide(new BigDecimal(total.get()), 3, RoundingMode.HALF_UP)
-                .add(notFullTypeTotal.divide(new BigDecimal(notFullTypeTotalNum.get()), 3, RoundingMode.HALF_UP));
-
-        if (payAmount.compareTo(threshold) < 0) {
-            throw new BizException("优惠券使用错误,交易失败!");
-        }
-        //将各种类型的优惠券合并 算出总面试 总减免金额
-        Map<String, CouponPayTypeInfo> couponTypeInfo = new HashMap<>();
-        //根据CouponDetailTypeEnum 进行分组集合
-        WrapperUtil.groupList(sysCouponCodeDtoList, SysCouponCodeDto::getTypeDetail)
-                .forEach((typeDetail, list) -> {
-                    CouponPayTypeInfo coupon = new CouponPayTypeInfo();
-                    list.forEach(c -> {
-                        coupon.setCouponAmount(coupon.getCouponAmount().add(c.getFullAmount()));
-                        coupon.setFaceValue(coupon.getFaceValue().add(c.getFaceValue()));
-                    });
-                    coupon.setTypeDetail(typeDetail);
-                    couponTypeInfo.put(typeDetail, coupon);
-                });
-
-        CouponPayParam result = new CouponPayParam();
-        result.setCouponRemitTotal(couponRemitTotal);
-        result.setCouponTypeInfo(couponTypeInfo);
-        return result;
-    }
-
     @Override
     @Transactional(rollbackFor = Exception.class)
     public HttpResponseResult pay(RegisterPayDto registerPayDto) throws Exception {

File diff suppressed because it is too large
+ 196 - 199
mec-biz/src/main/java/com/ym/mec/biz/service/impl/PracticeGroupServiceImpl.java


+ 2 - 0
mec-biz/src/main/java/com/ym/mec/biz/service/impl/StudentRegistrationServiceImpl.java

@@ -673,6 +673,8 @@ public class StudentRegistrationServiceImpl extends BaseServiceImpl<Long, Studen
             //获取使用了优惠券之后的总金额
             BigDecimal useCouponAmount = WrapperUtil.sumList(studentPaymentOrderDetailList, StudentPaymentOrderDetail::getRemitFee);
             studentPaymentOrder.setCouponRemitFee(useCouponAmount);
+            //核销优惠券
+            sysCouponCodeService.useCoupon(registerPayDto.getCouponIdList());
         }
         //添加订单明细
         studentPaymentOrderDetailService.batchAdd(studentPaymentOrderDetailList);

+ 29 - 26
mec-biz/src/main/java/com/ym/mec/biz/service/impl/StudentRepairServiceImpl.java

@@ -9,7 +9,6 @@ import com.ym.mec.biz.dal.dao.*;
 import com.ym.mec.biz.dal.dto.BasicUserDto;
 import com.ym.mec.biz.dal.dto.GoodsSellDto;
 import com.ym.mec.biz.dal.dto.RepairGoodsDto;
-import com.ym.mec.biz.dal.dto.SysCouponCodeDto;
 import com.ym.mec.biz.dal.entity.*;
 import com.ym.mec.biz.dal.enums.*;
 import com.ym.mec.biz.dal.page.RepairStudentQueryInfo;
@@ -22,7 +21,7 @@ import com.ym.mec.common.service.IdGeneratorService;
 import com.ym.mec.common.service.impl.BaseServiceImpl;
 import com.ym.mec.thirdparty.message.MessageSenderPluginContext;
 import com.ym.mec.util.collection.MapUtil;
-
+import org.apache.commons.collections.CollectionUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -36,6 +35,7 @@ import java.math.BigDecimal;
 import java.util.*;
 import java.util.stream.Collectors;
 
+import static com.ym.mec.biz.dal.enums.CouponDetailTypeEnum.*;
 import static com.ym.mec.biz.dal.enums.DealStatusEnum.*;
 
 @Service
@@ -130,18 +130,18 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
 
             if (orderByOrderNo.getStatus() == SUCCESS) {
                 throw new BizException("您已支付请勿重复提交");
-            }else if(orderByOrderNo.getStatus() == ING){
-            	
-            	// 查询订单状态
+            } else if (orderByOrderNo.getStatus() == ING) {
+
+                // 查询订单状态
                 PayStatus payStatus = studentPaymentOrderService.queryPayStatus(orderByOrderNo.getPaymentChannel(), orderByOrderNo.getOrderNo(), orderByOrderNo.getTransNo());
-                if(payStatus != PayStatus.FAILED){
-                	if(payStatus == PayStatus.SUCCESSED){
-                		throw new BizException("订单已支付成功,请勿重复支付");
-                	}/*else if(payStatus == PayStatus.PAYING){
+                if (payStatus != PayStatus.FAILED) {
+                    if (payStatus == PayStatus.SUCCESSED) {
+                        throw new BizException("订单已支付成功,请勿重复支付");
+                    }/*else if(payStatus == PayStatus.PAYING){
                 		throw new BizException("订单还在交易中,请稍后重试");
                 	}*/
                 }
-                
+
                 orderByOrderNo.setStatus(CLOSE);
                 studentPaymentOrderService.update(orderByOrderNo);
                 if (orderByOrderNo.getBalancePaymentAmount() != null && orderByOrderNo.getBalancePaymentAmount().compareTo(BigDecimal.ZERO) > 0) {
@@ -158,7 +158,7 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
         }
         Integer studentId = studentGoodsSell.getUserId();
         List<GoodsSellDto> goodsSellDtos = studentGoodsSell.getGoodsSellDtos();
-        if (goodsSellDtos == null || goodsSellDtos.size() == 0) {
+        if (CollectionUtils.isEmpty(goodsSellDtos)) {
             throw new BizException("请选择需要购买的商品");
         }
         if (studentId == null) {
@@ -186,7 +186,7 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
                 studentGoodsSell.setCooperationOrganId(musicGroup.getCooperationOrganId());
             }
         }
-        List<Integer> goodsIds = goodsSellDtos.stream().map(e -> e.getGoodsId()).collect(Collectors.toList());
+        List<Integer> goodsIds = goodsSellDtos.stream().map(GoodsSellDto::getGoodsId).collect(Collectors.toList());
         Map<Integer, String> integerStringMap = getMap("goods", "id_", "type_", goodsIds, Integer.class, String.class);
         Map<Integer, BigDecimal> map = getMap("goods", "id_", "discount_price_", goodsIds, Integer.class, BigDecimal.class);
         Map<Integer, BigDecimal> groupPriceMap = getMap("goods", "id_", "group_purchase_price_", goodsIds, Integer.class, BigDecimal.class);
@@ -208,14 +208,17 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
             groupAmount = groupAmount.add(goodsSellDto.getGoodsGroupPrice());
         }
         amount = amount.subtract(studentGoodsSell.getMarketAmount());
-        if(amount.compareTo(groupAmount) < 0){
+        if (amount.compareTo(groupAmount) < 0) {
             throw new BizException("操作失败:该金额减免后商品价格不可低于商品团购价");
         }
         if (amount.signum() < 0) {
             throw new BizException("操作失败:订单金额异常");
         }
+        //优惠券使用范围
+        String[] checkCoupon = CouponDetailTypeEnum.getAllowType(ACCESSORIES, MUSICAL);
         //使用优惠券
-        StudentPaymentOrder studentPaymentOrder = sysCouponCodeService.use(studentGoodsSell.getCouponIdList(),amount,studentGoodsSell.getType() != 1);
+        StudentPaymentOrder studentPaymentOrder = sysCouponCodeService.use(studentGoodsSell.getCouponIdList(),
+                amount, studentGoodsSell.getType() != 1, checkCoupon);
         amount = studentPaymentOrder.getActualAmount();
         // 判断金额是否正确
 //        if (studentGoodsSell.getTotalAmount().compareTo(amount) != 0) {
@@ -228,8 +231,8 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
         String orderNo = idGeneratorService.generatorId("payment") + "";
         studentGoodsSell.setOrderNo(orderNo);
         List<Integer> couponIdList = studentGoodsSell.getCouponIdList();
-        if(couponIdList != null && couponIdList.size() > 0){
-            studentGoodsSell.setCouponIds(StringUtils.join(couponIdList,","));
+        if (couponIdList != null && couponIdList.size() > 0) {
+            studentGoodsSell.setCouponIds(StringUtils.join(couponIdList, ","));
         }
         studentGoodsSellDao.insert(studentGoodsSell);
 
@@ -352,10 +355,10 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
         studentGoodsSellDao.update(studentGoodsSell);
         String couponIds = studentGoodsSell.getCouponIds();
         List<Integer> couponIdList = new ArrayList<>();
-        if(StringUtils.isNotEmpty(couponIds)){
+        if (StringUtils.isNotEmpty(couponIds)) {
             couponIdList = Arrays.stream(couponIds.split(",")).mapToInt(Integer::valueOf).boxed().collect(Collectors.toList());
         }
-        studentPaymentOrder = sysCouponCodeService.use(couponIdList,amount,true);
+        studentPaymentOrder = sysCouponCodeService.use(couponIdList, amount, true);
         amount = studentPaymentOrder.getActualAmount();
         studentPaymentOrder.setUserId(studentId);
         studentPaymentOrder.setGroupType(GroupType.GOODS_SELL);
@@ -475,10 +478,10 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
         if (amount.compareTo(BigDecimal.ZERO) < 0) {
             throw new BizException("特权减免金额不能大于总金额");
         }
-        StudentPaymentOrder studentPaymentOrder = sysCouponCodeService.use(couponIdList,amount,repairInfo.getType() != 1);
+        StudentPaymentOrder studentPaymentOrder = sysCouponCodeService.use(couponIdList, amount, repairInfo.getType() != 1);
         amount = studentPaymentOrder.getActualAmount();
-        if(couponIdList != null && couponIdList.size() > 0){
-            repairInfo.setCouponIds(StringUtils.join(couponIdList,","));
+        if (couponIdList != null && couponIdList.size() > 0) {
+            repairInfo.setCouponIds(StringUtils.join(couponIdList, ","));
         }
         studentRepairDao.insert(repairInfo);
         if (repairInfo.getType() == 1) {
@@ -606,7 +609,7 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
     public StudentRepair getRepairInfo(Integer id) {
         StudentRepair repairInfo = studentRepairDao.getRepairInfo(id);
         String couponIds = repairInfo.getCouponIds();
-        if(StringUtils.isNotEmpty(couponIds)){
+        if (StringUtils.isNotEmpty(couponIds)) {
             List<Integer> collect = Arrays.stream(couponIds.split(",")).map(e -> Integer.valueOf(e)).collect(Collectors.toList());
             repairInfo.setCouponCodeDtos(sysCouponCodeService.findByIdList(collect));
         }
@@ -649,10 +652,10 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
 
         String channelType = "";
         List<Integer> couponIdList = new ArrayList<>();
-        if(StringUtils.isNotEmpty(studentRepair.getCouponIds())){
+        if (StringUtils.isNotEmpty(studentRepair.getCouponIds())) {
             couponIdList = Arrays.stream(studentRepair.getCouponIds().split(",")).mapToInt(Integer::valueOf).boxed().collect(Collectors.toList());
         }
-        StudentPaymentOrder studentPaymentOrder = sysCouponCodeService.use(couponIdList,amount,true);
+        StudentPaymentOrder studentPaymentOrder = sysCouponCodeService.use(couponIdList, amount, true);
         amount = studentPaymentOrder.getActualAmount();
         studentPaymentOrder.setUserId(studentRepair.getStudentId());
         studentPaymentOrder.setGroupType(GroupType.REPAIR);
@@ -1121,7 +1124,7 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
             sysUserCashAccountDetailService.insert(paymentDetail);
             //优惠券减免金额
             BigDecimal couponRemitFee = studentPaymentOrder.getCouponRemitFee();
-            if(couponRemitFee.compareTo(BigDecimal.ZERO) > 0 && repairInfo.getAmount().compareTo(BigDecimal.ZERO) > 0){
+            if (couponRemitFee.compareTo(BigDecimal.ZERO) > 0 && repairInfo.getAmount().compareTo(BigDecimal.ZERO) > 0) {
                 //获取维修金额
                 BigDecimal repairAmount = repairInfo.getAmount();
                 //获取比例
@@ -1140,7 +1143,7 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
                             null,
                             goodsIds,
                             studentPaymentOrder.getExpectAmount().add(repairInfo.getExemptionAmount()),
-                            studentPaymentOrder.getBalancePaymentAmount(),couponRemitFee);
+                            studentPaymentOrder.getBalancePaymentAmount(), couponRemitFee);
                 }
             }
 

+ 98 - 98
mec-biz/src/main/java/com/ym/mec/biz/service/impl/SubjectChangeServiceImpl.java

@@ -1,9 +1,5 @@
 package com.ym.mec.biz.service.impl;
 
-import static com.ym.mec.biz.dal.enums.DealStatusEnum.SUCCESS;
-import static com.ym.mec.biz.dal.enums.GroupType.SUBJECT_CHANGE;
-import static com.ym.mec.biz.dal.enums.GroupType.VIP;
-
 import com.ym.mec.biz.dal.dao.*;
 import com.ym.mec.biz.dal.dto.SubjectChangeParamDto;
 import com.ym.mec.biz.dal.entity.*;
@@ -18,7 +14,6 @@ import com.ym.mec.common.service.impl.BaseServiceImpl;
 import com.ym.mec.thirdparty.message.MessageSenderPluginContext;
 import com.ym.mec.util.collection.MapUtil;
 import com.ym.mec.util.date.DateUtil;
-
 import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -31,6 +26,9 @@ import java.math.BigDecimal;
 import java.util.*;
 import java.util.stream.Collectors;
 
+import static com.ym.mec.biz.dal.enums.DealStatusEnum.SUCCESS;
+import static com.ym.mec.biz.dal.enums.GroupType.SUBJECT_CHANGE;
+
 @Service
 public class SubjectChangeServiceImpl extends BaseServiceImpl<Integer, SubjectChange> implements SubjectChangeService {
 
@@ -103,18 +101,18 @@ public class SubjectChangeServiceImpl extends BaseServiceImpl<Integer, SubjectCh
         List<StudentPaymentOrder> orders = studentPaymentOrderDao.findMusicGroupApplyOrderByStatus(subjectChange.getStudentId(), subjectChange.getMusicGroupId(), SUCCESS);
         //获取原报名订单的乐器费用
         if (subjectChange.getOriginalMusical() != null) {
-            if(orders != null && orders.size() > 0){
+            if (orders != null && orders.size() > 0) {
                 List<Long> collect = orders.stream().map(e -> e.getId()).collect(Collectors.toList());
                 List<Goods> musical = studentPaymentOrderDetailDao.getGoodsSellPrice(collect, "MUSICAL");
-                if(musical != null && musical.size() > 0){
+                if (musical != null && musical.size() > 0) {
                     subjectChange.setOriginalMusicalGoods(musical.get(0));
                 }
             }
         }
         if (subjectChange.getOriginalAccessories() != null) {
-            if(orders != null && orders.size() > 0){
+            if (orders != null && orders.size() > 0) {
                 List<Long> collect = orders.stream().map(e -> e.getId()).collect(Collectors.toList());
-                subjectChange.setOriginalAccessoriesGoods(studentPaymentOrderDetailDao.getGoodsSellPrice(collect,"ACCESSORIES"));
+                subjectChange.setOriginalAccessoriesGoods(studentPaymentOrderDetailDao.getGoodsSellPrice(collect, "ACCESSORIES"));
             }
         }
 
@@ -135,19 +133,19 @@ public class SubjectChangeServiceImpl extends BaseServiceImpl<Integer, SubjectCh
         SubjectChange subjectChange = subjectChangeDao.get(subjectChangeParamDto.getId());
         if (subjectChangeParamDto.getRepay()) {
             StudentPaymentOrder studentPaymentOrder = studentPaymentOrderService.get(subjectChange.getOrderId().longValue());
-            
-            if(studentPaymentOrder != null && studentPaymentOrder.getStatus() == DealStatusEnum.ING){
-                
+
+            if (studentPaymentOrder != null && studentPaymentOrder.getStatus() == DealStatusEnum.ING) {
+
                 // 查询订单状态
                 PayStatus payStatus = studentPaymentOrderService.queryPayStatus(studentPaymentOrder.getPaymentChannel(), studentPaymentOrder.getOrderNo(), studentPaymentOrder.getTransNo());
-                if(payStatus != PayStatus.FAILED){
-                	if(payStatus == PayStatus.SUCCESSED){
-                		throw new BizException("订单已支付成功,请勿重复支付");
-                	}/*else if(payStatus == PayStatus.PAYING){
+                if (payStatus != PayStatus.FAILED) {
+                    if (payStatus == PayStatus.SUCCESSED) {
+                        throw new BizException("订单已支付成功,请勿重复支付");
+                    }/*else if(payStatus == PayStatus.PAYING){
                 		throw new BizException("订单还在交易中,请稍后重试");
                 	}*/
                 }
-                
+
                 studentPaymentOrder.setStatus(DealStatusEnum.CLOSE);
                 studentPaymentOrder.setUpdateTime(date);
                 studentPaymentOrderService.update(studentPaymentOrder);
@@ -176,7 +174,9 @@ public class SubjectChangeServiceImpl extends BaseServiceImpl<Integer, SubjectCh
 
         String channelType = "";
 
-        StudentPaymentOrder studentPaymentOrder = sysCouponCodeService.use(subjectChangeParamDto.getCouponIdList(),amount,true);
+        //只允许使用这类型的优惠券
+        String[] checkCoupon = CouponDetailTypeEnum.getAllowType(CouponDetailTypeEnum.ACCESSORIES, CouponDetailTypeEnum.MUSICAL);
+        StudentPaymentOrder studentPaymentOrder = sysCouponCodeService.use(subjectChangeParamDto.getCouponIdList(), amount, true, checkCoupon);
         amount = studentPaymentOrder.getActualAmount();
         studentPaymentOrder.setUserId(subjectChange.getStudentId());
         studentPaymentOrder.setGroupType(SUBJECT_CHANGE);
@@ -262,49 +262,49 @@ public class SubjectChangeServiceImpl extends BaseServiceImpl<Integer, SubjectCh
     @Override
     @Transactional(rollbackFor = Exception.class)
     public SubjectChange addChange(SubjectChange subjectChange) {
-    	String musicGroupId = subjectChange.getMusicGroupId();
+        String musicGroupId = subjectChange.getMusicGroupId();
         SubjectChange studentWaitPay = subjectChangeDao.getStudentWaitPay(subjectChange.getStudentId(), musicGroupId);
         if (studentWaitPay != null) {
             throw new BizException("已有未支付的声部更改,请勿重复创建");
         }
-        
+
         studentWaitPay = subjectChangeDao.getStudentLastChange(subjectChange.getStudentId(), musicGroupId);
         if (studentWaitPay != null) {
             throw new BizException("声部更换只能操作一次,请勿重复操作");
         }
-        
+
         Set<String> musicGroupIds = new HashSet<String>();
         musicGroupIds.add(musicGroupId);
-        
+
         List<MusicGroupPurchaseList> musicGroupPurchaseListCount = musicGroupPurchaseListDao.getCount(musicGroupIds);
         if (musicGroupPurchaseListCount.size() > 0) {
             throw new BizException("乐器清单已确认,不能做声部更改");
         }
-        
+
         //判断新声部人数是否已满
         MusicGroupSubjectPlan changeSubjectPlan = musicGroupSubjectPlanService.getMusicOneSubjectClassPlan(musicGroupId, subjectChange.getChangeSubjectId());
-		if (changeSubjectPlan.getExpectedStudentNum() <= changeSubjectPlan.getPaidStudentNum()) {
-			throw new BizException("当前声部人数已满");
-		}
-		
-		StudentRegistration studentRegistration = studentRegistrationDao.getStudentRegister(musicGroupId, subjectChange.getStudentId());
-		if(studentRegistration == null){
-			throw new BizException("用户乐团报名信息查询失败");
-		}
-        
-        if(studentRegistration.getNoneNeedCloudTeacher() != null && studentRegistration.getNoneNeedCloudTeacher() == 1){
-        	if(changeSubjectPlan.getPaidZeroNum() != null && changeSubjectPlan.getPaidZeroNum() > 0){
-        		throw new BizException("声部更换失败,当前用户是0元入团,且当前声部已有0元入团学生");
-        	}
-        }        
+        if (changeSubjectPlan.getExpectedStudentNum() <= changeSubjectPlan.getPaidStudentNum()) {
+            throw new BizException("当前声部人数已满");
+        }
+
+        StudentRegistration studentRegistration = studentRegistrationDao.getStudentRegister(musicGroupId, subjectChange.getStudentId());
+        if (studentRegistration == null) {
+            throw new BizException("用户乐团报名信息查询失败");
+        }
+
+        if (studentRegistration.getNoneNeedCloudTeacher() != null && studentRegistration.getNoneNeedCloudTeacher() == 1) {
+            if (changeSubjectPlan.getPaidZeroNum() != null && changeSubjectPlan.getPaidZeroNum() > 0) {
+                throw new BizException("声部更换失败,当前用户是0元入团,且当前声部已有0元入团学生");
+            }
+        }
         Date nowDate = new Date();
         SubjectChange studentOriginal = getStudentOriginal(subjectChange.getStudentId(), subjectChange.getMusicGroupId());
-        if(studentOriginal != null){
-        	subjectChange.setOriginalOrderId(studentOriginal.getOrderId());
-        	subjectChange.setOriginalCost(studentOriginal.getOriginalCost());
-        	subjectChange.setOriginalAccessories(studentOriginal.getOriginalAccessories());
+        if (studentOriginal != null) {
+            subjectChange.setOriginalOrderId(studentOriginal.getOrderId());
+            subjectChange.setOriginalCost(studentOriginal.getOriginalCost());
+            subjectChange.setOriginalAccessories(studentOriginal.getOriginalAccessories());
             subjectChange.setOriginalKitGroupPurchaseType(
-            studentOriginal.getOriginalKitGroupPurchaseType() == null ? KitGroupPurchaseTypeEnum.OWNED : studentOriginal.getOriginalKitGroupPurchaseType());
+                    studentOriginal.getOriginalKitGroupPurchaseType() == null ? KitGroupPurchaseTypeEnum.OWNED : studentOriginal.getOriginalKitGroupPurchaseType());
         }
         subjectChange.setStatus(SubjectChangeStatusEnum.WAIT_PAY);
         MusicGroup musicGroup = musicGroupDao.get(subjectChange.getMusicGroupId());
@@ -348,7 +348,7 @@ public class SubjectChangeServiceImpl extends BaseServiceImpl<Integer, SubjectCh
         //差价小于0退到余额
         if (amountMargin.compareTo(BigDecimal.ZERO) <= 0) {
 //            sysUserCashAccountService.updateBalance(subjectChange.getStudentId(), amountMargin.negate(), PlatformCashAccountDetailTypeEnum.REFUNDS, "声部更换退还");
-            if(amountMargin.compareTo(BigDecimal.ZERO) < 0){
+            if (amountMargin.compareTo(BigDecimal.ZERO) < 0) {
                 SysUserCashAccountLog sysUserCashAccountLog = new SysUserCashAccountLog();
                 sysUserCashAccountLog.setUserId(subjectChange.getStudentId());
                 sysUserCashAccountLog.setGroupType(SUBJECT_CHANGE);
@@ -395,7 +395,7 @@ public class SubjectChangeServiceImpl extends BaseServiceImpl<Integer, SubjectCh
                         goodsIdList,
                         BigDecimal.ZERO,
                         BigDecimal.ZERO,
-                        subjectChange.getKitGroupPurchaseType(),BigDecimal.ZERO);
+                        subjectChange.getKitGroupPurchaseType(), BigDecimal.ZERO);
             }
             //乐保处理
             studentInstrumentService.subjectChangeUpdateInstrument(subjectChange);
@@ -419,12 +419,12 @@ public class SubjectChangeServiceImpl extends BaseServiceImpl<Integer, SubjectCh
 
     @Override
     public SubjectChange getStudentOriginal(Integer studentId, String musicGroupId) {
-    	
+
         //1、存在历史的更换
         SubjectChange subjectChange = new SubjectChange();
         SubjectChange studentLastChange = subjectChangeDao.getStudentLastChange(studentId, musicGroupId);
         if (studentLastChange != null) {
-        	throw new BizException("声部更换只能操作一次,请勿重复操作");
+            throw new BizException("声部更换只能操作一次,请勿重复操作");
         	/*List<Long> orderIdList = new ArrayList<Long>();
         	orderIdList.add(studentLastChange.getOrderId().longValue());
         	Set<Integer> refundSellOrderGoodsIds = getRefundGoodsId(orderIdList);
@@ -467,51 +467,51 @@ public class SubjectChangeServiceImpl extends BaseServiceImpl<Integer, SubjectCh
         if (studentRegistration == null) {
             throw new BizException("用户注册信息不存在");
         }
-        
+
         List<StudentPaymentOrder> studentPaymentOrderList = new ArrayList<StudentPaymentOrder>();
-        
-		if (studentRegistration.getMusicGroupPaymentCalenderId() == null) {
-			if(studentRegistration.getMusicGroupStatus() == StudentMusicGroupStatusEnum.NORMAL){
-				studentPaymentOrderList = studentPaymentOrderService.findMusicGroupApplyOrderByStatus(studentId, musicGroupId, SUCCESS);
-			}
-		} else {
-			studentPaymentOrderList = studentPaymentOrderService.queryByBatchNo(studentId, studentRegistration.getMusicGroupPaymentCalenderId() + "",
-					DealStatusEnum.SUCCESS);
-		}
-        
+
+        if (studentRegistration.getMusicGroupPaymentCalenderId() == null) {
+            if (studentRegistration.getMusicGroupStatus() == StudentMusicGroupStatusEnum.NORMAL) {
+                studentPaymentOrderList = studentPaymentOrderService.findMusicGroupApplyOrderByStatus(studentId, musicGroupId, SUCCESS);
+            }
+        } else {
+            studentPaymentOrderList = studentPaymentOrderService.queryByBatchNo(studentId, studentRegistration.getMusicGroupPaymentCalenderId() + "",
+                    DealStatusEnum.SUCCESS);
+        }
+
         List<Long> paymentOrderIdList = studentPaymentOrderList.stream().map(t -> t.getId()).collect(Collectors.toList());
 
         List<StudentPaymentOrderDetail> details = new ArrayList<StudentPaymentOrderDetail>();
-		if (paymentOrderIdList != null && paymentOrderIdList.size() > 0) {
-			details = studentPaymentOrderDetailDao.getWithIds(paymentOrderIdList);
-		}
+        if (paymentOrderIdList != null && paymentOrderIdList.size() > 0) {
+            details = studentPaymentOrderDetailDao.getWithIds(paymentOrderIdList);
+        }
 
         //查询乐器订单
         Long paymentOrderId = null;
-        		
-        for(StudentPaymentOrderDetail detail : details){
-        	if(detail.getType() == OrderDetailTypeEnum.ACCESSORIES || detail.getType() == OrderDetailTypeEnum.MUSICAL){
-	        	if(paymentOrderId == null || paymentOrderId < detail.getPaymentOrderId()){
-	    			paymentOrderId = detail.getPaymentOrderId();
-	    		}
-        	}
-        }
-
-		Set<Integer> refundSellOrderGoodsIds = new HashSet<Integer>();
-		if (details.size() > 0) {
-			refundSellOrderGoodsIds = getRefundGoodsId(details.stream().map(t -> t.getPaymentOrderId()).collect(Collectors.toList()));
-		}
-		
+
+        for (StudentPaymentOrderDetail detail : details) {
+            if (detail.getType() == OrderDetailTypeEnum.ACCESSORIES || detail.getType() == OrderDetailTypeEnum.MUSICAL) {
+                if (paymentOrderId == null || paymentOrderId < detail.getPaymentOrderId()) {
+                    paymentOrderId = detail.getPaymentOrderId();
+                }
+            }
+        }
+
+        Set<Integer> refundSellOrderGoodsIds = new HashSet<Integer>();
+        if (details.size() > 0) {
+            refundSellOrderGoodsIds = getRefundGoodsId(details.stream().map(t -> t.getPaymentOrderId()).collect(Collectors.toList()));
+        }
+
         MusicGroup musicGroup = musicGroupDao.get(musicGroupId);
         subjectChange.setStudentId(studentId);
         subjectChange.setOrganId(musicGroup.getOrganId());
         subjectChange.setCooperationOrganId(musicGroup.getCooperationOrganId());
         subjectChange.setMusicGroupId(musicGroupId);
 
-        if(paymentOrderId != null){
-        	subjectChange.setOrderId(paymentOrderId.intValue());
+        if (paymentOrderId != null) {
+            subjectChange.setOrderId(paymentOrderId.intValue());
         }
-		
+
         String accessoriesIds = "";
         BigDecimal accessoriesPrice = BigDecimal.ZERO;
         for (StudentPaymentOrderDetail detail : details) {
@@ -525,7 +525,7 @@ public class SubjectChangeServiceImpl extends BaseServiceImpl<Integer, SubjectCh
                     subjectChange.setOriginalMusicalGoods(goodsDao.get(goodsId));
                     subjectChange.setOriginalMusicalPrice(detail.getPrice());
                 }
-            } else if (detail.getType().equals(OrderDetailTypeEnum.ACCESSORIES)){
+            } else if (detail.getType().equals(OrderDetailTypeEnum.ACCESSORIES)) {
                 if (StringUtils.isNotBlank(detail.getGoodsIdList())) {
                     accessoriesPrice = accessoriesPrice.add(detail.getPrice());
                     accessoriesIds = accessoriesIds.length() > 0 ? accessoriesIds + "," + detail.getGoodsIdList() : detail.getGoodsIdList();
@@ -543,29 +543,29 @@ public class SubjectChangeServiceImpl extends BaseServiceImpl<Integer, SubjectCh
             }
 
             subjectChange.setOriginalAccessories(accessoriesId);
-            
+
             Map<Integer, Goods> goodsMap = goodsService.findGoodsByIds(accessoriesId).stream().collect(Collectors.toMap(Goods::getId, Goods -> Goods));
 
-			String[] accessoriesIdList = StringUtils.split(accessoriesId, ',');
-
-			for (String s : accessoriesIdList) {
-				if(StringUtils.isBlank(s)){
-					continue;
-				}
-				
-				if(subjectChange.getOriginalAccessoriesGoods() == null){
-					subjectChange.setOriginalAccessoriesGoods(new ArrayList<Goods>());
-				}
-				subjectChange.getOriginalAccessoriesGoods().add(goodsMap.get(Integer.parseInt(s)));
-			}
-            
+            String[] accessoriesIdList = StringUtils.split(accessoriesId, ',');
+
+            for (String s : accessoriesIdList) {
+                if (StringUtils.isBlank(s)) {
+                    continue;
+                }
+
+                if (subjectChange.getOriginalAccessoriesGoods() == null) {
+                    subjectChange.setOriginalAccessoriesGoods(new ArrayList<Goods>());
+                }
+                subjectChange.getOriginalAccessoriesGoods().add(goodsMap.get(Integer.parseInt(s)));
+            }
+
             //BigDecimal price = accessoriesGoods.stream().map(Goods::getGroupPurchasePrice).reduce(BigDecimal.ZERO, BigDecimal::add);
             subjectChange.setOriginalAccessoriesPrice(accessoriesPrice);
         }
         //2.2 计算销售成本
         BigDecimal orderSellCost = BigDecimal.ZERO;
         if (paymentOrderIdList != null && paymentOrderIdList.size() > 0) {
-        	orderSellCost = sellOrderDao.getOrderSellCost(paymentOrderIdList);
+            orderSellCost = sellOrderDao.getOrderSellCost(paymentOrderIdList);
         }
         subjectChange.setOriginalCost(orderSellCost == null ? BigDecimal.ZERO : orderSellCost);
         return subjectChange;
@@ -641,8 +641,8 @@ public class SubjectChangeServiceImpl extends BaseServiceImpl<Integer, SubjectCh
             if (StringUtils.isNotBlank(goodsIds)) {
                 List<Integer> goodsIdList = Arrays.stream(goodsIds.split(",")).map(Integer::parseInt).collect(Collectors.toList());
                 //退原订单商品
-                if(subjectChange.getOriginalOrderId() != null){
-                	sellOrderService.refundByOrderId(subjectChange.getOriginalOrderId().longValue(), false);
+                if (subjectChange.getOriginalOrderId() != null) {
+                    sellOrderService.refundByOrderId(subjectChange.getOriginalOrderId().longValue(), false);
                 }
                 //添加新订单
                 List<SellOrder> sellOrders = this.addSellOrder(studentPaymentOrder.getId(),
@@ -650,8 +650,8 @@ public class SubjectChangeServiceImpl extends BaseServiceImpl<Integer, SubjectCh
                         goodsIdList,
                         studentPaymentOrder.getExpectAmount(),
                         studentPaymentOrder.getBalancePaymentAmount(),
-                        subjectChange.getKitGroupPurchaseType(),studentPaymentOrder.getCouponRemitFee());
-                if(sellOrders != null && sellOrders.size() > 0){
+                        subjectChange.getKitGroupPurchaseType(), studentPaymentOrder.getCouponRemitFee());
+                if (sellOrders != null && sellOrders.size() > 0) {
                     SubjectChange change = subjectChangeDao.get(subjectChange.getId());
                     BigDecimal instrumentAmount = sellOrders.stream().filter(e -> e.getType() == SellTypeEnum.INSTRUMENT).map(e -> e.getExpectAmount()).reduce(BigDecimal.ZERO, BigDecimal::add);
                     BigDecimal accessoriesAmount = sellOrders.stream().filter(e -> e.getType() == SellTypeEnum.ACCESSORIES).map(e -> e.getExpectAmount()).reduce(BigDecimal.ZERO, BigDecimal::add);
@@ -772,7 +772,7 @@ public class SubjectChangeServiceImpl extends BaseServiceImpl<Integer, SubjectCh
                                         List<Integer> goodsIds,
                                         BigDecimal totalAmount,
                                         BigDecimal balance,
-                                        KitGroupPurchaseTypeEnum kitGroupPurchaseType,BigDecimal couponRemitAmount) {
+                                        KitGroupPurchaseTypeEnum kitGroupPurchaseType, BigDecimal couponRemitAmount) {
         if (goodsIds == null || goodsIds.size() <= 0) {
             return null;
         }

+ 93 - 6
mec-biz/src/main/java/com/ym/mec/biz/service/impl/SysCouponCodeServiceImpl.java

@@ -5,9 +5,8 @@ import com.ym.mec.biz.dal.dao.SysCouponCodeDao;
 import com.ym.mec.biz.dal.dao.SysCouponDao;
 import com.ym.mec.biz.dal.dto.HorseRaceLampDto;
 import com.ym.mec.biz.dal.dto.SysCouponCodeDto;
-import com.ym.mec.biz.dal.entity.StudentPaymentOrder;
-import com.ym.mec.biz.dal.entity.SysCoupon;
-import com.ym.mec.biz.dal.entity.SysCouponCode;
+import com.ym.mec.biz.dal.entity.*;
+import com.ym.mec.biz.dal.enums.CouponDetailTypeEnum;
 import com.ym.mec.biz.dal.enums.CouponTypeEnum;
 import com.ym.mec.biz.dal.page.SysCouponCodeQueryInfo;
 import com.ym.mec.biz.service.SysCouponCodeService;
@@ -31,7 +30,9 @@ import org.springframework.transaction.annotation.Isolation;
 import org.springframework.transaction.annotation.Transactional;
 
 import java.math.BigDecimal;
+import java.math.RoundingMode;
 import java.util.*;
+import java.util.concurrent.atomic.AtomicInteger;
 
 @Service
 public class SysCouponCodeServiceImpl extends BaseServiceImpl<Long, SysCouponCode> implements SysCouponCodeService {
@@ -217,12 +218,18 @@ public class SysCouponCodeServiceImpl extends BaseServiceImpl<Long, SysCouponCod
     @Override
     @Transactional(rollbackFor = Exception.class)
     public StudentPaymentOrder use(List<Integer> couponIdList, BigDecimal amount, Boolean useFlag) {
+        return use(couponIdList, amount, useFlag, null);
+    }
+
+    @Override
+    @Transactional(rollbackFor = Exception.class)
+    public StudentPaymentOrder use(List<Integer> couponIdList, BigDecimal amount, Boolean useFlag, String... allowType) {
         StudentPaymentOrder studentPaymentOrder = new StudentPaymentOrder();
         if (CollectionUtils.isNotEmpty(couponIdList)) {
             BigDecimal fullAmount = BigDecimal.ZERO;
             BigDecimal faceAmount = BigDecimal.ZERO;
             //校验优惠券
-            List<SysCouponCodeDto> couponCodeDtoList = checkCoupon(couponIdList);
+            List<SysCouponCodeDto> couponCodeDtoList = checkCoupon(couponIdList, allowType);
 
             for (SysCouponCodeDto sysCouponCodeDto : couponCodeDtoList) {
                 CouponTypeEnum couponType = sysCouponCodeDto.getCouponType();
@@ -258,9 +265,12 @@ public class SysCouponCodeServiceImpl extends BaseServiceImpl<Long, SysCouponCod
     }
 
     @Override
-    public List<SysCouponCodeDto> checkCoupon(List<Integer> couponIdList) {
+    public List<SysCouponCodeDto> checkCoupon(List<Integer> couponIdList, String... allowType) {
         Date date = new Date();
         List<SysCouponCodeDto> couponCodeDtoList = findByIdList(couponIdList);
+        if (CollectionUtils.isEmpty(couponCodeDtoList)) {
+            throw new BizException("操作失败:未查询到优惠券");
+        }
         if (couponIdList.size() != couponCodeDtoList.size()) {
             throw new BizException("操作失败:优惠券数据异常");
         }
@@ -275,11 +285,88 @@ public class SysCouponCodeServiceImpl extends BaseServiceImpl<Long, SysCouponCod
             if (date.after(sysCouponCodeDto.getUseDeadlineDate())) {
                 throw new BizException("操作失败:优惠券已过期");
             }
+            //该商品只允许使用 allowType参数内的优惠券类型,null则不验证
+            if (Objects.nonNull(allowType) && !WrapperUtil.checkStr(sysCouponCodeDto.getTypeDetail(), allowType)) {
+                throw new BizException("操作失败 : 优惠券类型使用错误");
+            }
         }
-
         return couponCodeDtoList;
     }
 
+    /**
+     * 获取优惠券相关参数-校验优惠券和传入的支付金额是否合法
+     *
+     * @param couponIdList 优惠券集合
+     * @param total        本次订单所有商品的总数量
+     * @param payAmount    页面传入的本次支付的金额
+     * @param allowType    本次允许使用的优惠券类型,null则不验证
+     */
+    @Override
+    public CouponPayParam getCouponPayParam(List<Integer> couponIdList, Integer total, BigDecimal payAmount, String... allowType) {
+        if (CollectionUtils.isEmpty(couponIdList)) {
+            return null;
+        }
+        //查询本次付款使用的优惠券
+        List<SysCouponCodeDto> sysCouponCodeDtoList = checkCoupon(couponIdList, allowType);
+        //全类型优惠券的总额度
+        BigDecimal fullTypeTotal = new BigDecimal(0);
+        //非全类型优惠券的总额度
+        BigDecimal notFullTypeTotal = new BigDecimal(0);
+        //非全类型优惠券的总数量
+        AtomicInteger notFullTypeTotalNum = new AtomicInteger(0);
+        //优惠券减免的总额度
+        BigDecimal couponRemitTotal = new BigDecimal(0);
+        //交易阈值 最后付款的金额大于该值就不对
+        BigDecimal threshold;
+
+        for (SysCouponCodeDto d : sysCouponCodeDtoList) {
+            if (d.getTypeDetail().equals(CouponDetailTypeEnum.FULLCOUPON.getCode())) {
+                fullTypeTotal = fullTypeTotal.add(d.getFullAmount());
+            } else {
+                notFullTypeTotal = notFullTypeTotal.add(d.getFullAmount());
+                notFullTypeTotalNum.set(notFullTypeTotalNum.incrementAndGet());
+            }
+            couponRemitTotal = couponRemitTotal.add(d.getFaceValue());
+        }
+        //阈值 = (全品类券总面值 ÷ 总商品数量) + (非全品类券总面值 ÷ 非全品类券的数量)
+        threshold = fullTypeTotal.divide(new BigDecimal(total), 3, RoundingMode.HALF_UP)
+                .add(notFullTypeTotal.divide(new BigDecimal(notFullTypeTotalNum.get()), 3, RoundingMode.HALF_UP));
+
+        if (payAmount.compareTo(threshold) < 0) {
+            throw new BizException("优惠券使用错误,交易失败!");
+        }
+        //将各种类型的优惠券合并 算出总面试 总减免金额
+        Map<String, CouponPayTypeInfo> couponTypeInfo = new HashMap<>();
+        //根据CouponDetailTypeEnum 进行分组集合
+        WrapperUtil.groupList(sysCouponCodeDtoList, SysCouponCodeDto::getTypeDetail)
+                .forEach((typeDetail, list) -> {
+                    CouponPayTypeInfo coupon = new CouponPayTypeInfo();
+                    list.forEach(c -> {
+                        coupon.setCouponAmount(coupon.getCouponAmount().add(c.getFullAmount()));
+                        coupon.setFaceValue(coupon.getFaceValue().add(c.getFaceValue()));
+                    });
+                    coupon.setTypeDetail(typeDetail);
+                    couponTypeInfo.put(typeDetail, coupon);
+                });
+
+        CouponPayParam result = new CouponPayParam();
+        result.setCouponRemitTotal(couponRemitTotal);
+        result.setCouponTypeInfo(couponTypeInfo);
+        return result;
+    }
+
+    /**
+     * 获取优惠券相关参数-校验优惠券和传入的支付金额是否合法
+     *
+     * @param couponIdList 优惠券集合
+     * @param total        本次订单所有商品的总数量
+     * @param payAmount    页面传入的本次支付的金额
+     */
+    @Override
+    public CouponPayParam getCouponPayParam(List<Integer> couponIdList, Integer total, BigDecimal payAmount) {
+        return getCouponPayParam(couponIdList, total, payAmount, null);
+    }
+
     @Override
     public List<SysCouponCodeDto> findByIdList(List<Integer> couponIdList) {
         return sysCouponCodeDao.findByIdList(couponIdList);

File diff suppressed because it is too large
+ 3518 - 3513
mec-biz/src/main/java/com/ym/mec/biz/service/impl/VipGroupServiceImpl.java


+ 12 - 0
mec-common/common-core/src/main/java/com/ym/mec/common/page/WrapperUtil.java

@@ -158,6 +158,18 @@ public class WrapperUtil<T> {
     }
 
     /**
+     * 检查str中是否包含 o
+     * 包含返回true
+     */
+    public static boolean checkStr(String o, String... str) {
+        if (ObjPredicate.test(str)) {
+            return Arrays.asList(str).contains(o);
+        } else {
+            return false;
+        }
+    }
+
+    /**
      * 根据分组groupVal进行聚合分组
      *
      * @param list     待聚合的集合

Some files were not shown because too many files changed in this diff