package org.ts.ddcs.worker;

import com.alibaba.fastjson.JSONObject;
import org.ts.ddcs.log.LogHelper;
import org.springframework.web.client.RestTemplate;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URI;
import java.util.concurrent.Callable;

@SuppressWarnings("all")
public class LogDataCollectWorker implements Callable<Integer> {

    private String datagram;

    public LogDataCollectWorker(String datagram) {
        this.datagram = datagram;
    }

    @Override
    public Integer call() throws Exception {
        try {
            Thread.sleep(1000);
            LogHelper.info("从日志中抽取数据");
            String log_path = "D:\\logs\\ts\\";
            File file = new File(log_path);
            File[] files = file.listFiles();
            for (File f : files) {
                if (f.isFile()) {
                    Thread.sleep(1000);
                    System.out.println("file name " + f.getName());
                    BufferedReader reader = null;
                    try {
                        System.out.println("以行为单位读取文件内容，一次读一整行：");
                        reader = new BufferedReader(new FileReader(f));
                        String tempString = null;
                        while ((tempString = reader.readLine()) != null) {
                            if (tempString.contains("queue_datagram_process body:")) {
                                int s = tempString.indexOf("queue_datagram_process body:");
                                String body = tempString.substring(s + "queue_datagram_process body:".length());
                                JSONObject data = JSONObject.parseObject(body);
                                if (!data.containsKey("datagramPickTime")) {
                                    String datagramPickTime = tempString.substring(0, "2020-11-10".length()) + "000000";
                                    datagramPickTime = datagramPickTime.replace("-", "");
                                    data.put("datagramPickTime", datagramPickTime);
                                }
                                String datagram = data.getString("datagram");
                                if (datagram.indexOf("68") == 0) {
                                    if (datagram.substring("682468B31522001600".length(), "682468B31522001600".length() + 2).equals("C0")) {
                                        if (datagram.substring("682468".length(), "682468".length() + 2).equals("B3")) {
                                            String relayhttp = "http://172.9.0.195:8804/DataCollectServer/Collect/Datagram/Supplement";
                                            RestTemplate restTemplate = new RestTemplate();
                                            JSONObject result = restTemplate.postForObject(new URI(relayhttp), data, JSONObject.class);
                                        }
                                    }
                                }
                            } else if (tempString.contains("datagramProcess queue message:")) {
                                int s = tempString.indexOf("datagramProcess queue message:");
                                String body = tempString.substring(s + "datagramProcess queue message:".length());
                                JSONObject data = JSONObject.parseObject(body);
                                if (!data.containsKey("datagramPickTime")) {
                                    String datagramPickTime = tempString.substring(0, "2020-11-10".length()) + "000000";
                                    datagramPickTime = datagramPickTime.replace("-", "");
                                    data.put("datagramPickTime", datagramPickTime);
                                }
                                String datagram = data.getString("datagram");
                                if (datagram.indexOf("68") == 0) {
                                    if (datagram.substring("682468B31522001600".length(), "682468B31522001600".length() + 2).equals("C0")) {
                                        if (datagram.substring("682468".length(), "682468".length() + 2).equals("B3")) {
                                            String relayhttp = "http://172.9.0.195:8804/DataCollectServer/Collect/Datagram/Supplement";
                                            RestTemplate restTemplate = new RestTemplate();
                                            JSONObject result = restTemplate.postForObject(new URI(relayhttp), data, JSONObject.class);
                                        }
                                    }
                                }
                            }
                        }
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        if (reader != null) {
                            try {
                                reader.close();
                            } catch (IOException e1) {
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            LogHelper.error(e);
        }
        return 0;
    }
}
