package com.keao.edu.thirdparty.adapay;

import com.alibaba.fastjson.JSON;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yingyong.wang
 */
public class SettleAccount extends ConfigInit {

    /**
     * 运行结算账户类接口
     *
     * @throws Exception 异常
     */
    public static void executeSettleAccountTest(String merchantKey, String app_id, String member_id) throws Exception {
        SettleAccount demo = new SettleAccount();
        // 创建结算账户
        Map<String, Object> settlecount = demo.executeCreateSettleAccount(merchantKey, app_id, member_id);
        String settleCount_id = (String) settlecount.get("id");


        // 查询结算账户
        demo.executeQuerySettleAccount(merchantKey, settleCount_id, app_id, member_id);

        // 查询结算账户明细列表
        demo.executeQuerySettleDetails(merchantKey, app_id, member_id, settleCount_id);
        // 删除结算账户
        demo.executeDeleteSettleAccount(merchantKey, settleCount_id, app_id, member_id);

        member_id = "user_test_10001";
        settleCount_id = "0023056905335360";

        demo.executeModifySettleAccount(merchantKey, settleCount_id, app_id, member_id);

        demo.executeDrawCash(merchantKey, app_id, member_id);

        demo.executeQueryBalance(merchantKey, app_id, member_id, settleCount_id);
    }

    /**
     * 运行查询结算明细列表接口
     *
     * @throws Exception 异常
     */
//    public static void executeQuerySettleDetailTest(String merchantKey, String appId, String memberId, String settleAccountId, String beginDate, String endDate) throws Exception {
//        SettleAccountDemo demo = new SettleAccountDemo();
//        demo.executeQuerySettleDetails(merchantKey, appId, memberId, settleAccountId, beginDate, endDate);
//    }


    /**
     * 创建 settleCount
     *
     * @return 创建的settleCount 对象
     * @throws Exception 异常
     */
    public Map<String, Object> executeCreateSettleAccount(String merchantKey, String app_id, String member_id) throws Exception {
        System.out.println("=======execute Create SettleAccount begin=======");
        Map<String, Object> settleCountParams = new HashMap<String, Object>(2);
        Map<String, Object> accountInfo = new HashMap<String, Object>(2);
        accountInfo.put("card_id", "6222021703001692221");
        accountInfo.put("card_name", "袁电茜");
        accountInfo.put("cert_id", "310109200006062491");
        accountInfo.put("cert_type", "00");
        accountInfo.put("tel_no", "18888888881");
        accountInfo.put("bank_code", "03060000");
        accountInfo.put("bank_acct_type", "1");
        accountInfo.put("prov_code", "0031");
        accountInfo.put("area_code", "3100");
        settleCountParams.put("member_id", member_id);
        settleCountParams.put("app_id", app_id);
        settleCountParams.put("channel", "bank_account");
        settleCountParams.put("account_info", accountInfo);
        System.out.println("创建结算账户，请求参数：" + JSON.toJSONString(settleCountParams));
        Map<String, Object> settleCount = com.huifu.adapay.model.SettleAccount.create(settleCountParams, merchantKey);
        System.out.println("创建结算账户，返回参数：" + JSON.toJSONString(settleCount));
        System.out.println("=======execute Create SettleAccount end=======");

        return settleCount;

    }

    /**
     * 查询 SettleAccount
     *
     * @param settleCount_id 待查询的settleCount_id
     * @param app_id         app_id
     * @return 查询的settleCount 对象
     * @throws Exception 异常
     */
    public Map<String, Object> executeQuerySettleAccount(String merchantKey, String settleCount_id, String app_id, String member_id) throws Exception {
        System.out.println("=======execute query SettleAccount begin=======");
        Map<String, Object> settleCountParams = new HashMap<String, Object>(2);
        settleCountParams.put("settle_account_id", settleCount_id);
        settleCountParams.put("member_id", member_id);
        settleCountParams.put("app_id", app_id);
        System.out.println("查询结算账户，请求参数：" + JSON.toJSONString(settleCountParams));
        Map<String, Object> settleCount = com.huifu.adapay.model.SettleAccount.query(settleCountParams, merchantKey);
        System.out.println("查询结算账户，返回参数：" + JSON.toJSONString(settleCount));
        System.out.println("=======execute query SettleAccount end=======");

        return settleCount;
    }

