|
|
package com.yohoufo.order.service.impl;
|
|
|
|
|
|
import com.sun.xml.internal.ws.api.message.AddressingUtils;
|
|
|
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.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.OrderCodeType;
|
|
|
import com.yohoufo.order.common.SkupStatus;
|
|
|
import com.yohoufo.order.constants.OrderConstant;
|
|
|
import com.yohoufo.order.model.dto.OrderBuilder;
|
|
|
import com.yohoufo.order.request.ShoppingRequest;
|
|
|
import com.yohoufo.order.response.OrderSubmitResponse;
|
|
|
import com.yohoufo.order.response.ShoppingPaymentResponse;
|
|
|
import com.yohoufo.order.service.IOrdeCreationService;
|
|
|
import com.yohoufo.order.common.OrderListType;
|
|
|
import com.yohoufo.order.request.OrderListRequest;
|
|
|
import com.yohoufo.order.response.OrderInfo;
|
|
|
import com.yohoufo.order.service.IBuyerOrderService;
|
|
|
import com.yohoufo.order.service.support.ShoppingSupport;
|
|
|
import com.yohoufo.order.service.support.codegenerator.OrderCodeGenerator;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
|
public class BuyerOrderServiceImpl implements IBuyerOrderService {
|
|
|
|
|
|
private final Logger logger = LoggerFactory.getLogger(getClass());
|
|
|
|
|
|
@Autowired
|
|
|
ShoppingSupport shoppingSupport;
|
|
|
BuyerOrderMapper buyerOrderMapper;
|
|
|
|
|
|
@Autowired
|
|
|
SellerOrderGoodsMapper sellerOrderGoodsMapper;
|
|
|
BuyerOrderGoodsMapper buyerOrderGoodsMapper;
|
|
|
|
|
|
@Autowired
|
|
|
OrderCodeGenerator orderCodeGenerator;
|
|
|
SellerOrderGoodsMapper sellerOrderGoodsMapper;
|
|
|
|
|
|
@Autowired
|
|
|
IOrdeCreationService ordeCreationService;
|
|
|
|
|
|
@Autowired
|
|
|
ServiceCaller serviceCaller;
|
|
|
|
|
|
/**
|
|
|
* 结算页数据
|
|
|
* @param request
|
|
|
* @return
|
|
|
*/
|
|
|
public ShoppingPaymentResponse payment(ShoppingRequest request){
|
|
|
|
|
|
// 入口参数检查
|
|
|
if (request.getSkup() < 0 || request.getUid() <0){
|
|
|
logger.warn("payment uid or skup is null");
|
|
|
public void getOrderList(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);
|
|
|
}
|
|
|
|
|
|
// 检查 商品sku是否可售
|
|
|
SellerOrderGoods skupGood = checkSkupSellOrNot(request.getSkup());
|
|
|
request.setPage(request.getPage() > 0 ? request.getPage() : 1);
|
|
|
request.setLimit( request.getLimit() > 0 ? request.getLimit() : 10 );
|
|
|
|
|
|
ShoppingPaymentResponse response = new ShoppingPaymentResponse();
|
|
|
response.setPaymentWay(shoppingSupport.getPaymentWay());
|
|
|
response.setDeliveryWay(shoppingSupport.getDeliverWay());
|
|
|
response.setGoodsList(Arrays.asList(shoppingSupport.getGoodsInfo(skupGood)));
|
|
|
response.setPromotionFormulaList(shoppingSupport.getPromotionFormula(skupGood));
|
|
|
|
|
|
// TODO 赔偿金计算
|
|
|
// response.setDamagesDesc(shoppingSupport.getDamagesDesc(null));
|
|
|
int total = buyerOrderMapper.selectCntByUidStatus(request.getUid(), orderListType.getStatus());
|
|
|
|
|
|
return response;
|
|
|
}
|
|
|
int limit = request.getLimit();
|
|
|
int offset = (request.getPage() - 1) * limit;
|
|
|
|
|
|
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);
|
|
|
}
|
|
|
List<BuyerOrder> buyerOrderList = buyerOrderMapper.selectListByUidStatus(request.getUid(), orderListType.getStatus(),offset, limit);
|
|
|
|
|
|
// 不可售
|
|
|
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){
|
|
|
|
|
|
// 入口参数检查
|
|
|
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<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()));
|
|
|
|
|
|
// 检查地址是否合法
|
|
|
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);
|
|
|
}
|
|
|
|
|
|
// 检查skup是否可售
|
|
|
SellerOrderGoods skup = checkSkupSellOrNot(shoppingRequest.getSkup());
|
|
|
buyerOrderList.stream().forEach(buyerOrder -> {
|
|
|
OrderInfo orderInfo = new OrderInfo();
|
|
|
orderInfo.setOrderCode(buyerOrder.getOrderCode());
|
|
|
orderInfo.setRealPrice(buyerOrder.getAmount() == null ? "" : buyerOrder.getAmount().toPlainString());
|
|
|
|
|
|
// 生成订单号
|
|
|
long orderCode = orderCodeGenerator.generate(OrderCodeType.BUYER_TYPE);
|
|
|
BuyerOrderGoods buyerOrderGoods = buyerOrderGoodsMap.get(buyerOrder.getOrderCode());
|
|
|
if (buyerOrderGoods == null){
|
|
|
return;
|
|
|
}
|
|
|
orderInfo.setSkup(buyerOrderGoods.getSkup());
|
|
|
|
|
|
UserAddressRspBO addressInfo = getAddressInfo(shoppingRequest, addressId);
|
|
|
SellerOrderGoods sellerOrderGoods = sellerOrderGoodsMap.get(buyerOrderGoods.getSkup());
|
|
|
orderInfo.setProductName(sellerOrderGoods.getProductName());
|
|
|
orderInfo.setImageUrl(sellerOrderGoods.getImageUrl());
|
|
|
orderInfo.setColorName(sellerOrderGoods.getColorName());
|
|
|
orderInfo.setSizeName(sellerOrderGoods.getSizeName());
|
|
|
|
|
|
// 减库存,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();
|
|
|
// 订单状态显示
|
|
|
|
|
|
ordeCreationService.create(orderBuilder);
|
|
|
// 按钮显示
|
|
|
|
|
|
|
|
|
// 返回结果
|
|
|
return OrderSubmitResponse.builder().orderCode(orderCode).paymentStatus(OrderConstant.N_STR).build();
|
|
|
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取用户信息
|
|
|
* @param shoppingRequest
|
|
|
* @param addressId
|
|
|
* @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;
|
|
|
}
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
} |
...
|
...
|
|