package com.ruoyi.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.ruoyi.demo.entity.*;
import com.ruoyi.demo.entity.bo.*;
import com.ruoyi.demo.entity.vo.ChannelMapAceeptVo;
import com.ruoyi.demo.mapper.*;
import com.ruoyi.demo.service.ChannelMapService;
import com.ruoyi.demo.utils.InitMapUtil;
import com.ruoyi.demo.utils.WdRedisStoreage;
import io.netty.util.concurrent.CompleteFuture;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
@Transactional
public class ChannelMapServiceImpl implements ChannelMapService {

    @Autowired
    private AddrCategoryDao addrCategoryDao;

    @Autowired
    InitMapUtil initMapUtil;


    @Autowired
    AttentionPoolDao attentionPoolDao;

    @Autowired
    WdInfoDao wdInfoDao;


    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    WdRedisStoreage wdRedisStoreage;

    @Autowired
    StoreWdDao storeWdDao;

    @Autowired
    HouseWdDao houseWdDao;

    @Autowired
    BuildWdDao buildWdDao;

    @Autowired
    TrafficWdDao trafficWdDao;

    @Autowired
    EnterpriseWdDao enterpriseWdDao;


    /**
     * 地图模式：查看所有地区的网点数量（v1.0）
     * @param channelMapAceeptVo
     * @return
     */
/*    @Override
    public WdCount area(ChannelMapAceeptVo channelMapAceeptVo) {
        //1.根据不同级别得到 需要封装的 地区码
        List<String> addrCodeList = new ArrayList<>();
        if ("province".equals(channelMapAceeptVo.getRankType())) {
            //省码
            for (String s : channelMapAceeptVo.getAddrCode()) {
                String substring = s.substring(0, 2);
                addrCodeList.add(substring);
            }
        } else if ("city".equals(channelMapAceeptVo.getRankType())) {
            //省的所有市
            for (String s : channelMapAceeptVo.getAddrCode()) {
                String substring = s.substring(0, 2);
                addrCodeList.add(substring);
            }
        } else if ("zone".equals(channelMapAceeptVo.getRankType())) {
            //市的所有区
            for (String s : channelMapAceeptVo.getAddrCode()) {
                String substring = s.substring(0, 4);
                addrCodeList.add(substring);
            }
        } else {
            return null;
        }


        //2.找到所有需要封装的 地区信息
        QueryWrapper<AddrCategory> queryWrapper = new QueryWrapper<>();
        if ("province".equals(channelMapAceeptVo.getRankType())) {
            queryWrapper.eq("city", "");
        } else if ("city".equals(channelMapAceeptVo.getRankType())) {
            queryWrapper.eq("district", "").and(addrCategoryQueryWrapper -> {
                addrCategoryQueryWrapper.ne("city", "");
            });
        } else if ("zone".equals(channelMapAceeptVo.getRankType())) {
            queryWrapper.ne("district", "");
        }
        queryWrapper.and(addrCategoryQueryWrapper -> {
            addrCategoryQueryWrapper.or(addrCategoryQueryWrapper1 -> {
                for (String s : addrCodeList) {
                    addrCategoryQueryWrapper1.likeRight("addr_code", s).or();
                }
            });
        });
        List<AddrCategory> addrCategories = addrCategoryDao.selectList(queryWrapper);


        //3.组装地区信息
        List<WdCountBody> result = new ArrayList<>();
        HashMap<String,Integer> statistics = new HashMap<>(); //统计信息
        for (AddrCategory addrCategory : addrCategories) {
            WdCountBody wdCountBody = new WdCountBody();

            //组装
            wdCountBody.setLat(addrCategory.getLatGd());
            wdCountBody.setLng(addrCategory.getLngGd());
            wdCountBody.setAddrCode(addrCategory.getAddrCode());
            if ("province".equals(channelMapAceeptVo.getRankType())) {
                wdCountBody.setName(addrCategory.getProvince());
                statistics.put(addrCategory.getAddrCode().substring(0,2),0);
            } else if ("city".equals(channelMapAceeptVo.getRankType())) {
                wdCountBody.setName(addrCategory.getCity());
                statistics.put(addrCategory.getAddrCode().substring(0,4),0);
            } else if ("zone".equals(channelMapAceeptVo.getRankType())) {
                wdCountBody.setName(addrCategory.getDistrict());
                statistics.put(addrCategory.getAddrCode().substring(0,6),0);
            }
            result.add(wdCountBody);
        }


        //4.组装条件构造器找到网点信息网点
        //2.1 渠道、地区分类
        QueryWrapper<WdInfo> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.select("addr_code","count(*) as audit");
        queryWrapper2.in("wd_type_code", channelMapAceeptVo.getChannel()).and(originWdInfoQueryWrapper -> {
            for (String s : addrCodeList) {
                originWdInfoQueryWrapper.likeRight("addr_code", s).or();
            }
        });

        //搜索字段
        if (channelMapAceeptVo.getSearchText() != null && !channelMapAceeptVo.getSearchText().trim().equals("")) {
            queryWrapper2.and(originWdInfoQueryWrapper -> {
                originWdInfoQueryWrapper.like("wd_name", channelMapAceeptVo.getSearchText() );
            });
        }

        //城市等级分类
        if(channelMapAceeptVo.getCityTier() != null && channelMapAceeptVo.getCityTier().length > 0){
            List<String> tierCode = new ArrayList<>();
            for (String s : channelMapAceeptVo.getCityTier()) {
                List<String> list = initMapUtil.getInitCityTierListMap(s);
                if (list != null){
                    tierCode.addAll(list);
                }
            }
            queryWrapper.and(wdInfoQueryWrapper -> {
                for (String s : tierCode) {
                    wdInfoQueryWrapper.likeRight("addr_code", s).or();
                }
            });
        }
        queryWrapper2.groupBy("addr_code");

        //5.统计
        int total = 0;
        List<WdInfo> wdInfos = wdInfoDao.selectList(queryWrapper2);
        for (WdInfo wdInfo : wdInfos) {
            if("province".equals(channelMapAceeptVo.getRankType())){
                statistics.put(wdInfo.getAddrCode().substring(0,2),statistics.get(wdInfo.getAddrCode().substring(0,2))+wdInfo.getAudit());
            } else if ("city".equals(channelMapAceeptVo.getRankType())) {
                statistics.put(wdInfo.getAddrCode().substring(0,4),statistics.get(wdInfo.getAddrCode().substring(0,4))+wdInfo.getAudit());
            } else if ("zone".equals(channelMapAceeptVo.getRankType())) {
                statistics.put(wdInfo.getAddrCode().substring(0,6),statistics.get(wdInfo.getAddrCode().substring(0,6))+wdInfo.getAudit());
            }
            total+=+wdInfo.getAudit();
        }


        //5.计算radio
        for (WdCountBody wdCountBody : result) {
            String code =  null;
            if("province".equals(channelMapAceeptVo.getRankType())){
                code = wdCountBody.getAddrCode().substring(0,2);
            }else if("city".equals(channelMapAceeptVo.getRankType())){
                code = wdCountBody.getAddrCode().substring(0,4);
            }else if("zone".equals(channelMapAceeptVo.getRankType())){
                code = wdCountBody.getAddrCode().substring(0,6);
            }

            if(statistics.get(code) != null){
                wdCountBody.setCount(statistics.get(code));
            }else {
                wdCountBody.setCount(0);
            }

            if (total != 0){
                BigDecimal bigDecimal = new BigDecimal((float) wdCountBody.getCount() / total);
                float v = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
                wdCountBody.setRadio(v);
            }
            else
                wdCountBody.setRadio(0.0f);
        }
        WdCount wdCount = new WdCount();
        wdCount.setWdCountBodyList(result);
        wdCount.setTotal(total);
        return wdCount;
    }*/


