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.PaySourceEnum;
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("advancePayTypeService")
@Slf4j
public class AdvancePayTypeServiceImpl 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.BALANCE_RECHARGE.getType());
        if(payOrderReqVO.getTradeType()!=null){
            rechargeReqDTO.setTradeType(payOrderReqVO.getTradeType());
        }else{
            rechargeReqDTO.setTradeType(DistributorTradeTypeEnum.BALANCE_TRANS.getType());
        }
        rechargeReqDTO.setOrderId(String.valueOf(tradeOrderPayDO.getOrderId()));
        if(payOrderReqVO.getCanChangePayType() ==null || payOrderReqVO.getCanChangePayType()== YES){
            rechargeReqDTO.setIsFixed(0);
        }else{
            rechargeReqDTO.setIsFixed(1);
        }
        if(ObjectUtils.equalsAny(tradeOrderPayDO.getSellMethod(), SellMethodEnum.OTC.getType(),SellMethodEnum.OTC_MOVE_ORDER.getType()
                , PaySourceEnum.CREDIT_BILL_REPAYMENT.getType(),PaySourceEnum.STOCK_DIRECT_ORDER.getType()) && payOrderReqVO.getStoreId()!=null){
            rechargeReqDTO.setStoreId(payOrderReqVO.getStoreId());
        }
        if(ObjectUtil.equal(tradeOrderPayDO.getSellMethod(),PaySourceEnum.STOCK_DIRECT_ORDER.getType())){
            rechargeReqDTO.setTradeType(DistributorTradeTypeEnum.STOCK_DIRECT_TRANS.getType());
        }
        CommonResult<DistributorRechargeRespDTO> result = otaDistributorApi.tradeRecharge(rechargeReqDTO);
        DistributorRechargeRespDTO respDTO = result.getCheckedData();
        if(respDTO != null){
            //说明不用扣款
            if(ObjectUtil.equal(respDTO.getRechargeType(),DistributorTradeTypeEnum.CREDIT_TRANS.getType())){
                //如果返回的是授信方式，需要更换支付方式
                tradeOrderPayDO.setPaymentType(PayTypeEnum.CREDIT_GRANTING.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.BALANCE_RECHARGE.getType());//余额
        rechargeReqDTO.setTradeType(DistributorTradeTypeEnum.BALANCE_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());
            }
        }
        if(ObjectUtil.equal(refundDO.getSellMethod(),PaySourceEnum.STOCK_DIRECT_ORDER.getType())){
            rechargeReqDTO.setStoreId(refundDO.getStoreId());
            rechargeReqDTO.setTradeType(DistributorTradeTypeEnum.STOCK_DIRECT_REFUND.getType());
        }else if(ObjectUtil.equal(refundDO.getSellMethod(),PaySourceEnum.CREDIT_BILL_REPAYMENT.getType())){
            rechargeReqDTO.setStoreId(refundDO.getStoreId());
            rechargeReqDTO.setTradeType(DistributorTradeTypeEnum.BALANCE_REFUND.getType());
        }else if(ObjectUtil.equal(refundDO.getSellMethod(),PaySourceEnum.GUIDE_REFUND_PLAT_CHARGEBACK.getType())){
            rechargeReqDTO.setStoreId(refundDO.getStoreId());
            rechargeReqDTO.setTradeType(DistributorTradeTypeEnum.GUIDE_REFUND_BALANCE_TRANS_CANCEL.getType());
        }
        CommonResult<DistributorRechargeRespDTO> result = otaDistributorApi.tradeRecharge(rechargeReqDTO);
        DistributorRechargeRespDTO respDTO = result.getCheckedData();
        return respDTO==null?0L:respDTO.getTradeId();
    }
}
