package com.trs.project.test;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.trs.project.entity.GcJihuaInfo;
import com.trs.project.entity.GcTenderbidfile;
import com.trs.project.util.RSAUtils;
import com.xxl.job.core.context.XxlJobHelper;
import org.apache.commons.text.StringEscapeUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import javax.sql.DataSource;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class Test {
    public static void main(String[] args) {
//        TaskRecord byId = taskRecordService.getById("GC_NOTICEANDFILE");
//        if(null == byId){
//            byId = new TaskRecord();
//            byId.setTableName("GC_NOTICEANDFILE");
//            byId.setUpdateTime(new Date());
//            byId.setLastDealTime(new Date());
//        }
//        Date startDateTime = byId.getLastDealTime();
////                String startDateStr = Log.getTxtData("time.txt");
////                startDate = sdf.parse(startDateStr).getTime();
//        long startDate = startDateTime.getTime();
//
//        Date afterDate = new Date(startDate + 30*60*1000);//30分钟后的时间
//        //Log.Logger("time.txt",sdf.format(afterDate));
//        byId.setLastDealTime(afterDate);
//        byId.setUpdateTime(new Date());
//        taskRecordService.saveOrUpdate(byId);

        Long timeMillis = System.currentTimeMillis();
        System.out.println("enterTasks定时任务执行时间："+new Date());
        timeMillis = System.currentTimeMillis();
        System.out.println(timeMillis);
        System.out.println("1672543421");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startDate = null;
        try {
            startDate = format.parse("2000-09-28 00:00:00");
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }

        System.out.println(new Date().getTime());
        try{
            JSONObject jsonObject = httpGetMethod("https://ggzyjy.nmg.gov.cn/adminNmg/api/down.do?platform_code=e64975be&table_name=JSGC_ProjectJHInfo&page=1&page_size=20&startTime="+startDate.getTime()+"  ");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            String data = RSAUtils.decrypt(jsonObject.getString("data"));
            JSONArray jsonArray = JSONArray.parseArray(data);
            System.out.println(jsonArray);
            List<GcJihuaInfo> gcJihuaInfos = JSONObject.parseArray(jsonArray.toJSONString(), GcJihuaInfo.class);
            for (GcJihuaInfo gcJihuaInfo : gcJihuaInfos) {
                gcJihuaInfo.setDataTimestamp(Long.parseLong(simpleDateFormat.format(new Date())));
            }
            System.out.println(gcJihuaInfos);
        }catch (Exception e){
            e.printStackTrace();
        }
    }





    //http方法
    public static JSONObject httpGetMethod(String uri){
//        JSONObject response = restTemplate.getForObject(uri,JSONObject.class);
        Map<String, String> parameters = new HashMap<String, String>();
        //parameters.put("channel", "channel");
        String str = sendGet(uri, parameters);
        JSONObject response =JSONObject.parseObject(str);
        return response;
    }


    public static String sendGet(String url, Map<String, String> parameters) {
        String result="";
        BufferedReader in = null;// 读取响应输入流
        StringBuffer sb = new StringBuffer();// 存储参数
        String params = "";// 编码之后的参数
        try {
            // 编码请求参数

            //System.out.println(full_url);
            // 创建URL对象
            java.net.URL connURL = new java.net.URL(url);
            // 打开URL连接
            java.net.HttpURLConnection httpConn = (java.net.HttpURLConnection) connURL
                    .openConnection();
            // 设置通用属性
            httpConn.setRequestProperty("Accept", "*/*");
            httpConn.setRequestProperty("Connection", "Keep-Alive");
            httpConn.setRequestProperty("User-Agent",
                    "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
            // 建立实际的连接
            httpConn.connect();
            // 响应头部获取
            Map<String, List<String>> headers = httpConn.getHeaderFields();
            // 遍历所有的响应头字段
            for (String key : headers.keySet()) {
                //System.out.println(key + "\t：\t" + headers.get(key));
            }
            // 定义BufferedReader输入流来读取URL的响应,并设置编码方式
            in = new BufferedReader(new InputStreamReader(httpConn
                    .getInputStream(), "UTF-8"));
            String line;
            // 读取返回的内容
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result ;
    }


}
