...
|
...
|
@@ -8,13 +8,13 @@ import com.yohoufo.common.ApiResponse; |
|
|
import com.yohoufo.common.caller.UfoServiceCaller;
|
|
|
import com.yohoufo.dal.order.*;
|
|
|
import com.yohoufo.dal.order.model.*;
|
|
|
import com.yohoufo.order.common.ClientType;
|
|
|
import com.yohoufo.order.common.OrderStatus;
|
|
|
import com.yohoufo.order.common.SellerOrderStatus;
|
|
|
import com.yohoufo.order.common.SkupStatus;
|
|
|
import com.yohoufo.order.constants.MetaKey;
|
|
|
import com.yohoufo.order.model.dto.OrderBuilder;
|
|
|
import com.yohoufo.order.service.IOrdeCreationService;
|
|
|
import com.yohoufo.product.controller.ProductController;
|
|
|
import com.yohoufo.order.service.ISubmitOrderService;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
...
|
...
|
@@ -22,146 +22,165 @@ import org.springframework.stereotype.Service; |
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
@Service
|
|
|
public class OrdeCreationServiceImpl implements IOrdeCreationService {
|
|
|
public class SubmitOrderServiceImpl implements ISubmitOrderService {
|
|
|
|
|
|
private final Logger logger = LoggerFactory.getLogger(OrdeCreationServiceImpl.class);
|
|
|
private final Logger logger = LoggerFactory.getLogger(SubmitOrderServiceImpl.class);
|
|
|
|
|
|
@Autowired
|
|
|
SellerOrderGoodsMapper sellerOrderGoodsMapper;
|
|
|
OrderCreateService orderCreateService;
|
|
|
|
|
|
@Autowired
|
|
|
SellerOrderMapper sellerOrderMapper;
|
|
|
private UfoServiceCaller serviceCaller;
|
|
|
|
|
|
@Autowired
|
|
|
BuyerOrderMapper buyerOrderMapper;
|
|
|
|
|
|
@Autowired
|
|
|
BuyerOrderGoodsMapper buyerOrderGoodsMapper;
|
|
|
public void doSumbitOrder(OrderBuilder orderBuilder){
|
|
|
|
|
|
@Autowired
|
|
|
BuyerOrderMetaMapper buyerOrderMetaMapper;
|
|
|
// 减库存
|
|
|
descreaseStorage(orderBuilder);
|
|
|
|
|
|
@Autowired
|
|
|
private UfoServiceCaller serviceCaller;
|
|
|
// 创建订单
|
|
|
orderCreateService.createOrder(orderBuilder);
|
|
|
}
|
|
|
|
|
|
public void descreaseStorage(OrderBuilder orderBuilder){
|
|
|
|
|
|
/**
|
|
|
* 创建订单
|
|
|
* @param orderBuilder
|
|
|
*/
|
|
|
@Transactional
|
|
|
public void create(OrderBuilder orderBuilder){
|
|
|
|
|
|
boolean increaseNeed = false;
|
|
|
try{
|
|
|
|
|
|
// 扣减库存
|
|
|
// 入口参数:skup, product_id
|
|
|
ApiResponse apiResponse = serviceCaller.call("ufo.product.saleSkup",
|
|
|
ApiResponse.class, orderBuilder.getProductId(), orderBuilder.getSkup());
|
|
|
// 减库存失败
|
|
|
if (apiResponse.getCode() != 200){
|
|
|
logger.warn("submit create order, decrease storage faile, orderCode is {}, uid is {}, skup is {}, productId is {}",
|
|
|
orderBuilder.getOrderCode(), orderBuilder.getUid(), orderBuilder.getSkup(), orderBuilder.getProductId());
|
|
|
throw new ServiceException(ServiceError.ORDER_SKUP_CANNOT_SELL);
|
|
|
}
|
|
|
ApiResponse apiResponse = serviceCaller.call("ufo.product.saleSkup",
|
|
|
ApiResponse.class, orderBuilder.getProductId(), orderBuilder.getSkup());
|
|
|
// 减库存失败
|
|
|
if (apiResponse.getCode() != 200){
|
|
|
logger.warn("submit create order, decrease storage faile, orderCode is {}, uid is {}, skup is {}, productId is {}",
|
|
|
orderBuilder.getOrderCode(), orderBuilder.getUid(), orderBuilder.getSkup(), orderBuilder.getProductId());
|
|
|
throw new ServiceException(ServiceError.ORDER_SKUP_CANNOT_SELL);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Service
|
|
|
public static class OrderCreateService{
|
|
|
|
|
|
@Autowired
|
|
|
SellerOrderGoodsMapper sellerOrderGoodsMapper;
|
|
|
|
|
|
@Autowired
|
|
|
SellerOrderMapper sellerOrderMapper;
|
|
|
|
|
|
@Autowired
|
|
|
BuyerOrderMapper buyerOrderMapper;
|
|
|
|
|
|
@Autowired
|
|
|
BuyerOrderGoodsMapper buyerOrderGoodsMapper;
|
|
|
|
|
|
@Autowired
|
|
|
BuyerOrderMetaMapper buyerOrderMetaMapper;
|
|
|
|
|
|
// product处减库存失败
|
|
|
increaseNeed = true;
|
|
|
@Autowired
|
|
|
private UfoServiceCaller serviceCaller;
|
|
|
|
|
|
/**
|
|
|
* 1.更新 表seller_order_goods skup的状态为不可售
|
|
|
* 2.更新 表seller_order 状态为已经售出
|
|
|
* 3.插入订单 表buyer_order
|
|
|
* 4.插入订单商品 buyer_order_goods
|
|
|
* 5.插入收货地址 buyer_order_meta
|
|
|
*/
|
|
|
|
|
|
// 1.表seller_order_goods ==> 不可售
|
|
|
SellerOrderGoods sellerOrderGoods = updSellerOrderGoods(orderBuilder);
|
|
|
/**
|
|
|
* 创建订单
|
|
|
* @param orderBuilder
|
|
|
*/
|
|
|
@Transactional
|
|
|
public void createOrder(OrderBuilder orderBuilder){
|
|
|
|
|
|
// 2.更新 表seller_order 状态 ==> 已售出
|
|
|
SellerOrder sellerOrder = updSellerOrder(orderBuilder);
|
|
|
try{
|
|
|
|
|
|
// 3.插入订单 表buyer_order
|
|
|
insertOrder(orderBuilder, sellerOrder.getUid());
|
|
|
/**
|
|
|
* 1.更新 表seller_order_goods skup的状态为不可售
|
|
|
* 2.更新 表seller_order 状态为已经售出
|
|
|
* 3.插入订单 表buyer_order
|
|
|
* 4.插入订单商品 buyer_order_goods
|
|
|
* 5.插入收货地址 buyer_order_meta
|
|
|
*/
|
|
|
|
|
|
// 4.插入订单商品 buyer_order_goods
|
|
|
insertOrderGoods(orderBuilder, sellerOrderGoods);
|
|
|
// 1.表seller_order_goods ==> 不可售
|
|
|
SellerOrderGoods sellerOrderGoods = updSellerOrderGoods(orderBuilder);
|
|
|
|
|
|
// 5.插入收货地址 buyer_order_meta 买家收货地址
|
|
|
insertOrderMeta(orderBuilder);
|
|
|
// 2.更新 表seller_order 状态 ==> 已售出
|
|
|
SellerOrder sellerOrder = updSellerOrder(orderBuilder);
|
|
|
|
|
|
// 3.插入订单 表buyer_order
|
|
|
insertOrder(orderBuilder, sellerOrder.getUid());
|
|
|
|
|
|
}catch (Exception ex){
|
|
|
// 4.插入订单商品 buyer_order_goods
|
|
|
insertOrderGoods(orderBuilder, sellerOrderGoods);
|
|
|
|
|
|
// 5.插入收货地址 buyer_order_meta 买家收货地址
|
|
|
insertOrderMeta(orderBuilder);
|
|
|
|
|
|
|
|
|
}catch (Exception ex){
|
|
|
|
|
|
if (increaseNeed){
|
|
|
// order处 更新状态失败,需要补库存
|
|
|
ApiResponse apiResponse = serviceCaller.call("ufo.product.cancelSaleSkup",ApiResponse.class, orderBuilder.getSkup());
|
|
|
// 补库存失败,需要报警手动补库存 TODO
|
|
|
if (apiResponse.getCode() != 200){
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
if (!(ex instanceof ServiceException)) {
|
|
|
//上报告警事件
|
|
|
if (!(ex instanceof ServiceException)) {
|
|
|
//上报告警事件
|
|
|
// EventBusPublisher.publishEvent(new SmsAlarmEvent(OrderHanders.submit, "submit", "订单(" + order.getOrderCode() + ")创建失败"));
|
|
|
throw new ServiceException(ServiceError.SHOPPING_SUBMIT_ORDER_FAIL, ex);
|
|
|
throw new ServiceException(ServiceError.SHOPPING_SUBMIT_ORDER_FAIL, ex);
|
|
|
}
|
|
|
throw ex;
|
|
|
}
|
|
|
throw ex;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
private void insertOrderMeta(OrderBuilder orderBuilder) {
|
|
|
BuyerOrderMeta buyerOrderMeta = new BuyerOrderMeta();
|
|
|
buyerOrderMeta.setUid(orderBuilder.getUid());
|
|
|
buyerOrderMeta.setOrderCode(orderBuilder.getOrderCode());
|
|
|
buyerOrderMeta.setMetaKey(MetaKey.BUYER_DELIVERY_ADDRESS);
|
|
|
buyerOrderMeta.setMetaValue(JSONObject.toJSONString(orderBuilder.getAddressInfo()));
|
|
|
buyerOrderMetaMapper.insert(buyerOrderMeta);
|
|
|
}
|
|
|
|
|
|
private void insertOrderMeta(OrderBuilder orderBuilder) {
|
|
|
BuyerOrderMeta buyerOrderMeta = new BuyerOrderMeta();
|
|
|
buyerOrderMeta.setUid(orderBuilder.getUid());
|
|
|
buyerOrderMeta.setOrderCode(orderBuilder.getOrderCode());
|
|
|
buyerOrderMeta.setMetaKey(MetaKey.BUYER_DELIVERY_ADDRESS);
|
|
|
buyerOrderMeta.setMetaValue(JSONObject.toJSONString(orderBuilder.getAddressInfo()));
|
|
|
buyerOrderMetaMapper.insert(buyerOrderMeta);
|
|
|
}
|
|
|
private void insertOrderGoods(OrderBuilder orderBuilder, SellerOrderGoods sellerOrderGoods) {
|
|
|
BuyerOrderGoods buyerOrderGoods = new BuyerOrderGoods();
|
|
|
buyerOrderGoods.setSkup(orderBuilder.getSkup());
|
|
|
buyerOrderGoods.setOrderCode(orderBuilder.getOrderCode());
|
|
|
buyerOrderGoods.setGoodsPrice(sellerOrderGoods.getGoodsPrice());
|
|
|
buyerOrderGoods.setGoodsAmount(sellerOrderGoods.getGoodsPrice());
|
|
|
buyerOrderGoodsMapper.insertSelective(buyerOrderGoods);
|
|
|
}
|
|
|
|
|
|
private void insertOrderGoods(OrderBuilder orderBuilder, SellerOrderGoods sellerOrderGoods) {
|
|
|
BuyerOrderGoods buyerOrderGoods = new BuyerOrderGoods();
|
|
|
buyerOrderGoods.setSkup(orderBuilder.getSkup());
|
|
|
buyerOrderGoods.setOrderCode(orderBuilder.getOrderCode());
|
|
|
buyerOrderGoods.setGoodsPrice(sellerOrderGoods.getGoodsPrice());
|
|
|
buyerOrderGoods.setGoodsAmount(sellerOrderGoods.getGoodsPrice());
|
|
|
buyerOrderGoodsMapper.insertSelective(buyerOrderGoods);
|
|
|
}
|
|
|
private void insertOrder(OrderBuilder orderBuilder, int sellerUid) {
|
|
|
BuyerOrder buyerOrder = new BuyerOrder();
|
|
|
buyerOrder.setUid(orderBuilder.getUid());
|
|
|
buyerOrder.setOrderCode(orderBuilder.getOrderCode());
|
|
|
buyerOrder.setSellerUid(sellerUid);
|
|
|
buyerOrder.setPaymentType((byte) orderBuilder.getPaymentType());
|
|
|
buyerOrder.setAmount(orderBuilder.getAmount());
|
|
|
buyerOrder.setShipFee(orderBuilder.getShipFee());
|
|
|
buyerOrder.setChannelNo(orderBuilder.getChannelNo());
|
|
|
int now = DateUtil.getCurrentTimeSecond();
|
|
|
buyerOrder.setClientType(ClientType.getClientType(orderBuilder.getClientType()));
|
|
|
buyerOrder.setCreateTime(now);
|
|
|
buyerOrder.setUpdateTime(now);
|
|
|
buyerOrder.setStatus((byte) OrderStatus.WAITING_PAY.getCode());
|
|
|
buyerOrderMapper.insert(buyerOrder);
|
|
|
}
|
|
|
|
|
|
private void insertOrder(OrderBuilder orderBuilder, int sellerUid) {
|
|
|
BuyerOrder buyerOrder = new BuyerOrder();
|
|
|
buyerOrder.setUid(orderBuilder.getUid());
|
|
|
buyerOrder.setOrderCode(orderBuilder.getOrderCode());
|
|
|
buyerOrder.setSellerUid(sellerUid);
|
|
|
buyerOrder.setPaymentType((byte) orderBuilder.getPaymentType());
|
|
|
buyerOrder.setAmount(orderBuilder.getAmount());
|
|
|
buyerOrder.setShipFee(orderBuilder.getShipFee());
|
|
|
buyerOrder.setChannelNo(orderBuilder.getChannelNo());
|
|
|
int now = DateUtil.getCurrentTimeSecond();
|
|
|
buyerOrder.setCreateTime(now);
|
|
|
buyerOrder.setUpdateTime(now);
|
|
|
buyerOrder.setStatus((byte) OrderStatus.WAITING_PAY.getCode());
|
|
|
buyerOrderMapper.insert(buyerOrder);
|
|
|
}
|
|
|
private SellerOrder updSellerOrder(OrderBuilder orderBuilder) {
|
|
|
SellerOrder sellerOrder = sellerOrderMapper.selectBySkup(orderBuilder.getSkup());
|
|
|
sellerOrder.setUpdateTime(DateUtil.getCurrentTimeSecond());
|
|
|
sellerOrder.setStatus(SellerOrderStatus.HAS_SELLED.getCode());
|
|
|
sellerOrderMapper.updateByPrimaryKeySelective(sellerOrder);
|
|
|
return sellerOrder;
|
|
|
}
|
|
|
|
|
|
private SellerOrder updSellerOrder(OrderBuilder orderBuilder) {
|
|
|
SellerOrder sellerOrder = sellerOrderMapper.selectBySkup(orderBuilder.getSkup());
|
|
|
sellerOrder.setUpdateTime(DateUtil.getCurrentTimeSecond());
|
|
|
sellerOrder.setStatus(SellerOrderStatus.HAS_SELLED.getCode());
|
|
|
sellerOrderMapper.updateByPrimaryKeySelective(sellerOrder);
|
|
|
return sellerOrder;
|
|
|
private SellerOrderGoods updSellerOrderGoods(OrderBuilder orderBuilder) {
|
|
|
SellerOrderGoods sellerOrderGoods = new SellerOrderGoods();
|
|
|
sellerOrderGoods.setId(orderBuilder.getSkup());
|
|
|
sellerOrderGoods.setStatus(SkupStatus.CAN_NOT_SELL.getCode());
|
|
|
sellerOrderGoodsMapper.updateByPrimaryKeySelective(sellerOrderGoods);
|
|
|
return sellerOrderGoods;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private SellerOrderGoods updSellerOrderGoods(OrderBuilder orderBuilder) {
|
|
|
SellerOrderGoods sellerOrderGoods = new SellerOrderGoods();
|
|
|
sellerOrderGoods.setId(orderBuilder.getSkup());
|
|
|
sellerOrderGoods.setStatus(SkupStatus.CAN_NOT_SELL.getCode());
|
|
|
sellerOrderGoodsMapper.updateByPrimaryKeySelective(sellerOrderGoods);
|
|
|
return sellerOrderGoods;
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|