package com.benyun.core.service.impl;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.benyun.core.dao.*;
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.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.ZoneId;
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;



    @Override
    public void analyse() {
        for (WdInfo wdInfo : wdInfoDao.selectList(null)) {
            //1.找到该点半径 r = 4.89km 内的所有点
            QueryWrapper<WdInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.likeRight("geo_hash",wdInfo.getGeoHash().substring(0,5));
            List<WdInfo> wdInfos = wdInfoDao.selectList(queryWrapper);

            //经营分类统计
            HashMap<String,HashMap<String,Integer>>[] storeCategory = new HashMap[anly.size()];

            //小区分析统计
            HashMap<String,Double>[] houseData = new HashMap[anly.size()];


            //楼宇网点分析统计
            HashMap<String,Integer>[] buildCategory = new HashMap[anly.size()];

            //初始化
            for(int i=0;i<anly.size();i++){
                storeCategory[i] = new HashMap<>();
                houseData[i] = new HashMap<>();
                houseData[i].put("totalHouseholds",0.0);   //总户数
                houseData[i].put("totalPeopleCount",0.0);  //总人口数
                houseData[i].put("totalRent",0.0); //房价
                buildCategory[i] = new HashMap<>();
            }

            //各网点列表统计
            List<WdTopological> wdTopologicalList = new ArrayList<>();
            List<TopologicalEnterpriseWd> topologicalEnterpriseWds = new ArrayList<>();
            List<TopologicalStoreWd> topologicalStoreWds = new ArrayList<>();
            List<TopologicalHouseWd> topologicalHouseWds = new ArrayList<>();
            List<TopologicalBuildingWd> topologicalBuildingWds = new ArrayList<>();
            for (WdInfo info : wdInfos) {
                //计算距离
                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);
                        p = i;
                        break;
                    }
                }
                WdTopological wdTopological = new WdTopological();
                wdTopological.setCenterWdId(wdInfo.getWdId());
                wdTopological.setCenterWdName(wdInfo.getWdName());
                wdTopological.setRadius(r);


                wdTopological.setAroundWdId(info.getWdId());
                wdTopological.setAroundWdName(info.getWdName());
                wdTopological.setAroundWdTypeCode(info.getWdTypeCode());
                wdTopological.setAddrCode(info.getAddrCode());
                wdTopological.setAddrCodeInfo(info.getAddrInfo());
                wdTopological.setAddrInfo(info.getAddrInfo());
                wdTopological.setLat(info.getLat());
                wdTopological.setLng(info.getLng());
                wdTopological.setGeoHash(info.getGeoHash());
                wdTopological.setAroundTypeCodeBy(info.getTypeCodeBy());

                wdTopological.setDistance(meter2);
                //规定时区
                ZoneId zone = ZoneId.of("Asia/Shanghai");
                wdTopological.setAnalyseTime(LocalDateTime.now(zone));
                wdTopologicalList.add(wdTopological);

                if(wdTopological.getAroundWdTypeCode().equals("1")){
                    //门店网点
                    TopologicalStoreWd topologicalStoreWd = new TopologicalStoreWd();
                    topologicalStoreWd.setCenterWdId(wdTopological.getCenterWdId());
                    topologicalStoreWd.setRadius(wdTopological.getRadius());

                    topologicalStoreWd.setAroundWdId(wdTopological.getAroundWdId());
                    topologicalStoreWd.setAroundWdName(wdTopological.getAroundWdName());
                    topologicalStoreWd.setAroundWdTypeCode(wdTopological.getAroundWdTypeCode());

                    topologicalStoreWd.setAddrCode(wdTopological.getAddrCode());
                    topologicalStoreWd.setAddrCodeInfo(wdTopological.getAddrCodeInfo());
                    topologicalStoreWd.setAddrInfo(wdTopological.getAddrInfo());

                    topologicalStoreWd.setDistance(wdTopological.getDistance());

                    //门店分析
                    StoreWd storeWd = storeWdDao.selectById(info.getWdId());
                    if(storeWd != null){
                        topologicalStoreWd.setIndustryCode(storeWd.getIndustryCode());
                        String manageTypeCode = storeWd.getManageTypeCode();
                        topologicalStoreWd.setManageTypeCode(manageTypeCode);

                        //分析经营分类
                        HashMap<String, HashMap<String, Integer>> stringHashMapHashMap = storeCategory[p];
                        HashMap<String, Integer> hashMap = stringHashMapHashMap.get(manageTypeCode.substring(0,4));
                        if(hashMap == null){
                            HashMap<String, Integer> map = new HashMap<>();
                            map.put(manageTypeCode,1);
                        }else {
                            Integer integer = hashMap.get(manageTypeCode);
                            if (integer == null)
                                hashMap.put(manageTypeCode,1);
                            else
                                hashMap.put(manageTypeCode,hashMap.get(manageTypeCode)+1);
                        }
                    }

                    topologicalStoreWd.setAnalyseTime(wdTopological.getAnalyseTime());
                    topologicalStoreWds.add(topologicalStoreWd);
                }else if(wdTopological.getAroundWdTypeCode().equals("2")){
                    //小区网点
                    TopologicalHouseWd topologicalHouseWd = new TopologicalHouseWd();
                    topologicalHouseWd.setCenterWdId(wdTopological.getCenterWdId());
                    topologicalHouseWd.setRadius(wdTopological.getRadius());

                    topologicalHouseWd.setAroundWdId(wdTopological.getAroundWdId());
                    topologicalHouseWd.setAroundWdName(wdTopological.getAroundWdName());
                    topologicalHouseWd.setAroundWdTypeCode(wdTopological.getAroundWdTypeCode());

                    topologicalHouseWd.setAddrCode(wdTopological.getAddrCode());
                    topologicalHouseWd.setAddrCodeInfo(wdTopological.getAddrCodeInfo());
                    topologicalHouseWd.setAddrInfo(wdTopological.getAddrInfo());

                    topologicalHouseWd.setDistance(wdTopological.getDistance());


                    //小区分析
                    HouseWd houseWd = houseWdDao.selectById(info.getWdId());
                    if(houseWd != null){
                        topologicalHouseWd.setHouseholds(houseWd.getHouseholds());
                        topologicalHouseWd.setRent(info.getRent());
                        topologicalHouseWd.setPeopleCount(houseWd.getPeopleCount());
                        topologicalHouseWd.setProperty_type(houseWd.getPropertyType());

                        //小区关键信息统计
                        HashMap<String, Double> houseDatum = houseData[p];
                        houseDatum.put("totalRent",houseDatum.get("totalRent")+topologicalHouseWd.getRent());
                        houseDatum.put("totalHouseholds",houseDatum.get("totalHouseholds")+topologicalHouseWd.getHouseholds());
                        houseDatum.put("totalPeopleCount",houseDatum.get("totalPeopleCount")+topologicalHouseWd.getPeopleCount());
                    }

                    topologicalHouseWd.setAnalyseTime(wdTopological.getAnalyseTime());
                    topologicalHouseWds.add(topologicalHouseWd);
                }else if(wdTopological.getAroundWdTypeCode().equals("3")){
                    //楼宇网点
                    TopologicalBuildingWd topologicalBuildingWd = new TopologicalBuildingWd();
                    topologicalBuildingWd.setCenterWdId(wdTopological.getCenterWdId());
                    topologicalBuildingWd.setRadius(wdTopological.getRadius());

                    topologicalBuildingWd.setAroundWdId(wdTopological.getAroundWdId());
                    topologicalBuildingWd.setAroundWdName(wdTopological.getAroundWdName());
                    topologicalBuildingWd.setAroundWdTypeCode(wdTopological.getAroundWdTypeCode());

                    topologicalBuildingWd.setAddrCode(wdTopological.getAddrCode());
                    topologicalBuildingWd.setAddrCodeInfo(wdTopological.getAddrCodeInfo());
                    topologicalBuildingWd.setAddrInfo(wdTopological.getAddrInfo());

                    topologicalBuildingWd.setDistance(wdTopological.getDistance());

                    //楼宇分析
                    BuildWd buildWd = buildWdDao.selectById(info.getWdId());
                    if(buildWd != null){
                        topologicalBuildingWd.setRent(info.getRent());
                        topologicalBuildingWd.setBuildType(buildWd.getBuildType());
                        topologicalBuildingWd.setPropertyTpe(buildWd.getPropertyType());

                        //分析楼宇分类
                        HashMap<String, Integer> buildCategoryMap = buildCategory[p];
                        Integer integer = buildCategoryMap.get(topologicalBuildingWd.getPropertyTpe());
                        if(integer == null){
                            buildCategoryMap.put(topologicalBuildingWd.getPropertyTpe(),1);
                        }else
                            buildCategoryMap.put(topologicalBuildingWd.getPropertyTpe(),buildCategoryMap.get(topologicalBuildingWd.getPropertyTpe())+1);
                    }

                    topologicalBuildingWd.setAnalyseTime(wdTopological.getAnalyseTime());
                    topologicalBuildingWds.add(topologicalBuildingWd);
                }else if(wdTopological.getAroundWdTypeCode().equals("5")){
                    //公司网点
                    TopologicalEnterpriseWd topologicalEnterpriseWd = new TopologicalEnterpriseWd();
                    topologicalEnterpriseWd.setCenterWdId(wdTopological.getCenterWdId());
                    topologicalEnterpriseWd.setRadius(wdTopological.getRadius());

                    topologicalEnterpriseWd.setAroundWdId(wdTopological.getAroundWdId());
                    topologicalEnterpriseWd.setAroundWdName(wdTopological.getAroundWdName());
                    topologicalEnterpriseWd.setAroundWdTypeCode(wdTopological.getAroundWdTypeCode());

                    topologicalEnterpriseWd.setAddrCode(wdTopological.getAddrCode());
                    topologicalEnterpriseWd.setAddrCodeInfo(wdTopological.getAddrCodeInfo());
                    topologicalEnterpriseWd.setAddrInfo(wdTopological.getAddrInfo());

                    topologicalEnterpriseWd.setDistance(wdTopological.getDistance());

                    //公司分析
                    EnterpriseWd enterpriseWd = enterpriseWdDao.selectById(info.getWdId());
                    if(enterpriseWd != null){
                        Enterprise enterprise = enterpriseDao.selectById(enterpriseWd.getEnterpriseUsci());
                        if (enterprise != null){
                            topologicalEnterpriseWd.setIndustrycode(enterprise.getIndustryCode());
                            topologicalEnterpriseWd.setRegisteredCapital(enterprise.getRegisteredCapital());
                            topologicalEnterpriseWd.setEnterpriseScale(enterprise.getEnterpriseScale());
                        }
                    }
                    topologicalEnterpriseWd.setAnalyseTime(wdTopological.getAnalyseTime());
                    topologicalEnterpriseWds.add(topologicalEnterpriseWd);
                }


            }

            //保存网点拓扑
            wdTopologicalDao.insertList(wdTopologicalList);

            //保存拓扑门店信息
            topologicalStoreWdDao.insertList(topologicalStoreWds);
            topologicalHouseWdDao.insertList(topologicalHouseWds);
            topologicalBuildWdDao.insertList(topologicalBuildingWds);
            topologicalEnterpriseWdDao.insertList(topologicalEnterpriseWds);

            //TODO 门店分类
//            for(int i=0;i<anly.size();i++){
//                TopologicalStoreWdStatisticsCategory storeWdStatisticsCategory = new TopologicalStoreWdStatisticsCategory();
//                storeWdStatisticsCategory.setRadius(String.valueOf(anly.get(i)));
//                storeWdStatisticsCategory.setBigManageTypeCode();
//                storeCategory[i]
//            }


        }
    }
}