    /**
     * 删除 SettleAccount
     *
     * @param settleCount_id 待删除的settleCount_id
     * @param app_id         app_id
     * @return delete的settleCount 对象
     * @throws Exception 异常
     */
    public Map<String, Object> executeDeleteSettleAccount(String merchantKey, String settleCount_id, String app_id, String member_id) throws Exception {
        System.out.println("=======execute delete SettleAccount begin=======");
        Map<String, Object> settleCountParams = new HashMap<String, Object>(2);
        settleCountParams.put("settle_account_id", settleCount_id);
        settleCountParams.put("member_id", member_id);
        settleCountParams.put("app_id", app_id);
        System.out.println("删除结算账户，请求参数：" + JSON.toJSONString(settleCountParams));
        Map<String, Object> settleCount = com.huifu.adapay.model.SettleAccount.delete(settleCountParams, merchantKey);
        System.out.println("删除结算账户，返回参数：" + JSON.toJSONString(settleCount));
        System.out.println("=======execute delete SettleAccount end=======");

        return settleCount;
    }

    /**
     * 查询结算明细列表
     *
     * @param merchantKey
     * @param app_id          app_id
     * @param member_id       待查询的member_id
     * @param settleAccountId 待查询的settleAccountId
     * @return
     * @throws Exception 异常
     */
    public Map<String, Object> executeQuerySettleDetails(String merchantKey, String app_id, String member_id,
                                                         String settleAccountId) throws Exception {
        System.out.println("=======execute query settle details begin=======");
        Map<String, Object> querySettleDetailParams = new HashMap<String, Object>(2);
        querySettleDetailParams.put("app_id", app_id);
        querySettleDetailParams.put("member_id", member_id);
        querySettleDetailParams.put("settle_account_id", settleAccountId);
        querySettleDetailParams.put("begin_date", "20191008");
        querySettleDetailParams.put("end_date", "20191010");
        System.out.println("查询结算明细列表，请求参数：" + JSON.toJSONString(querySettleDetailParams));
        Map<String, Object> settleCount = com.huifu.adapay.model.SettleAccount.querySettleDetails(querySettleDetailParams, merchantKey);
        System.out.println("查询结算明细列表，返回参数：" + JSON.toJSONString(settleCount));
        System.out.println("=======execute query settle details end=======");

        return settleCount;
    }


    /**
     * 运行结算账户类接口
     *
     * @throws Exception 异常
     */
    public static void executeSettleAccountTest(String app_id, String member_id) throws Exception {
        SettleAccount demo = new SettleAccount();
        // 创建结算账户
        Map<String, Object> settlecount = demo.executeCreateSettleAccount(app_id, member_id);
        String settleCount_id = (String) settlecount.get("id");
        // 查询结算账户
        demo.executeQuerySettleAccount(settleCount_id, app_id, member_id);

        // 查询结算账户明细列表
        demo.executeQuerySettleDetails(app_id, member_id, settleCount_id);
        // 删除结算账户
        demo.executeDeleteSettleAccount(settleCount_id, app_id, member_id);
        member_id = "user_test_10001";
        settleCount_id = "0023056905335360";

        demo.executeModifySettleAccount(settleCount_id, app_id, member_id);

    }

    /**
     * 运行查询结算明细列表接口
     *
     * @throws Exception 异常
     */
//    public static void executeQuerySettleDetailTest( String appId, String memberId, String settleAccountId, String beginDate, String endDate) throws Exception {
//        SettleAccountDemo demo = new SettleAccountDemo();
//        demo.executeQuerySettleDetails( appId, memberId, settleAccountId, beginDate, endDate);
//    }


    /**
     * 创建 settleCount
     *
     * @return 创建的settleCount 对象
     * @throws Exception 异常
     */
    public Map<String, Object> executeCreateSettleAccount(String app_id, String member_id) throws Exception {
        System.out.println("=======execute Create SettleAccount begin=======");
        Map<String, Object> settleCountParams = new HashMap<String, Object>(2);
        Map<String, Object> accountInfo = new HashMap<String, Object>(2);
        accountInfo.put("card_id", "6222021703001692221");
        accountInfo.put("card_name", "袁电茜");
        accountInfo.put("cert_id", "310109200006062491");
        accountInfo.put("cert_type", "00");
        accountInfo.put("tel_no", "18888888881");
        accountInfo.put("bank_code", "03060000");
        accountInfo.put("bank_acct_type", "1");
        accountInfo.put("prov_code", "0031");
        accountInfo.put("area_code", "3100");
        settleCountParams.put("member_id", member_id);
        settleCountParams.put("app_id", app_id);
        settleCountParams.put("channel", "bank_account");
        settleCountParams.put("account_info", accountInfo);
        System.out.println("创建结算账户，请求参数：" + JSON.toJSONString(settleCountParams));
        Map<String, Object> settleCount = com.huifu.adapay.model.SettleAccount.create(settleCountParams);
        System.out.println("创建结算账户，返回参数：" + JSON.toJSONString(settleCount));
        System.out.println("=======execute Create SettleAccount end=======");

        return settleCount;

    }

