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

import cn.hutool.core.map.MapUtil;
import org.tio.sitexxx.im.server.vo.*;
import org.tio.sitexxx.service.service.ConfService;
import org.tio.sitexxx.service.service.pay.utils.AmountUtil;
import org.tio.sitexxx.service.service.pay.vo.ncount.*;
import org.tio.sitexxx.service.service.pay.vo.ncount.base.NCountBaseVo;
import org.tio.sitexxx.service.vo.Const;
import org.tio.sitexxx.service.vo.PayConst;

import java.util.Map;

/**
 * <pre>
 *    @author : vagrancy
 *    e-mail : 18050829067@163.com
 *    time   : 2025-11-25 10:29
 *    desc   :
 * </pre>
 */
public class NCountVoUtil {
    /**
     * 开户信息查询接口
     */
    public static final String Q001_SUBMIT_URL = "https://ncount.hnapay.com/api/q001.htm";

    /**
     * 交易订单查询
     */
    public static final String Q002_SUBMIT_URL = "https://ncount.hnapay.com/api/q002.htm";

    /**
     * 绑卡
     */
    public static final String	R007_SUBMIT_URL	= "https://ncount.hnapay.com/api/r007.htm";
    /**
     * 绑卡确认接口
     */
    public static final String	R008_SUBMIT_URL	= "https://ncount.hnapay.com/api/r008.htm";

    /**
     * 解绑卡接口
     */
    public static final String R009_SUBMIT_URL = "https://ncount.hnapay.com/api/r009.htm";

    /**
     * 充值接口
     */
    public static final String	T007_SUBMIT_URL	= "https://ncount.hnapay.com/api/t007.htm";
    /**
     * 充值确认接口
     */
    public static final String	T008_SUBMIT_URL	= "https://ncount.hnapay.com/api/t008.htm";

    /**
     * 转账接口
     */
    public static final String T003_SUBMIT_URL = "https://ncount.hnapay.com/api/t003.htm";

    /**
     * 提现
     */
    public static final String T002_SUBMIT_URL = "https://ncount.hnapay.com/api/t002.htm";

    public static NCountBaseVo getBaseVoByTranCode(String tranCode, Map<String, Object> params, Integer uid) {

        NCountBaseVo baseVo = null;
        switch (tranCode) {
            case PayConst.TranCode.BIND_CARD:
                baseVo = getR007CardBindVo(params, uid);
                break;
            case PayConst.TranCode.BIND_CARD_CONFIRM:
                baseVo = getR008CardBindConfirmVo(params, uid);
                break;
            case PayConst.TranCode.UNBIND_CARD:
                baseVo = getR009UnbindVo(params, uid);
                break;
            case PayConst.TranCode.WITHHOLD:
                baseVo = getT002WithholdVo(params, uid);
                break;
            case PayConst.TranCode.TRANSFER:
                baseVo = getT003TransferVo(params, uid);
                break;
            case PayConst.TranCode.RECHARGE:
                baseVo = getT007RechargeVo(params, uid);
                break;
            case PayConst.TranCode.RECHARGE_CONFIRM:
                baseVo = getT008RechargeConfirmVo(params, uid);
                break;
            case PayConst.TranCode.QUERY_USER_INFO:
                baseVo = getQ001UserInfoVo(params, uid);
                break;
            case PayConst.TranCode.QUERY_TRADE_INFO:
                baseVo = getQ002TradeVo(params, uid);
                break;
            default:
                break;
        }
        return baseVo;
    }

    /**
     * 调整-绑卡
     * @param params
     * @param uid
     * @return
     * @author xufei
     * 2021年3月8日 下午5:38:48
     */
    private static NCardBindVo getR007CardBindVo(Map<String, Object> params, Integer uid) {
        BindCardVo bindCardVo = BindCardVo.toBean(params);
        NCardBindVo r007Vo = new NCardBindVo();
        r007Vo.initCommonParams(PayConst.TranCode.BIND_CARD, uid);
        r007Vo.setSubmitUrl(R007_SUBMIT_URL);
        r007Vo.setCardNo(bindCardVo.getBankcardno());
        r007Vo.setHolderName(bindCardVo.getName());
        r007Vo.setCardAvailableDate(bindCardVo.getAvailabledate());
        r007Vo.setCvv2(bindCardVo.getCvv2());
        r007Vo.setMobileNo(bindCardVo.getMobile());
        r007Vo.setIdentityType("1");
        r007Vo.setIdentityCode(bindCardVo.getCardno());
        r007Vo.setMerUserIp(bindCardVo.getIp());
        return r007Vo;
    }

