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

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yxcheng.token.utils.*;
import org.junit.Test;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class TokenUnitTest {

//    static String host = "http://127.0.0.1:8762";
    static String host = "https://pushtoken.invservice.cn/";
//    static String host = "https://api.xfapiao.com";
//    String accessKeyID = "eyongyou";
//    static String accessKeySecret = "yongyou!@#566ccd";

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

//    String accessKeyID = "echangchen";
//    static String accessKeySecret = "echangchen#@rrwea1";
//    String accessKeyID = "edonggang";
//    static String accessKeySecret = "donggang@PO!#N98";

    String accessKeyID = "epei";
    static String accessKeySecret = "epei@asd%yjt&b5#";
    /**
     * zone   91430600081385810Q   -> 4300   特殊的是4403
     *
     * @throws Exception
     */
    @Test
    public void testPushToken() throws Exception{
        String url = host + "/client/pushToken?";
        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));
        String sign = getSign(signStr);


        url += "&taxno=91130700550417847R&dqdm=3200&serverRandom=123";


        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

        Map map = new HashMap<>();
        map.put("accessKeyID",accessKeyID);
        map.put("signatureNonce",signatureNonce);
        map.put("timeStamp",timeStamp);
        map.put("version",1.0);
        map.put("signature",sign);
        map.put("taxno","91430600081385810Q");
        map.put("zoneCode","4300");
        map.put("token","987654321");
        map.put("recevDate",sdf.format(new Date()));


        System.out.println(url);
        ResponseContent ret = HttpHelper.postJsonEntity(url, JSON.toJSONString(map));
        System.out.println(ret.getContent());
    }




    @Test
    public void testQueryTokenErrorInfo() throws Exception{
        String url = host + "/client/queryTokenErrorInfo";

        JSONObject json = new JSONObject();
        json.put("taxno", "91511011MA6444HM2E");
        json.put("userCode", "qixiangyun");

        System.out.println("testQueryTokenErrorInfo request url:" + url + ",param:" + json.toJSONString());

        ResponseContent ret = HttpHelper.postJsonEntity(url, json.toString());
        System.out.println(ret.getContent());
    }


    @Test
    public void testGetCookie() throws Exception{
        String url = host + "/client/getCookie";
        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","91510100MA6A5JGH7B");
        param.put("zoneCode","5100");
        System.out.println(url);
        ResponseContent ret = HttpHelper.postJsonEntity(url,JSON.toJSONString(param));
        System.out.println(ret.getContent());
    }

    @Test
    public void testGetRealCookie() throws Exception{
        String url = host + "/client/realLogin";
        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","91110108MABM7A651D");
        param.put("zoneCode","1100");
        param.put("checkType","2");

        Map userparam = new HashMap<>();
        userparam.put("username","18845135029");
        userparam.put("password","hcy829829");
        userparam.put("phone","18845135029");

        param.put("userInfoJson", userparam);
        System.out.println(url);
        System.out.println(JSON.toJSONString(param));
//        ResponseContent ret = HttpHelper.postJsonEntity(url,JSON.toJSONString(param));
//        System.out.println(ret.getContent());
    }

    @Test
    public void testGetEtaxCookie() throws Exception{
        String url = host + "/client/checkEtaxLoginInfo";
        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","91440101MA5CRHH33W");
        param.put("zoneCode","4400");

//        param.put("site","4400");
//        param.put("target","4400");

        Map userparam = new HashMap<>();

        userparam.put("loginType","b");
        userparam.put("nsrsbh","91440101MA5CRHH33W");
        userparam.put("username","Fanny123456");
        userparam.put("userType","办税人");
        userparam.put("password","Fanny123456");

        param.put("userInfoJson",userparam);
        System.out.println(url);
        System.out.println(JSON.toJSONString(param));
        ResponseContent ret = HttpHelper.postJsonEntity(url,JSON.toJSONString(param));
        System.out.println(ret.getContent());
    }

    @Test
    public void testgetToken() throws Exception{
        String url = host + "/client/getToken?";
        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));


        url += "&taxno=91440101MA9Y85B1X6&zoneCode=4400&userCode=edaxiang";




        System.out.println(url);
        ResponseContent ret = HttpHelper.getUrlRespContent(url);
        System.out.println(ret.getContent());
    }

    @Test
    public void testCleanCookie() throws Exception{
        String url = host + "/client/cleanLoginInfo";
        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","914404003980391901");
        param.put("zoneCode","4400");

        param.put("cleanType","3");

        Map userparam = new HashMap<>();
//
//        userparam.put("loginType","b");
//        userparam.put("nsrsbh","91440101MA5CRHH33W");
//        userparam.put("username","Fanny123456");
//        userparam.put("userType","办税人");
//        userparam.put("password","Fanny123456");
//
//        param.put("userInfoJson",userparam);
        userparam.put("cookie","T1C2K3y8C5PrmwN0+VGXnyf6d3pybZUulkIQ7dqhrOR0SypQS3MPYL2L+iB8DSE6c4ddIFSj8GDgjvZMJHSuE3K5ufDA1wBcHyGb/nUpbfN4MNfyItV60jVsyj1BbqgR+41BiAJRtF7JrXBGo42al0FO951dfSsDiy2jgE0HvIeMJG54KaDlyjGjSlDzAc+gfYex4AESzsQIMCLtMhGfM04LCPxUbB2mWy5d7jDL0ozvqjK64qk/wMAAPsbcdkGdXrfn6m4dGib/646uuv2LoxKL7u9bvjiZ3TVxhyjYoUEpYSVDwGia4lfqawHNlXP5AejN/LqEpfZEFhvlm18p4+XRJNKAMsXcgWsrLuPhsKDBT2jCMzWf3Z7Lk0MHrPjhJIfllgn6jDo8P4XQCgSaDpX2Cbte8eZ69+7c6hywR4H2RBy1BYTISUlZ8jYnuhthDuueuoX+FvB0X8scX+PqS2XPPctkYiAMFR6tv2nU2LQMcx6i2D+mz4lfPi2ThiVVCVRoLAD06GI2ApKp0JJD0hYTfGjg==");

        param.put("cookieInfoJson",userparam);

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

    @Test
    public void testUpdateErrorInfo() throws Exception{
        String url = host + "/client/updateErrorInfo";

        JSONObject json = new JSONObject();
        json.put("taxno", "91310000607266569Q");
        json.put("userCode", "daxiang");
        json.put("code", "4001");
        json.put("message", "用户名密码异常");

        System.out.println("testUpdateErrorInfo request url:" + url + ",param:" + json.toJSONString());

        ResponseContent ret = HttpHelper.postJsonEntity(url, json.toString());
        System.out.println(ret.getContent());
    }

    @Test
    public void testSelectErrorTokenList() throws Exception{
        String url = host + "/client/selectErrorTokenList";
        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("pageId",0);
        System.out.println(JSON.toJSONString(param));
        String ret = HttpHelper.postJsonEntity(url,JSON.toJSONString(param)).getContent();
        System.out.println(ret);
    }

    @Test
    public void testCleanLoginLockInfo() throws Exception{
        String url = host + "/client/cleanLoginLockInfo";
        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","914404003980391901");
        param.put("zoneCode","4400");

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

    @Test
    public void testSelectTokenListPage() throws Exception{
        String url = host + "/client/report/selectTokenListPage";

        JSONObject json = new JSONObject();
        json.put("taxno", "91150802MA7YPNPB9U");
        json.put("pageSize", "10");
        json.put("pageNumber", "1");
        json.put("userCode", "edaxiang");

        System.out.println("testUpdateErrorInfo request url:" + url + ",param:" + json.toJSONString());

        ResponseContent ret = HttpHelper.postJsonEntity(url, json.toString());
        System.out.println(ret.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;
    }
}
