package com.yfd.platform.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.yfd.platform.modules.patroltask.domain.TaskResult;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class TestReadChar {
    public static void main(String[] args) throws IOException {
        String localFilePath = "D:\\catalina.out";
        boolean b = false;
        int count = 0;
        int count1 = 0;
//        File file = new File("D:\\莱达四维\\hangben.txt");
//        FileWriter writer = new FileWriter(file);
        try (BufferedReader br = new BufferedReader(new FileReader(localFilePath))) {
            String line = "";
            while ((line = br.readLine()) != null) {
                if (!b && line.contains("智能分析识别通知")) {
                    b = true;
                }
                if (line.contains("智能分析识别通知") && line.contains("5901e99bd88744ffa88285d6274bcaec")) {
//                    System.out.println(line);
//                    line = line.replace("智能分析识别通知：", "");
                    line = "{" + StrUtil.subAfter(line, "{", false);
                    JSONObject jsonObject = JSONUtil.parseObj(line);
                    JSONArray resultList = jsonObject.getJSONArray("resultList");
//                    if (resultList.size() > 1) {
//                        count1++;
//                    }
                    for (int i = 0; i < resultList.size(); i++) {
                        JSONObject resultObject = resultList.getJSONObject(i);
                        if (resultObject == null) {
                            continue;
                        }
                        count++;
                        //7FE4BB37-F54A-4808-852B-6CAB43DD71B3-00001/2024/08/18/task_laida_21/CCD/正常SF6ylb (32)_原图_ori.jpg
                        JSONArray results = resultObject.getJSONArray("results");
                        JSONObject result1 = results.getJSONObject(0);
                        if(result1==null){
                            count1++;
                            continue;
                        }
                        String filename = result1.getStr("resImagePath");
                        String fileName = StrUtil.subAfter(filename, "/", true).replace("_原图.jpg", ".txt").replace("_原图.jpeg", ".txt").replace("_原图.JPG", ".txt").replace("_原图.png", ".txt").replace("_原图_ori.jpg", ".txt").replace("_原图_ori.jpeg", ".txt").replace("_原图_ori.JPG", ".txt").replace("_原图_ori.png", ".txt").replace("_原图_infer.jpg", ".txt").replace("_原图_infer.jpeg", ".txt").replace("_原图_infer.JPG", ".txt").replace("_原图_infer.png", ".txt");
                        File file = new File("D:\\海康威视\\txt\\" + fileName);
//                        if (FileUtil.exist(file)) {
//                            count1++;
//                        }
                        FileWriter writer = new FileWriter(file);
                        writer.write("");
                        for (int a = 0; a < results.size(); a++) {
                            JSONObject result = results.getJSONObject(a);
                            String code = result.getStr("code");
                            if ("2000".equals(code)) {
                                String posStr = "";
                                String pos = result.getStr("pos");
                                // 将框坐标统一存储画框
                                if (StrUtil.isNotBlank(pos)) {
                                    JSONArray jsonArray = JSONUtil.parseArray(pos);
                                    // 写入内容到文件
                                    if (jsonArray.size() > 0) {
                                        JSONObject posObject = jsonArray.getJSONObject(0);
                                        if (jsonObject.size() > 0) {
                                            JSONArray areas = posObject.getJSONArray("areas");
                                            JSONObject jsonObject1 = areas.getJSONObject(0);
                                            JSONObject jsonObject2 = areas.getJSONObject(1);
                                            posStr = " " + jsonObject1.getStr("x") + " " + jsonObject1.getStr("y") + " " + jsonObject2.getStr("x") + " " + jsonObject2.getStr("y");
                                        }
                                    }

                                }
                                String resultStr = "";
                                if ("1".equals(result.getStr("value"))) {
                                    resultStr = result.getStr("type") + " " + result.getStr("conf") + posStr + "\n";
                                }
                                writer.write(resultStr);
                            }
                        }
                        writer.close();
                    }
//                    writer.write(line + "\n");
//                    System.out.println(line);
//                    JSONObject jsonObject = JSONUtil.parseObj(line);
//                    JSONArray resultList = jsonObject.getJSONArray("resultList");

                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
//        writer.close();
        System.out.println("分析返回数量" + count + "结果返回数量" + count1);
    }
}