    /**
     * 地图模式：查看所有地区的网点数量（v2.0）
     * @param channelMapAceeptVo
     * @return
     */
    @Override
    public WdCount area(ChannelMapAceeptVo channelMapAceeptVo) {
        //1.根据不同级别得到 需要查询的 网点地区码
        CompletableFuture<List<String>> completableFuture1 = CompletableFuture.supplyAsync(() -> {
            return findWdAddrCode(channelMapAceeptVo.getRankType(),channelMapAceeptVo.getAddrCode());
        });

        //2.找到所有需要封装的 地区信息
        CompletableFuture<List<AddrCategory>> completableFuture2 = CompletableFuture.supplyAsync(() -> {
            return findCenterAddrCode(channelMapAceeptVo.getRankType(),channelMapAceeptVo.getAddrCode());
        });

        List<AddrCategory> addrCategories = completableFuture2.join();

        //3.组装地区信息
        List<WdCountBody> result = new ArrayList<>();
        HashMap<String,Integer> statistics = new HashMap<>(); //初始化统计信息
        for (AddrCategory addrCategory : addrCategories) {
            WdCountBody wdCountBody = new WdCountBody();

            //组装
            wdCountBody.setLat(addrCategory.getLatGd());
            wdCountBody.setLng(addrCategory.getLngGd());
            wdCountBody.setAddrCode(addrCategory.getAddrCode());
            if ("province".equals(channelMapAceeptVo.getRankType())) {
                wdCountBody.setName(addrCategory.getProvince());
                statistics.put(addrCategory.getAddrCode().substring(0,2),0);
            } else if ("city".equals(channelMapAceeptVo.getRankType())) {
                wdCountBody.setName(addrCategory.getCity());
                statistics.put(addrCategory.getAddrCode().substring(0,4),0);
            } else if ("zone".equals(channelMapAceeptVo.getRankType())) {
                wdCountBody.setName(addrCategory.getDistrict());
                statistics.put(addrCategory.getAddrCode().substring(0,6),0);
            }
            result.add(wdCountBody);
        }


        //4.组装条件构造器找到网点信息网点
        //2.1 渠道、地区分类
        List<String> wdAddrCodes = completableFuture1.join();
        QueryWrapper<WdInfo> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.select("addr_code","count(*) as audit");
        queryWrapper2.in("wd_type_code", channelMapAceeptVo.getChannel()).and(originWdInfoQueryWrapper -> {
            originWdInfoQueryWrapper.in("addr_code",wdAddrCodes);
        });

        //搜索字段
        if (channelMapAceeptVo.getSearchText() != null && !channelMapAceeptVo.getSearchText().trim().equals("")) {
            queryWrapper2.and(originWdInfoQueryWrapper -> {
                originWdInfoQueryWrapper.like("wd_name", channelMapAceeptVo.getSearchText() );
            });
        }

        //城市等级分类
        if(channelMapAceeptVo.getCityTier() != null && channelMapAceeptVo.getCityTier().length > 0){
            List<String> tierCode = new ArrayList<>();
            for (String s : channelMapAceeptVo.getCityTier()) {
                List<String> list = initMapUtil.getInitCityTierListMap(s);
                if (list != null){
                    tierCode.addAll(list);
                }
            }
            queryWrapper2.and(wdInfoQueryWrapper -> {
                wdInfoQueryWrapper.in("addr_code",tierCode);
            });
        }
        queryWrapper2.groupBy("addr_code");

        //5.统计
        int total = 0;
        List<WdInfo> wdInfos = wdInfoDao.selectList(queryWrapper2);
        for (WdInfo wdInfo : wdInfos) {
            if("province".equals(channelMapAceeptVo.getRankType())){
                statistics.put(wdInfo.getAddrCode().substring(0,2),statistics.get(wdInfo.getAddrCode().substring(0,2))+wdInfo.getAudit());
            } else if ("city".equals(channelMapAceeptVo.getRankType())) {
                statistics.put(wdInfo.getAddrCode().substring(0,4),statistics.get(wdInfo.getAddrCode().substring(0,4))+wdInfo.getAudit());
            } else if ("zone".equals(channelMapAceeptVo.getRankType())) {
                statistics.put(wdInfo.getAddrCode().substring(0,6),statistics.get(wdInfo.getAddrCode().substring(0,6))+wdInfo.getAudit());
            }
            total+=+wdInfo.getAudit();
        }


        //5.计算radio
        for (WdCountBody wdCountBody : result) {
            String code =  null;
            if("province".equals(channelMapAceeptVo.getRankType())){
                code = wdCountBody.getAddrCode().substring(0,2);
            }else if("city".equals(channelMapAceeptVo.getRankType())){
                code = wdCountBody.getAddrCode().substring(0,4);
            }else if("zone".equals(channelMapAceeptVo.getRankType())){
                code = wdCountBody.getAddrCode().substring(0,6);
            }

            if(statistics.get(code) != null){
                wdCountBody.setCount(statistics.get(code));
            }else {
                wdCountBody.setCount(0);
            }

            if (total != 0){
                BigDecimal bigDecimal = new BigDecimal((float) wdCountBody.getCount() / total);
                float v = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
                wdCountBody.setRadio(v);
            }
            else
                wdCountBody.setRadio(0.0f);
        }

        //5.封装结果
        WdCount wdCount = new WdCount();
        wdCount.setWdCountBodyList(result);
        wdCount.setTotal(total);
        return wdCount;
    }


