package org.springblade.modules.api.service;

import org.springblade.common.enums.GoodsStateEnum;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.modules.api.request.CartSubmitOrderRequest;
import org.springblade.modules.api.request.SubmitOrderRequest;
import org.springblade.modules.finance.entity.Order;
import org.springblade.modules.finance.entity.OrderDetail;
import org.springblade.modules.finance.entity.OrderGoods;
import org.springblade.modules.mall.entity.GoodsInfo;
import org.springblade.modules.mall.entity.GoodsSpec;
import org.springblade.modules.platform.entity.UserAddress;

/**
 * Author pangqijun
 * Date 2023/5/29
 * Description
 */
public interface SubmitOrderService {

    /**
     * 下单
     * @param request
     * @return
     */
    String submitOrder(SubmitOrderRequest request);

    /**
     * 购物车下单
     * @return
     */
    default String cartSubmitOrder(CartSubmitOrderRequest request) {
        return null;
    }

    /**
     * 支付成功后处理操作，默认不处理，各实现类可自行实现覆盖
     * @param order
     */
    default void paySuccessAfter(Order order) {}

    /**
     * 构建订单明细实体
     */
    default OrderDetail buildOrderDetail(String remark, UserAddress userAddress) {
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setProvince(userAddress.getProvince());
        orderDetail.setCity(userAddress.getCity());
        orderDetail.setDistrict(userAddress.getDistrict());
        orderDetail.setStreet(userAddress.getStreet());
        orderDetail.setAddress(userAddress.getDetail());
        orderDetail.setRemark(remark);
        return orderDetail;
    }

    /**
     * 校验商品状态
     */
    default void checkGoodsState(GoodsInfo goodsInfo) {
        if (goodsInfo == null) {
            throw new ServiceException("商品不存在！");
        }
        if (GoodsStateEnum.UNDERCARRIAGE.getValue().equals(goodsInfo.getIsSale())) {
            throw new ServiceException("商品[" + goodsInfo.getGoodsName() + "]已下架！");
        }
    }

    /**
     * 校验规格状态
     */
    default void checkGoodsSpecState(GoodsSpec goodsSpec) {
        if (goodsSpec == null) {
            throw new ServiceException("规格不存在！");
        }
        if (GoodsStateEnum.UNDERCARRIAGE.getValue().equals(goodsSpec.getIsSale())) {
            throw new ServiceException("规格[" + goodsSpec.getSpceName() + "]已下架！");
        }
        if (goodsSpec.getStock() < 1) {
            throw new ServiceException("规格[" + goodsSpec.getSpceName() + "]库存不足！");
        }
    }

    /**
     * 创建 OrderGoods
     * @param goodsInfo
     * @param goodsSpec
     * @param goodsNum
     * @param orderNo
     */
    default OrderGoods buildOrderGoods(GoodsInfo goodsInfo, GoodsSpec goodsSpec, Integer goodsNum, String orderNo) {
        OrderGoods orderGoods = new OrderGoods();
        orderGoods.setAgreementPrice(goodsSpec.getAgreementPrice());
        orderGoods.setSalePrice(goodsSpec.getSalePrice());
        orderGoods.setBusinessType(goodsInfo.getBusinessType());
        orderGoods.setGoodsImg(goodsInfo.getGoodsCover());
        orderGoods.setGoodsId(goodsInfo.getId());
        orderGoods.setGoodsPrice(goodsSpec.getSalePrice().doubleValue());
        orderGoods.setGoodsSpecNames(goodsSpec.getSpceName());
        orderGoods.setGoodsNum(goodsNum);
        orderGoods.setOutTradeNo(orderNo);
        orderGoods.setGoodsName(goodsInfo.getGoodsName());
        orderGoods.setGoodsSpecIds(goodsSpec.getId().toString());
        orderGoods.setGoodsType(goodsInfo.getZoneType());
        return orderGoods;
    }
}
