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


import com.yc.ship.module.trade.api.contract.ContractApi;
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.service.order.AdminTradeOrderService;
import com.yc.ship.module.trade.service.order.TradeOrderRepositoryService;
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;



/**
 * &#064;Description 合同处理
 * &#064;Author qsl
 * &#064;Date 2025/8/16 14:49
 */
@Component
@Order(TradeOrderHandler.CONTRACT)
@Slf4j
public class ContractHandler implements TradeOrderHandler{


    @Resource
    private ContractApi contractApi;
    @Resource
    private AdminTradeOrderService adminTradeOrderService;
    @Resource
    private TradeOrderRepositoryService tradeOrderRepositoryService;
    @Resource
    private TradeRefundRepositoryService tradeRefundRepositoryService;

    @Override
    public void afterProcessSuccess(TradeOrderDO tradeOrderDO) {
        try {
            adminTradeOrderService.applyContract(tradeOrderDO);
        }catch (Exception e){
            log.error("签合同异常",e);
        }
    }



    @Override
    public void afterRefundOrder(Long refundId) {
        try {
            RefundDO refundDO = tradeRefundRepositoryService.getById(refundId);
            TradeOrderDO tradeOrderDO = tradeOrderRepositoryService.getById(refundDO.getOrderId());
            if(tradeOrderDO.getIsContract() == null){
                return;
            }
            //先取消
            contractApi.cancelContractByOrder(tradeOrderDO.getId());
            //重签合同
            adminTradeOrderService.applyContract(tradeOrderDO);
        }catch (Exception e){
            log.error("签合同异常",e);
        }
    }

}
