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.taxservices.cn";


    @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","915106002051041291");
        param.put("taxType","0");
        System.out.println(url);
        ResponseContent ret = HttpHelper.postJsonEntity(url,JSON.toJSONString(param));
        System.out.println(ret.getContent());
    }

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

            String taxno = "915106002051041291";
            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","02");
            functionParam.put("optType","2");//0注册 1-启用
            functionParam.put("validType","0");
            functionParam.put("bussinessType","1");
            functionParam.put("keyType","2");
            functionParam.put("initMonth","1");
            functionParam.put("invSpec","0");
            userList.add(functionParam);

            param.put("functionList", userList);

            System.out.println(JSON.toJSONString(param));
            String ret = HttpHelper.postJsonEntity(url,JSON.toJSONString(param)).getContent();
            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","06");
            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","2");
            userList.add(functionParam);

            param.put("functionList", userList);

            System.out.println(JSON.toJSONString(param));
            ResponseContent ret = HttpHelper.postJsonEntity(url,JSON.toJSONString(param));
            System.out.println(ret.getContent("UTF-8"));
        }

    }

    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;
    }
}