    /**
     * 确认绑卡
     * @param params
     * @param uid
     * @return
     * @author xufei
     * 2021年3月8日 下午5:50:26
     */
    private static NCardBindConfirmVo getR008CardBindConfirmVo(Map<String, Object> params, Integer uid) {
        BindCardConfirmVo bindCardConfirmVo = BindCardConfirmVo.toBean(params);
        NCardBindConfirmVo r008Vo = new NCardBindConfirmVo();
        r008Vo.initCommonParams(PayConst.TranCode.BIND_CARD_CONFIRM, uid);
        r008Vo.setSubmitUrl(R008_SUBMIT_URL);
        r008Vo.setNcountOrderId(bindCardConfirmVo.getMerorderid());
        r008Vo.setSmsCode(bindCardConfirmVo.getSmscode());
        r008Vo.setMerUserIp(bindCardConfirmVo.getIp());
        return r008Vo;
    }

    /**
     * 解绑
     * @return
     * @author xufei
     * 2021年3月8日 下午5:56:20
     */
    private static NCardUnbindVo getR009UnbindVo(Map<String, Object> params, Integer uid) {
        UnBindCardVo cardVo = UnBindCardVo.toBean(params);
        NCardUnbindVo r009Vo = new NCardUnbindVo();
        r009Vo.initCommonParams(PayConst.TranCode.UNBIND_CARD, uid);
        r009Vo.setSubmitUrl(R009_SUBMIT_URL);
        r009Vo.setOriBindCardAgrNo(cardVo.getAgrno());
        return r009Vo;
    }

    /**
     * 提现
     * @param params
     * @param uid
     * @return
     * @author xufei
     * 2021年3月9日 下午1:41:09
     */
    private static NWithholdVoVo getT002WithholdVo(Map<String, Object> params, Integer uid) {
        WithholdVo withholdVo = WithholdVo.toBean(params);
        NWithholdVoVo t002Vo = new NWithholdVoVo();
        t002Vo.initCommonParams(PayConst.TranCode.WITHHOLD, uid);
        t002Vo.setSubmitUrl(T002_SUBMIT_URL);
        t002Vo.setTranAmount(AmountUtil.intAmountToDouble(withholdVo.getAmount()) + "");
        t002Vo.setBindCardAgrNo(withholdVo.getAgrno());
        t002Vo.setNotifyUrl(withholdVo.getNotifyUrl());
        t002Vo.setServiceAmount(AmountUtil.intAmountToDouble(withholdVo.getBizfee() + "") + "");
        t002Vo.setBusinessType("08");
        t002Vo.setCardNo("");
        return t002Vo;
    }

    private static NTransferVo getT003TransferVo(Map<String, Object> params, Integer uid) {
        TransferVo transferVo = TransferVo.toBean(params);
        NTransferVo t003Vo = new NTransferVo();
        t003Vo.initCommonParams(PayConst.TranCode.TRANSFER, uid);
        t003Vo.setSubmitUrl(T003_SUBMIT_URL);
        t003Vo.setTranAmount(AmountUtil.intAmountToDouble(transferVo.getCny()) + "");
        t003Vo.setBusinessType("02");
        return t003Vo;
    }

    /**
     * 支付
     * @param params
     * @param uid
     * @return
     * @author xufei
     * 2021年3月8日 下午6:47:45
     */
    private static NRechargeVo getT007RechargeVo(Map<String, Object> params, Integer uid) {
        RechargeVo rechargeVo = RechargeVo.toBean(params);
        String timeout = ConfService.getShort(Const.ConfMapping.WX_WALLET_RECHARGE_TIMEOUT, (short) 5) + "";
        if (rechargeVo.getTimeout() != null) {
            timeout = rechargeVo.getTimeout() + "";
        }
        NRechargeVo t007Vo = new NRechargeVo();
        t007Vo.initCommonParams(PayConst.TranCode.RECHARGE, uid);
        t007Vo.setSubmitUrl(T007_SUBMIT_URL);
        t007Vo.setTranAmount(AmountUtil.intAmountToDouble(rechargeVo.getAmount()) + "");
        t007Vo.setDivideDetail("");
        t007Vo.setDivideFlag("");
        t007Vo.setSubMerchantId("2012221657332944330");//此值固定不可变
        t007Vo.setGoodsInfo("");
        t007Vo.setRiskExpand("");
        t007Vo.setOrderExpireTime(timeout);
        t007Vo.setNotifyUrl(rechargeVo.getNotifyUrl());
        t007Vo.setFrontUrl("");
        t007Vo.setBindCardAgrNo(rechargeVo.getAgrno());
        t007Vo.setIdentityCode("");
        t007Vo.setIdentityType("");
        t007Vo.setMobileNo("");
        t007Vo.setCvv2("");
        t007Vo.setCardAvailableDate("");
        t007Vo.setHolderName("");
        t007Vo.setCardNo("");
        t007Vo.setPayType("3");//写死协议支付
        return t007Vo;
    }

