package com.yc.ship.module.trade.service.pay.impl;


import cn.hutool.core.util.ObjectUtil;
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.DistributorRechargeReqDTO;
import com.yc.ship.module.ota.api.dto.DistributorRechargeRespDTO;
import com.yc.ship.module.ota.enums.DistributorTradeTypeEnum;
import com.yc.ship.module.trade.controller.admin.order.vo.order.PayOrderReqVO;
import com.yc.ship.module.trade.dal.dataobject.order.TradeOrderDO;
import com.yc.ship.module.trade.dal.dataobject.order.TradeOrderPayDO;
import com.yc.ship.module.trade.dal.dataobject.refund.RefundDO;
import com.yc.ship.module.trade.dal.dataobject.refund.RefundRecordDO;
import com.yc.ship.module.trade.enums.PayTypeEnum;
import com.yc.ship.module.trade.enums.SellMethodEnum;
import com.yc.ship.module.trade.service.order.TradeOrderRepositoryService;
import com.yc.ship.module.trade.service.pay.PayTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.yc.ship.module.trade.enums.TravelhubDictTypeConstants.YES;


/**
 * &#064;Description ：授信支付
 * &#064;Author ：qsl
 * &#064;Date ：2025/6/4 16:18
 */
@Service("creditPayTypeService")
@Slf4j
public class CreditPayTypeServiceImpl implements PayTypeService {

    @Resource
    private OtaDistributorApi otaDistributorApi;
    @Resource
    private TradeOrderRepositoryService tradeOrderRepositoryService;

    @Override
    public Long deduct(TradeOrderPayDO tradeOrderPayDO, PayOrderReqVO payOrderReqVO) {
        DistributorRechargeReqDTO rechargeReqDTO = new DistributorRechargeReqDTO();
        rechargeReqDTO.setAccountId(payOrderReqVO.getAccountId());
        rechargeReqDTO.setAccountType(payOrderReqVO.getAccountType());
        rechargeReqDTO.setAmount(tradeOrderPayDO.getPayAmount());
        //授信
        rechargeReqDTO.setRechargeType(DistributorTradeTypeEnum.CREDIT_RECHARGE.getType());
        rechargeReqDTO.setTradeType(DistributorTradeTypeEnum.CREDIT_TRANS.getType());
        rechargeReqDTO.setOrderId(String.valueOf(tradeOrderPayDO.getOrderId()));
        if(ObjectUtils.equalsAny(tradeOrderPayDO.getSellMethod(), SellMethodEnum.OTC.getType(),SellMethodEnum.OTC_MOVE_ORDER.getType()) && payOrderReqVO.getStoreId()!=null){
            rechargeReqDTO.setStoreId(payOrderReqVO.getStoreId());
        }
        if(payOrderReqVO.getCanChangePayType() ==null || payOrderReqVO.getCanChangePayType()== YES){
            rechargeReqDTO.setIsFixed(0);
        }else{
            rechargeReqDTO.setIsFixed(1);
        }
        CommonResult<DistributorRechargeRespDTO> result = otaDistributorApi.tradeRecharge(rechargeReqDTO);
        DistributorRechargeRespDTO respDTO = result.getCheckedData();
        if(respDTO != null){
            if(ObjectUtil.equal(respDTO.getRechargeType(),DistributorTradeTypeEnum.BALANCE_TRANS.getType())){
                //如果返回的是预付款方式，需要更换支付方式
                tradeOrderPayDO.setPaymentType(PayTypeEnum.ADVANCE_PAYMENT.getType());
            }
            return respDTO.getTradeId();
        }
       return 0L;
    }

    @Override
    public Long refund(RefundDO refundDO, RefundRecordDO refundRecordDO) {
        DistributorRechargeReqDTO rechargeReqDTO = new DistributorRechargeReqDTO();
        rechargeReqDTO.setAccountId(refundDO.getRefundUser());
//        rechargeReqDTO.setAccountType();不区分类型
        rechargeReqDTO.setAmount(refundRecordDO.getRefundAmount());
        rechargeReqDTO.setRechargeType(DistributorTradeTypeEnum.CREDIT_RECHARGE.getType());//授信
        rechargeReqDTO.setTradeType(DistributorTradeTypeEnum.CREDIT_REFUND.getType());
        rechargeReqDTO.setOrderId(String.valueOf(refundDO.getOrderId()));
        if(ObjectUtils.equalsAny(refundDO.getSellMethod(), SellMethodEnum.OTC.getType(),SellMethodEnum.OTC_MOVE_ORDER.getType())){
            TradeOrderDO tradeOrderDO = tradeOrderRepositoryService.getById(refundDO.getOrderId());
            if(tradeOrderDO!=null){
                rechargeReqDTO.setStoreId(tradeOrderDO.getStoreId());
            }
        }
        CommonResult<DistributorRechargeRespDTO> result = otaDistributorApi.tradeRecharge(rechargeReqDTO);
        DistributorRechargeRespDTO respDTO = result.getCheckedData();
        return respDTO==null?0L:respDTO.getTradeId();
    }
}
