package org.tio.sitexxx.service.service.pay.impl.ncount;

import cn.hutool.core.map.MapUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.jfinal.kit.Ret;
import org.tio.sitexxx.service.service.ConfService;
import org.tio.sitexxx.service.service.pay.base.BasePay;
import org.tio.sitexxx.service.service.pay.base.BasePayReq;
import org.tio.sitexxx.service.service.pay.base.BasePayResp;
import org.tio.sitexxx.service.service.pay.base.BaseRespFormatPay;
import org.tio.sitexxx.service.service.pay.utils.ncount.NCountVoUtil;
import org.tio.sitexxx.service.service.pay.utils.ncount.NRequestUtils;
import org.tio.sitexxx.service.service.pay.vo.ncount.base.NCountBaseVo;
import org.tio.sitexxx.service.utils.RetUtils;
import org.tio.sitexxx.service.vo.Const;

/**
 * <pre>
 *    @author : vagrancy
 *    e-mail : 18050829067@163.com
 *    time   : 2025-11-26 11:12
 *    desc   :
 * </pre>
 */
public class PayNCountApi implements BasePay<BasePayReq, BasePayResp> {
    private static Logger log = LoggerFactory.getLogger(PayNCountApi.class);

    private BaseRespFormatPay formatPay = new PayNCountFormatApi();

    @Override
    public BasePayResp updateUser(BasePayReq payQuest, Integer uid) {
        return null;
    }

    @Override
    public BasePayResp getWalletInfo(BasePayReq payQuest, Integer uid) {
        String tranCode = PayNCountConst.TranCode.QUERY_USER_INFO;
        NCountBaseVo baseVo = NCountVoUtil.getBaseVoByTranCode(tranCode, payQuest.getParams(), uid);
        Ret ret = NRequestUtils.nRequest(baseVo);
        BasePayResp resp = NRequestUtils.checkRet(ret);
        if (resp.isOk()) {
            resp.setResp(formatPay.getWalletInfo(resp.getResp(), uid));
        }
        return resp;
    }

    @Override
    public BasePayResp recharge(BasePayReq payQuest, Integer uid) {
        String tranCode = PayNCountConst.TranCode.RECHARGE;
        NCountBaseVo baseVo = NCountVoUtil.getBaseVoByTranCode(tranCode, payQuest.getParams(), uid);
        Ret ret = NRequestUtils.nRequest(baseVo);
        BasePayResp resp = NRequestUtils.checkRet(ret);
        if (resp.isOk()) {
            resp.setResp(formatPay.recharge(resp.getResp(), uid));
        }
        return resp;
    }

    @Override
    public BasePayResp withhold(BasePayReq payQuest, Integer uid) {
        String tranCode = PayNCountConst.TranCode.WITHHOLD;
        payQuest.getParams().put("bizfee", commission(MapUtil.getLong(payQuest.getParams(), "amount")));
        NCountBaseVo baseVo = NCountVoUtil.getBaseVoByTranCode(tranCode, payQuest.getParams(), uid);
        Ret ret = NRequestUtils.nRequest(baseVo);
        BasePayResp resp = NRequestUtils.checkRet(ret);
        if (resp.isOk()) {
            resp.setResp(formatPay.withhold(resp.getResp(), uid));
        }
        return resp;
    }

    @Override
    public BasePayResp transfer(BasePayReq payQuest, Integer uid) {
        String tranCode = PayNCountConst.TranCode.TRANSFER;
        NCountBaseVo baseVo = NCountVoUtil.getBaseVoByTranCode(tranCode, payQuest.getParams(), uid);
        Ret ret = NRequestUtils.nRequest(baseVo);
        BasePayResp resp = NRequestUtils.checkRet(ret);
        if (resp.isOk()) {
            resp.setResp(formatPay.transfer(resp.getResp(), uid));
        }
        return resp;
    }

    @Override
    public BasePayResp withholdQuery(BasePayReq payQuest, Integer uid) {
        String tranCode = PayNCountConst.TranCode.QUERY_TRADE_INFO;
        payQuest.getParams().put("querytype", "PAY");
        NCountBaseVo baseVo = NCountVoUtil.getBaseVoByTranCode(tranCode, payQuest.getParams(), uid);
        Ret ret = NRequestUtils.nRequest(baseVo);
        if (ret.isFail()) {
            BasePayResp resp = BasePayResp.fail(RetUtils.getRetMsg(ret), "-1");
            log.error("系统异常，{}", RetUtils.getRetMsg(ret));
            return resp;
        }
        BasePayResp resp = new BasePayResp();
        resp.setOk(true);
        resp.setResp(formatPay.withholdQuery(RetUtils.getOkTData(ret), uid));
        return resp;
    }

    /*
     * 手续费
     */
    @Override
    public long commission(long amount) {
        if (amount <= 0) {
            return 0;
        }
        Integer rate = ConfService.getInt(Const.ConfMapping.WX_WALLET_WITHHOLD_COMMISSION, 5);
        Integer withholdconst = ConfService.getInt(Const.ConfMapping.WX_WALLET_WITHHOLD_COMMISSION_CONST, 50);
        return amount * rate / 1000 + withholdconst;
    }

    @Override
    public BasePayResp clientToken(BasePayReq payQuest, Integer uid) {
        return null;
    }

    @Override
    public BasePayResp sendRedpacket(BasePayReq payQuest, Integer uid) {
        return null;
    }

    @Override
    public BasePayResp redpacketQuery(BasePayReq payQuest, Integer uid) {
        String tranCode = PayNCountConst.TranCode.QUERY_TRADE_INFO;
        payQuest.getParams().put("querytype", "RECV");
        NCountBaseVo baseVo = NCountVoUtil.getBaseVoByTranCode(tranCode, payQuest.getParams(), uid);
        Ret ret = NRequestUtils.nRequest(baseVo);
        if (ret.isFail()) {
            BasePayResp resp = BasePayResp.fail(RetUtils.getRetMsg(ret), "-1");
            log.error("系统异常，{}", RetUtils.getRetMsg(ret));
            return resp;
        }
        BasePayResp resp = new BasePayResp();
        resp.setOk(true);
        resp.setResp(formatPay.redpacketQuery(RetUtils.getOkTData(ret), uid));
        return resp;
    }

    @Override
    public BasePayResp grabRedpacket(BasePayReq payQuest, Integer uid) {
        return null;
    }
}
