|
|
package com.yohoufo.order.service.seller.changePrice;
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
import com.yoho.error.ServiceError;
|
|
|
import com.yoho.error.exception.ServiceException;
|
|
|
import com.yohobuy.ufo.model.order.bo.OrderInfo;
|
|
|
import com.yohobuy.ufo.model.order.bo.SoldPrdComputeBo;
|
|
|
import com.yohobuy.ufo.model.order.common.TabType;
|
|
|
import com.yohobuy.ufo.model.order.constants.ChangePriceStatus;
|
|
|
import com.yohobuy.ufo.model.order.req.NESChangePriceReq;
|
|
|
import com.yohobuy.ufo.model.order.req.SellerOrderCancelReq;
|
|
|
import com.yohoufo.common.exception.UfoServiceException;
|
|
|
import com.yohoufo.common.utils.DateUtil;
|
|
|
import com.yohoufo.dal.order.OrdersPrePayMapper;
|
|
|
import com.yohoufo.dal.order.SellerChangePriceRecordMapper;
|
|
|
import com.yohoufo.dal.order.model.OrdersPrePay;
|
|
|
import com.yohoufo.dal.order.model.SellerChangePriceRecord;
|
|
|
import com.yohoufo.dal.order.model.SellerOrder;
|
|
|
import com.yohoufo.dal.order.model.SellerOrderGoods;
|
|
|
import com.yohoufo.order.convert.SellerOrderConvertor;
|
|
|
import com.yohoufo.order.event.OrderCancelEvent;
|
|
|
import com.yohoufo.order.model.NESCPOrderContext;
|
|
|
import com.yohoufo.order.model.dto.ChangePricePrepareDTO;
|
|
|
import com.yohoufo.order.model.response.OrderSubmitResp;
|
|
|
import com.yohoufo.order.service.impl.SellerOrderCancelService;
|
|
|
import com.yohoufo.order.service.impl.visitor.UserCancelCase;
|
|
|
import com.yohoufo.order.service.seller.SellerChangePriceRecordService;
|
|
|
import com.yohoufo.order.service.seller.processor.NESChangePricePrepareProcessor;
|
|
|
import com.yohoufo.order.service.seller.processor.NESChangePricePublishPrepareProcessor;
|
|
|
import com.yohoufo.order.utils.LoggerUtils;
|
...
|
...
|
@@ -20,7 +33,10 @@ import org.slf4j.Logger; |
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Objects;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* 非入驻的卖家 变价服务类
|
...
|
...
|
@@ -52,11 +68,19 @@ public class NotEntrySellerChangePriceService { |
|
|
private NESChangePricePrepareProcessor nesChangePricePrepareProcessor;
|
|
|
|
|
|
@Autowired
|
|
|
NESChangePricePublishPrepareProcessor nesChangePricePublishPrepareProcessor;
|
|
|
private NESChangePricePublishPrepareProcessor nesChangePricePublishPrepareProcessor;
|
|
|
|
|
|
@Autowired
|
|
|
NESChangePricePublishExcutor nesChangePricePublishExcutor;
|
|
|
private NESChangePricePublishExcutor nesChangePricePublishExcutor;
|
|
|
|
|
|
@Autowired
|
|
|
private OrdersPrePayMapper ordersPrePayMapper;
|
|
|
|
|
|
@Autowired
|
|
|
private SellerOrderCancelService sellerOrderCancelService;
|
|
|
|
|
|
@Autowired
|
|
|
private SellerChangePriceRecordService sellerChangePriceRecordService;
|
|
|
|
|
|
public boolean isChangePriceOrder(int skup){
|
|
|
|
...
|
...
|
@@ -65,8 +89,9 @@ public class NotEntrySellerChangePriceService { |
|
|
|
|
|
|
|
|
public boolean isChangePriceOrder(long orderCode){
|
|
|
//todo
|
|
|
return false;
|
|
|
//
|
|
|
SellerChangePriceRecord scpr = sellerChangePriceRecordMapper.selectByOrderCode(orderCode);
|
|
|
return Objects.nonNull(scpr);
|
|
|
}
|
|
|
|
|
|
/**
|
...
|
...
|
@@ -84,26 +109,22 @@ public class NotEntrySellerChangePriceService { |
|
|
|
|
|
/**
|
|
|
* change Price 4 Not Entry Seller
|
|
|
* @param req
|
|
|
* @return
|
|
|
*/
|
|
|
public OrderSubmitResp changePrice(NESChangePriceReq req){
|
|
|
logger.info("in NotEntrySellerChangePriceService.changePrice, req {}", req);
|
|
|
NESCPOrderContext ctx = nesChangePricePublishPrepareProcessor.buildPublishCtx(req);
|
|
|
//TODO
|
|
|
/*
|
|
|
step 1. 检查变价记录 seller_change_price_record
|
|
|
* step 1. 检查变价记录 seller_change_price_record
|
|
|
1.1 存在时 -> step 3
|
|
|
1.2 不存在时 -> step 2
|
|
|
step 2. 屏蔽待变价skup
|
|
|
2.1 何时恢复
|
|
|
step 3. 取消已有的待支付订单 (修改订单 商品状态 ,变价记录是否记录失败原因)
|
|
|
step 4. 生成待支付订单 待上架的商品,将订单号返回给客户端
|
|
|
|
|
|
step 5. 记录变价记录(新的订单号,新skup)
|
|
|
* @param req
|
|
|
* @return
|
|
|
*/
|
|
|
public OrderSubmitResp changePrice(NESChangePriceReq req){
|
|
|
logger.info("in NotEntrySellerChangePriceService.changePrice, req {}", req);
|
|
|
NESCPOrderContext ctx = nesChangePricePublishPrepareProcessor.buildPublishCtx(req);
|
|
|
SellerOrderGoods psog = ctx.getSellerOrderGoods();
|
|
|
int skup = psog.getId();
|
|
|
int preSkup = psog.getId();
|
|
|
SellerOrder sellerOrder = ctx.getSellerOrder();
|
|
|
Long preOrderCode = sellerOrder.getOrderCode();
|
|
|
List<Short> scprs = Lists.newArrayListWithCapacity(2);
|
...
|
...
|
@@ -112,26 +133,179 @@ public class NotEntrySellerChangePriceService { |
|
|
|
|
|
List<SellerChangePriceRecord> scprList = sellerChangePriceRecordMapper.selectByPreOrderCodeNStatus(preOrderCode, scprs);
|
|
|
if (CollectionUtils.isEmpty(scprList)){
|
|
|
logger.info("NotEntrySellerChangePriceService.changePrice there are not WAITING_DEAL or SUCCESS records, req {}", req);
|
|
|
//首次 或 全部失败
|
|
|
return nesChangePricePublishExcutor.publish(ctx);
|
|
|
//todo 屏蔽商品
|
|
|
OrderSubmitResp osResp = nesChangePricePublishExcutor.publish(ctx);
|
|
|
|
|
|
sellerChangePriceRecordService.add(preOrderCode, preSkup,osResp.getOrderCode(), osResp.getSkup() );
|
|
|
return osResp;
|
|
|
}
|
|
|
ChangePriceRecords cpr = splitChangePriceRecords(scprList);
|
|
|
//存在成功处理,有且只有一条才对(不完全正确,多终端操作同一个待变价商品)
|
|
|
|
|
|
if (CollectionUtils.isNotEmpty(cpr.successList)){
|
|
|
logger.warn("in NotEntrySellerChangePriceService.changePrice find ChangePriceRecords been deal successfully, req {}", req);
|
|
|
throw new UfoServiceException(400, "已经变价成功");
|
|
|
}
|
|
|
//没有一条成功处理,但是存在等待处理
|
|
|
if (CollectionUtils.isNotEmpty(cpr.waitingDealList)){
|
|
|
logger.info("NotEntrySellerChangePriceService.changePrice there are WAITING_DEAL records, req {}", req);
|
|
|
//TODO 检查待处理是否被支付(等待支付回调处理)
|
|
|
checkWaitingDeal(cpr.waitingDealList, preSkup);
|
|
|
batchCancel(req.getUid(), cpr.waitingDealList);
|
|
|
|
|
|
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
private void checkWaitingDeal(List<SellerChangePriceRecord> waitingDealList, int preSkup){
|
|
|
List<Long> orderCodes = waitingDealList.parallelStream()
|
|
|
.map(SellerChangePriceRecord::getOrderCode)
|
|
|
.collect(Collectors.toList());
|
|
|
//todo 到支付平台主动查询订单支付状态
|
|
|
|
|
|
//检查相关待支付订单是否存在超时内的预支付记录
|
|
|
List<OrdersPrePay> opps = ordersPrePayMapper.selectByOrderCodes(orderCodes);
|
|
|
if (CollectionUtils.isNotEmpty(opps)){
|
|
|
int secondsOfTimelimit = OrderInfo.SELLER_PAY_TIMEOUT;
|
|
|
int currentSeconds = DateUtil.getCurrentTimeSecond();
|
|
|
int startSeconds = currentSeconds - secondsOfTimelimit;
|
|
|
long prePayCnt = opps.stream().filter(opp -> opp.getCreateTime() > startSeconds).count();
|
|
|
if (prePayCnt > 0){
|
|
|
logger.warn("in checkWaitingDeal find exist prepay record, preSkup {}", preSkup);
|
|
|
throw new UfoServiceException(401, "请取消待支付的变价订单");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static class ChangePriceRecords{
|
|
|
List<SellerChangePriceRecord> successList;
|
|
|
List<SellerChangePriceRecord> waitingDealList;
|
|
|
}
|
|
|
|
|
|
ChangePriceRecords splitChangePriceRecords(List<SellerChangePriceRecord> scprList){
|
|
|
List<SellerChangePriceRecord> successList = new ArrayList<>(scprList.size());
|
|
|
List<SellerChangePriceRecord> waitingDealList = new ArrayList<>(scprList.size());
|
|
|
for(SellerChangePriceRecord scpr : scprList){
|
|
|
if (ChangePriceStatus.WAITING_DEAL.getCode() == scpr.getStatus()){
|
|
|
waitingDealList.add(scpr);
|
|
|
}
|
|
|
if (ChangePriceStatus.SUCCESS.getCode() == scpr.getStatus()){
|
|
|
successList.add(scpr);
|
|
|
}
|
|
|
}
|
|
|
ChangePriceRecords cpr = new ChangePriceRecords();
|
|
|
cpr.successList = successList;
|
|
|
cpr.waitingDealList = waitingDealList;
|
|
|
return cpr;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
*@param orderCode 支付成功的订单
|
|
|
*/
|
|
|
public void processAfterChangePriceOrderPaid(long orderCode){
|
|
|
//TODO
|
|
|
//1. 待支付订单 待上架商品
|
|
|
//2. 待变价skup进行下架
|
|
|
public void processAfterChangePriceOrderPaid(int uid, long orderCode){
|
|
|
logger.info("process After ChangePriceOrder Paid, uid {} orderCode {}", uid, orderCode);
|
|
|
//1. 已支付变价订单对应的记录状态变更
|
|
|
//2. 待变价skup进行下架-> 退还保证金
|
|
|
SellerChangePriceRecord scpr = sellerChangePriceRecordMapper.selectByOrderCode(orderCode);
|
|
|
if (Objects.isNull(scpr)) {
|
|
|
logger.warn("process After ChangePriceOrder Paid not a ChangePriceOrder, uid {} orderCode {}", uid, orderCode);
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
if(recordSuccess(scpr.getOrderCode(), ChangePriceStatus.WAITING_DEAL)==0){
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
long preOrderCode = scpr.getPreOrderCode();
|
|
|
sellerOrderCancelService.cancelSellerOrder(uid, preOrderCode);
|
|
|
}
|
|
|
|
|
|
public void batchCancel(int uid, List<SellerChangePriceRecord> scprList){
|
|
|
for(SellerChangePriceRecord scpr : scprList){
|
|
|
SellerOrderCancelReq req = SellerOrderCancelReq.builder().uid(uid)
|
|
|
.orderCode(scpr.getOrderCode()).build();
|
|
|
try {
|
|
|
cancel(req);
|
|
|
}catch (Exception ex){
|
|
|
logger.warn("in NotEntrySellerChangePriceService.batchCancel occur exception req {}", req, ex);
|
|
|
continue;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public int cancel(SellerOrderCancelReq req){
|
|
|
logger.info("not entry seller change price cancel order,req {}", req);
|
|
|
int result = 0;
|
|
|
int uid = req.getUid();
|
|
|
long orderCode = req.getOrderCode();
|
|
|
if ((result=recordFail(orderCode, ChangePriceStatus.FIFO, ChangePriceStatus.WAITING_DEAL))>0) {
|
|
|
sellerOrderCancelService.cancelSellerOrder(uid, orderCode);
|
|
|
}
|
|
|
//
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
private int recordSuccess(long orderCode,ChangePriceStatus sourceStatus){
|
|
|
SellerChangePriceRecord scprc = new SellerChangePriceRecord();
|
|
|
scprc.setOrderCode(orderCode);
|
|
|
scprc.setStatus(ChangePriceStatus.SUCCESS.getCode());
|
|
|
scprc.setUpdateTime(DateUtil.getCurrentTimeSecond());
|
|
|
scprc.setSourceStatus(sourceStatus.getCode());
|
|
|
return sellerChangePriceRecordMapper.updateByOrderCodeNStatus(scprc);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 卖家主动取消
|
|
|
* @param uid
|
|
|
* @param orderCode
|
|
|
* @return
|
|
|
*/
|
|
|
public int recordSelfCancelIfPresent(int uid, long orderCode){
|
|
|
ChangePriceStatus tcps = ChangePriceStatus.SELF_CANCEL,
|
|
|
scps = ChangePriceStatus.WAITING_DEAL;
|
|
|
return recordFailIfPresent(uid, orderCode, tcps, scps);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 卖家主动取消
|
|
|
* @param uid
|
|
|
* @param orderCode
|
|
|
* @return
|
|
|
*/
|
|
|
public int recordAutoCancelIfPresent(int uid, long orderCode){
|
|
|
ChangePriceStatus tcps = ChangePriceStatus.TIME_OUT,
|
|
|
scps = ChangePriceStatus.WAITING_DEAL;
|
|
|
return recordFailIfPresent(uid, orderCode, tcps, scps);
|
|
|
}
|
|
|
|
|
|
|
|
|
private int recordFailIfPresent(int uid, long orderCode, ChangePriceStatus tcps, ChangePriceStatus sourceStatus){
|
|
|
logger.info("record Fail If Present SellerChangePriceRecord,uid {} orderCode {} target status {}",
|
|
|
uid, orderCode, tcps);
|
|
|
if (isChangePriceOrder(orderCode)){
|
|
|
return 0;
|
|
|
}
|
|
|
SellerChangePriceRecord scprc = new SellerChangePriceRecord();
|
|
|
scprc.setOrderCode(orderCode);
|
|
|
scprc.setStatus(tcps.getCode());
|
|
|
scprc.setUpdateTime(DateUtil.getCurrentTimeSecond());
|
|
|
scprc.setRemark(tcps.getDesc());
|
|
|
scprc.setSourceStatus(sourceStatus.getCode());
|
|
|
return sellerChangePriceRecordMapper.updateByOrderCodeNStatus(scprc);
|
|
|
}
|
|
|
|
|
|
public int recordFail(long orderCode, ChangePriceStatus cps,ChangePriceStatus sourceStatus){
|
|
|
SellerChangePriceRecord scprc = new SellerChangePriceRecord();
|
|
|
scprc.setOrderCode(orderCode);
|
|
|
scprc.setStatus(cps.getCode());
|
|
|
scprc.setUpdateTime(DateUtil.getCurrentTimeSecond());
|
|
|
scprc.setRemark(cps.getDesc());
|
|
|
scprc.setSourceStatus(sourceStatus.getCode());
|
|
|
return sellerChangePriceRecordMapper.updateByOrderCodeNStatus(scprc);
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|