|
|
package com.yohoufo.order.service.impl;
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
import com.yoho.core.rest.client.ServiceCaller;
|
|
|
import com.yoho.error.ServiceError;
|
|
|
import com.yoho.error.exception.ServiceException;
|
|
|
import com.yoho.service.model.request.UserAddressReqBO;
|
|
|
import com.yoho.service.model.response.UserAddressRspBO;
|
|
|
import com.yohoufo.common.utils.AddressUtil;
|
|
|
import com.yohoufo.common.utils.DateUtil;
|
|
|
import com.yohoufo.dal.order.BuyerOrderGoodsMapper;
|
|
|
import com.yohoufo.dal.order.BuyerOrderMapper;
|
|
|
import com.yohoufo.dal.order.SellerOrderGoodsMapper;
|
|
|
import com.yohoufo.dal.order.model.BuyerOrder;
|
|
|
import com.yohoufo.dal.order.model.BuyerOrderGoods;
|
|
|
import com.yohoufo.dal.order.model.SellerOrderGoods;
|
|
|
import com.yohoufo.order.common.OrderListType;
|
|
|
import com.yohoufo.order.common.OrderStatus;
|
|
|
import com.yohoufo.order.request.OrderListRequest;
|
|
|
import com.yohoufo.order.response.OrderInfo;
|
|
|
import com.yohoufo.order.response.OrderInfoListRsp;
|
|
|
import com.yohoufo.order.service.IBuyerOrderService;
|
|
|
import com.yohoufo.order.common.OrderCodeType;
|
|
|
import com.yohoufo.order.common.SkupStatus;
|
|
|
import com.yohoufo.order.constants.OrderConstant;
|
...
|
...
|
@@ -24,7 +33,10 @@ import org.slf4j.Logger; |
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.concurrent.TimeUnit;
|
...
|
...
|
@@ -35,141 +47,133 @@ public class BuyerOrderServiceImpl implements IBuyerOrderService { |
|
|
private final Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
|
|
|
@Autowired
|
|
|
ShoppingSupport shoppingSupport;
|
|
|
|
|
|
@Autowired
|
|
|
SellerOrderGoodsMapper sellerOrderGoodsMapper;
|
|
|
BuyerOrderMapper buyerOrderMapper;
|
|
|
|
|
|
@Autowired
|
|
|
OrderCodeGenerator orderCodeGenerator;
|
|
|
BuyerOrderGoodsMapper buyerOrderGoodsMapper;
|
|
|
|
|
|
@Autowired
|
|
|
IOrdeCreationService ordeCreationService;
|
|
|
SellerOrderGoodsMapper sellerOrderGoodsMapper;
|
|
|
|
|
|
@Autowired
|
|
|
ServiceCaller serviceCaller;
|
|
|
|
|
|
/**
|
|
|
* 结算页数据
|
|
|
* 订单列表
|
|
|
* @param request
|
|
|
* @return
|
|
|
*/
|
|
|
public ShoppingPaymentResponse payment(ShoppingRequest request){
|
|
|
public OrderInfoListRsp getOrderList(OrderListRequest request){
|
|
|
|
|
|
// 入口参数检查
|
|
|
if (request.getSkup() < 0 || request.getUid() <0){
|
|
|
logger.warn("payment uid or skup is null");
|
|
|
throw new ServiceException(ServiceError.ORDER_REQUEST_PARM_IS_EMPTY);
|
|
|
}
|
|
|
// check and init请求参数
|
|
|
List<Integer> statusQuery = initOrderListRequest(request);
|
|
|
|
|
|
// 检查 商品sku是否可售
|
|
|
SellerOrderGoods skupGood = checkSkupSellOrNot(request.getSkup());
|
|
|
int total = buyerOrderMapper.selectCntByUidStatus(request.getUid(), statusQuery);
|
|
|
|
|
|
ShoppingPaymentResponse response = new ShoppingPaymentResponse();
|
|
|
response.setPaymentWay(shoppingSupport.getPaymentWay());
|
|
|
response.setDeliveryWay(shoppingSupport.getDeliverWay());
|
|
|
response.setGoodsList(Arrays.asList(shoppingSupport.getGoodsInfo(skupGood)));
|
|
|
response.setPromotionFormulaList(shoppingSupport.getPromotionFormula(skupGood));
|
|
|
int limit = request.getLimit();
|
|
|
int offset = (request.getPage() - 1) * limit;
|
|
|
|
|
|
// TODO 赔偿金计算
|
|
|
// response.setDamagesDesc(shoppingSupport.getDamagesDesc(null));
|
|
|
List<BuyerOrder> buyerOrderList = buyerOrderMapper.selectListByUidStatus(request.getUid(), statusQuery,offset, limit);
|
|
|
|
|
|
return response;
|
|
|
}
|
|
|
|
|
|
private SellerOrderGoods checkSkupSellOrNot(int skup) {
|
|
|
// skup是否是可售状态
|
|
|
SellerOrderGoods skupGood = sellerOrderGoodsMapper.selectByPrimaryKey(skup);
|
|
|
if (skupGood == null){
|
|
|
logger.warn("payment skup not exist, skup is {}", skup);
|
|
|
throw new ServiceException(ServiceError.ORDER_SKUP_CANNOT_SELL);
|
|
|
}
|
|
|
|
|
|
// 不可售
|
|
|
if (skupGood.getStatus() == null
|
|
|
|| skupGood.getStatus().intValue() == SkupStatus.CAN_NOT_SELL.getCode()){
|
|
|
logger.warn("payment skup status, skup is {}", skup);
|
|
|
throw new ServiceException(ServiceError.ORDER_SKUP_CANNOT_SELL);
|
|
|
}
|
|
|
return skupGood;
|
|
|
}
|
|
|
List<Long> orderCodeList = buyerOrderList.stream().map(BuyerOrder::getOrderCode).collect(Collectors.toList());
|
|
|
|
|
|
// buyer_order_goods ===> skup+商品成交价
|
|
|
List<BuyerOrderGoods> buyerOrderGoodsList = buyerOrderGoodsMapper.selectByOrderCode(orderCodeList);
|
|
|
Map<Long, BuyerOrderGoods> buyerOrderGoodsMap = buyerOrderGoodsList.stream().collect(Collectors.toMap(BuyerOrderGoods::getOrderCode, Function.identity()));
|
|
|
|
|
|
/**
|
|
|
* 提交订单
|
|
|
* @param shoppingRequest
|
|
|
* @return
|
|
|
*/
|
|
|
public OrderSubmitResponse submit(ShoppingRequest shoppingRequest){
|
|
|
List<Integer> skups = buyerOrderGoodsList.stream().map(BuyerOrderGoods::getSkup).collect(Collectors.toList());
|
|
|
// seller_order_goods ===> 商品信息
|
|
|
List<SellerOrderGoods> sellerOrderGoodsList = sellerOrderGoodsMapper.selectBySkups(skups);
|
|
|
Map<Integer, SellerOrderGoods> sellerOrderGoodsMap = sellerOrderGoodsList.stream().collect(Collectors.toMap(SellerOrderGoods::getId, Function.identity()));
|
|
|
|
|
|
// 入口参数检查
|
|
|
if (shoppingRequest.getSkup() < 0
|
|
|
|| shoppingRequest.getUid() < 0
|
|
|
|| StringUtils.isEmpty(shoppingRequest.getAddressId())){
|
|
|
logger.warn("submit param is null");
|
|
|
throw new ServiceException(ServiceError.ORDER_REQUEST_PARM_IS_EMPTY);
|
|
|
}
|
|
|
List<OrderInfo> data = Lists.newArrayList();
|
|
|
|
|
|
// 检查地址是否合法
|
|
|
int addressId = AddressUtil.getDecryptStr(shoppingRequest.getAddressId());
|
|
|
if (addressId < 0){
|
|
|
logger.warn("submit addressId invalidate, uid is {}, skup is {}, addressId is {}", shoppingRequest.getUid(),
|
|
|
shoppingRequest.getSkup(), shoppingRequest.getAddressId());
|
|
|
throw new ServiceException(ServiceError.ORDER_ADDRESSID_INVALIDATE);
|
|
|
}
|
|
|
buyerOrderList.stream().forEach(buyerOrder -> {
|
|
|
OrderInfo orderInfo = convertOrderInfo(buyerOrderGoodsMap, sellerOrderGoodsMap, buyerOrder);
|
|
|
if (orderInfo == null) return;
|
|
|
|
|
|
// 检查skup是否可售
|
|
|
SellerOrderGoods skup = checkSkupSellOrNot(shoppingRequest.getSkup());
|
|
|
data.add(orderInfo);
|
|
|
|
|
|
// 生成订单号
|
|
|
long orderCode = orderCodeGenerator.generate(OrderCodeType.BUYER_TYPE);
|
|
|
});
|
|
|
|
|
|
UserAddressRspBO addressInfo = getAddressInfo(shoppingRequest, addressId);
|
|
|
OrderInfoListRsp orderInfoListRsp = OrderInfoListRsp.builder()
|
|
|
.data(data)
|
|
|
.currPage(request.getPage())
|
|
|
.limit(limit)
|
|
|
.total(total)
|
|
|
.currTotal(data.size())
|
|
|
.pageSize((total % limit == 0) ? (total / limit) : (total / limit + 1)).build();
|
|
|
|
|
|
// 减库存,skup更新成不可售,入库
|
|
|
BigDecimal shipFee = new BigDecimal(OrderConstant.DELIVERY_WAY_SF_COST);
|
|
|
OrderBuilder orderBuilder = OrderBuilder.builder()
|
|
|
.uid(shoppingRequest.getUid())
|
|
|
.skup(shoppingRequest.getSkup())
|
|
|
.orderCode(orderCode)
|
|
|
.productId(skup.getProductId())
|
|
|
.paymentType(OrderConstant.PAYMENT_ONLINE)
|
|
|
.deliverWay(OrderConstant.DELIVERY_WAY_SF)
|
|
|
.channelNo(shoppingRequest.getChannelNo())
|
|
|
.amount(skup.getGoodsPrice().add(shipFee))
|
|
|
.shipFee(shipFee)
|
|
|
.addressInfo(addressInfo)
|
|
|
.build();
|
|
|
return orderInfoListRsp;
|
|
|
|
|
|
ordeCreationService.create(orderBuilder);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取返回结果
|
|
|
* @param buyerOrderGoodsMap
|
|
|
* @param sellerOrderGoodsMap
|
|
|
* @param buyerOrder
|
|
|
* @return
|
|
|
*/
|
|
|
private OrderInfo convertOrderInfo(Map<Long, BuyerOrderGoods> buyerOrderGoodsMap,
|
|
|
Map<Integer, SellerOrderGoods> sellerOrderGoodsMap,
|
|
|
BuyerOrder buyerOrder) {
|
|
|
|
|
|
/**
|
|
|
* 1.查询 buyer_order
|
|
|
* 2.查询 buyer_order_goods ==> skup
|
|
|
* 3.查询 seller_order_goods
|
|
|
*/
|
|
|
OrderInfo orderInfo = new OrderInfo();
|
|
|
orderInfo.setOrderCode(buyerOrder.getOrderCode());
|
|
|
orderInfo.setRealPrice(buyerOrder.getAmount() == null ? "" : buyerOrder.getAmount().toPlainString());
|
|
|
// 订单中状态显示
|
|
|
orderInfo.setStatus(buyerOrder.getStatus() == null ? -1 : buyerOrder.getStatus());
|
|
|
OrderStatus orderStatus = OrderStatus.getOrderStatus(orderInfo.getStatus());
|
|
|
orderInfo.setStatuStr(orderStatus == null ? "" : orderStatus.getListStatusStr());
|
|
|
|
|
|
// 按钮显示
|
|
|
orderInfo.setButtons(orderStatus == null ? Lists.newArrayList() : orderStatus.getButtonList());
|
|
|
|
|
|
// 当订单状态是待付款, 显示leftTime
|
|
|
if (orderInfo.getStatus() == OrderStatus.WAITING_PAY.getCode()){
|
|
|
orderInfo.setLeftTime(DateUtil.getCurrentTimeSecond() - buyerOrder.getCreateTime());
|
|
|
}
|
|
|
|
|
|
// 返回结果
|
|
|
return OrderSubmitResponse.builder().orderCode(orderCode).paymentStatus(OrderConstant.N_STR).build();
|
|
|
BuyerOrderGoods buyerOrderGoods = buyerOrderGoodsMap.get(buyerOrder.getOrderCode());
|
|
|
if (buyerOrderGoods == null){
|
|
|
return null;
|
|
|
}
|
|
|
orderInfo.setSkup(buyerOrderGoods.getSkup());
|
|
|
|
|
|
SellerOrderGoods sellerOrderGoods = sellerOrderGoodsMap.get(buyerOrderGoods.getSkup());
|
|
|
if (sellerOrderGoods == null){
|
|
|
return null;
|
|
|
}
|
|
|
orderInfo.setProductName(sellerOrderGoods.getProductName());
|
|
|
orderInfo.setImageUrl(sellerOrderGoods.getImageUrl());
|
|
|
orderInfo.setColorName(sellerOrderGoods.getColorName());
|
|
|
orderInfo.setSizeName(sellerOrderGoods.getSizeName());
|
|
|
return orderInfo;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取用户信息
|
|
|
* @param shoppingRequest
|
|
|
* @param addressId
|
|
|
* 根据type获取 指定的状态
|
|
|
* @param request
|
|
|
* @return
|
|
|
*/
|
|
|
private UserAddressRspBO getAddressInfo(ShoppingRequest shoppingRequest, int addressId) {
|
|
|
// 收货地址信息, 入口参数:uid, address_id
|
|
|
UserAddressReqBO userAddressReqBO = new UserAddressReqBO();
|
|
|
userAddressReqBO.setUid(shoppingRequest.getUid());
|
|
|
userAddressReqBO.setId(addressId);
|
|
|
|
|
|
//超时,使用默认地址,后续需要手动补充地址
|
|
|
UserAddressRspBO defaultResponse = new UserAddressRspBO();
|
|
|
defaultResponse.setAreaCode(OrderConstant.EMPTY_ADDRESS_CODE);
|
|
|
defaultResponse.setId(String.valueOf(addressId));
|
|
|
UserAddressRspBO userAddressBO = serviceCaller.asyncCall("users.getAddress", userAddressReqBO,
|
|
|
UserAddressRspBO.class, defaultResponse).get(500, TimeUnit.MILLISECONDS);
|
|
|
|
|
|
return userAddressBO;
|
|
|
}
|
|
|
private List<Integer> initOrderListRequest(OrderListRequest request) {
|
|
|
if (request.getUid() < 0){
|
|
|
logger.warn("getOrderList uid is {}", request.getUid());
|
|
|
throw new ServiceException(ServiceError.ORDER_UID_IS_EMPTY);
|
|
|
}
|
|
|
|
|
|
OrderListType orderListType = OrderListType.getOrderListType(request.getType());
|
|
|
if (orderListType == null){
|
|
|
logger.warn("getOrderList type invalidate, uid is {}, type is {}",request.getUid(), request.getType());
|
|
|
throw new ServiceException(ServiceError.ORDER_REQUEST_PARM_IS_EMPTY);
|
|
|
}
|
|
|
|
|
|
request.setPage(request.getPage() > 0 ? request.getPage() : 1);
|
|
|
request.setLimit( request.getLimit() > 0 ? request.getLimit() : 10 );
|
|
|
return orderListType.getStatusQuery();
|
|
|
}
|
|
|
} |
...
|
...
|
|