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


import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.util.object.ObjectUtils;
import com.yc.ship.module.ota.api.OtaDistributorApi;
import com.yc.ship.module.ota.api.dto.BillOrderVerifyReqDTO;
import com.yc.ship.module.ota.api.dto.BillOrderVerifyRespDTO;
import com.yc.ship.module.trade.enums.SellMethodEnum;
import com.yc.ship.module.trade.service.bill.BillService;
import com.yc.ship.module.trade.service.order.bo.TradeOrderBO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import static com.yc.ship.framework.common.exception.util.ServiceExceptionUtil.exception0;


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


    @Resource
    private OtaDistributorApi otaDistributorApi;

    @Resource
    private BillService billService;


    @Override
    public void beforeOrderCreate(TradeOrderBO order) {
        if(ObjectUtils.equalsAny(order.getTradeOrderDO().getSellMethod(), SellMethodEnum.OTC.getType(),SellMethodEnum.OTC_MOVE_ORDER.getType())){
            BillOrderVerifyReqDTO billOrderVerifyReqDTO = new BillOrderVerifyReqDTO();
            billOrderVerifyReqDTO.setAgencyId(Long.valueOf(order.getTradeOrderDO().getSourceId()));
            billOrderVerifyReqDTO.setStoreId(order.getTradeOrderDO().getStoreId());
            if(billOrderVerifyReqDTO.getStoreId() == null){
                //门店为空不处理
                return;
            }
            BillOrderVerifyRespDTO result = billService.billOrderVerify(billOrderVerifyReqDTO);
            if (result!=null) {
                // 账单未还款禁止下单
                String msg = result.getType()==2?"门店":"旅行社";
                throw exception0(50_111,"账单："+result.getBillId()+"未还款,"+msg+"禁止下单！");
            }
        }
    }
}