    /**
     * 查询 SettleAccount
     *
     * @param settleCount_id 待查询的settleCount_id
     * @param app_id         app_id
     * @return 查询的settleCount 对象
     * @throws Exception 异常
     */
    public Map<String, Object> executeQuerySettleAccount(String settleCount_id, String app_id, String member_id) throws Exception {
        System.out.println("=======execute query SettleAccount begin=======");
        Map<String, Object> settleCountParams = new HashMap<String, Object>(2);
        settleCountParams.put("settle_account_id", settleCount_id);
        settleCountParams.put("member_id", member_id);
        settleCountParams.put("app_id", app_id);
        System.out.println("查询结算账户，请求参数：" + JSON.toJSONString(settleCountParams));
        Map<String, Object> settleCount = com.huifu.adapay.model.SettleAccount.query(settleCountParams);
        System.out.println("查询结算账户，返回参数：" + JSON.toJSONString(settleCount));
        System.out.println("=======execute query SettleAccount end=======");

        return settleCount;
    }

    /**
     * 修改 SettleAccount
     *
     * @param settleCount_id 待修改的settleCount_id
     * @param app_id         app_id
     * @return 修改的settleCount 对象
     * @throws Exception 异常
     */
    public Map<String, Object> executeModifySettleAccount(String settleCount_id, String app_id, String member_id) throws Exception {
        System.out.println("=======execute modify SettleAccount begin=======");
        Map<String, Object> settleCountParams = new HashMap<String, Object>(2);
        settleCountParams.put("settle_account_id", settleCount_id);
        settleCountParams.put("member_id", member_id);
        settleCountParams.put("app_id", app_id);

        settleCountParams.put("min_amt", "0.10");
        settleCountParams.put("remained_amt", "0.10");
        System.out.println("修改结算账户，请求参数：" + JSON.toJSONString(settleCountParams));
        Map<String, Object> settleCount = com.huifu.adapay.model.SettleAccount.modify(settleCountParams);
        System.out.println("修改结算账户，返回参数：" + JSON.toJSONString(settleCount));
        System.out.println("=======execute modify SettleAccount end=======");

        return settleCount;
    }

    /**
     * 修改 SettleAccount
     *
     * @param settleCount_id 待修改的settleCount_id
     * @param app_id         app_id
     * @return 修改的settleCount 对象
     * @throws Exception 异常
     */
    public Map<String, Object> executeModifySettleAccount(String merchantKey, String settleCount_id, String app_id, String member_id) throws Exception {
        System.out.println("=======execute modify SettleAccount begin=======");
        Map<String, Object> settleCountParams = new HashMap<String, Object>(2);
        settleCountParams.put("settle_account_id", settleCount_id);
        settleCountParams.put("member_id", member_id);
        settleCountParams.put("app_id", app_id);
        settleCountParams.put("min_amt", "");
        settleCountParams.put("remained_amt", "");

        System.out.println("修改结算账户，请求参数：" + JSON.toJSONString(settleCountParams));
        Map<String, Object> settleCount = com.huifu.adapay.model.SettleAccount.modify(settleCountParams, merchantKey);
        System.out.println("修改结算账户，返回参数：" + JSON.toJSONString(settleCount));
        System.out.println("=======execute modify SettleAccount end=======");

        return settleCount;
    }


    /**
     * 删除 SettleAccount
     *
     * @param settleCount_id 待删除的settleCount_id
     * @param app_id         app_id
     * @return delete的settleCount 对象
     * @throws Exception 异常
     */
    public Map<String, Object> executeDeleteSettleAccount(String settleCount_id, String app_id, String member_id) throws Exception {
        System.out.println("=======execute delete SettleAccount begin=======");
        Map<String, Object> settleCountParams = new HashMap<String, Object>(2);
        settleCountParams.put("settle_account_id", settleCount_id);
        settleCountParams.put("member_id", member_id);
        settleCountParams.put("app_id", app_id);
        System.out.println("删除结算账户，请求参数：" + JSON.toJSONString(settleCountParams));
        Map<String, Object> settleCount = com.huifu.adapay.model.SettleAccount.delete(settleCountParams);
        System.out.println("删除结算账户，返回参数：" + JSON.toJSONString(settleCount));
        System.out.println("=======execute delete SettleAccount end=======");

        return settleCount;
    }

    /**
     * 查询结算明细列表
     *
     * @param app_id          app_id
     * @param member_id       待查询的member_id
     * @param settleAccountId 待查询的settleAccountId
     * @return
     * @throws Exception 异常
     */
    public Map<String, Object> executeQuerySettleDetails(String app_id, String member_id,
                                                         String settleAccountId) throws Exception {
        System.out.println("=======execute query settle details begin=======");
        Map<String, Object> querySettleDetailParams = new HashMap<String, Object>(2);
        querySettleDetailParams.put("app_id", app_id);
        querySettleDetailParams.put("member_id", member_id);
        querySettleDetailParams.put("settle_account_id", settleAccountId);
        querySettleDetailParams.put("begin_date", "20191008");
        querySettleDetailParams.put("end_date", "20191010");
        System.out.println("查询结算明细列表，请求参数：" + JSON.toJSONString(querySettleDetailParams));
        Map<String, Object> settleCount = com.huifu.adapay.model.SettleAccount.querySettleDetails(querySettleDetailParams);
        System.out.println("查询结算明细列表，返回参数：" + JSON.toJSONString(settleCount));
        System.out.println("=======execute query settle details end=======");

        return settleCount;
    }

