package com.ruoyi.benyun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.benyun.entity.vo.TopologicalWdAceeptVo;
import com.ruoyi.benyun.mapper.WdInfoDao;
import com.ruoyi.benyun.service.TopologicalCommonService;
import com.ruoyi.benyun.entity.WdInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.benyun.utils.InitMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.GeoResults;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@Service
public class TopologicalCommonServiceImpl implements TopologicalCommonService {
    @Autowired
    WdInfoDao wdInfoDao;

    @Autowired
    InitMapUtil initMapUtil;

    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public List<WdInfo> prefixSearch(String searchText) {
        QueryWrapper<WdInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("wd_name",searchText);
        queryWrapper.and(wdInfoQueryWrapper -> {
            wdInfoQueryWrapper.in("wd_type_code", Arrays.asList("1","2","3","5"));
        });

        PageHelper.startPage(1,8);
        List<WdInfo> wdInfos = wdInfoDao.selectList(queryWrapper);
        PageInfo<WdInfo> pageInfo = new PageInfo<>(wdInfos);
        return pageInfo.getList();
    }

    @Override
    public void search(String searchText, int radius) {

    }

//    @Override
//    public List<WdInfo> map(TopologicalWdAceeptVo topologicalWdAceeptVo) {
//        QueryWrapper<WdInfo> queryWrapper = new QueryWrapper<>();
//
//        //条件构造
//        queryWrapper.in("wd_type_code",topologicalWdAceeptVo.getChannel());
//        queryWrapper.and(wdInfoQueryWrapper -> {
//            wdInfoQueryWrapper.likeRight("geo_hash",topologicalWdAceeptVo.getGeoHash().substring(0,5));
//        });
//
//        //查询结果
//        List<WdInfo> wdInfos = wdInfoDao.selectList(queryWrapper);
//
//        List<WdInfo> list = new ArrayList<>();  //返回结果集
//        GlobalCoordinates source = new GlobalCoordinates(topologicalWdAceeptVo.getLat(), topologicalWdAceeptVo.getLng());
//        for (WdInfo wdInfo : wdInfos) {
//            //1.计算距离
//            GlobalCoordinates target = new GlobalCoordinates(wdInfo.getLat().doubleValue(), wdInfo.getLng().doubleValue());
//            double meter2 = DistanceUtil.getDistanceMeter(source, target, Ellipsoid.WGS84);
//
//            if(meter2 <= topologicalWdAceeptVo.getRadius()){
//                list.add(wdInfo);
//            }
//        }
//        return list;
//    }

    @Override
    public List<WdInfo> map(TopologicalWdAceeptVo topologicalWdAceeptVo) {
        //1.根据中心网点Id找到网点详细信息
        QueryWrapper<WdInfo> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("wd_id",topologicalWdAceeptVo.getCenterWdId());
        WdInfo wdInfo1 = wdInfoDao.selectOne(queryWrapper1);

        //2.根据中心网点找到周边网点Id
        // 以经纬度为中心，获取半径不超过最大距离的所有元素
        Point point = new Point(wdInfo1.getLat().doubleValue(),wdInfo1.getLng().doubleValue());
        //半径范围
        Distance distance = new Distance(topologicalWdAceeptVo.getRadius(), RedisGeoCommands.DistanceUnit.METERS);
        //圆
        Circle circle = new Circle(point, distance);
        GeoResults<RedisGeoCommands.GeoLocation<Object>> radius = redisTemplate.boundGeoOps(wdInfo1.getAddrCode().substring(0, 4)).radius(circle);
        //周边网点Id
        List<String> aroundWdId = new ArrayList<>();
        radius.getContent().stream().forEach(item -> {
            //记录网点坐标及距离
            aroundWdId.add((String) item.getContent().getName());
        });

        //条件构造
        QueryWrapper<WdInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("wd_id","wd_name","addr_info","addr_code","lat","lng","typeCodeBy","typeNameBy","wdTypeCode")
                .in("wd_id",aroundWdId);

        //查询结果
        List<WdInfo> wdInfos = wdInfoDao.selectList(queryWrapper);
        for (WdInfo wdInfo : wdInfos) {
            wdInfo.setAddrCode(initMapUtil.getInitAddrCodeMap(wdInfo.getAddrCode()));
        }

        return wdInfos;
    }

}