    /**
     * 地图模式：查看省级地区的网点数量（v2.0）
     * @param channelMapAceeptVo
     * @return
     */
    @Override
    public WdCount areaProvince(ChannelMapAceeptVo channelMapAceeptVo) {
        //1.找到所有需要封装的 地区信息
        List<AddrCategory> addrCategories = findCenterAddrCode(channelMapAceeptVo.getRankType(),channelMapAceeptVo.getAddrCode());

        //2.组装地区信息
        List<WdCountBody> result = new ArrayList<>();
        HashMap<String,Integer> statistics = new HashMap<>(); //初始化统计信息
        for (AddrCategory addrCategory : addrCategories) {
            WdCountBody wdCountBody = new WdCountBody();

            //组装
            wdCountBody.setLat(addrCategory.getLatGd());
            wdCountBody.setLng(addrCategory.getLngGd());
            wdCountBody.setAddrCode(addrCategory.getAddrCode());
            wdCountBody.setName(addrCategory.getProvince());
            statistics.put(addrCategory.getAddrCode().substring(0,2),0);
            result.add(wdCountBody);
        }


        //3.找到符合条件的网点信息
        QueryWrapper<WdInfo> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.select("addr_code","count(*) as audit");
        assembleQueryWrapper(queryWrapper2,channelMapAceeptVo);
        queryWrapper2.groupBy("addr_code");


        //4.统计各个地区网点数量
        int total = 0;
        List<WdInfo> wdInfos = wdInfoDao.selectList(queryWrapper2);
        for (WdInfo wdInfo : wdInfos) {
            statistics.put(wdInfo.getAddrCode().substring(0,2),statistics.get(wdInfo.getAddrCode().substring(0,2))+wdInfo.getAudit());
            total+=+wdInfo.getAudit();
        }


        //5.计算radio
        for (WdCountBody wdCountBody : result) {
            String code =  null;
            code = wdCountBody.getAddrCode().substring(0,2);
            if(statistics.get(code) != null){
                wdCountBody.setCount(statistics.get(code));
            }else {
                wdCountBody.setCount(0);
            }

            if (total != 0){
                BigDecimal bigDecimal = new BigDecimal((float) wdCountBody.getCount() / total);
                float v = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
                wdCountBody.setRadio(v);
            }
            else
                wdCountBody.setRadio(0.0f);
        }

        //5.封装结果
        WdCount wdCount = new WdCount();
        wdCount.setWdCountBodyList(result);
        wdCount.setTotal(total);
        return wdCount;
    }

