package com.tax.sign.web.controller.test;


import com.ulisesbocchio.jasyptspringboot.encryptor.DefaultLazyEncryptor;
import com.yxcheng.token.utils.*;
import net.sf.json.JSONObject;
import org.jasypt.encryption.StringEncryptor;
import org.junit.Test;
import org.springframework.core.env.StandardEnvironment;

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

public class SignTest {
    String accessKeyID = "bwgf-limit";
    static String accessKeySecret = "bw@2u9%5Ts#2";
   // static String host = "http://localhost:8763";
    static String host = "http://bwtoken.invservice.cn:7779";
//    String accessKeyID = "yongyou";
//    static String accessKeySecret = "yongyou!@#443abd";

    String token = null;
    @Test
    public void testGetLoginSign() throws Exception{
        String url = host + "/signService/getLoginSign?";
        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=91130700550417847R&dqdm=3200&serverRandom=123";
        //url += "&taxno=9111010856577744XT&dqdm=3700";


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


    @Test
    public void testDeduSignNew() throws Exception{
        String url = host + "/signService/getDeduSignNew?";
        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=91130700550417847R&token=3232435464&urlPath=/4343.do";
        //url += "&taxno=9111010856577744XT&dqdm=3700";


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


    @Test
    public void testGetOtherSign() throws Exception{
        String url =host +  "/signService/getOtherSign?";
        String signStr = "accessKeyID=%s&signatureNonce=%s&timeStamp=%s&version=1.0";

        String signatureNonce = UUIDGenerator.generate();
//        long time = this.getTime();
        long time = System.currentTimeMillis();
        String timeStamp = DateUtils.formatDate(time,DateUtils.yyyymmddthhmmssz);

        signStr = String.format(signStr,accessKeyID,signatureNonce, timeStamp);
        url += signStr + ("&signature=" + getSign(signStr));



        url += "&cert=123&token=456";
        url += "&taxno=9131000005587870XB&dqdm=3100";


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


    @Test
    public void testGetOneInvSign() throws Exception{
        //token.invservice.cn
        //String url = "http://39.107.28.117:8763/signService/getDkDownloadSign?";
        String url = "http://localhost:8763/signService/getOneInvSign?";
       // String url = "http://localhost:8763/signService/list?";
        String signStr = "accessKeyID=%s&signatureNonce=%s&timeStamp=%s&version=1.0";

        String signatureNonce = UUIDGenerator.generate();


        long time = this.getTime();

        String timeStamp = DateUtils.formatDate(time,DateUtils.yyyymmddthhmmssz);

        signStr = String.format(signStr,accessKeyID,signatureNonce, timeStamp);
        url += signStr + ("&signature=" + getSign(signStr));



        url += "&taxno=9111010856577744XT&dqdm=1100&dkyf=1522400683377&fphm=07904369&token=2~0~0~~1~0~0~0~cd90cd6d-3c72-4ccd-b508-34238f5cf72b";


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


    public long getTime() throws Exception{
        String url = host + "/signService/getTime";
        ResponseContent ret = HttpHelper.getUrlRespContent(url);
        System.out.println(ret.getContent());
        JSONObject obj = JSONObject.fromObject(ret.getContent());
        long data = obj.getLong("data");
        return data;
    }



    public void testTime(){

    }




    @Test
    public void test1() throws Exception {
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        EncryptFactoryString fact = new EncryptFactoryString();
//        String  taxno = "91110108339805094M";
//        String token = "2~0~0~~1~0~0~0~cd90cd6d-3c72-4ccd-b508-34238f5cf72b";
////        long time = sdf.parse("2018-03-30 17:34:27").getTime();
////        String timeSec = String.valueOf(time);
//        String dkyf = "201802";
//        String zoneCode = "1100";
//        String time = "1522400683377";
//        String ts = EncryptFactoryString.createTimestamp(taxno, "04", "08", Long.valueOf(time));
//        String result = fact.checkDeduDownString(taxno, token, time, dkyf, zoneCode, ts, null, null, null);
//
//        System.out.println(result);
    }

    @Test
    public void testFlushTaxnoList() throws Exception{
        //token.invservice.cn
        String url = "http://localhost:8763/signService/flushTaxnoList";

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



    @Test
    public void testQuerymm() throws Exception{
      //  String url = "https://fpdk.bjsat.gov.cn/SbsqWW/querymm.do?";
      // String url = "http://5ecd678b.ngrok.io/SbsqWW/querymm.do?";
      //  String url = "https://fpdk.hl-n-tax.gov.cn/SbsqWW/querymm.do?";
        String url = "https://fpdk.gs-n-tax.gov.cn/SbsqWW/querymm.do?";

        url += "&cert=9111010856577744XT&funType=01";


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


        ret =ret.replace("null(","");
        ret =   ret.substring(0,ret.length() - 1);
        System.out.println(ret);
        String ts = JSONObject.fromObject(ret).getString("ts");

        System.out.println(DateUtils.getDate(Long.valueOf(ts)));

    }

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


    public static void main(String[] args) {
//        Long l = 1524186742893L;
//
//
//        System.out.println(DateUtils.getDate(l));


//        String strDate = "2018-04-23 14:33:20";
//        String cDate = "2018-04-23 14:42:12";
//
//        long sTime = DateUtils.getDetailDate(strDate).getTime();
//        System.out.println("sTime:" + sTime);
//        long cTime = DateUtils.getDetailDate(cDate).getTime();
//        System.out.println("cTime:" + cTime);
//        long diff = cTime - sTime;
//        System.out.println("diff:" + diff);
//        long currentDateLong = System.currentTimeMillis();
//        System.out.println("currentDateLong:" + currentDateLong);
//        long curSerDateLong = currentDateLong -diff;
//        System.out.println("curSerDateLong:" + curSerDateLong);
//        Date resultDate = DateUtils.getDate(1000L);
////
////        System.out.println(DateUtils.formatDate(resultDate,"yyyy-MM-dd HH:mm:ss"));

       Long l = 1524656942416L;
        Date date = DateUtils.getDate(l);

        System.out.println(DateUtils.date2Str(date,DateUtils.date_sdf));
    }


    @Test
    public void testJasyptENC() {
        // 对应application-dev.yml 中配置的根密码
//        System.setProperty("jasypt.encryptor.password", "taxLowog");
        System.setProperty("jasypt.encryptor.password", "taxLowog");
        StringEncryptor stringEncryptor = new DefaultLazyEncryptor(new StandardEnvironment());

        //加密方法
        System.out.println(stringEncryptor.encrypt("nikanba888"));
        System.out.println(stringEncryptor.encrypt("d-CxDown018<>"));

        //解密方法
        System.out.println(stringEncryptor.decrypt("4fRw5Dd8DR2rJBaJsWFU9oDSWT8jV8nM"));
        //测试redis
        System.out.println("redis:" + stringEncryptor.decrypt("5tQ0YmySPk+wyksG79wY/LE+YYSTcUYX"));
        System.out.println("db:" + stringEncryptor.decrypt("0YywKABbg4XBEkgGZhhKiDT6576ZTH4R"));
        System.out.println("db1:" + stringEncryptor.decrypt("HymuJ3cI4hob3D5VvaoLrvYHQJPuasi8"));
        System.out.println(stringEncryptor.decrypt("pUECzD6ZHPKbJTW0lKAWkiA4rqH2lkLc"));
        //mq
        System.out.println(stringEncryptor.decrypt("0YywKABbg4XBEkgGZhhKiDT6576ZTH4R"));
        System.out.println(stringEncryptor.decrypt("C0Rm8VGlrRL9/DrW9tuMO2dwfGAQ04GP"));
        System.out.println("---");
    }



}