    /**
     * 取现
     *
     * @param merchantKey
     * @param app_id
     * @param member_id
     * @return
     * @throws Exception
     */
    public static Map<String, Object> executeDrawCash(String merchantKey, String app_id, String member_id) throws Exception {
        System.out.println("=======execute modify SettleAccount begin=======");
        Map<String, Object> settleCountParams = new HashMap<String, Object>();

        settleCountParams.put("order_no", "jsdk_payment_" + System.currentTimeMillis());
        settleCountParams.put("cash_amt", "0.01");
        settleCountParams.put("member_id", "user_00008");
        settleCountParams.put("app_id", app_id);
        // settleCountParams.put("settle_account_id", "0008919797515968");
        settleCountParams.put("cash_type", "T1");
        settleCountParams.put("notify_url", "");

        System.out.println("取现接口，请求参数：" + JSON.toJSONString(settleCountParams));
        Map<String, Object> settleCount = com.huifu.adapay.model.SettleAccount.drawCash(settleCountParams,merchantKey);
        System.out.println("取现接口返回参数" + JSON.toJSONString(settleCount));
        System.out.println("=======execute modify SettleAccount end=======");

        return settleCount;
    }

    /**
     * 查询余额
     *
     * @param merchantKey
     * @param app_id
     * @param member_id
     * @param settleCount_id
     * @return
     * @throws Exception
     */
    public static Map<String, Object> executeQueryBalance(String merchantKey, String app_id, String member_id, String settleCount_id) throws Exception {
        System.out.println("=======execute modify SettleAccount begin=======");
        Map<String, Object> settleCountParams = new HashMap<String, Object>();

        settleCountParams.put("app_id", app_id);
        settleCountParams.put("member_id", "user_00008");
        settleCountParams.put("settle_account_id", "0035172521665088");


        System.out.println("查询余额账户，请求参数：" + JSON.toJSONString(settleCountParams));
        Map<String, Object> settleCount = com.huifu.adapay.model.SettleAccount.queryBalance(settleCountParams,merchantKey);
        System.out.println("查询余额账户，返回参数：" + JSON.toJSONString(settleCount));
        System.out.println("=======execute modify SettleAccount end=======");

        return settleCount;
    }
    /**
     * 取现
     *

     * @param app_id
     * @param member_id
     * @return
     * @throws Exception
     */
    public static Map<String, Object> executeDrawCash(String app_id, String member_id) throws Exception {
        System.out.println("=======execute modify SettleAccount begin=======");
        Map<String, Object> settleCountParams = new HashMap<String, Object>();

        settleCountParams.put("order_no", "jsdk_payment_" + System.currentTimeMillis());
        settleCountParams.put("cash_amt", "0.01");
        settleCountParams.put("member_id", "user_00008");
        settleCountParams.put("app_id", app_id);
        // settleCountParams.put("settle_account_id", "0008919797515968");
        settleCountParams.put("cash_type", "T1");
        settleCountParams.put("notify_url", "");

        System.out.println("取现接口，请求参数：" + JSON.toJSONString(settleCountParams));
        Map<String, Object> settleCount = com.huifu.adapay.model.SettleAccount.drawCash(settleCountParams);
        System.out.println("取现接口返回参数" + JSON.toJSONString(settleCount));
        System.out.println("=======execute modify SettleAccount end=======");

        return settleCount;
    }

    /**
     * 查询余额
     *

     * @param app_id
     * @param member_id
     * @param settleCount_id
     * @return
     * @throws Exception
     */
    public static Map<String, Object> executeQueryBalance( String app_id, String member_id, String settleCount_id) throws Exception {
        System.out.println("=======execute modify SettleAccount begin=======");
        Map<String, Object> settleCountParams = new HashMap<String, Object>();

        settleCountParams.put("app_id", app_id);
        settleCountParams.put("member_id", "user_00008");
        settleCountParams.put("settle_account_id", "0035172521665088");


        System.out.println("查询余额账户，请求参数：" + JSON.toJSONString(settleCountParams));
        Map<String, Object> settleCount = com.huifu.adapay.model.SettleAccount.queryBalance(settleCountParams);
        System.out.println("查询余额账户，返回参数：" + JSON.toJSONString(settleCount));
        System.out.println("=======execute modify SettleAccount end=======");

        return settleCount;
    }

}
