package com.tax.file.test;

import com.alibaba.fastjson.JSON;
import com.yxcheng.token.utils.*;
import org.apache.http.HttpException;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TestInterface {
//    static String host = "http://file.taxservices.cn";
//    static String host = "http://localhost:8765";
    static String host = "http://bwtoken.invservice.cn:7779/";
    String accessKeyID = "bwgf-limit";
    static String accessKeySecret = "bw@2u9%5Ts#2";


    public static void main(String[] args) throws Exception {
        TestInterface testInterface = new TestInterface();
        for(int i=1;i<10;i++){
            testInterface.taxManage();
        }
    }


    @Test
    public void taxManage() throws Exception {
        String url = host + "/invoicefile/taxManage";
        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 += signStr + ("&signature=" + getSign(signStr));
        System.out.println(url);
        Map<String,Object> map = new HashMap<>();
        List<Map<String,String>>dataList =new ArrayList<>();
        map.put("accessKeyID",accessKeyID);
        map.put("signatureNonce",signatureNonce);
        map.put("timeStamp",timeStamp);
        map.put("version","1.0");
        map.put("signature", getSign(signStr));
        map.put("taxno","91130426MA09PBHQ2C");
        map.put("zoneCode","1100");




        Map <String,String> data = new HashMap();
        data.put("functionNo","01");
        data.put("optType","0");
        data.put("validType","1");
        data.put("bussinessType","0");
        data.put("keyType","2");
        data.put("initMonth","2");
        data.put("invSpec","2");


        dataList.add(data);

        map.put("functionList",dataList);
        System.out.println(JSON.toJSONString(map));
        ResponseContent responseContent = HttpHelper.postJsonEntity(url, JSON.toJSONString(map));

        System.out.println(responseContent.getContent());
    }

    @Test
    public void testHtmlUtil() throws IOException, HttpException {
        String url = "http://fpdk.yunnan.chinatax.gov.cn:7888/NSbsqWW/nsrcx.do";
        Map<String,Object> map = new HashMap<>();
        map.put("cert","91530112MA6KX5DY0R");
        map.put("token","2~0~0~~1~0~0~0~0~0~1~c4ed193b-d9de-4b9e-823e-533f7ffe5dd7@@91530112MA6KX5DY0R@@00");
        map.put("ymbb","4.0.07");
        map.put("id","nsrcx");
        ResponseContent responseContent = HttpHelper.postEntity(url, map);
        System.out.println(responseContent.getContent());
    }


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