    /**
     * 地图模式：查看市级地区的网点数量（v2.0）
     * @param channelMapAceeptVo
     * @return
     */
    @Override
    public WdCount areaCity(ChannelMapAceeptVo channelMapAceeptVo) {
        //1.根据不同级别得到 需要查询的 网点地区码
        CompletableFuture<List<String>> completableFuture1 = CompletableFuture.supplyAsync(() -> {
            return findWdAddrCode(channelMapAceeptVo.getRankType(),channelMapAceeptVo.getAddrCode());
        });

        //2.找到所有需要封装的 地区信息
        CompletableFuture<List<AddrCategory>> completableFuture2 = CompletableFuture.supplyAsync(() -> {
            return findCenterAddrCode(channelMapAceeptVo.getRankType(),channelMapAceeptVo.getAddrCode());
        });

        List<AddrCategory> addrCategories = completableFuture2.join();

        //3.组装地区信息
        List<WdCountBody> result = new ArrayList<>();
        HashMap<String,Integer> statistics = new HashMap<>(); //初始化统计信息
        for (AddrCategory addrCategory : addrCategories) {
            WdCountBody wdCountBody = new WdCountBody();

            //组装
            wdCountBody.setLat(addrCategory.getLatGd());
            wdCountBody.setLng(addrCategory.getLngGd());
            wdCountBody.setAddrCode(addrCategory.getAddrCode());
            wdCountBody.setName(addrCategory.getCity());
            statistics.put(addrCategory.getAddrCode().substring(0,4),0);
            result.add(wdCountBody);
        }


        //4.组装条件构造器找到网点信息网点
        //2.1 渠道、地区分类
        List<String> wdAddrCodes = completableFuture1.join();
        QueryWrapper<WdInfo> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.select("addr_code","count(*) as audit");
        queryWrapper2.in("wd_type_code", channelMapAceeptVo.getChannel()).and(originWdInfoQueryWrapper -> {
            originWdInfoQueryWrapper.in("addr_code",wdAddrCodes);
        });

        //搜索字段
        if (channelMapAceeptVo.getSearchText() != null && !channelMapAceeptVo.getSearchText().trim().equals("")) {
            queryWrapper2.and(originWdInfoQueryWrapper -> {
                originWdInfoQueryWrapper.like("wd_name", channelMapAceeptVo.getSearchText() );
            });
        }

        //城市等级分类
        if(channelMapAceeptVo.getCityTier() != null && channelMapAceeptVo.getCityTier().length > 0){
            List<String> tierCode = new ArrayList<>();
            for (String s : channelMapAceeptVo.getCityTier()) {
                List<String> list = initMapUtil.getInitCityTierListMap(s);
                if (list != null){
                    tierCode.addAll(list);
                }
            }
            queryWrapper2.and(wdInfoQueryWrapper -> {
                wdInfoQueryWrapper.in("addr_code",tierCode);
            });
        }
        queryWrapper2.groupBy("addr_code");

        //5.统计
        int total = 0;
        List<WdInfo> wdInfos = wdInfoDao.selectList(queryWrapper2);
        for (WdInfo wdInfo : wdInfos) {
            statistics.put(wdInfo.getAddrCode().substring(0,4),statistics.get(wdInfo.getAddrCode().substring(0,4))+wdInfo.getAudit());
            total+=+wdInfo.getAudit();
        }


        //5.计算radio
        for (WdCountBody wdCountBody : result) {
            String code =  null;
            code = wdCountBody.getAddrCode().substring(0,4);
            if(statistics.get(code) != null){
                wdCountBody.setCount(statistics.get(code));
            }else {
                wdCountBody.setCount(0);
            }

            if (total != 0){
                BigDecimal bigDecimal = new BigDecimal((float) wdCountBody.getCount() / total);
                float v = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
                wdCountBody.setRadio(v);
            }
            else
                wdCountBody.setRadio(0.0f);
        }

        //5.封装结果
        WdCount wdCount = new WdCount();
        wdCount.setWdCountBodyList(result);
        wdCount.setTotal(total);
        return wdCount;
    }

