|
|
package com.yohoufo.order.service.impl;
|
|
|
|
|
|
import com.yoho.error.ServiceError;
|
|
|
import com.yoho.error.exception.ServiceException;
|
|
|
import com.yohobuy.ufo.model.order.bo.MerchantOrderAttachInfo;
|
|
|
import com.yohobuy.ufo.model.order.common.OrderStatus;
|
|
|
import com.yohobuy.ufo.model.order.common.SellerOrderStatus;
|
|
|
import com.yohobuy.ufo.model.order.common.SkupStatus;
|
|
|
import com.yohobuy.ufo.model.order.common.TabType;
|
|
|
import com.yohobuy.ufo.model.order.common.*;
|
|
|
import com.yohoufo.common.alarm.EventBusPublisher;
|
|
|
import com.yohoufo.common.alarm.SmsAlarmEvent;
|
|
|
import com.yohoufo.common.exception.UfoServiceException;
|
|
|
import com.yohoufo.common.utils.BigDecimalHelper;
|
|
|
import com.yohoufo.common.utils.DateUtil;
|
|
|
import com.yohoufo.dal.order.BuyerOrderMapper;
|
|
|
import com.yohoufo.dal.order.OrderCouponMapper;
|
|
|
import com.yohoufo.dal.order.SellerOrderGoodsMapper;
|
|
|
import com.yohoufo.dal.order.SellerOrderMapper;
|
|
|
import com.yohoufo.dal.order.*;
|
|
|
import com.yohoufo.dal.order.model.*;
|
|
|
import com.yohoufo.order.common.BillTradeStatus;
|
|
|
import com.yohoufo.order.common.RefundCase;
|
...
|
...
|
@@ -22,7 +20,10 @@ import com.yohoufo.order.event.BillLogEvent; |
|
|
import com.yohoufo.order.event.ErpBuyerOrderEvent;
|
|
|
import com.yohoufo.order.model.PayRefundBo;
|
|
|
import com.yohoufo.order.model.bo.CouponBo;
|
|
|
import com.yohoufo.order.model.dto.BuyerPenalty;
|
|
|
import com.yohoufo.order.model.dto.BuyerPenaltyCalResult;
|
|
|
import com.yohoufo.order.model.request.PaymentRequest;
|
|
|
import com.yohoufo.order.model.request.TranseferCellNode;
|
|
|
import com.yohoufo.order.model.request.TransferMoneyRequest;
|
|
|
import com.yohoufo.order.service.IPaymentService;
|
|
|
import com.yohoufo.order.service.cache.CacheCleaner;
|
...
|
...
|
@@ -40,6 +41,8 @@ import org.springframework.stereotype.Service; |
|
|
import javax.annotation.Resource;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Collection;
|
|
|
import java.util.Objects;
|
|
|
import java.util.concurrent.Callable;
|
|
|
import java.util.concurrent.Future;
|
|
|
|
...
|
...
|
@@ -83,6 +86,111 @@ public class BuyerOrderCancelService { |
|
|
@Autowired
|
|
|
private SellerService sellerService;
|
|
|
|
|
|
@Autowired
|
|
|
private MetaConfigService metaConfigService;
|
|
|
|
|
|
@Autowired
|
|
|
private OrdersPayMapper ordersPayMapper;
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
* @param buyerUid
|
|
|
* @param orderActualAmount 订单实付金额
|
|
|
* @return
|
|
|
*/
|
|
|
public BuyerPenaltyCalResult calBuyerPenalty(Integer buyerUid, Long orderCode, OrderStatus orderStatus,
|
|
|
BigDecimal orderActualAmount,
|
|
|
OrderAttributes orderAttributes
|
|
|
){
|
|
|
if (Objects.isNull(orderAttributes)){
|
|
|
logger.warn("in calBuyerPenalty order Attributes illegal buyerUid {} orderCode {} orderAttributes {}", buyerUid, orderCode, orderAttributes);
|
|
|
throw new UfoServiceException(501, "order Attributes illegal");
|
|
|
}
|
|
|
BuyerPenalty buyerPenalty = metaConfigService.getBuyerPenalty();
|
|
|
if (Objects.isNull(buyerPenalty)){
|
|
|
logger.warn("in calBuyerPenalty order buyerPenalty config illegal buyerUid {} orderCode {} orderAttributes {}", buyerUid, orderCode, orderAttributes);
|
|
|
throw new UfoServiceException(501, "buyerPenalty config missed");
|
|
|
}
|
|
|
BuyerPenalty.Fee mfee = null;
|
|
|
//common order
|
|
|
if (Objects.equals(orderAttributes.getCode(), OrderAttributes.COMMON_IN_STOCK.getCode())){
|
|
|
OrdersPay ordersPay = ordersPayMapper.selectOrdersPay(orderCode, buyerUid);
|
|
|
Integer payTime;
|
|
|
if(Objects.isNull(ordersPay) || Objects.isNull(payTime = ordersPay.getCreateTime())){
|
|
|
logger.warn("in calBuyerPenalty not exist paid record.orderCode {}", orderCode);
|
|
|
throw new ServiceException(ServiceError.ORDER_HAS_NOT_PAID);
|
|
|
}
|
|
|
int currentTime = DateUtil.getCurrentTimeSecond();
|
|
|
int diffTime = currentTime - payTime;
|
|
|
|
|
|
//before seller deliver
|
|
|
if (Objects.equals(orderStatus.getCode(), OrderStatus.BUYER_CANCEL_BEFORE_SELLER_DELIVER.getCode())){
|
|
|
Collection<BuyerPenalty.Fee> fees = buyerPenalty.getStockCaseMap().get("goodsInStock")
|
|
|
.getTriggerCaseMap().get("beforeSellerDeliver").getStagedCollection();
|
|
|
mfee = findByTimeCompared(fees, diffTime, orderCode);
|
|
|
}
|
|
|
//after seller deliver
|
|
|
|
|
|
if (Objects.equals(orderStatus.getCode(), OrderStatus.BUYER_CANCEL_BEFORE_DEPOT_RECEIVE.getCode())){
|
|
|
Collection<BuyerPenalty.Fee> fees = buyerPenalty.getStockCaseMap().get("goodsInStock")
|
|
|
.getTriggerCaseMap().get("beforeDepotReceive").getStagedCollection();
|
|
|
mfee = fees.iterator().next();
|
|
|
}
|
|
|
|
|
|
if (Objects.isNull(mfee)){
|
|
|
logger.warn("in calBuyerPenalty can not match feeRate.orderCode {} diffTime {} buyerPenalty {}",
|
|
|
orderCode, diffTime, buyerPenalty);
|
|
|
throw new UfoServiceException(501, "not match calFee");
|
|
|
}
|
|
|
}
|
|
|
//TODO quick deliver
|
|
|
BuyerPenaltyCalResult bpcr = calBuyerPenalty( mfee,buyerUid, orderCode, orderActualAmount);
|
|
|
return bpcr;
|
|
|
}
|
|
|
|
|
|
public BuyerPenaltyCalResult calBuyerPenalty(BuyerPenalty.Fee mfee,Integer buyerUid, Long orderCode,
|
|
|
BigDecimal orderActualAmount){
|
|
|
BigDecimal penaltyRate = mfee.getRate();
|
|
|
BigDecimal penaltyAmount = BigDecimalHelper.halfUp(orderActualAmount.multiply(penaltyRate));
|
|
|
BuyerPenalty.Range moneyRange = mfee.getMoneyRange();
|
|
|
if (penaltyAmount.compareTo(moneyRange.getMin()) < 0){
|
|
|
penaltyAmount = moneyRange.getMin();
|
|
|
}
|
|
|
BigDecimal leftAmount = orderActualAmount.subtract(penaltyAmount);
|
|
|
|
|
|
BuyerPenaltyCalResult bpcr = BuyerPenaltyCalResult.builder().uid(buyerUid).orderCode(orderCode)
|
|
|
.orderActualAmount(orderActualAmount).penaltyRate(penaltyRate)
|
|
|
.penaltyAmount(penaltyAmount).leftAmount(leftAmount).build();
|
|
|
logger.info("calBuyerPenalty mfee {} BuyerPenaltyCalResult {}", mfee, bpcr);
|
|
|
return bpcr;
|
|
|
}
|
|
|
|
|
|
private BuyerPenalty.Fee findByTimeCompared(Collection<BuyerPenalty.Fee> fees, int diffTime, Long orderCode){
|
|
|
BuyerPenalty.Fee mfee = null;
|
|
|
for(BuyerPenalty.Fee fee : fees){
|
|
|
BuyerPenalty.Range timeRange = fee.getTimeRange();
|
|
|
BigDecimal min = timeRange.getMin();
|
|
|
BigDecimal max = timeRange.getMax();
|
|
|
boolean isgt = false;
|
|
|
if (Objects.nonNull(min)){
|
|
|
isgt = diffTime > min.intValue();
|
|
|
}
|
|
|
//可以不设置最大值,没有就默认为无穷大,所以默认为满足小于最大值
|
|
|
boolean islt = true;
|
|
|
if (Objects.nonNull(max)){
|
|
|
islt = diffTime <= max.intValue();
|
|
|
}
|
|
|
|
|
|
if (isgt && islt){
|
|
|
mfee = fee;
|
|
|
logger.info("findByTimeCompared orderCode {} diffTime {} mfee {}", orderCode, diffTime, mfee);
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
return mfee;
|
|
|
}
|
|
|
|
|
|
|
|
|
public void cancel(BeforeSellerDeliverEvent bsdEvent){
|
|
|
OrderDynamicConfig.BuyerCancelCompensateNode compensate = orderDynamicConfig.getBeforeSellerDeliverBCCN();
|
...
|
...
|
|