//package com.benyun.core.service.impl;
//
//import com.alibaba.fastjson.JSON;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.benyun.core.mapper.*;
//import com.benyun.core.entity.*;
//import com.benyun.core.service.AnalyseWdService;
//import com.benyun.core.utils.DistanceUtil;
//import org.gavaghan.geodesy.Ellipsoid;
//import org.gavaghan.geodesy.GlobalCoordinates;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Qualifier;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.time.LocalDateTime;
//import java.util.*;
//
//
//@Service
//@Transactional
//public class AnalyseWdServiceImpl implements AnalyseWdService {
//
//    //分析范围集
//    List<Integer> anly = Arrays.asList(2000, 1000, 500);
//
//    @Autowired
//    WdInfoDao wdInfoDao;
//
//    @Autowired
//    StoreWdDao storeWdDao;
//
//    @Autowired
//    HouseWdDao houseWdDao;
//
//    @Autowired
//    BuildWdDao buildWdDao;
//
//    @Autowired
//    EnterpriseWdDao enterpriseWdDao;
//
//    @Autowired
//    EnterpriseDao enterpriseDao;
//
//    /*分析信息*/
//    @Autowired
//    WdTopologicalDao wdTopologicalDao;
//
//    @Autowired
//    TopologicalBuildWdDao topologicalBuildWdDao;
//
//
//    @Autowired
//    TopologicalStoreWdDao topologicalStoreWdDao;
//
//    @Autowired
//    TopologicalHouseWdDao topologicalHouseWdDao;
//
//    @Autowired
//    TopologicalEnterpriseWdDao topologicalEnterpriseWdDao;
//
//    /*统计信息*/
//    @Autowired
//    TopologicalHouseWdStatisticsDataDao houseWdStatisticsDataDao;
//    @Autowired
//    TopologicalBuildWdStatisticsCategoryDao buildWdStatisticsCategoryDao;
//    @Autowired
//    TopologicalStoreWdStatisticsCategoryDao storeWdStatisticsCategoryDao;
//    @Autowired
//    WdTopologicalInfoDao wdTopologicalInfoDao;
//
//
//    @Autowired
//    @Qualifier("addrCodeMap")
//    HashMap<String,String> addrCodeMap;
//
//
//    @Override
//    public void analyse(WdInfo wdInfo,LocalDateTime now,int size,int index) {
//
//            //1.找到该点半径 r = 4.89km 内的所有点
//            QueryWrapper<WdInfo> queryWrapper = new QueryWrapper<>();
//            queryWrapper.likeRight("geo_hash", wdInfo.getGeoHash().substring(0, 5));
//            List<WdInfo> wdInfos = wdInfoDao.selectList(queryWrapper);
//
//            //2.初始化
//            //关键信息统计
//            HashMap<String, HashMap<String, Integer>>[] storeCategory = new HashMap[anly.size()];  //经营分类统计：<中类,<小类,数量>>
//            HashMap<String, Double>[] houseData = new HashMap[anly.size()];  //小区分析统计：key：totalHouseholds、totalPeopleCount、totalRent
//            HashMap<String, Integer>[] buildCategory = new HashMap[anly.size()];   //楼宇网点分析统计，key：甲类、乙类...
//            HashMap<String, Integer>[] wdTopologicalInfoHash = new HashMap[anly.size()];  //分类tag：key：byCode value:数量
//            List<WdToologicalItem>[] wdToologicalItemList = new ArrayList[anly.size()];
//
//            for (int i = 0; i < anly.size(); i++) {
//                storeCategory[i] = new HashMap<>();
//                houseData[i] = new HashMap<>();
//                houseData[i].put("houseWdCount", 0.0);
//                houseData[i].put("totalHouseholds", 0.0);   //总户数
//                houseData[i].put("totalPeopleCount", 0.0);  //总人口数
//                houseData[i].put("totalRent", 0.0); //房价
//                buildCategory[i] = new HashMap<>();
//                wdTopologicalInfoHash[i] = new HashMap<>();
//                wdToologicalItemList[i] = new ArrayList<>();
//            }
//
//            //关键信息集合
//            List<TopologicalStoreWdStatisticsCategory> storeWdStatisticsCategoryList = new ArrayList<>();
//            List<TopologicalBuildWdStatisticsCategory> buildWdStatisticsCategoryList = new ArrayList<>();
//            List<TopologicalHouseWdStatisticsData> houseWdStatisticsDataList = new ArrayList<>();
//            List<WdTopologicalInfo> wdTopologicalInfoList = new ArrayList<>();
//
//            //各拓扑网点集合
//            List<WdTopological> wdTopologicals = new ArrayList<>();
//            System.out.println("process：" + size + "/" + index);
//
//            //WdTopological
//            WdTopological wdTopological = new WdTopological();
//            wdTopological.setCenterWdId(wdInfo.getWdId());
//            wdTopological.setCenterWdName(wdInfo.getWdName());
//            wdTopological.setAnalyseTime(now);
//
//            //3.开始分析
//            for (WdInfo info : wdInfos) {
//
//                //网点等于自己不要
//                if (info.getWdId().equals(wdInfo.getWdId()))
//                    continue;
//
//                //计算距离
//                GlobalCoordinates source = new GlobalCoordinates(wdInfo.getLat().doubleValue(), wdInfo.getLng().doubleValue());
//                GlobalCoordinates target = new GlobalCoordinates(info.getLat().doubleValue(), info.getLng().doubleValue());
//                double meter2 = DistanceUtil.getDistanceMeter(source, target, Ellipsoid.WGS84);
//
//                //大于4.89Km不要
//                if (meter2 > anly.get(0))
//                    continue;
//
//                Integer r = anly.get(anly.size() - 1);
//                int p = anly.size() - 1; //记录r位置
//                for (int i = 1; i < anly.size(); i++) {
//                    if (meter2 > anly.get(i)) {
//                        r = anly.get(i - 1);
//                        p = i - 1;
//                        break;
//                    } else if (meter2 == anly.get(i)) {
//                        r = anly.get(i);
//                        p = i;
//                        break;
//                    }
//                }
//
//                //3.2 组装WdTologicalItem
//                WdToologicalItem wdToologicalItem = new WdToologicalItem();
//                wdToologicalItem.setAroundWdId(info.getWdId());
//                wdToologicalItem.setAroundWdName(info.getWdName());
//                wdToologicalItem.setAddrCodeInfo(addrCodeMap.get(info.getAddrCode()));
//                wdToologicalItem.setAddrCode(info.getAddrCode());
//                wdToologicalItem.setAddrInfo(info.getAddrInfo());
//                wdToologicalItem.setLat(info.getLat());
//                wdToologicalItem.setLng(info.getLng());
//                wdToologicalItem.setAroundWdTypeCode(info.getWdTypeCode());
//                wdToologicalItem.setAroundTypeCodeBy(info.getTypeCodeBy());
//                wdToologicalItem.setDistance(meter2);
//
//                //3.3 将WdTologicalItem放入满足半径的区域
//                for(int i=p;i>=0;i--){
//                    wdToologicalItemList[p].add(wdToologicalItem);
//                }
//
//                //3.4 犇云标签统计
//                if (info.getTypeCodeBy() != null && !info.getTypeCodeBy().equals("")) {
//                    for(int i=p;i>=0;i--){
//                        HashMap<String, Integer> wdTopologicalInfoHash1 = wdTopologicalInfoHash[i];
//                        Integer put = wdTopologicalInfoHash1.get(info.getTypeCodeBy());
//                        if (put == null) {
//                            wdTopologicalInfoHash1.put(info.getTypeCodeBy(), 1);
//                        } else {
//                            wdTopologicalInfoHash1.put(info.getTypeCodeBy(), wdTopologicalInfoHash1.get(info.getTypeCodeBy()) + 1);
//                        }
//                    }
//                }
//
//
//                if (wdToologicalItem.getAroundWdTypeCode().equals("1")) {
//                    //门店分析
//                    StoreWd storeWd = storeWdDao.selectById(info.getWdId());
//                    if (storeWd != null) {
//                        String manageTypeCode = storeWd.getManageTypeCode();
//
//                        //TODO 分析经营分类
//                        if (manageTypeCode != null && !manageTypeCode.equals("")) {
//                            for(int i=p;i>=0;i--){
//                                HashMap<String, HashMap<String, Integer>> stringHashMapHashMap = storeCategory[i];
//                                HashMap<String, Integer> hashMap = stringHashMapHashMap.get(manageTypeCode.substring(0, 4));
//                                if (hashMap == null) {
//                                    HashMap<String, Integer> map = new HashMap<>();
//                                    map.put(manageTypeCode, 1);
//                                    stringHashMapHashMap.put(manageTypeCode.substring(0, 4),map);
//                                } else {
//                                    Integer integer = hashMap.get(manageTypeCode);
//                                    if (integer == null)
//                                        hashMap.put(manageTypeCode, 1);
//                                    else
//                                        hashMap.put(manageTypeCode, hashMap.get(manageTypeCode) + 1);
//                                }
//                            }
//                        }
//                    }
//                } else if (wdToologicalItem.getAroundWdTypeCode().equals("2")) {
//                    //小区分析
//                    HouseWd houseWd = houseWdDao.selectById(info.getWdId());
//                    //小区关键信息
//                    for(int i=p;i>=0;i--){
//                        HashMap<String, Double> houseDatum = houseData[i];
//                        houseDatum.put("houseWdCount", houseDatum.get("houseWdCount") + 1.0);
//
//                        //小区关键信息统计
//                        if (houseWd != null) {
//                            houseDatum.put("totalRent", houseDatum.get("totalRent") + info.getRent());
//                            houseDatum.put("totalHouseholds", houseDatum.get("totalHouseholds") + houseWd.getHouseholds());
//                            houseDatum.put("totalPeopleCount", houseDatum.get("totalPeopleCount") + houseWd.getPeopleCount());
//                        }
//                    }
//                } else if (wdToologicalItem.getAroundWdTypeCode().equals("3")) {
//                    //楼宇分析
//                    BuildWd buildWd = buildWdDao.selectById(info.getWdId());
//                    if (buildWd != null) {
//                        //分析楼宇分类
//                        if (buildWd.getPropertyType() != null && !buildWd.getPropertyType().trim().equals("")) {
//                            for(int i=p;i>=0;i--){
//                                HashMap<String, Integer> buildCategoryMap = buildCategory[i];
//                                Integer integer = buildCategoryMap.get(buildWd.getPropertyType());
//                                if (integer == null) {
//                                    buildCategoryMap.put(buildWd.getPropertyType(), 1);
//                                } else
//                                    buildCategoryMap.put(buildWd.getPropertyType(), buildCategoryMap.get(buildWd.getPropertyType()) + 1);
//                            }
//                        }
//                    }
//
//                }
//            }
//
//            //TODO 门店分类
//            for (int i = 0; i < anly.size(); i++) {
//                HashMap<String, HashMap<String, Integer>> stringHashMapHashMap = storeCategory[i];
//                for (String s : stringHashMapHashMap.keySet()) {
//                    TopologicalStoreWdStatisticsCategory storeWdStatisticsCategory = new TopologicalStoreWdStatisticsCategory();
//                    storeWdStatisticsCategory.setCenterWdId(wdInfo.getWdId());
//                    storeWdStatisticsCategory.setRadius(anly.get(i));
//                    storeWdStatisticsCategory.setBigManageTypeCode(s.substring(0, 2) + "0000");
//
//                    HashMap<String, Integer> hashMap = stringHashMapHashMap.get(s);
//                    StringBuilder stringBuilder = new StringBuilder();
//                    int total = 0;
//                    for (String s1 : hashMap.keySet()) {
//                        total+=hashMap.get(s1);
//
//                        if(!s.equals(s1)){
//                            stringBuilder.append(s1);
//                            stringBuilder.append(":");
//                            stringBuilder.append(hashMap.get(s1));
//                            stringBuilder.append(";");
//                        }else
//                            break;
//                    }
//                    storeWdStatisticsCategory.setMidManageTypeCode(s+":"+total);
//                    storeWdStatisticsCategory.setSubCategoryCodeStatistics(stringBuilder.toString());
//                    storeWdStatisticsCategory.setAnalyseTime(now);
//                    storeWdStatisticsCategoryList.add(storeWdStatisticsCategory);
//                }
//            }
//
//            //TODO 楼宇分类
//            for (int i = 0; i < anly.size(); i++) {
//                TopologicalBuildWdStatisticsCategory buildWdStatisticsCategory = new TopologicalBuildWdStatisticsCategory();
//                buildWdStatisticsCategory.setCenterWdId(wdInfo.getWdId());
//                buildWdStatisticsCategory.setRadius(anly.get(i));
//
//                HashMap<String, Integer> hashMap = buildCategory[i];
//                StringBuilder stringBuilder = new StringBuilder();
//                for (String s : hashMap.keySet()) {
//                    stringBuilder.append(s + ":" + hashMap.get(s) + ";");
//                }
//                buildWdStatisticsCategory.setPropertyTypeStatistics(stringBuilder.toString());
//                buildWdStatisticsCategory.setAnalyseTime(now);
//                buildWdStatisticsCategoryList.add(buildWdStatisticsCategory);
//            }
//
//            //TODO 社区关键数据
//            for (int i = 0; i < anly.size(); i++) {
//                TopologicalHouseWdStatisticsData houseWdStatisticsData = new TopologicalHouseWdStatisticsData();
//                houseWdStatisticsData.setCenterWdId(wdInfo.getWdId());
//                houseWdStatisticsData.setRadius(anly.get(i));
//                HashMap<String, Double> houseDatum = houseData[i];
//                double houseWdCount = houseDatum.get("houseWdCount");
//                double totalHouseholds = houseDatum.get("totalHouseholds");
//                double totalPeopleCount = houseDatum.get("totalPeopleCount");
//                double totalRent = houseDatum.get("totalRent");
//                houseWdStatisticsData.setHouseWdCount((int) houseWdCount);
//                houseWdStatisticsData.setTotalHouseholds((int) totalHouseholds);
//                houseWdStatisticsData.setTotalPeopleCount((int) totalPeopleCount);
//                if(houseWdCount != 0.0)
//                    houseWdStatisticsData.setAverageRent(totalRent / houseWdCount);
//                else
//                    houseWdStatisticsData.setAverageRent(0.0);
//
//                houseWdStatisticsData.setAnalyseTime(now);
//                houseWdStatisticsDataList.add(houseWdStatisticsData);
//            }
//
//            //TODO 网点拓扑信息
//            for (int i = 0; i < anly.size(); i++) {
//                WdTopologicalInfo wdTopologicalInfo = new WdTopologicalInfo();
//                wdTopologicalInfo.setCenterWdId(wdInfo.getWdId());
//                wdTopologicalInfo.setCenterWdName(wdInfo.getWdName());
//                wdTopologicalInfo.setCenterWdTypeCode(wdInfo.getWdTypeCode());
//                wdTopologicalInfo.setTypeCodeBy(wdInfo.getTypeCodeBy());
//                wdTopologicalInfo.setLat(wdInfo.getLat());
//                wdTopologicalInfo.setLng(wdInfo.getLng());
//                wdTopologicalInfo.setRadius(anly.get(i));
//                wdTopologicalInfo.setAddrCode(wdInfo.getAddrCode());
//                wdTopologicalInfo.setAddrCodeInfo(addrCodeMap.get(wdInfo.getAddrCode()));
//                wdTopologicalInfo.setAddrInfo(wdInfo.getAddrInfo());
//                wdTopologicalInfo.setGeoHash(wdInfo.getGeoHash());
//                wdTopologicalInfo.setAnalyseTime(now);
//
//                HashMap<String, Integer> wdTopologicalInfoHash1 = wdTopologicalInfoHash[i];
//                StringBuilder stringBuilder = new StringBuilder();
//                for (String s : wdTopologicalInfoHash1.keySet()) {
//                    stringBuilder.append(s + ":" + wdTopologicalInfoHash1.get(s) + ";");
//                }
//                wdTopologicalInfo.setTag(stringBuilder.toString());
//                wdTopologicalInfoList.add(wdTopologicalInfo);
//            }
//
//            //TODO 网点拓扑
//            for (int i = 0; i < anly.size(); i++) {
//                int start = 0,end = 100;
//                while(start < wdToologicalItemList[i].size()){
//                    if(end >= wdToologicalItemList[i].size()){
//                        end = wdToologicalItemList[i].size();
//                    }
//
//                    WdTopological wdTopological1 = new WdTopological();
//                    wdTopological1.setCenterWdId(wdInfo.getWdId());
//                    wdTopological1.setCenterWdName(wdInfo.getWdName());
//                    wdTopological1.setAnalyseTime(now);
//                    wdTopological1.setRadius(anly.get(i));
//                    List<WdToologicalItem> wdToologicalItemList1 = wdToologicalItemList[i].subList(start, end);
//                    String json = JSON.toJSON(wdToologicalItemList1).toString();
//                    wdTopological1.setAroundWdInfo(json);
//                    wdTopologicals.add(wdTopological1);
//
//                    start = end;
//                    end+=100;
//                }
//            }
//
//            System.out.println("完成------");
//
//
//            //4.导入数据库
//            if (!wdTopologicals.isEmpty())
//                wdTopologicalDao.insertList(wdTopologicals);//保存网点拓扑
//
//
//            //4.2 保存其他相关统计信息
//            if (!storeWdStatisticsCategoryList.isEmpty())
//                storeWdStatisticsCategoryDao.insertList(storeWdStatisticsCategoryList);
//            if (!houseWdStatisticsDataList.isEmpty())
//                houseWdStatisticsDataDao.insertList(houseWdStatisticsDataList);
//            if (!buildWdStatisticsCategoryList.isEmpty())
//                buildWdStatisticsCategoryDao.insertList(buildWdStatisticsCategoryList);
//            if (!wdTopologicalInfoList.isEmpty())
//                wdTopologicalInfoDao.insertList(wdTopologicalInfoList);
//
//        }
//
//}
