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


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.module.ota.api.OtaDistributorApi;
import com.yc.ship.module.ota.api.dto.DistributorRespDTO;
import com.yc.ship.module.trade.dal.dataobject.order.TradeOrderDO;
import com.yc.ship.module.trade.enums.SellMethodEnum;
import com.yc.ship.module.trade.enums.TradeOrderConfirmTypeEnum;
import com.yc.ship.module.trade.framework.common.ConfigUtils;
import com.yc.ship.module.trade.framework.mq.TradePublishUtils;
import com.yc.ship.module.trade.service.order.TradeOrderBizService;
import com.yc.ship.module.trade.service.order.TradeOrderRepositoryService;
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.impl.TradeOrderTravelService;
import com.yc.ship.module.trade.service.ota.TiktokCalendarService;
import com.yc.ship.module.trade.service.refund.TradeRefundRepositoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;

import static com.yc.ship.module.product.enums.YesOrNoEnum.YES;


/**
 * &#064;Description 通用处理
 * &#064;Author qsl
 * &#064;Date 2025/8/16 14:49
 */
@Component
@Order(TradeOrderHandler.COMMON)
@Slf4j
public class CommonHandler implements TradeOrderHandler{

    @Resource
    private TradeOrderBizService tradeOrderBizService;
    @Resource
    private TradeRefundRepositoryService tradeRefundRepositoryService;
    @Resource
    private TradeOrderRepositoryService tradeOrderRepositoryService;
    @Resource
    private TradePublishUtils tradePublishUtils;
    @Resource
    private ConfigUtils configUtils;
    @Resource
    private TradeOrderTravelService tradeOrderTravelService;
    @Resource
    private OtaDistributorApi otaDistributorApi;
    @Resource
    private TiktokCalendarService tiktokCalendarService;

    @Override
    public void processOrderCreateSuccess(TradeOrderDO order) {
        try {
//            if (ObjectUtil.equals(order.getSellMethod(), SellMethodEnum.OTA.getCate())) {
//                DistributorRespDTO distributorRespDTO = otaDistributorApi.getDistributorById(Long.parseLong(order.getSourceId()));
//                if (distributorRespDTO!=null){
//                    if (ObjectUtil.equals(distributorRespDTO.getTypeCode(),"tiktokCalendar")){
//                        log.error("收到接单成功推送到抖音通知："+order.getExternalOrderNo()+";"+order.getOrderNo());
//                        tiktokCalendarService.orderConfirm(distributorRespDTO, order.getExternalOrderNo(), 1);
//                    }
//                }
//            }
        }catch (Exception e) {
            log.error("抖音推送异常",e);
        }
    }



    @Override
    public void afterOrderCreateFail(TradeOrderBO order) {
//        try {
//            log.error("收到接单失败推送到抖音通知："+order.getTradeOrderDO().getExternalOrderNo());
//            TradeOrderDO tradeOrderDO = order.getTradeOrderDO();
//            if (ObjectUtil.equals(tradeOrderDO.getSellMethod(), SellMethodEnum.OTA.getCate())) {
//                CommonResult<DistributorRespDTO> result = otaDistributorApi.getDistributorById(Long.parseLong(tradeOrderDO.getSourceId()));
//                if (result.isSuccess()){
//                    DistributorRespDTO distributorRespDTO = result.getCheckedData();
//                    if (ObjectUtil.equals(distributorRespDTO.getTypeCode(),"tiktokCalendar")){
//                        tiktokCalendarService.orderConfirm(distributorRespDTO, tradeOrderDO.getExternalOrderNo(), 2);
//                    }
//                }
//
//            }
//        }catch (Exception e) {
//            log.error("抖音推送异常",e);
//        }
    }

    @Override
    public void afterProcessSuccess(TradeOrderDO tradeOrderDO) {
        //旅行社平台金额
//        tradeOrderBizService.updatePlatAmountAfterProcessSuccess(tradeOrderDO);
    }


    @Override
    public void afterRefundAmountSuccess(Long orderId, Long refundId, BigDecimal refundAmount) {
        TradeOrderDO tradeOrderDO = tradeOrderRepositoryService.getById(orderId);
        tradeOrderBizService.updatePlatAmountAfterRefund(tradeOrderDO);

        // 出行状态设置
        try {
            tradeOrderTravelService.updateOrderTravelStatus(orderId);
        } catch (Exception e) {
            log.error("退款出行状态设置", e);
        }

    }
}
