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


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yc.ship.module.trade.dal.dataobject.order.TradeOrderBindDO;
import com.yc.ship.module.trade.dal.dataobject.order.TradeOrderDO;
import com.yc.ship.module.trade.dal.dataobject.refund.RefundDO;
import com.yc.ship.module.trade.enums.RefundStatusEnum;
import com.yc.ship.module.trade.enums.TradeOrderBindEnum;
import com.yc.ship.module.trade.service.order.TradeOrderRepositoryService;
import com.yc.ship.module.trade.service.pay.TradeOrderPayService;
import com.yc.ship.module.trade.service.refund.TradeRefundRepositoryService;
import com.yc.ship.module.trade.service.refund.TradeRefundService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import java.time.LocalDateTime;

import static com.yc.ship.module.trade.enums.ApiConstants.ORDER_CHANGE_TYPE_NEW;
import static com.yc.ship.module.trade.enums.ApiConstants.ORDER_CHANGE_TYPE_OLD;


/**
 * &#064;Description 改签处理
 * &#064;Author qsl
 * &#064;Date 2025/8/16 14:49
 */
@Component
@Order(TradeOrderHandler.CHANGE_ORDER)
@Slf4j
public class ChangeOrderHandler implements TradeOrderHandler{

    @Resource
    private TradeOrderRepositoryService tradeOrderRepositoryService;

    @Resource
    private TradeRefundService tradeRefundService;
    @Resource
    private TradeRefundRepositoryService tradeRefundRepositoryService;
    @Resource
    private TradeOrderPayService tradeOrderPayService;

    @Override
    public void afterCancelOrder(TradeOrderDO tradeOrderDO) {
       //取消订单
        try {
            Long orderId = tradeOrderDO.getId();
            tradeOrderPayService.cancelOrder(orderId);
        }catch (Exception e){
            log.error("取消改签订单处理异常",e);
        }
    }

    private void cancelChange(TradeOrderDO tradeOrderDO){
        //如果是改签订单
        boolean isChangeOrderPay = tradeOrderDO.getIsChangeOrder()!=null && tradeOrderDO.getIsChangeOrder() == ORDER_CHANGE_TYPE_NEW;
        if(!isChangeOrderPay){
            return;
        }
        TradeOrderBindDO bindOrder = tradeOrderRepositoryService.getBindOrder(tradeOrderDO.getId(), TradeOrderBindEnum.CHANGE.getType());
        RefundDO changeOrderRefund = tradeRefundRepositoryService.getChangeOrderRefund(bindOrder.getOrderId());
        if(changeOrderRefund != null){
            //原订单还原
            tradeRefundRepositoryService.recoveryStatus(changeOrderRefund);
            //退款单取消
            tradeRefundRepositoryService.updateRefundStatus(changeOrderRefund.getId(), RefundStatusEnum.CANCELED);
        }
        //解除绑定关系
        cancelBind(bindOrder.getOrderId(),tradeOrderDO.getId());
    }

    private void cancelBind(Long orderId,Long bindId){
        TradeOrderBindDO tradeOrderBindDO = new TradeOrderBindDO();
        tradeOrderBindDO.setOrderId(orderId);
        tradeOrderBindDO.setBindId(bindId);
        tradeOrderBindDO.setType(TradeOrderBindEnum.CHANGE.getType());
        tradeOrderRepositoryService.cancelBindOrder(tradeOrderBindDO);
    }

    @Override
    public void afterProcessFail(TradeOrderDO tradeOrderDO) {
        //出票失败
        try {
            cancelChange(tradeOrderDO);
        }catch (Exception e){
            log.error("改签订单出票失败处理异常",e);
        }
    }

    @Override
    public void afterProcessSuccess(TradeOrderDO tradeOrderDO) {
        try {
            //如果是改签订单，原订单发起退款
            boolean isChangeOrderPay = tradeOrderDO.getIsChangeOrder()!=null && tradeOrderDO.getIsChangeOrder() == ORDER_CHANGE_TYPE_NEW;
            if(!isChangeOrderPay){
                return;
            }
            //原订单退款
            TradeOrderBindDO bindOrder = tradeOrderRepositoryService.getBindOrder(tradeOrderDO.getId(), TradeOrderBindEnum.CHANGE.getType());
            RefundDO changeOrderRefund = tradeRefundRepositoryService.getChangeOrderRefund(bindOrder.getOrderId());
            //继续退款
            tradeRefundService.refundByPaymentType(changeOrderRefund.getId());
            tradeOrderRepositoryService.markChangeOrder(bindOrder.getOrderId(),ORDER_CHANGE_TYPE_OLD);
        }catch (Exception e){
            log.error("改签订单出票成功处理异常",e);
        }
    }
}
