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.util.HashMap;
import java.util.List;
import java.util.Map;

public class FileListTest {
    String accessKeyID = "epeijing";
    static String accessKeySecret = "epeijing@des@#13";
//    static String host = "http://127.0.0.1:8767";
//    static String host = "http://file.taxservices.cn";
    static String host = "https://api.taxservices.cn";

    @Test
    public void testDownloadList() throws Exception{
        String url = host + "/invoicefile/downloadList";
        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("taxno","91440101MA59NM4B5F");
        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/downloadListRes";
        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","40280fe67da34f41017da36b97d80043");
        param.put("taxno","91120110777339337A");
        System.out.println(url);
        String ret = HttpHelper.postWithJson(url,JSON.toJSONString(param));
        System.out.println(ret);
    }

    @Test
    public void testFlushUserToCache() throws Exception{
        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{
        List<String> a = Lists.newArrayList();
        a.add("91440605MA54MWTA3N");

        for(String taxno:a) {
            Map param = new HashMap<>();
            param.put("accessKeyID", accessKeyID);
            param.put("taxno", taxno);
            String url = "https://api.taxservices.cn/invoicefile/flushExpCacheByTaxno";
            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;
    }
}
