package com.yc.ship.module.trade.service.order.handler;

import com.yc.ship.module.product.api.VoyageApi;
import com.yc.ship.module.product.api.dto.OrderRoomUseDTO;
import com.yc.ship.module.product.api.dto.ReduceStockReqDTO;
import com.yc.ship.module.product.enums.DistributorOrStoreEnum;
import com.yc.ship.module.resource.api.room.RoomApi;
import com.yc.ship.module.trade.controller.admin.order.vo.order.TradeOrderRoomModelVO;
import com.yc.ship.module.trade.dal.dataobject.order.TradeOrderDO;
import com.yc.ship.module.trade.dal.dataobject.order.TradeOrderRoomModelDO;
import com.yc.ship.module.trade.dal.dataobject.order.TradeVisitorDO;
import com.yc.ship.module.trade.dal.mysql.order.TradeOrderRoomModelMapper;
import com.yc.ship.module.trade.dal.mysql.order.TradeVisitorMapper;
import com.yc.ship.module.trade.enums.TradeOrderStatusEnum;
import com.yc.ship.module.trade.service.order.TradeOrderService;
import com.yc.ship.module.trade.service.order.bo.TradeOrderBO;
import com.yc.ship.module.trade.service.order.bo.TradeOrderDetailBO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Component
@Order(TradeOrderHandler.PRE_ORDER_STOCK)
@Slf4j
public class TradeStockOrderHandler implements TradeOrderHandler{

    @Resource
    private VoyageApi voyageApi;

    @Resource
    private TradeOrderService tradeOrderService;

    @Resource
    private TradeVisitorMapper tradeVisitorMapper;

    @Resource
    private TradeOrderRoomModelMapper tradeOrderRoomModelMapper;

    @Override
    public void beforeOrderCreate(TradeOrderBO order) {
        Integer sellMethod = order.getTradeOrderDO().getSellMethod();
        if (sellMethod == 1||sellMethod == 4||sellMethod == 7||sellMethod == 0||sellMethod == 3) {
            reduceStock(order);
        }

    }

    private void reduceStock(TradeOrderBO order) {
        ReduceStockReqDTO reqDTO = new ReduceStockReqDTO();
        TradeOrderDO lastOrder = order.getOldTradeOrderDO();
        if(lastOrder != null){
            reqDTO.setOldOrderId(lastOrder.getId());
        }
        reqDTO.setOrderId(order.getTradeOrderDO().getId());
        reqDTO.setOrderStatus(order.getTradeOrderDO().getOrderStatus());
        //TODO: 当前默认为分销商，扣分销商库存，门店的后续再完善
        reqDTO.setType(DistributorOrStoreEnum.DISTRIBUTOR.getValue());
        reqDTO.setVoyageId(order.getTradeOrderDO().getVoyageId());
        reqDTO.setDistributorId(Long.valueOf(order.getTradeOrderDO().getSourceId()));
        reqDTO.setStoreId(order.getTradeOrderDO().getStoreId());
        reqDTO.setUseRoomNum(order.getSummary().getUseRoomTotalNum());

        List<OrderRoomUseDTO> orderRoomUseDTOList = new ArrayList<>();
        List<TradeOrderRoomModelVO> roomModelList = order.getRoomModelList();
        roomModelList.forEach(roomModelVO -> {
            OrderRoomUseDTO orderRoomUseDTO = new OrderRoomUseDTO();
            orderRoomUseDTO.setRoomId(roomModelVO.getRoomId());
            orderRoomUseDTO.setNum(BigDecimal.valueOf(roomModelVO.getUseRoomNum()));
            orderRoomUseDTO.setRoomModelId(roomModelVO.getRoomModelId());
            orderRoomUseDTO.setFloor(roomModelVO.getFloor());
            orderRoomUseDTOList.add(orderRoomUseDTO);
        });
        reqDTO.setOrderRoomList(orderRoomUseDTOList);
        Integer orderStatus = reqDTO.getOrderStatus();
        if(Objects.equals(orderStatus, TradeOrderStatusEnum.YD.getStatus())) {
            voyageApi.preReduceStock(reqDTO);
        }else {
            if(lastOrder!= null) {
                reqDTO.setOldOrderStatus(lastOrder.getOrderStatus());
            }
            voyageApi.reduceStock(reqDTO);
        }
    }