    /**
     * 地图模式：查看区级地区的网点数量（v2.0）
     * @param channelMapAceeptVo
     * @return
     */
    @Override
    public WdCount areaZone(ChannelMapAceeptVo channelMapAceeptVo) {
        //1.根据不同级别得到 需要查询的 网点地区码
        CompletableFuture<List<String>> completableFuture1 = CompletableFuture.supplyAsync(() -> {
            return findWdAddrCode(channelMapAceeptVo.getRankType(),channelMapAceeptVo.getAddrCode());
        });

        //2.找到所有需要封装的 地区信息
        CompletableFuture<List<AddrCategory>> completableFuture2 = CompletableFuture.supplyAsync(() -> {
            return findCenterAddrCode(channelMapAceeptVo.getRankType(),channelMapAceeptVo.getAddrCode());
        });

        List<AddrCategory> addrCategories = completableFuture2.join();

        //3.组装地区信息
        List<WdCountBody> result = new ArrayList<>();
        HashMap<String,Integer> statistics = new HashMap<>(); //初始化统计信息
        for (AddrCategory addrCategory : addrCategories) {
            WdCountBody wdCountBody = new WdCountBody();

            //组装
            wdCountBody.setLat(addrCategory.getLatGd());
            wdCountBody.setLng(addrCategory.getLngGd());
            wdCountBody.setAddrCode(addrCategory.getAddrCode());
            wdCountBody.setName(addrCategory.getDistrict());
            statistics.put(addrCategory.getAddrCode().substring(0,6),0);
            result.add(wdCountBody);
        }


        //4.组装条件构造器找到网点信息网点
        //2.1 渠道、地区分类
        List<String> wdAddrCodes = completableFuture1.join();
        QueryWrapper<WdInfo> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.select("addr_code","count(*) as audit");
        queryWrapper2.in("wd_type_code", channelMapAceeptVo.getChannel()).and(originWdInfoQueryWrapper -> {
            originWdInfoQueryWrapper.in("addr_code",wdAddrCodes);
        });

        //搜索字段
        if (channelMapAceeptVo.getSearchText() != null && !channelMapAceeptVo.getSearchText().trim().equals("")) {
            queryWrapper2.and(originWdInfoQueryWrapper -> {
                originWdInfoQueryWrapper.like("wd_name", channelMapAceeptVo.getSearchText() );
            });
        }

        //城市等级分类
        if(channelMapAceeptVo.getCityTier() != null && channelMapAceeptVo.getCityTier().length > 0){
            List<String> tierCode = new ArrayList<>();
            for (String s : channelMapAceeptVo.getCityTier()) {
                List<String> list = initMapUtil.getInitCityTierListMap(s);
                if (list != null){
                    tierCode.addAll(list);
                }
            }
            queryWrapper2.and(wdInfoQueryWrapper -> {
                wdInfoQueryWrapper.in("addr_code",tierCode);
            });
        }
        queryWrapper2.groupBy("addr_code");

        //5.统计
        int total = 0;
        List<WdInfo> wdInfos = wdInfoDao.selectList(queryWrapper2);
        for (WdInfo wdInfo : wdInfos) {
            statistics.put(wdInfo.getAddrCode().substring(0,6),statistics.get(wdInfo.getAddrCode().substring(0,6))+wdInfo.getAudit());
            total+=+wdInfo.getAudit();
        }


        //5.计算radio
        for (WdCountBody wdCountBody : result) {
            String code =  null;
            code = wdCountBody.getAddrCode().substring(0,6);

            if(statistics.get(code) != null){
                wdCountBody.setCount(statistics.get(code));
            }else {
                wdCountBody.setCount(0);
            }

            if (total != 0){
                BigDecimal bigDecimal = new BigDecimal((float) wdCountBody.getCount() / total);
                float v = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
                wdCountBody.setRadio(v);
            }
            else
                wdCountBody.setRadio(0.0f);
        }

        //5.封装结果
        WdCount wdCount = new WdCount();
        wdCount.setWdCountBodyList(result);
        wdCount.setTotal(total);
        return wdCount;
    }

