package com.skyversation.poiaddr.service.impl;

import com.skyversation.poiaddr.addquery.AddressQueryEngine;
import com.skyversation.poiaddr.addquery.Constant;
import com.skyversation.poiaddr.bean.AddressResult;
import com.skyversation.poiaddr.entity.TestData;
import com.skyversation.poiaddr.service.TestDataRepository;
import com.skyversation.poiaddr.util.status.AddressLevel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

@Service
public class TestDataServiceImpl {
    @Autowired
    private TestDataRepository testDataRepository;

    //  分页查询地址表
    public Page<TestData> getAllDataPage(int size) {
        Pageable pageable = PageRequest.of(0, size);
        return testDataRepository.findByAddress1Containing(pageable);
    }

    //   批量修改地址表
    public List<TestData> updateDatas(List<TestData> testDataList) {
        return testDataRepository.saveAll(testDataList);
    }

    private boolean isOtherDistrictThanSongJiang(String address) {
        // 将地址字符串转换为小写，以便进行不区分大小写的比较
        String lowerCaseAddress = address.toLowerCase();
        // 检查地址是否包含 "青浦区" 字样，如果不包含，则可能是其他区
        // 列出上海市的其他区（已补充完整）
        String[] otherDistricts = {"黄浦区", "徐汇区", "长宁区", "静安区", "普陀区", "虹口区", "杨浦区", "闵行区", "宝山区", "嘉定区", "金山区", "青浦区", "奉贤区", "崇明区", "浦东新区"};
        for (String district : otherDistricts) {
            if (lowerCaseAddress.contains(district.toLowerCase())) {
                return false;
            }
        }
        return true;
    }

