Commit d5b815b6 authored by 技术总监-万宁's avatar 技术总监-万宁

完成售后退货修改未支付次数的支付金额

parent dad5ce1c
package com.ssy.lingxi.order.model.dto;
import java.io.Serializable;
import java.math.BigDecimal;
/**
* 售后服务 - 退货 - 重新计算支付金额接口 - 退货商品信息
* @author 万宁
* @version 2.0.0
* @date 2021-09-03
*/
public class AfterSaleReturnProductDetailDTO implements Serializable {
private static final long serialVersionUID = -6908551587670303260L;
/**
* 订单商品Id
*/
private Long orderProductId;
/**
* 购买数量
*/
private BigDecimal quantity;
/**
* 到手价格
*/
private BigDecimal refPrice;
/**
* 退货数量
*/
private BigDecimal returnCount;
public Long getOrderProductId() {
return orderProductId;
}
public void setOrderProductId(Long orderProductId) {
this.orderProductId = orderProductId;
}
public BigDecimal getQuantity() {
return quantity;
}
public void setQuantity(BigDecimal quantity) {
this.quantity = quantity;
}
public BigDecimal getRefPrice() {
return refPrice;
}
public void setRefPrice(BigDecimal refPrice) {
this.refPrice = refPrice;
}
public BigDecimal getReturnCount() {
return returnCount;
}
public void setReturnCount(BigDecimal returnCount) {
this.returnCount = returnCount;
}
}
package com.ssy.lingxi.order.model.dto;
import java.io.Serializable;
import java.util.List;
import java.math.BigDecimal;
/**
* 售后服务 - 退货 - 重新计算支付金额接口DTO
* 售后服务 - 订单退货退款 - 修改剩余支付次数支付金额
* @author 万宁
* @version 2.0.0
* @date 2021-09-03
* @date 2021-09-11
*/
public class AfterSaleReturnProductDTO implements Serializable {
private static final long serialVersionUID = -3325664129030062311L;
public class OrderReturnAmountDTO implements Serializable {
private static final long serialVersionUID = -7230468219971330004L;
public OrderReturnAmountDTO() {
}
public OrderReturnAmountDTO(Long orderId, BigDecimal leftAmount) {
this.orderId = orderId;
this.leftAmount = leftAmount;
}
/**
* 订单Id
......@@ -18,9 +26,9 @@ public class AfterSaleReturnProductDTO implements Serializable {
private Long orderId;
/**
* 退货商品信息
* 退货后的商品总金额
*/
private List<AfterSaleReturnProductDetailDTO> products;
private BigDecimal leftAmount;
public Long getOrderId() {
return orderId;
......@@ -30,11 +38,11 @@ public class AfterSaleReturnProductDTO implements Serializable {
this.orderId = orderId;
}
public List<AfterSaleReturnProductDetailDTO> getProducts() {
return products;
public BigDecimal getLeftAmount() {
return leftAmount;
}
public void setProducts(List<AfterSaleReturnProductDetailDTO> products) {
this.products = products;
public void setLeftAmount(BigDecimal leftAmount) {
this.leftAmount = leftAmount;
}
}
......@@ -25,7 +25,7 @@ public interface OrderPaymentRepository extends JpaRepository<OrderPaymentDO, Lo
List<OrderPaymentDO> findByOrderAndPayAmountGreaterThan(OrderDO order, BigDecimal payAmount, Sort sort);
List<OrderPaymentDO> findByOrderInAndPayAmountGreaterThanAndOuterStatusIn(List<Long> orderIds, BigDecimal payAmount, List<Integer> outerStatus);
List<OrderPaymentDO> findByOrderInAndPayAmountGreaterThanAndOuterStatusIn(List<OrderDO> orders, BigDecimal payAmount, List<Integer> outerStatus);
@Transactional
void deleteByOrder(OrderDO order);
......
......@@ -6,7 +6,7 @@ import com.ssy.lingxi.order.entity.OrderPaymentDO;
import com.ssy.lingxi.order.model.bo.OrderPayResultBO;
import com.ssy.lingxi.order.model.bo.OrderPaymentBO;
import com.ssy.lingxi.order.model.bo.PayNodeBO;
import com.ssy.lingxi.order.model.dto.AfterSaleReturnProductDTO;
import com.ssy.lingxi.order.model.dto.OrderReturnAmountDTO;
import com.ssy.lingxi.order.model.vo.basic.request.OrderPaymentVO;
import com.ssy.lingxi.order.model.vo.buyer.response.BuyerOrderPayNodeQueryVO;
import com.ssy.lingxi.order.model.vo.buyer.response.BuyerOrderPaymentDetailVO;
......@@ -130,8 +130,9 @@ public interface IBaseOrderPaymentService {
/**
* 售后服务 - 退货 - 修改支付次数的金额
* @param afterSaleReturnProducts 售后退货商品
* @param orders 订单列表
* @param returnAmounts 订单Id及退款金额
* @return 操作结果
*/
Wrapper<Void> batchUpdateOrderPayments(List<AfterSaleReturnProductDTO> afterSaleReturnProducts);
Wrapper<Void> batchUpdateOrderPayments(List<OrderDO> orders, List<OrderReturnAmountDTO> returnAmounts);
}
......@@ -13,7 +13,7 @@ import com.ssy.lingxi.order.model.constant.BuyerInnerStatusEnum;
import com.ssy.lingxi.order.model.constant.OrderOuterStatusEnum;
import com.ssy.lingxi.order.model.constant.OrderServiceContants;
import com.ssy.lingxi.order.model.constant.VendorInnerStatusEnum;
import com.ssy.lingxi.order.model.dto.AfterSaleReturnProductDTO;
import com.ssy.lingxi.order.model.dto.OrderReturnAmountDTO;
import com.ssy.lingxi.order.model.dto.PayResultDTO;
import com.ssy.lingxi.order.model.vo.basic.request.OrderPaymentVO;
import com.ssy.lingxi.order.model.vo.buyer.response.BuyerOrderPayNodeQueryVO;
......@@ -780,29 +780,45 @@ public class BaseOrderPaymentServiceImpl implements IBaseOrderPaymentService {
/**
* 售后服务 - 退货 - 修改支付次数的金额
*
* @param afterSaleReturnProducts 售后退货商品
* @param orders 订单列表
* @param returnAmounts 订单Id及退款金额
* @return 操作结果
*/
@Override
public Wrapper<Void> batchUpdateOrderPayments(List<AfterSaleReturnProductDTO> afterSaleReturnProducts) {
if(CollectionUtils.isEmpty(afterSaleReturnProducts)) {
public Wrapper<Void> batchUpdateOrderPayments(List<OrderDO> orders, List<OrderReturnAmountDTO> returnAmounts) {
if(CollectionUtils.isEmpty(returnAmounts)) {
return Wrapper.success();
}
// //Step 1: 如果没有退货,返回
// List<Long> orderIds = afterSaleReturnProducts.stream().filter(afterSale -> afterSale.getProducts().stream().anyMatch(afterSaleProduct -> afterSaleProduct.getReturnCount().compareTo(BigDecimal.ZERO) > 0)).map(OrderAfterSaleVO::getOrderId).collect(Collectors.toList());
// if(CollectionUtils.isEmpty(orderIds)) {
// return Wrapper.success();
// }
//
// //Step 2: 如果没有支付记录,返回
// List<OrderPaymentDO> orderPaymentList = orderPaymentRepository.findByOrderInAndPayAmountGreaterThanAndOuterStatusIn(orderIds, BigDecimal.ZERO, Stream.of(OrderOuterStatusEnum.TO_PAY.getCode(), OrderOuterStatusEnum.PAYMENT_NOT_ACCOMPLISH.getCode()).collect(Collectors.toList()));
// if(CollectionUtils.isEmpty(orderPaymentList)) {
// return Wrapper.success();
// }
//查询支付金额大于0,且外部状态为“待支付”和“确认未到账”的支付记录
List<Integer> outerStatus = Stream.of(OrderOuterStatusEnum.TO_PAY.getCode(), OrderOuterStatusEnum.PAYMENT_NOT_ACCOMPLISH.getCode()).collect(Collectors.toList());
List<OrderPaymentDO> orderPaymentList = orderPaymentRepository.findByOrderInAndPayAmountGreaterThanAndOuterStatusIn(orders, BigDecimal.ZERO, outerStatus);
return null;
}
for (OrderReturnAmountDTO returnAmount : returnAmounts) {
List<OrderPaymentDO> orderPayments = orderPaymentList.stream().filter(payment -> payment.getOrder().getId().equals(returnAmount.getOrderId())).collect(Collectors.toList());
if(CollectionUtils.isEmpty(orderPayments)) {
continue;
}
Map<Integer, BigDecimal> payAmountMap = averageLeftPayAmount(returnAmount.getLeftAmount(), orderPayments);
orderPayments.forEach(payment -> payment.setPayAmount(payAmountMap.getOrDefault(payment.getBatchNo(), payment.getPayAmount())));
}
orderPaymentRepository.saveAll(orderPaymentList);
return Wrapper.success();
}
/**
* 退货后,计算剩余支付次数的支付金额
* @param leftAmount (退货后的)商品总额
* @param orderPayments 支付次数,支付比例保留4位小数
* @return 每批次支付的金额
*/
private Map<Integer, BigDecimal> averageLeftPayAmount(BigDecimal leftAmount, List<OrderPaymentDO> orderPayments) {
Integer maxBatch = orderPayments.stream().map(OrderPaymentDO::getBatchNo).max(Comparator.comparingInt(Integer::intValue)).orElse(0);
Map<Integer, BigDecimal> map = orderPayments.stream().filter(payment -> payment.getBatchNo().compareTo(maxBatch) < 0).collect(Collectors.toMap(OrderPaymentDO::getBatchNo, payment -> leftAmount.multiply(payment.getPayRate()).setScale(2, RoundingMode.HALF_UP)));
map.put(maxBatch, leftAmount.subtract(map.values().stream().reduce(BigDecimal::add).orElse(BigDecimal.ZERO)));
return map;
}
}
......@@ -17,8 +17,10 @@ import com.ssy.lingxi.order.api.model.vo.response.*;
import com.ssy.lingxi.order.entity.*;
import com.ssy.lingxi.order.model.bo.*;
import com.ssy.lingxi.order.model.constant.OrderOuterStatusEnum;
import com.ssy.lingxi.order.model.dto.OrderReturnAmountDTO;
import com.ssy.lingxi.order.repository.OrderProductRepository;
import com.ssy.lingxi.order.repository.OrderRepository;
import com.ssy.lingxi.order.service.base.IBaseOrderPaymentService;
import com.ssy.lingxi.order.service.base.IBaseOrderProcessService;
import com.ssy.lingxi.order.service.feign.IOrderFeignService;
import com.ssy.lingxi.order.service.web.IBuyerOrderService;
......@@ -48,6 +50,9 @@ public class OrderFeignServiceImpl implements IOrderFeignService {
private OrderProductRepository orderProductRepository;
@Resource
private IBaseOrderPaymentService baseOrderPaymentService;
@Resource
private IBuyerOrderService buyerOrderService;
@Resource
......@@ -202,7 +207,7 @@ public class OrderFeignServiceImpl implements IOrderFeignService {
}
List<Long> orderProductIds = afterSales.stream().flatMap(a -> a.getProducts().stream().map(OrderProductAfterSaleVO::getProductId)).collect(Collectors.toList());
List<OrderProductDO> orderProducts = orderProductRepository.findAllById(orderProductIds);
List<OrderProductDO> orderProducts = orderProductRepository.findByOrderIn(orders);
if(CollectionUtils.isEmpty(orderProducts) || orderProducts.size() != orderProductIds.stream().distinct().count()) {
return Wrapper.fail(ResponseCode.ORDER_PRODUCT_DOES_NOT_EXIST);
}
......@@ -216,7 +221,10 @@ public class OrderFeignServiceImpl implements IOrderFeignService {
}));
orderProductRepository.saveAll(orderProducts);
return Wrapper.success();
return baseOrderPaymentService.batchUpdateOrderPayments(orders, orderProducts.stream().collect(Collectors.groupingBy(p -> p.getOrder().getId())).entrySet().stream().map(entry ->
new OrderReturnAmountDTO(entry.getKey(), entry.getValue().stream().map(p -> p.getQuantity().subtract(p.getReturnCount()).multiply(p.getRefPrice())).reduce(BigDecimal.ZERO, BigDecimal::add))
).collect(Collectors.toList()));
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment