package com.yxcheng.file.web.controller.test;

import com.alibaba.fastjson.JSON;
import com.google.api.client.util.Lists;
import com.yxcheng.token.utils.*;
import org.junit.Test;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserManageTest {

    String accessKeyID = "echangcheng";
    static String accessKeySecret = "echangcheng#@rrwea2";

    static String host = "https://api.xfapiao.com";


    @Test
    public void testGetTaxInfo() throws Exception {
        String url = host + "/invoicefile/getTaxInfo";
        String signStr = "AccessKeyID=%s&SignatureNonce=%s&TimeStamp=%s&Version=1.0";

        String signatureNonce = UUIDGenerator.generate();
        long time = System.currentTimeMillis();

        String timeStamp = DateUtils.formatDate(time,DateUtils.yyyymmddthhmmssz);

        signStr = String.format(signStr,accessKeyID,signatureNonce, timeStamp);

        Map param = new HashMap<>();
        param.put("accessKeyID",accessKeyID);
        param.put("signatureNonce",signatureNonce);
        param.put("timeStamp",timeStamp);
        param.put("version","1.0");
        param.put("signature",getSign(signStr));
        param.put("taxno","91330110MA28U72K75");
        param.put("taxType","1");
        System.out.println(url);
        String ret = HttpHelper.postWithJson(url,JSON.toJSONString(param));
        System.out.println(ret);
    }

    @Test
    public void testTaxManage() throws Exception{
        String url = host + "/invoicefile/taxManage";
        String signStr = "AccessKeyID=%s&SignatureNonce=%s&TimeStamp=%s&Version=1.0";

            String taxno = "91310104698836108Q";
            taxno = taxno.trim();
            String signatureNonce = UUIDGenerator.generate();
            long time = System.currentTimeMillis();

            String timeStamp = DateUtils.formatDate(time,DateUtils.yyyymmddthhmmssz);

            String psignStr = String.format(signStr,accessKeyID,signatureNonce, timeStamp);

            Map param = new HashMap<>();
            param.put("accessKeyID",accessKeyID);
            param.put("signatureNonce",signatureNonce);
            param.put("timeStamp",timeStamp);
            param.put("version","1.0");
            param.put("zoneCode",ZoneUtil.getDqByTaxno(taxno));
            param.put("signature",getSign(psignStr));
            param.put("taxno",taxno);
            param.put("userType","1");
            param.put("userStatus","1");
//            param.put("secert",Base64Util.encode(secert));

            List<Object> userList = Lists.newArrayList();
            Map functionParam = new HashMap<>();
            functionParam.put("functionNo","05");
            functionParam.put("optType","0");//0注册 1-启用
            functionParam.put("validType","0");
            functionParam.put("bussinessType","0");
            functionParam.put("keyType","2");
            functionParam.put("initMonth","3");
            functionParam.put("invSpec","0");
            userList.add(functionParam);

            param.put("functionList", userList);

            System.out.println(JSON.toJSONString(param));
            String ret = HttpHelper.postWithJson(url,JSON.toJSONString(param));
            System.out.println(ret);

    }

    @Test
    public void testBatchTaxManage() throws Exception{
        String url = host + "/invoicefile/taxManage";
        String signStr = "AccessKeyID=%s&SignatureNonce=%s&TimeStamp=%s&Version=1.0";

        List<String> a = Lists.newArrayList();

        a.addAll(FileUtils.readLines(new File("D:\\te11.txt")));
        int i=0;
        for (String userInfo:a) {
            String taxno = "";
            String secert = "";
            if(userInfo.contains("==")) {
                taxno = userInfo.split("==")[0];
                secert  = userInfo.split("==")[1].trim();
            } else {
                taxno = userInfo;
            }
            i++;
            System.out.println("========" + i + "===========" + taxno);
            taxno = taxno.trim();
            String signatureNonce = UUIDGenerator.generate();
            long time = System.currentTimeMillis();

            String timeStamp = DateUtils.formatDate(time,DateUtils.yyyymmddthhmmssz);

            String psignStr = String.format(signStr,accessKeyID,signatureNonce, timeStamp);

            Map param = new HashMap<>();
            param.put("accessKeyID",accessKeyID);
            param.put("signatureNonce",signatureNonce);
            param.put("timeStamp",timeStamp);
            param.put("version","1.0");
            param.put("zoneCode",ZoneUtil.getDqByTaxno(taxno));
            param.put("signature",getSign(psignStr));
            param.put("taxno",taxno);
            param.put("userType","1");
            param.put("userStatus","1");
//            param.put("secert",Base64Util.encode(secert));

            List<Object> userList = Lists.newArrayList();
            Map functionParam = new HashMap<>();
            functionParam.put("functionNo","05");
            functionParam.put("optType","0");//0注册 1-启用
            functionParam.put("validType","0");
            functionParam.put("bussinessType","0");
            functionParam.put("keyType","2");
            functionParam.put("initMonth","3");
            functionParam.put("invSpec","0");
            userList.add(functionParam);

            param.put("functionList", userList);

            System.out.println(JSON.toJSONString(param));
            String ret = HttpHelper.postWithJson(url,JSON.toJSONString(param));
            System.out.println(ret);
        }

    }

    public static String getSign(String data){
        try {
            return Security.getSignature(data.getBytes("UTF-8"),accessKeySecret.getBytes("UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