    /**
     * 获取网点列表
     * @param channelMapAceeptVo
     * @return
     */
    @Override
    public HashMap list(ChannelMapAceeptVo channelMapAceeptVo) {
        //1.根据条件获取网点
        QueryWrapper<WdInfo> queryWrapper = new QueryWrapper<>();
        assembleQueryWrapper(queryWrapper,channelMapAceeptVo);
        Page<WdInfo> page = new Page<>(channelMapAceeptVo.getPageNum(), channelMapAceeptVo.getPageSize());
        Page<WdInfo> page1 = wdInfoDao.selectPage(page, queryWrapper);

        //2.获取标签并组装
        List<WdTopologicalInfoBo> result = new ArrayList<>();   //返回的封装网点数据集
        for (WdInfo wdInfo : page1.getRecords()) {
            WdTopologicalInfoBo wdTopologicalInfoBo = new WdTopologicalInfoBo(wdInfo);
            wdTopologicalInfoBo.setAddrCodeInfo(initMapUtil.getInitAddrCodeMap(wdInfo.getAddrCode()));
            List<String> tags = wdRedisStoreage.getWdTag(wdInfo);
            wdTopologicalInfoBo.setTag(tags);
            result.add(wdTopologicalInfoBo);
        }

        //3.返回结果集
        page1.setRecords(null);
        HashMap data = new HashMap();
        data.put("data",result);
        data.put("page",page1);

        return data;
    }

    /**
     * 获取区下的具体网点信息
     * @param channelMapAceeptVo
     * @return
     */
    @Override
    public Page<WdInfo> point(ChannelMapAceeptVo channelMapAceeptVo) {
        //1.查找符合条件的网点
        QueryWrapper<WdInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("addr_code",channelMapAceeptVo.getAddrCode()[0])
                .and(wdInfoQueryWrapper -> {
                    wdInfoQueryWrapper.in("wd_type_code", channelMapAceeptVo.getChannel());
                });

        if (channelMapAceeptVo.getSearchText() != null && !channelMapAceeptVo.getSearchText().trim().equals("")) {
            queryWrapper.and(originWdInfoQueryWrapper -> {
                originWdInfoQueryWrapper.like("wd_name", channelMapAceeptVo.getSearchText());
            });
        }

        Page<WdInfo> page = new Page<>(channelMapAceeptVo.getPageNum(), channelMapAceeptVo.getPageSize());
        Page<WdInfo> page1 = wdInfoDao.selectPage(page, queryWrapper);
        return page1;
    }