    /**
     * 首先要得到一个Page<T>对象，然后判断是否还有别的数据，有的话接着请求并返回新的Page<T>对象并迭代处理数据
     */
    public void iterativeProcessing(Integer pageSize, Integer level, Integer init) {
        System.out.println("<<<<<<<<------iterativeProcessing{pageSize:" + pageSize + "}");
        long startTime = System.currentTimeMillis();
        Page<TestData> outData = getAllDataPage(pageSize);
        if (outData.getTotalElements() > 0) {
            List<TestData> listData = outData.getContent();
            // 创建线程池
            ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
            List<Future<?>> futures = new ArrayList<>();
            for (TestData item : listData) {
                futures.add(executorService.submit(() -> {
                    if (init == 1) {
                        item.setInTheArea(null);
                        item.setTwon(null);
                        item.setLevel(null);
                        item.setAdministrativeDivision(null);
                        item.setStandardizedAddress(null);
                        item.setMarket(null);
                        item.setLat(null);
                        item.setLon(null);
                        item.setResultAddrKey(null);
                        item.setUpdateTime(null);
                    } else {
                        List<String> addrList = new ArrayList<>();
                        if (item.getSkHjdz() != null && !item.getSkHjdz().isEmpty() && isOtherDistrictThanSongJiang(item.getSkHjdz())) {
//                          实口户籍地址
                            addrList.add("上海市" + Constant.getArea() + "区" + AddressQueryEngine.addressReplaceAll(item.getSkHjdz()));
                        }
                        if (item.getSkJzdz() != null && !item.getSkJzdz().isEmpty() && isOtherDistrictThanSongJiang(item.getSkJzdz())) {
//                          实口居住地址
                            addrList.add("上海市" + Constant.getArea() + "区" + AddressQueryEngine.addressReplaceAll(item.getSkJzdz()));
                        }
                        if (item.getJcHjdz() != null && !item.getJcHjdz().isEmpty() && isOtherDistrictThanSongJiang(item.getJcHjdz())) {
//                          基层户籍地址
                            addrList.add("上海市" + Constant.getArea() + "区" + AddressQueryEngine.addressReplaceAll(item.getJcHjdz()));
                        }
                        if (item.getJcJzdz() != null && !item.getJcJzdz().isEmpty() && isOtherDistrictThanSongJiang(item.getJcJzdz())) {
//                          基层居住地址
                            addrList.add("上海市" + Constant.getArea() + "区" + AddressQueryEngine.addressReplaceAll(item.getJcJzdz()));
                        }
//                      判断地址是否属于松江区
                        if (addrList.size() > 0) {
//                          开始查询
                            AddressResult addressResult = AddressQueryEngine.getInstance().commonSearchByName(addrList, AddressLevel.values()[level - 1]);
                            if (addressResult != null) {
                                if (addressResult.getData() == null || addressResult.getData().size() < 1) {
                                    item.setInTheArea("异常");
                                    item.setResultAddrKey("未匹配到符合规则的结果！");
                                } else {
                                    try {
                                        for (AddressResult.ContentBean contentBean : addressResult.getData()) {
                                            String resultAddrKey = contentBean.getAddress();
                                            if (resultAddrKey != null && contentBean.getLon() != null && contentBean.getLat() != null && isOtherDistrictThanSongJiang(resultAddrKey) && contentBean.getAdJson() != null && contentBean.getTownJson() != null) {
                                                String lng = contentBean.getLat() + "";
                                                String lat = contentBean.getLon() + "";
                                                item.setInTheArea("是");
                                                if (contentBean.getAdJson().getString("name") != null) {
                                                    item.setAdministrativeDivision(contentBean.getAdJson().getString("name"));
                                                }
                                                if (contentBean.getTownJson().getString("name") != null) {
                                                    item.setTwon(contentBean.getTownJson().getString("name"));
                                                }
                                                item.setLat(lat);
                                                item.setLon(lng);
                                                item.setResultAddrKey(resultAddrKey);
                                                item.setMarket("上海市");
//                                              开始规范化地址（把查询地址的市、区、街镇替换为空，然后把得到的区、街镇拼接上去）（得到区、街镇）
                                                String oldAddress = contentBean.getSearchAddress().replaceAll("上海市", "").replaceAll("松江区", "");
                                                String[] towns = Constant.getTowns();
                                                for (String town : towns) {
                                                    oldAddress = oldAddress.replaceAll(town, "");
                                                }
                                                if (contentBean.getCjJson() != null && contentBean.getCjJson().containsKey("所属居委")) {
                                                    oldAddress = contentBean.getCjJson().getString("所属居委") + oldAddress;
                                                }
                                                item.setStandardizedAddress("上海市" + item.getAdministrativeDivision() + item.getTwon() + oldAddress);
                                                break;
                                            } else {
                                                item.setInTheArea("异常");
                                                item.setResultAddrKey("未匹配到符合规则的结果！");
                                            }
                                        }
                                    } catch (Exception e) {
                                        item.setInTheArea("异常");
                                        System.err.println("查询结果处理异常：" + e);
                                    }
                                }
                            } else {
                                item.setInTheArea("异常");
                                item.setResultAddrKey("未匹配到符合规则的结果！");
                            }
                        } else {
//                          更新时间，添加是否松江判断字段为否
                            item.setInTheArea("否");
                        }
                        item.setUpdateTime(new Date());
                        item.setLevel(level.toString());
                    }
                }));
            }

            // 等待所有任务完成
            for (Future<?> future : futures) {
                try {
                    future.get();
                } catch (InterruptedException | ExecutionException e) {
                    System.err.println("线程异常：" + e);
                }
            }
            // 关闭线程池
            executorService.shutdown();
            updateDatas(listData);
            long endTime = System.currentTimeMillis();
            System.out.println("处理单批次用时" + (endTime - startTime) / 1000 + "秒！");
            if (outData.getTotalElements() - pageSize > 0) {
                System.out.println("剩余处理条数：" + (outData.getTotalElements() - pageSize));
                iterativeProcessing(pageSize, level, init);
            } else {
                System.out.println("<<<<<<<<------任务处理完成！");
            }
        } else {
            System.out.println("<<<<<<<<------任务处理完成！");
        }
    }
}
