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



import com.yc.ship.framework.common.pojo.CommonResult;
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.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.service.pay.PayTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 已废弃2025-04-01，定向库存支付方式只有微信、支付宝、预付款
 * &#064;Description ：定向库存资金池,主要用于分销商购买定向库存，和回收定向库存
 * &#064;Author ：qsl
 * &#064;Date ：2025/6/4 16:18
 */
@Service("stockDirectFundPayTypeService")
@Slf4j
public class StockDirectFundPayTypeServiceImpl implements PayTypeService {

    @Resource
    private OtaDistributorApi otaDistributorApi;

    @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.STOCK_DIRECT_RECHARGE.getType());
        rechargeReqDTO.setTradeType(DistributorTradeTypeEnum.STOCK_DIRECT_TRANS.getType());
        rechargeReqDTO.setOrderId(String.valueOf(tradeOrderPayDO.getOrderId()));
        rechargeReqDTO.setIsFixed(1);
        CommonResult<DistributorRechargeRespDTO> result = otaDistributorApi.tradeRecharge(rechargeReqDTO);
        DistributorRechargeRespDTO respDTO = result.getCheckedData();
        if(respDTO != null){
            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.STOCK_DIRECT_RECHARGE.getType());
        rechargeReqDTO.setTradeType(DistributorTradeTypeEnum.STOCK_DIRECT_REFUND.getType());
        rechargeReqDTO.setOrderId(String.valueOf(refundDO.getOrderId()));
        CommonResult<DistributorRechargeRespDTO> result = otaDistributorApi.tradeRecharge(rechargeReqDTO);
        DistributorRechargeRespDTO respDTO = result.getCheckedData();
        return respDTO==null?0L:respDTO.getTradeId();
    }
}