    /***
     *  修改还原库存
     */
    private void reduceBackStock(TradeOrderBO order){
        ReduceStockReqDTO reqDTO = new ReduceStockReqDTO();
        TradeOrderDO lastOrder = tradeOrderService.getLastOrderByOrderNo(order.getTradeOrderDO().getOrderNo());
        if(lastOrder == null){
            return;
        }
        reqDTO.setOrderId(lastOrder.getId());
        reqDTO.setOrderStatus(order.getTradeOrderDO().getOrderStatus());
        //TODO: 当前默认为分销商，扣分销商库存，门店的后续再完善
        reqDTO.setType(DistributorOrStoreEnum.DISTRIBUTOR.getValue());
        reqDTO.setVoyageId(lastOrder.getVoyageId());
        reqDTO.setDistributorId(Long.valueOf(order.getTradeOrderDO().getSourceId()));
        reqDTO.setStoreId(lastOrder.getStoreId());
        reqDTO.setUseRoomNum(order.getSummary().getUseRoomTotalNum());

        List<OrderRoomUseDTO> orderRoomUseDTOList = new ArrayList<>();

        List<TradeOrderRoomModelDO> roomModelList = tradeOrderRoomModelMapper.selectListByOrderId(lastOrder.getId());
        roomModelList.forEach(roomModelVO -> {
            OrderRoomUseDTO orderRoomUseDTO = new OrderRoomUseDTO();
            orderRoomUseDTO.setNum(BigDecimal.valueOf(roomModelVO.getUseRoomNum()));
            orderRoomUseDTO.setRoomModelId(roomModelVO.getRoomModelId());
            orderRoomUseDTO.setFloor(roomModelVO.getFloor());
            orderRoomUseDTOList.add(orderRoomUseDTO);
        });
        reqDTO.setOrderRoomList(orderRoomUseDTOList);
        Integer orderStatus = reqDTO.getOrderStatus();
        if(Objects.equals(orderStatus, TradeOrderStatusEnum.YD.getStatus())) {
            voyageApi.preCancelReduceStock(reqDTO);
        }else {
            voyageApi.cancelReduceStock(reqDTO);
        }
    }

    @Override
    public void afterOrderCreate(TradeOrderBO order) {
        TradeOrderHandler.super.afterOrderCreate(order);
    }

    @Override
    public void afterOrderModify(TradeOrderBO order) {
        Integer sellMethod = order.getTradeOrderDO().getSellMethod();
        if (sellMethod == 1||sellMethod == 4||sellMethod == 7||sellMethod == 0||sellMethod == 3) {
//            reduceBackStock(order);
        }
    }

    @Override
    public void afterCancelOrder(TradeOrderDO tradeOrderDO) {
        TradeOrderHandler.super.afterCancelOrder(tradeOrderDO);
    }

    @Override
    public void afterOrderCreateFail(TradeOrderBO order) {
        TradeOrderHandler.super.afterOrderCreateFail(order);
    }

    @Override
    public void afterSupplierOrderCreateFail(TradeOrderDO tradeOrderDO) {
        TradeOrderHandler.super.afterSupplierOrderCreateFail(tradeOrderDO);
    }

    @Override
    public void afterProcessSuccess(TradeOrderDO tradeOrderDO) {
        TradeOrderHandler.super.afterProcessSuccess(tradeOrderDO);
    }

    @Override
    public void afterProcessFail(TradeOrderDO tradeOrderDO) {
        TradeOrderHandler.super.afterProcessFail(tradeOrderDO);
    }

    @Override
    public void afterRefundOrder(Long refundId) {
        TradeOrderHandler.super.afterRefundOrder(refundId);
    }

    @Override
    public void afterRefundAmountSuccess(Long orderId, Long refundId, BigDecimal refundAmount) {
        TradeOrderHandler.super.afterRefundAmountSuccess(orderId, refundId, refundAmount);
    }

    @Override
    public void afterCheckSuccess(Long orderId) {
        TradeOrderHandler.super.afterCheckSuccess(orderId);
    }

    @Override
    public void processOrderCreateSuccess(TradeOrderDO tradeOrderDO) {
        TradeOrderHandler.super.processOrderCreateSuccess(tradeOrderDO);
    }
}
