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


import com.yc.ship.module.trade.api.insurance.InsuranceApi;
import com.yc.ship.module.trade.api.insurance.dto.InsuredDTO;
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.TradeOrderRepositoryService;
import com.yc.ship.module.trade.service.order.bo.TradeVisitorBO;
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.util.ArrayList;
import java.util.List;


/**
 * &#064;Description 保险处理
 * &#064;Author qsl
 * &#064;Date 2025/8/16 14:49
 */
@Component
@Order(TradeOrderHandler.INSURANCE)
@Slf4j
public class InsuranceHandler implements TradeOrderHandler{


    @Resource
    private InsuranceApi insuranceApi;
    @Resource
    private TradeOrderRepositoryService tradeOrderRepositoryService;
    @Resource
    private TradeRefundRepositoryService tradeRefundRepositoryService;

    @Override
    public void afterProcessSuccess(TradeOrderDO tradeOrderDO) {
        try {
            //订单任务统一处理
//            log.error("开始处理出票成功投保业务"+tradeOrderDO.getId());
//            insure(tradeOrderDO);
        }catch (Exception e){
            log.error("投保异常",e);
        }
    }
    private void insure(TradeOrderDO tradeOrderDO){
        log.error("投保参数{}",tradeOrderDO.getId());
        boolean res = insuranceApi.applyInsurance(tradeOrderDO.getId()).getCheckedData();
        log.error("投保请求返回{}",res);
        tradeOrderRepositoryService.updateOrderInsureStatus(tradeOrderDO.getId(),res?1:0);
    }
    @Override
    public void afterRefundOrder(Long refundId) {
        try {
            RefundDO refundDO = tradeRefundRepositoryService.getById(refundId);
            TradeOrderDO tradeOrderDO = tradeOrderRepositoryService.getById(refundDO.getOrderId());
            if(tradeOrderDO.getIsInsure() == null){
                return;
            }
            insuranceApi.cancelInsurance(tradeOrderDO.getId());
        }catch (Exception e){
            log.error("再投保异常",e);
        }
    }
}
