diff --git a/src/main/java/com/peanut/modules/common/service/impl/CouponServiceImpl.java b/src/main/java/com/peanut/modules/common/service/impl/CouponServiceImpl.java index b48f774..10bd914 100644 --- a/src/main/java/com/peanut/modules/common/service/impl/CouponServiceImpl.java +++ b/src/main/java/com/peanut/modules/common/service/impl/CouponServiceImpl.java @@ -304,6 +304,19 @@ public class CouponServiceImpl extends ServiceImpl impl @Override public void refundZSCouponHistoryByOrder(BuyOrder order){ + MyUserEntity userEntity = userDao.selectById(order.getUserId()); + BigDecimal refundAllJF = BigDecimal.ZERO; + List grantJfList = jfTransactionDetailsDao.selectList(new LambdaQueryWrapper() + .eq(JfTransactionDetails::getRelationId, order.getOrderId()) + .eq(JfTransactionDetails::getUserId, order.getUserId()) + .eq(JfTransactionDetails::getActType, 0) + .gt(JfTransactionDetails::getChangeAmount, BigDecimal.ZERO) + .like(JfTransactionDetails::getRemark, "优惠券换积分")); + for (JfTransactionDetails grantJf : grantJfList) { + refundAllJF = refundAllJF.add(grantJf.getChangeAmount()); + } + boolean orderGrantedJf = refundAllJF.compareTo(BigDecimal.ZERO) > 0; + MPJLambdaWrapper wrapper = new MPJLambdaWrapper(); wrapper.leftJoin(BuyOrderProduct.class,BuyOrderProduct::getProductId,CouponToProduct::getProductId); wrapper.leftJoin(CouponEntity.class,CouponEntity::getId,CouponToProduct::getCouponId); @@ -314,54 +327,42 @@ public class CouponServiceImpl extends ServiceImpl impl wrapper.select(BuyOrderProduct::getQuantity); List> buyOrderProducts = couponToProductDao.selectJoinMaps(wrapper); - MyUserEntity userEntity = userDao.selectById(order.getUserId()); - List userVipList = userVipDao.selectList(new LambdaQueryWrapper() - .eq(UserVip::getUserId, userEntity.getId()) - .eq(UserVip::getState,0) - .lt(UserVip::getStartTime,order.getPaymentDate())); - boolean isVip = false; - if (userVipList.size() > 0) { - isVip = true; - } - System.out.println("isVip:"+isVip); - boolean isFyszVip = userVipList.stream().anyMatch(userVip -> Integer.valueOf(10).equals(userVip.getType())); - BigDecimal refundAllJF = BigDecimal.ZERO; Boolean isRefundCoupon = false; for (Map map : buyOrderProducts) { ShopProduct shopProduct = shopProductDao.selectById(map.get("product_id").toString()); - System.out.println("goodsType:"+shopProduct.getGoodsType()); - if ("03".equals(shopProduct.getGoodsType())){ - int productId = Integer.parseInt(map.get("product_id").toString()); - int couponId = Integer.parseInt(map.get("coupon_id").toString()); - CouponEntity couponEntity = couponDao.selectById(couponId); - boolean shouldRefundJf = couponEntity.getCouponType()==0 && ((productId == 2023 && isFyszVip) || (productId != 2023 && isVip)); - //现金券 - if (shouldRefundJf){ - BigDecimal quantity = new BigDecimal(map.get("quantity").toString()); - BigDecimal sumRefundJF = couponEntity.getCouponAmount().multiply(quantity); - refundAllJF = refundAllJF.add(sumRefundJF); - - QueryWrapper couponHistoryRemoveWrapper = new QueryWrapper<>(); - couponHistoryRemoveWrapper.eq("order_id",order.getOrderId()); - couponHistoryRemoveWrapper.eq("status",1); - couponHistoryService.remove(couponHistoryRemoveWrapper); - }else{ + if ("03".equals(shopProduct.getGoodsType())) { + if (!orderGrantedJf) { isRefundCoupon = true; } - }else{ + } else { isRefundCoupon = true; } } - if(refundAllJF.compareTo(BigDecimal.ZERO)>0 && userEntity.getJf().compareTo(refundAllJF)>=0){ - BigDecimal userJF = userEntity.getJf().subtract(refundAllJF); - userEntity.setJf(userJF.compareTo(BigDecimal.ZERO)>=0?userJF:BigDecimal.ZERO); - userDao.updateById(userEntity); - jfTransactionDetailsService.recordZsJfTransaction(order,userEntity, refundAllJF); + + if (orderGrantedJf) { + BigDecimal userJf = userEntity.getJf() == null ? BigDecimal.ZERO : userEntity.getJf(); + BigDecimal actualDeduct; + if (userJf.compareTo(refundAllJF) >= 0) { + actualDeduct = refundAllJF; + } else if (userJf.compareTo(BigDecimal.ZERO) > 0) { + actualDeduct = userJf; + } else { + actualDeduct = BigDecimal.ZERO; + } + if (actualDeduct.compareTo(BigDecimal.ZERO) > 0) { + userEntity.setJf(userJf.subtract(actualDeduct)); + userDao.updateById(userEntity); + jfTransactionDetailsService.recordZsJfTransaction(order, userEntity, actualDeduct); + QueryWrapper couponHistoryRemoveWrapper = new QueryWrapper<>(); + couponHistoryRemoveWrapper.eq("order_id", order.getOrderId()); + couponHistoryRemoveWrapper.eq("status", 1); + couponHistoryService.remove(couponHistoryRemoveWrapper); + } } - if(isRefundCoupon){ + if (isRefundCoupon) { QueryWrapper couponHistoryRemoveWrapper = new QueryWrapper<>(); - couponHistoryRemoveWrapper.eq("order_id",order.getOrderId()); - couponHistoryRemoveWrapper.eq("status",0); + couponHistoryRemoveWrapper.eq("order_id", order.getOrderId()); + couponHistoryRemoveWrapper.eq("status", 0); couponHistoryService.remove(couponHistoryRemoveWrapper); } @@ -379,9 +380,7 @@ public class CouponServiceImpl extends ServiceImpl impl List> buyOrderProducts = couponToProductDao.selectJoinMaps(wrapper); for (Map map : buyOrderProducts) { ShopProduct shopProduct = shopProductDao.selectById(map.get("product_id").toString()); - //预售书赠送规则: - //1) 指定预售书(2023):仅妇幼生殖vip(type=10)且现金券时,改为送积分 - //2) 其他预售书:保持原规则(任意vip且现金券时,改为送积分) + //预售书+赠送现金券:VIP用户将券额转为积分;仅产品id=2023时需为妇幼生殖vip(type=10) if ("03".equals(shopProduct.getGoodsType())){ MyUserEntity userEntity = userDao.selectById(order.getUserId()); List userVipList = userVipDao.selectList(new LambdaQueryWrapper() @@ -391,7 +390,7 @@ public class CouponServiceImpl extends ServiceImpl impl int couponId = Integer.parseInt(map.get("coupon_id").toString()); CouponEntity couponEntity = couponDao.selectById(couponId); boolean isFyszVip = userVipList.stream().anyMatch(userVip -> Integer.valueOf(10).equals(userVip.getType())); - boolean couponToJf = couponEntity.getCouponType() == 0 && ((productId == 2023 && isFyszVip) || (productId != 2023 && userVipList.size() > 0)); + boolean couponToJf = couponEntity.getCouponType() == 0 && (productId == 2023 ? isFyszVip : userVipList.size() > 0); if (couponToJf){ BigDecimal jf = BigDecimal.ZERO; for (int i=0;i