package cn.iocoder.yudao.module.order.listener;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.constant.Constant;
import cn.iocoder.yudao.module.order.dal.dataobject.orderException.OrderExceptionDO;
import cn.iocoder.yudao.module.order.dal.dataobject.orderItem.OrderItemDO;
import cn.iocoder.yudao.module.order.enums.OrderExceptionEnum;
import cn.iocoder.yudao.module.order.enums.OrderExceptionResult;
import cn.iocoder.yudao.module.order.service.order.OrderBusinessService;
import cn.iocoder.yudao.module.order.service.order.OrderQueryService;
import cn.iocoder.yudao.module.order.service.orderException.OrderExceptionService;
import cn.iocoder.yudao.module.order.vo.orderException.OrderExceptionResultHandlerVo;
import cn.iocoder.yudao.module.product.dal.dataobject.product.ProductPriceDO;
import cn.iocoder.yudao.module.product.vo.productPrice.ClearancePriceEvent;
import cn.iocoder.yudao.module.product.vo.productPrice.HandleData;
import cn.iocoder.yudao.module.product.vo.productPrice.HandleExceptionEvent;
import cn.iocoder.yudao.module.product.vo.productPrice.HandleExceptionEventSync;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.AllArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionPhase;
import org.springframework.transaction.event.TransactionalEventListener;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 未报价异常处理
 * lanbm 2024-05-31 添加注释
 */
@Component("ProductPriceExceptionHandleListener")
@AllArgsConstructor
public class ProductPriceExceptionHandleListener {

    private final OrderQueryService orderQueryService;
    private final OrderExceptionService orderExceptionService;
    private final OrderBusinessService orderBusinessService;

    @EventListener(HandleExceptionEvent.class)
    @Async
    public void asyncReceive(HandleExceptionEvent event) {
        orderOtherFeeEvent(event);
    }

    @EventListener(HandleExceptionEventSync.class)
    public void syncReceive(HandleExceptionEventSync event) {
        orderOtherFeeEvent(event);
    }


    /*
     处理好报价相关的订单的异常 lanbm 2024-05-31
     */
    public void orderOtherFeeEvent(HandleData event) {
        List<ProductPriceDO> priceList = event.getNeedHandleList();

        //ecw_product_price
        //找出符合条件的订单
        for (ProductPriceDO productPriceDO : priceList) {
            //线路ID
            Long lineId = productPriceDO.getWarehouseLineId();
            //渠道ID  ecw_channel
            Long channelId = productPriceDO.getShippingChannelId();
            Long productId = productPriceDO.getProductId();

            if (channelId != null && channelId == 0L) channelId = null;

            //获取订单项信息
            List<OrderItemDO> orderItemList =
                    orderQueryService.getOrderItemByLineIdAndProductId(lineId,
                            productId,
                            channelId);
            if (CollectionUtil.isEmpty(orderItemList)) continue;

            //找出订单的未报价异常
            Set<Long> orderItemIdSet = orderItemList.stream()
                    .map(OrderItemDO::getOrderItemId)
                    .collect(Collectors.toSet());
            //找出这些订单的未处理的未报价异常
            List<OrderExceptionDO> exceptionDOList =
                    orderExceptionService.list(new LambdaQueryWrapper<OrderExceptionDO>()
                    .eq(OrderExceptionDO::getOrderExceptionType,OrderExceptionEnum.ORDER_NO_QUOTE_EXCEPTION.getKey())
                    .in(OrderExceptionDO::getOrderItemId, orderItemIdSet)
                    .ne(OrderExceptionDO::getOrderExceptionStatus, 2)
            );
            if (CollectionUtil.isNotEmpty(exceptionDOList)) {
                for (OrderExceptionDO orderExceptionDO : exceptionDOList) {
                    //修改这些订单的未报价异常为已处理
//                    OrderExceptionResultHandlerVo orderExceptionResultHandlerVo = new OrderExceptionResultHandlerVo();
//                    orderExceptionResultHandlerVo.setOrderExceptionId(orderExceptionDO.getId());
//                    orderExceptionResultHandlerVo.setOrderExceptionHandlerResult(OrderExceptionResult.ORDER_NO_QUOTE_EXCEPTION_RESULT.PROCESSED);
//                    orderExceptionResultHandlerVo.setHandlerId(Constant.SYSTEM_USER_ADMIN_ID); //admin
//                    orderExceptionResultHandlerVo.setOrderExceptionHandlerRemark("系统自动处理");
//                    orderExceptionService.handlerExceptionByExceptionId(orderExceptionResultHandlerVo);
                    orderBusinessService.handleOrderNoQuote(
                            orderExceptionDO.getOrderId());
                }
            }

            //只要有价格就处理清关费未报价异常
//            if (productPriceDO.getPriceType() == 0) {
                //找出这些订单的未处理的未报价清关费异常
                List<OrderExceptionDO> clearanceExceptionDOList = orderExceptionService.list(new LambdaQueryWrapper<OrderExceptionDO>()
                        .eq(OrderExceptionDO::getOrderExceptionType, OrderExceptionEnum.CUSTOMS_FEE_NOT_QUOTE_EXCEPTION.getKey())
                        .in(OrderExceptionDO::getOrderItemId, orderItemIdSet)
                        .ne(OrderExceptionDO::getOrderExceptionStatus, 2)
                );
                for (OrderExceptionDO orderExceptionDO : clearanceExceptionDOList) {
                    //修改这些订单的未报价异常为已处理
//                    OrderExceptionResultHandlerVo orderExceptionResultHandlerVo = new OrderExceptionResultHandlerVo();
//                    orderExceptionResultHandlerVo.setOrderExceptionId(orderExceptionDO.getId());
//                    orderExceptionResultHandlerVo.setOrderExceptionHandlerResult(OrderExceptionResult.CUSTOMS_FEE_NOT_QUOTE_EXCEPTION_RESULT.PROCESSED);
//                    orderExceptionResultHandlerVo.setHandlerId(Constant.SYSTEM_USER_ADMIN_ID); //admin
//                    orderExceptionResultHandlerVo.setOrderExceptionHandlerRemark("系统自动处理");
//                    orderExceptionService.handlerExceptionByExceptionId(orderExceptionResultHandlerVo);
                    orderBusinessService.handleOrderNoQuote(
                            orderExceptionDO.getOrderId());
                }
//            }


        }

    }

    @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT, classes = ClearancePriceEvent.class)
    @Async
    public void clearancePriceEvent(ClearancePriceEvent event) {
        orderExceptionService.customsFeeCallback(event.getProductId());
    }
}
