package org.ts.ddcs.worker;

import com.alibaba.fastjson.JSONObject;
import org.ts.ddcs.log.LogHelper;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;

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

    private String datagram;

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

    @Override
    public Integer call() throws Exception {
        try {
            Thread.sleep(2000);
            LogHelper.info("从日志中抽取数据");
            String log_path = "D:\\logs\\ts\\";
            File file = new File(log_path);
            File[] files = file.listFiles();

            String rtuCode=null;
            String key =null;

            Map<String ,String> keys = new HashMap<>();

            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("0533525433 down file datagram")){
//                                LogHelper.info(tempString);
//                            }
                            if (tempString.contains("success pick datagram")) {
                                int s = tempString.indexOf("success pick datagram");
                                String datagram = tempString.substring(s + "success pick datagram".length());
                             //   {"agreement":"szy2012","rtuCode":"1508001B00","datagram":"680768B01508001B00E1B616","datagramPickTime":"20201208183601"}
                                JSONObject data = JSONObject.parseObject(datagram);
                                 rtuCode = data.getString("rtuCode");
                                if (rtuCode.equals("0533525433")){
                                    key = tempString.substring("2020-12-08 18:36:01.531 ".length(),"2020-12-08 18:36:01.531 ".length()+"7f60b7e1221c49dea47a24670b985ba5".length());
                                    if (!keys.containsKey(key)){
                                        keys.put(key,key);
                                    }
                                }
                            }
                        }
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        if (reader != null) {
                            try {
                                reader.close();
                            } catch (IOException e1) {
                            }
                        }
                    }
                }
            }


            Set<String>  key_list = keys.keySet();

            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) {
                            for(String k :key_list){
                                if (tempString.indexOf(k)>=0){
                                    LogHelper.info(tempString);
                                    break;
                                }
                            }
                        }
                        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;
    }
}