    /**
     * 获取网点详细信息
     * @param wdId
     * @return
     */
    @Override
    public WdInfo getWdInfo(String wdId) {
        QueryWrapper<WdInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("wd_id",wdId);

        WdInfo wdInfo = wdInfoDao.selectOne(queryWrapper);
        if(wdInfo == null)
            return wdInfo;

        if("1".equals(wdInfo.getWdTypeCode())){
            //门店网点
            QueryWrapper<StoreWd> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("wd_id",wdId);
            StoreWd storeWd = storeWdDao.selectOne(queryWrapper1);
            StoreWdInfoBo storeWdInfoBo = new StoreWdInfoBo(wdInfo);
            storeWdInfoBo.setStoreWd(storeWd);
            storeWdInfoBo.setTags(wdRedisStoreage.getWdTag(wdInfo));
            storeWdInfoBo.setAddrCode(initMapUtil.getInitAddrCodeMap(storeWdInfoBo.getAddrCode()));
            return storeWdInfoBo;
        }else if("2".equals(wdInfo.getWdTypeCode())){
            //小区网点
            QueryWrapper<HouseWd> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("wd_id",wdId);
            HouseWd houseWd = houseWdDao.selectOne(queryWrapper1);
            HouseWdInfoBo houseWdInfoBo = new HouseWdInfoBo(wdInfo);
            houseWdInfoBo.setHouseWd(houseWd);
            houseWdInfoBo.setTags(wdRedisStoreage.getWdTag(wdInfo));
            houseWdInfoBo.setAddrCode(initMapUtil.getInitAddrCodeMap(houseWdInfoBo.getAddrCode()));
            return houseWdInfoBo;
        }else if("3".equals(wdInfo.getWdTypeCode())){
            //楼宇网点
            QueryWrapper<BuildWd> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("wd_id",wdId);
            BuildWd buildWd = buildWdDao.selectOne(queryWrapper1);
            BuildWdInfoBo buildWdInfoBo = new BuildWdInfoBo(wdInfo);
            buildWdInfoBo.setBuildWd(buildWd);
            buildWdInfoBo.setTags(wdRedisStoreage.getWdTag(wdInfo));
            buildWdInfoBo.setAddrCode(initMapUtil.getInitAddrCodeMap(buildWdInfoBo.getAddrCode()));
            return buildWdInfoBo;
        }else if("4".equals(wdInfo.getWdTypeCode())){
            //交通设施网点
            QueryWrapper<TrafficWd> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("wd_id",wdId);
            TrafficWd trafficWd = trafficWdDao.selectOne(queryWrapper1);
            TrafficWdInfoBo trafficWdInfoBo = new TrafficWdInfoBo(wdInfo);
            trafficWdInfoBo.setTrafficWd(trafficWd);
            trafficWdInfoBo.setTags(wdRedisStoreage.getWdTag(wdInfo));
            trafficWdInfoBo.setAddrCode(initMapUtil.getInitAddrCodeMap(trafficWdInfoBo.getAddrCode()));
            return trafficWdInfoBo;
        }else if("5".equals(wdInfo.getWdTypeCode())){
            //企业网点
            QueryWrapper<EnterpriseWd> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("wd_id",wdId);
            EnterpriseWd enterpriseWd = enterpriseWdDao.selectOne(queryWrapper1);
            EnterpriseWdInfoBo enterpriseWdInfoBo = new EnterpriseWdInfoBo(wdInfo);
            enterpriseWdInfoBo.setEnterpriseWd(enterpriseWd);
            enterpriseWdInfoBo.setTags(wdRedisStoreage.getWdTag(wdInfo));
            enterpriseWdInfoBo.setAddrCode(initMapUtil.getInitAddrCodeMap(enterpriseWdInfoBo.getAddrCode()));
            return enterpriseWdInfoBo;
        }

        return wdInfo;
    }


    /**
     * 封装网点过滤条件（v2.0）
     * @param queryWrapper
     * @param channelMapAceeptVo
     */
    public void assembleQueryWrapper(QueryWrapper<WdInfo> queryWrapper,ChannelMapAceeptVo channelMapAceeptVo){
        //1.根据不同级别得到需要查询的 地区码
        List<String> wdAddrCode = findWdAddrCode(channelMapAceeptVo.getRankType(), channelMapAceeptVo.getAddrCode());

        //店龄
        if(channelMapAceeptVo.getStoreAge() != null && !channelMapAceeptVo.getStoreAge().equals("")){
            queryWrapper.and(wdInfoQueryWrapper -> {
                wdInfoQueryWrapper.eq("store_age",channelMapAceeptVo.getStoreAge());
            });
        }

        //地区过滤
        queryWrapper.and(wdInfoQueryWrapper -> {
            wdInfoQueryWrapper.in("addr_code", wdAddrCode);
        });


        //渠道过滤
        if(channelMapAceeptVo.getChannel() != null && channelMapAceeptVo.getChannel().length != 6){
            queryWrapper.and(wdInfoQueryWrapper -> {
                wdInfoQueryWrapper.in("wd_type_code", channelMapAceeptVo.getChannel());
            });
        }

        //城市等级分类
        if(channelMapAceeptVo.getCityTier() != null && channelMapAceeptVo.getCityTier().length > 0){
            List<String> tierCode = new ArrayList<>();
            for (String s : channelMapAceeptVo.getCityTier()) {
                List<String> list = initMapUtil.getInitCityTierListMap(s);
                if (list != null){
                    tierCode.addAll(list);
                }
            }
            queryWrapper.and(wdInfoQueryWrapper -> {
                wdInfoQueryWrapper.in("addr_code",tierCode);
            });
        }


        //是否关联品牌
        if(channelMapAceeptVo.getIsBrand() != null && !channelMapAceeptVo.getIsBrand().equals("")){
            queryWrapper.and(wdInfoQueryWrapper -> {
                wdInfoQueryWrapper.isNotNull("brand_name");
            });
        }

        //是否有联系方式
        if(channelMapAceeptVo.getIsTelephone() != null && !channelMapAceeptVo.getIsTelephone().equals("")){
            queryWrapper.and(wdInfoQueryWrapper -> {
                wdInfoQueryWrapper.isNotNull("teletphone");
            });
        }

        //搜索字段
        if (channelMapAceeptVo.getSearchText() != null && !channelMapAceeptVo.getSearchText() .trim().equals("")) {
            queryWrapper.and(originWdInfoQueryWrapper -> {
                originWdInfoQueryWrapper.like("wd_name", channelMapAceeptVo.getSearchText() );
            });
        }

        //排序字段查询
        if (channelMapAceeptVo.getOrderby() != null && !channelMapAceeptVo.getOrderby().trim().equals("")) {
            PageHelper.orderBy(channelMapAceeptVo.getOrderby());
        }

    }


