package com.trs.project.timer;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.trs.project.entity.*;
import com.trs.project.service.*;
import com.trs.project.util.RSAUtils;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.quartz.DisallowConcurrentExecution;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;

import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;


public class BusinessTaskTest {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private GcBidcandidateService gcBidcandidateService;
    @Autowired
    private GcProjectService gcProjectService;
    @Autowired
    private GcTenderprojectService gcTenderprojectService;
    @Autowired
    private GcNoticeandfileService gcNoticeandfileService;

    @Autowired
    private TaskRecordService taskRecordService;


    private Long timeMillis = System.currentTimeMillis();


   // @Test
    public void execute2() {
        try{
            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);
            }

            MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
            params.add("platform_code", "e64975be");
            params.add("table_name", "NM_NOTICE");
            params.add("page", "1");
            params.add("page_size", "1");
            params.add("startTime", startDate.getTime());
            JSONObject jsonObject1 = httpGetMethod("https://ggzyjy.nmg.gov.cn/adminNmg/api/down.do",params);//&startTime=1676540529
            System.out.println(jsonObject1);
        }catch (Exception e){
            e.printStackTrace();
        }

    }


    public JSONObject httpGetMethod(String uri, MultiValueMap<String, Object> params){
        JSONObject response  = restTemplate.getForObject(uri, JSONObject.class, params);
        return response;
    }

   @Test
    public void execute() {
//        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);


        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=NM_RESULT_NOTICE&page=1&page_size=20&startTime="+startDate.getTime()+"  ");

            String data = RSAUtils.decrypt(jsonObject.getString("data"));
            JSONArray jsonArray = JSONArray.parseArray(data);
            System.out.println(jsonArray);
                List list = JSONObject.parseArray(jsonArray.toJSONString(), NmResultNotice.class);
                for (int j=0;j<list.size();j++){
                    NmResultNotice nmResultNoticeNmResultNotice = (NmResultNotice)list.get(j);
                    System.out.println(nmResultNoticeNmResultNotice);
                }
        }catch (Exception e){
            e.printStackTrace();
        }
    }





    //http方法
    public 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 ;
    }


}
