From 71c138388f789cf2c5dc08c0c6cc36d9731c955f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=9D=BE?= <8605635+zhang3064194730@user.noreply.gitee.com> Date: Wed, 20 Nov 2024 17:33:51 +0800 Subject: [PATCH] =?UTF-8?q?feat:=201.=E9=80=80=E6=AC=BE=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/shopimpl/TbShopTableServiceImpl.java | 157 +++++++++++++++--- 1 file changed, 130 insertions(+), 27 deletions(-) diff --git a/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/shopimpl/TbShopTableServiceImpl.java b/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/shopimpl/TbShopTableServiceImpl.java index 7cdd07f0..6f98aac9 100644 --- a/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/shopimpl/TbShopTableServiceImpl.java +++ b/eladmin-system/src/main/java/cn/ysk/cashier/service/impl/shopimpl/TbShopTableServiceImpl.java @@ -2,6 +2,7 @@ package cn.ysk.cashier.service.impl.shopimpl; import cn.hutool.core.bean.BeanUtil; +import cn.hutool.core.collection.CollUtil; import cn.hutool.core.convert.Convert; import cn.hutool.core.date.DateUtil; import cn.hutool.core.thread.ThreadUtil; @@ -454,6 +455,7 @@ public class TbShopTableServiceImpl implements TbShopTableService { tbCashierCart.setSalePrice(tbCashierCart.getSalePrice()); tbCashierCart.setTotalAmount(updateCartDTO.getNum().multiply(tbCashierCart.getSalePrice())); tbCashierCart.setNote(updateCartDTO.getNote()); + tbCashierCart.setIsPrint(updateCartDTO.getIsPrint()); if (updateCartDTO.getIsPack() != null) { if (!updateCartDTO.getIsPack()) { @@ -1308,13 +1310,12 @@ public class TbShopTableServiceImpl implements TbShopTableService { // 创建结余购物车 balanceCart = BeanUtil.copyProperties(cashierCart, TbCashierCart.class); - BigDecimal num = balanceNum; balanceCart.setUserCouponId(null); balanceCart.setId(null); balanceCart.setNumber(balanceNum); balanceCart.setTotalNumber(balanceNum); - balanceCart.setPackFee(singlePackFee.multiply(num)); - balanceCart.setTotalAmount(cashierCart.getTotalAmountByNum(balanceNum).add(singlePackFee.multiply(num))); + balanceCart.setPackFee(singlePackFee.multiply(balanceNum)); + balanceCart.setTotalAmount(cashierCart.getTotalAmountByNum(balanceNum).add(singlePackFee.multiply(balanceNum))); balanceCartList.add(balanceCart); } else { currentUseNum =cashierCart.getNumber(); @@ -1334,6 +1335,7 @@ public class TbShopTableServiceImpl implements TbShopTableService { tbActivateOutRecord.setCreateTime(DateUtil.date()); tbActivateOutRecord.setRefNum(0); outRecords.add(tbActivateOutRecord); + cashierCart.setUseCouponInfo(JSONObject.toJSONString(tbActivateOutRecord)); couponVo.setFinalDiscountAmount(discountAmount); // 优惠券未消耗完毕 @@ -1526,6 +1528,7 @@ public class TbShopTableServiceImpl implements TbShopTableService { } + /** * 根据商品优惠券 * @@ -1608,6 +1611,10 @@ public class TbShopTableServiceImpl implements TbShopTableService { orderDetail.setIsMember(cashierCart.getIsMember()); orderDetail.setIsTemporary(cashierCart.getIsTemporary()); orderDetail.setOrderId(orderInfo == null ? null : orderInfo.getId()); + orderDetail.setIsMember(cashierCart.getIsMember()); + orderDetail.setIsPrint(cashierCart.getIsPrint()); + orderDetail.setUseCouponInfo(cashierCart.getUseCouponInfo()); + priceDTO.getOrderDetailList().add(orderDetail); } return priceDTO; @@ -2106,8 +2113,8 @@ public class TbShopTableServiceImpl implements TbShopTableService { orderInfo.setProductCouponDiscountAmount(productDiscount); // 更新订单信息 - - OrderPriceDTO priceDTO = createOrderDetailWithCoupon(activateCartInfo, orderInfo, payDTO.getShopId(), false, null); + ShopEatTypeInfoDTO shopEatTypeInfoDTO = checkEatModel(orderInfo.getShopId(), orderInfo.getTableId()); + OrderPriceDTO priceDTO = createOrderDetailWithCoupon(activateCartInfo, orderInfo, payDTO.getShopId(), false, shopEatTypeInfoDTO); BigDecimal finalAmount = priceDTO.getTotalAmount().multiply(BigDecimal.valueOf(payDTO.getDiscount())).setScale(2, RoundingMode.HALF_UP); orderInfo.setUpdatedAt(System.currentTimeMillis()); @@ -2137,6 +2144,34 @@ public class TbShopTableServiceImpl implements TbShopTableService { return orderInfo.getOrderAmount(); } + /** + * 根据折扣设置订单信息 + */ + private BigDecimal resetAmountByDiscount(PayDTO payDTO, TbOrderInfo orderInfo) { + List cashierCarts = mpCashierCartService.selectByOrderIdAndState(orderInfo.getId()); + List detailList = mpOrderDetailService.selectByOrderId(orderInfo.getId()); + BigDecimal discount = BigDecimal.valueOf(payDTO.getDiscount()); + cashierCarts.forEach(item -> { + item.setTotalAmount(item.getTotalAmount().multiply(discount).setScale(2, RoundingMode.HALF_UP)); + }); + + detailList.forEach(item -> { + item.setPriceAmount(item.getPriceAmount().multiply(discount).setScale(2, RoundingMode.HALF_UP)); + }); + + mpCashierCartService.updateBatchById(cashierCarts); + mpOrderDetailService.updateBatchById(detailList); + BigDecimal oldAmount = orderInfo.getOrderAmount(); + BigDecimal finalAmount = orderInfo.getOrderAmount().multiply(BigDecimal.valueOf(payDTO.getDiscount())).setScale(2, RoundingMode.HALF_UP); + BigDecimal discountAmount = oldAmount.subtract(finalAmount); + orderInfo.setDiscountAmount(discountAmount); + orderInfo.setOrderAmount(finalAmount); + orderInfo.setAmount(finalAmount); + orderInfo.setSettlementAmount(finalAmount); + mpOrderInfoService.updateById(orderInfo); + return finalAmount; + } + @Override // @Transactional public Object pay(PayDTO payDTO) { @@ -2156,8 +2191,6 @@ public class TbShopTableServiceImpl implements TbShopTableService { payDTO.setVipUserId(orderInfo.getMemberId() != null ? Integer.valueOf(orderInfo.getMemberId()) : null); } - - if (ObjectUtil.isEmpty(orderInfo)) { throw new BadRequestException("订单不存在"); } @@ -2171,17 +2204,20 @@ public class TbShopTableServiceImpl implements TbShopTableService { payDTO.setDiscount((double) 1); } - // 计算优惠券积分折扣信息 BigDecimal finalAmount = null; + // 计算订单折扣信息 + if (payDTO.getDiscount() != 1) { + finalAmount = resetAmountByDiscount(payDTO, orderInfo); + }else { + finalAmount = orderInfo.getOrderAmount(); + } + + // 计算优惠券积分折扣信息 if (payDTO.getVipUserId() != null) { // 计算商品券折扣 finalAmount = calcDiscountAndUpdateInfo(payDTO, orderInfo); } - if (finalAmount == null) { - finalAmount = orderInfo.getOrderAmount().multiply(BigDecimal.valueOf(payDTO.getDiscount())).setScale(2, RoundingMode.HALF_UP); - } - boolean isOnline = false; switch (payDTO.getPayType()) { @@ -2670,15 +2706,15 @@ public class TbShopTableServiceImpl implements TbShopTableService { throw new BadRequestException("订单明细数量不一致"); } + HashMap data = new HashMap<>(); BigDecimal returnAmount = BigDecimal.ZERO; BigDecimal packAMount = BigDecimal.ZERO; BigDecimal saleAmount = BigDecimal.ZERO; ArrayList remainOrderDetailList = new ArrayList<>(); for (TbOrderDetail orderDetail : detailList) { - if (orderDetail.getUserCouponId() != null) { - throw new BadRequestException("选择了优惠券抵扣商品,无法退款"); - } + // 退款数量 BigDecimal returnNum = returnNumMap.get(orderDetail.getId().toString()); + // 剩余数量 BigDecimal remainNum = orderDetail.getNum().subtract(returnNum); if (remainNum.compareTo(BigDecimal.ZERO) < 0) { throw new BadRequestException("{}最多可退数量为: {}", orderDetail.getProductName(), orderDetail.getNum()); @@ -2699,18 +2735,43 @@ public class TbShopTableServiceImpl implements TbShopTableService { remainOrderDetailList.add(remainOrderDetail); } - returnAmount = returnAmount.add(orderDetail.getPriceAmount() - .divide(orderDetail.getNum(), 2, RoundingMode.DOWN) - .multiply(returnNum)); - saleAmount = saleAmount.add(orderDetail.getPrice()); - packAMount = packAMount.add(orderDetail.getPackAmount() - .divide(orderDetail.getNum(), 2, RoundingMode.DOWN) - .multiply(returnNum)); - BigDecimal returnPackFee = packFee.multiply(returnNum); + BigDecimal currentDetailAMount = BigDecimal.ZERO; + // 优惠券抵扣商品直接退券 + if (StrUtil.isNotBlank(orderDetail.getUseCouponInfo())) { + data.put("isCouponDiscount", true); + TbActivateOutRecord outRecord = JSONObject.parseObject(orderDetail.getUseCouponInfo(), TbActivateOutRecord.class); + outRecord.setRefNum(returnNum.intValue()); + shopCouponService.refund(CollUtil.newArrayList(outRecord)); + currentDetailAMount = returnNum.multiply(orderDetail.getPrice()) + .add(returnPackFee); + // 使用了满减或积分,获取普通付款商品计算退款比例,退部分现金 + }else if (oldOrderInfo.getFullCouponDiscountAmount().compareTo(BigDecimal.ZERO) > 0 || oldOrderInfo.getPointsDiscountAmount().compareTo(BigDecimal.ZERO) > 0){ + // 计算当前商品占比 + BigDecimal ratio = calcDetailRatio(orderDetail); + BigDecimal realAmount = orderDetail.getPriceAmount().multiply(ratio); + currentDetailAMount = realAmount + .divide(orderDetail.getNum(), 8, RoundingMode.HALF_UP) + .multiply(returnNum).setScale(2, RoundingMode.HALF_UP); + returnAmount = returnAmount.add(currentDetailAMount); + saleAmount = saleAmount.add(orderDetail.getPrice()); + packAMount = packAMount.add(orderDetail.getPackAmount() + .divide(orderDetail.getNum(), 8, RoundingMode.HALF_UP) + .multiply(returnNum)).setScale(2, RoundingMode.HALF_UP); + + }else { + currentDetailAMount = orderDetail.getPriceAmount() + .divide(orderDetail.getNum(), 8, RoundingMode.HALF_UP) + .multiply(returnNum).setScale(2, RoundingMode.HALF_UP); + returnAmount = returnAmount.add(currentDetailAMount); + saleAmount = saleAmount.add(orderDetail.getPrice()); + packAMount = packAMount.add(orderDetail.getPackAmount() + .divide(orderDetail.getNum(), 8, RoundingMode.HALF_UP) + .multiply(returnNum)).setScale(2, RoundingMode.HALF_UP); + } + orderDetail.setNum(returnNum); - orderDetail.setPriceAmount(returnNum.multiply(orderDetail.getPrice()) - .add(returnPackFee)); + orderDetail.setPriceAmount(currentDetailAMount); orderDetail.setPackAmount(returnPackFee); orderDetail.setRefundNumber(returnNum); // orderDetail.setStatus(isOnline ? "refunding" : "refund"); @@ -2756,12 +2817,23 @@ public class TbShopTableServiceImpl implements TbShopTableService { updateStockAndRecord(detailList); mpOrderDetailService.updateBatchById(detailList); - HashMap data = new HashMap<>(); data.put("returnOrder", returnOrder); data.put("returnAmount", returnAmount); return data; } + private BigDecimal calcDetailRatio(TbOrderDetail orderDetail) { + List detailList = mpOrderDetailService.selectByOrderIdAndState(orderDetail.getId(), TableConstant.OrderInfo.Status.CLOSED); + BigDecimal totalAmount = BigDecimal.ZERO; + for (TbOrderDetail item : detailList) { + if (item.getUserCouponId() == null) { + totalAmount = totalAmount.add(item.getPriceAmount()); + } + } + + return orderDetail.getPriceAmount().divide(totalAmount, 2, RoundingMode.HALF_UP); + } + private void updateStockAndRecord(List orderDetailList) { // 更新商品库存 for (TbOrderDetail detail : orderDetailList) { @@ -2825,7 +2897,7 @@ public class TbShopTableServiceImpl implements TbShopTableService { String shopId = orderInfo.getShopId(); // // 线上退款 - orderInfo.setRefundAmount(returnOrderInfo.getRefundAmount()); + orderInfo.setRefundAmount(orderInfo.getRefundAmount().add(returnOrderInfo.getRefundAmount())); orderInfo.setRefundRemark(returnOrderDTO.getNote()); if ("scanCode".equals(payType) || "wx_lite".equals(payType)) { payService.returnOrder(Integer.valueOf(shopId), orderInfo, returnOrderInfo); @@ -2841,6 +2913,10 @@ public class TbShopTableServiceImpl implements TbShopTableService { returnOrderDTO.getOrderId(), returnOrderDTO.getOrderDetails().stream().map(ReturnOrderDTO.OrderDetail::getId).collect(Collectors.toList())); } orderInfo.setStatus(TableConstant.OrderInfo.Status.CLOSED.getValue()); + // 订单金额全退,退优惠券以及积分 + if (orderInfo.getPayAmount().compareTo(orderInfo.getRefundAmount()) <= 0) { + returnCoupon(orderInfo); + } orderInfoMapper.updateById(orderInfo); // 打印退款小票 // producer.printMechine(newOrderInfo.getId().toString()); @@ -2893,4 +2969,31 @@ public class TbShopTableServiceImpl implements TbShopTableService { return true; } + + @Override + public TbCashierCart updatePrice(UpdatePriceDTO updatePriceDTO) { + TbCashierCart cashierCart = mpCashierCartService.selectByShopIdAndId(updatePriceDTO.getShopId(), updatePriceDTO.getCartId(), TableConstant.OrderInfo.Status.CREATE); + if (cashierCart == null) { + throw new BadRequestException("购物车商品不存在"); + } + + if (updatePriceDTO.getSaleAmount().compareTo(cashierCart.getDiscountSaleAmount()) == 0) { + return cashierCart; + } + + BigDecimal newTotalPrice = updatePriceDTO.getSaleAmount().multiply(cashierCart.getTotalNumber()).add(cashierCart.getPackFee()); + BigDecimal oldAmount = cashierCart.getTotalAmount(); + cashierCart.setTotalAmount(newTotalPrice); + cashierCart.setUpdatedAt(DateUtil.current()); + cashierCart.setDiscountSaleNote(updatePriceDTO.getNote()); + cashierCart.setDiscountSaleAmount(updatePriceDTO.getSaleAmount()); + mpCashierCartService.updateById(cashierCart); + + // 更新订单和detail价格 + if (cashierCart.getOrderId() != null) { + mpOrderDetailService.updatePriceByCartId(cashierCart.getId(), updatePriceDTO.getSaleAmount(), newTotalPrice); + mpOrderInfoService.incrAmount(cashierCart.getOrderId(), newTotalPrice.subtract(oldAmount)); + } + return cashierCart; + } }