package com.skyversation.poiaddr.util;

import com.skyversation.poiaddr.addquery.AddressQueryEngine;
import com.skyversation.poiaddr.entity.SjArrDzbzhSjWcbryDzxx;

import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class SerializationUtils {

    /**
     * 将 List<T> 序列化并保存到本地文件
     *
     * @param list     要序列化的 List
     * @param filePath 保存文件的路径
     * @param <T>      泛型类型
     */
    public static <T extends Serializable> void serialize(List<T> list, String filePath) {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath))) {
            oos.writeObject(list);
            System.out.println("数据已成功序列化并保存到 " + filePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 从本地文件中反序列化读取 List<T>
     *
     * @param filePath 读取文件的路径
     * @param <T>      泛型类型
     * @return 反序列化后的 List
     */
    @SuppressWarnings("unchecked")
    public static <T extends Serializable> List<T> deserialize(String filePath) {
        List<T> list = null;
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath))) {
            list = (List<T>) ois.readObject();
            System.out.println("数据已成功从 " + filePath + " 反序列化");
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
        return list;
    }

    public static void main(String[] args) throws IOException {
        test();
//        System.out.println(roleUpdate("上海市松江区中山街道东果子弄2号小区", "上海市松江区上海市松江区东果子弄2号"));
        /*List<SjArrDzbzhSjWcbryDzxx> listData = deserialize("output/testDataBase_all.ser");
//      将序列化的文件转储为xlsx文件
        ExcelReaderUtils.writeClassToExcel(listData, "output/allData.xlsx");*/
    }

    public static boolean roleUpdate(String returnAddress, String address) {
        boolean roleUpdateTag = false;
        Set<String> addressString = AddressQueryEngine.tokenizeString(AddressTools.parseAddressCJ(returnAddress)[4]).get(0);
        Set<String> addressNumber = AddressQueryEngine.tokenizeString(AddressTools.parseAddressCJ(returnAddress)[4]).get(1);
        Set<String> address2String = AddressQueryEngine.tokenizeString(AddressTools.parseAddressCJ(address)[4]).get(0);
        Set<String> address2Number = AddressQueryEngine.tokenizeString(AddressTools.parseAddressCJ(address)[4]).get(1);
        if (addressString != null && addressString.size() > 1) {
            int addressStrSize = addressString.size();
            for (String addr2str : address2String) {
                if (addressString.contains(addr2str)) {
                    addressStrSize--;
                    if (addressStrSize == 0) {
                        if (addressNumber.size() == 0) {
                            roleUpdateTag = true;
                            return roleUpdateTag;
                        } else {
                            int addressNumSize = addressNumber.size();
                            for (String addr2Num : address2Number) {
                                if (addressNumber.contains(addr2Num)) {
                                    addressNumSize--;
                                    if (addressNumSize == 0) {
                                        roleUpdateTag = true;
                                        return roleUpdateTag;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return roleUpdateTag;
    }

    /**
     * 读取本地xlsx文件得到List<Map<String,Object>>结果，然后得到所有规范化结果地址字段，然后匹配街镇关键字，并将匹配到的街镇关键字放到新的一列
     * 2、根据主键address和标准化地址standardAddress直接跑规则，属于rule_4的要替换掉同时添加updateTag
     * 3、标准化地址还要做一层去空和街镇异常处理，有变动的也添加updateTag
     */
    public static void test() {
//       根据地址读取xlsx数据
        try {
            List<Map<String, Object>> fileDatas = ExcelReaderUtils.readExcel("output/yysk_wcb_address_standardization.xlsx");
            float lecel = 1.4f;
            int sum = 0;
            int erSum = 0;
            int nullSum = 0;
            for (Map<String, Object> item : fileDatas) {
                if (!"异常".equals(item.get("match_level").toString()) && !"否".equals(item.get("match_level").toString())) {
                    sum++;
                    if (!"rule_2".equals(item.get("match_level").toString()) && !"rule_4".equals(item.get("match_level").toString())) {
                        //                  查询地址、主键
                        float address = Float.parseFloat(item.get("match_level").toString());
                        if (address < lecel) {
                            erSum++;
                        }
                    }
                } else {
                    nullSum++;
                }
            }
            System.out.println("总数据条数：" + fileDatas.size());
            System.out.println("打分小于" + lecel + "数据条数：" + erSum);
            System.out.println("异常数据条数：" + nullSum);
            System.out.println("有效数据条数：" + (sum - erSum));
            System.out.println("有效数据数据占比：" + (float) (sum - erSum) / fileDatas.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}