    private static NRechargeConfirmVo getT008RechargeConfirmVo(Map<String, Object> params, Integer uid) {
        RechargeConfirmVo confirmVo = RechargeConfirmVo.toBean(params);
        NRechargeConfirmVo t008Vo = new NRechargeConfirmVo();
        t008Vo.initCommonParams(PayConst.TranCode.RECHARGE_CONFIRM, uid);
        t008Vo.setSubmitUrl(T008_SUBMIT_URL);
        t008Vo.setNcountOrderId(confirmVo.getMerorderid());
        t008Vo.setSmsCode(confirmVo.getSmscode());
        t008Vo.setBusinessType("03");
        t008Vo.setFeeType("0");//0:客户无感知-无手续费
        t008Vo.setDivideAcctDtl("");
        t008Vo.setFeeAmountUser("");
        return t008Vo;
    }

    // 查询
    private static NUserInfoVo getQ001UserInfoVo(Map<String, Object> params, Integer uid) {
        NUserInfoVo q001Vo = new NUserInfoVo();
        q001Vo.initCommonParams(PayConst.TranCode.QUERY_USER_INFO, uid);
        q001Vo.setUserId(MapUtil.getStr(params, "walletid"));
        q001Vo.setSubmitUrl(Q001_SUBMIT_URL);
        return q001Vo;
    }

    /**
     * 交易查询
     * @param params
     * @param uid
     * @return
     * @author xufei
     * 2021年3月9日 下午3:01:09
     */
    private static NTradeVo getQ002TradeVo(Map<String, Object> params, Integer uid) {
        NTradeVo q002Vo = new NTradeVo();
        q002Vo.initCommonParams(PayConst.TranCode.QUERY_TRADE_INFO, uid);
        q002Vo.setTranMerOrderId(MapUtil.getStr(params, "reqid"));
        q002Vo.setSubmitUrl(Q002_SUBMIT_URL);
        q002Vo.setQueryType(MapUtil.getStr(params, "querytype"));
        return q002Vo;
    }

    public static String getVerifyDataByTranCode(String tranCode, Map<String, Object> responseMap) {

        StringBuffer sb = new StringBuffer();

        String[] verifyFields = getVerifyArrByTranCode(tranCode);

        for (int i = 0; i < verifyFields.length; i++) {
            sb.append(verifyFields[i]);
            sb.append("=[");
            sb.append(responseMap.get(verifyFields[i]) == null ? "" : responseMap.get(verifyFields[i]));
            sb.append("]");
        }

        return sb.toString();

    }

    private static String[] getVerifyArrByTranCode(String tranCode) {
        switch (tranCode) {
            case PayConst.TranCode.OPEN_USER:
                return NUserOpenVo.verifyArr;
            case PayConst.TranCode.BIND_CARD:
                return NCardBindVo.verifyArr;
            case PayConst.TranCode.BIND_CARD_CONFIRM:
                return NCardBindConfirmVo.verifyArr;
            case PayConst.TranCode.UNBIND_CARD:
                return NCardUnbindVo.verifyArr;
            case PayConst.TranCode.WITHHOLD:
                return NWithholdVoVo.verifyArr;
            case PayConst.TranCode.TRANSFER:
                return NTransferVo.verifyArr;
            case PayConst.TranCode.RECHARGE:
                return NRechargeVo.verifyArr;
            case PayConst.TranCode.RECHARGE_CONFIRM:
                return NRechargeConfirmVo.verifyArr;
            case PayConst.TranCode.QUERY_USER_INFO:
                return NUserInfoVo.verifyArr;
            case PayConst.TranCode.QUERY_TRADE_INFO:
                return NTradeVo.verifyArr;
            default:
                return null;
        }
    }
}
