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 FileTest {
//    String accessKeyID = "edaxiang";
//    static String accessKeySecret = "edaxiang#@f26ads";

//        String accessKeyID = "ediliankeji";
//        static String accessKeySecret = "ediliankeji#@sda23f";

//    String accessKeyID = "eqixiangyun";
//    static String accessKeySecret = "eqixiangyuns@sda32s";

//    String accessKeyID = "qixiangyun";
//    static String accessKeySecret = "qixiangyuns@21$&!";

//    String accessKeyID = "esscj";
//    static String accessKeySecret = "esscj#@rrab1s";

//    String accessKeyID = "sscj";
//    static String accessKeySecret = "sscj#@rrab1s";

//    String accessKeyID = "yongyou";
//    static String accessKeySecret = "yongyou!@#443abd";

//    String accessKeyID = "eyongyou";
//    static String accessKeySecret = "yongyou!@#566ccd";

    String accessKeyID = "eruiyuan";
    static String accessKeySecret = "ruiyuan@q2e4r5_";

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

//    String accessKeyID = "qixiangyun";
//    static String accessKeySecret = "qixiangyuns@21$&!";

//    String accessKeyID = "ebiaopuyun";
//    static String accessKeySecret = "ebiaopuyun123#@!";

    //    String accessKeyID = "bwgf-limit";
//    static String accessKeySecret = "qixiangyuns@21$&!";

    static String host = "http://127.0.0.1:8767";
//    static String host = "https://api.taxservices.cn";
//    static String host = "http://101.201.74.244:8767";
//    static String host = "http://bwtoken.invservice.cn:7779";


    @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","91120116MA05TT0A0D");
        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";

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

        a.addAll(FileUtils.readLines(new File("D:\\te11.txt")));
//        a.add("91441900MA519W5D1B");
        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);

            //url += "&taxno=9111010856577744XT&dqdm=3700";
            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));

//            Map userparam = new HashMap<>();
//            userparam.put("nsrsbh","91441900617560710F");
//            userparam.put("username","431128200002167664");
//            userparam.put("password","211421hqT@");
//            userparam.put("loginType","b");
//
//             param.put("userInfoJson",userparam);

            List<Object> userList = Lists.newArrayList();
            Map functionParam = new HashMap<>();
            functionParam.put("functionNo","05");
            functionParam.put("optType","0");
            functionParam.put("validType","0");
            functionParam.put("bussinessType","0");
            functionParam.put("keyType","2");
            functionParam.put("initMonth","3");
            functionParam.put("invSpec","0");

//            functionParam.put("functionNo","02");
//            functionParam.put("optType","1");
//            functionParam.put("bussinessType","0");
//            functionParam.put("keyType","2");
//            functionParam.put("validType","2");
//            functionParam.put("invSpec","0");
//            functionParam.put("initMonth","2");

//            functionParam.put("functionNo","04");
//            functionParam.put("optType","0");
//            functionParam.put("bussinessType","0");
//            functionParam.put("keyType","0");
//            functionParam.put("validType","0");
//            functionParam.put("invSpec","0");

//            functionParam.put("functionNo","01");
//            functionParam.put("optType","0");
//            functionParam.put("bussinessType","1");
//            functionParam.put("keyType","2");
//            functionParam.put("validType","2");
//            functionParam.put("initMonth","60");
//            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 testTaxManageOne() throws Exception{
        String url = "https://api.taxservices.cn/invoicefile/taxManage";
        String signStr = "AccessKeyID=%s&SignatureNonce=%s&TimeStamp=%s&Version=1.0";
        String taxno = "91430102MABW0XPAAA";
        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","");
        List<Object> userList = Lists.newArrayList();
        Map userparam = new HashMap<>();
        userparam.put("functionNo","06");
        userparam.put("optType","0");
        userparam.put("validType","2");
        userparam.put("bussinessType","0");
        userparam.put("keyType","2");
        userparam.put("initMonth","3");
        userparam.put("invSpec","0");
        userList.add(userparam);

        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 testDownload() throws Exception{
        String url = host + "/invoicefile/download";
        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);

        //url += "&taxno=9111010856577744XT&dqdm=3700";
        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));
        System.out.println(url);
        String ret = HttpHelper.postWithJson(url,JSON.toJSONString(param));
//        ResponseContent ret = HttpHelper.getUrlRespContent(url);
        System.out.println(ret);
    }

    @Test
    public void testDownloadRes() throws Exception{
        String url = host + "/invoicefile/downloadRes";
        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);

        //url += "&taxno=9111010856577744XT&dqdm=3700";
        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("taskno","40280fe67da34f41017da35efdeb001e");
        System.out.println(url);
        String ret = HttpHelper.postWithJson(url,JSON.toJSONString(param));
        System.out.println(ret);
    }

    @Test
    public void testFlushUserToCache() throws Exception{
//        String url = host + "/client/flushUserToCache";
        String url = "http://127.0.0.1:8764/client/flushUserToCache";
        System.out.println(url);
        ResponseContent ret = HttpHelper.getUrlRespContent(url);
        System.out.println(ret.getContent());
    }

    @Test
    public void testFlushExpCacheByTaxno() throws Exception{
        Map param = new HashMap<>();
        param.put("accessKeyID",accessKeyID);
        param.put("taxno","91442000304052790Q");
        String url = "http://127.0.0.1:8767/invoicefile/flushExpCacheByTaxno";
        System.out.println(url);
        String ret = HttpHelper.postWithJson(url,JSON.toJSONString(param));
        System.out.println(ret);
    }

    @Test
    public void flushUserUrlCache() throws Exception{
        Map param = new HashMap<>();
        param.put("accessKeyID",accessKeyID);
//        String url = "http://127.0.0.1:8767/invoicefile/flushUserUrlCache";
        String url = host + "/invoicefile/flushUserUrlCache";
        System.out.println(url);
        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;
    }
}