    /**
     * 找到所有满足条件的网点addrCode
     * @param rankType
     * @param addrCode
     * @return
     */
    private List<String> findWdAddrCode(String rankType,String[] addrCode){
        //1.根据不同级别得到 找到所有 完整的 区码
        List<String> addrCodeList = new ArrayList<>();
        if ("province".equals(rankType)) {
            //addrCode：省码
            for (String string:addrCode)
                addrCodeList.add(string.substring(0, 2));
        } else if ("city".equals(rankType)) {
            //addrCode：省码（只允许有一个）
            String substring = addrCode[0].substring(0, 2); //根据该省码找到旗下的所有的区
            addrCodeList.add(substring);
        } else if ("zone".equals(rankType)) {
            //addrCode：市码（只允许有一个）
            String substring = addrCode[0].substring(0, 4); //根据该市码找到旗下的所有的区
            addrCodeList.add(substring);
        } else {
            for (String string:addrCode)
                addrCodeList.add(string.substring(0, 2));
        }

        //2.找到所有完整的区码
        QueryWrapper<AddrCategory> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.ne("district", "");
        queryWrapper1.and(addrCategoryQueryWrapper -> {
            for (String s : addrCodeList) {
                addrCategoryQueryWrapper.likeRight("addr_code", s).or();
            }
        });
        List<AddrCategory> addrCategories1 = addrCategoryDao.selectList(queryWrapper1);
        List<String> collect = addrCategories1.stream().map(item -> {
            return item.getAddrCode();
        }).collect(Collectors.toList());

        return collect;
    }

    /**
     * 找到需要展示的中心addrCode
     * @param rankType
     * @param addrCode
     * @return
     */
    private List<AddrCategory> findCenterAddrCode(String rankType,String[] addrCode){
        //1.根据不同级别得到 找到所有 完整的 区码
        List<String> addrCodeList = new ArrayList<>();
        if ("province".equals(rankType)) {
            //addrCode：省码
            addrCodeList.addAll(Arrays.asList(addrCode));
        } else if ("city".equals(rankType)) {
            //addrCode：省码（只允许有一个）
            String substring = addrCode[0].substring(0, 2); //根据该省码找到旗下的所有的区
            addrCodeList.add(substring);
        } else if ("zone".equals(rankType)) {
            //addrCode：市码（只允许有一个）
            String substring = addrCode[0].substring(0, 4); //根据该市码找到旗下的所有的区
            addrCodeList.add(substring);
        } else {
            return null;
        }

        //2.找到所有需要封装的 地区信息
        QueryWrapper<AddrCategory> queryWrapper = new QueryWrapper<>();
        if ("province".equals(rankType)) {
            queryWrapper.eq("city", "");
        } else if ("city".equals(rankType)) {
            queryWrapper.eq("district", "").and(addrCategoryQueryWrapper -> {
                addrCategoryQueryWrapper.ne("city", "");
            });
        } else if ("zone".equals(rankType)) {
            queryWrapper.ne("district", "");
        }
        queryWrapper.and(addrCategoryQueryWrapper -> {
            for (String s : addrCodeList) {
                addrCategoryQueryWrapper.likeRight("addr_code", s).or();
            }
        });
        List<AddrCategory> addrCategories = addrCategoryDao.selectList(queryWrapper);
        return addrCategories;
    }
}
