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.WdInfo;
import com.ruoyi.demo.entity.vo.TopologicalWdAceeptVo;
import com.ruoyi.demo.mapper.WdInfoDao;
import com.ruoyi.demo.service.TopologicalCommonService;
import com.ruoyi.demo.utils.InitMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
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 org.springframework.transaction.annotation.Transactional;

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

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

    @Autowired
    InitMapUtil initMapUtil;

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 点位评估中心点查询
     * @param searchText
     * @param size
     * @return
     */
    @Override
    public List<WdInfo> prefixSearch(String searchText,Integer size) {
        QueryWrapper<WdInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.likeRight("wd_name",searchText);
        queryWrapper.and(wdInfoQueryWrapper -> {
            wdInfoQueryWrapper.in("wd_type_code", Arrays.asList("1","2","3","5"));
        });

        int pageSize = size == null ? 8:size;
        Page<WdInfo> page = new Page<>(1,pageSize);
        Page<WdInfo> page1 = wdInfoDao.selectPage(page, queryWrapper);

        return page1.getRecords();
    }

    /**
     * 获取周边点坐标
     * @param topologicalWdAceeptVo
     * @return
     */
    @Override
    public List<WdInfo> map(TopologicalWdAceeptVo topologicalWdAceeptVo) {
        List<String> aroundWdId = new ArrayList<>();

        //1.根据经纬度 找到 周边网点Id 及 距离
        getAroundWdIdList(topologicalWdAceeptVo,aroundWdId,null);
        if (aroundWdId.isEmpty())
            return new ArrayList<>();

        //2.查询符合的周边网点信息
        QueryWrapper<WdInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("wd_id","wd_name","addr_info","addr_code","lat","lng","type_code_by","type_name_by","wd_type_code")
                .in("wd_id",aroundWdId);
        List<WdInfo> wdInfos = wdInfoDao.selectList(queryWrapper);

        return wdInfos;
    }

    /**
     * 根据坐标获取周边网点Id
     * @param topologicalWdAceeptVo
     * @param aroundWdIdList
     * @param wdDistMap 存储 周边网点与中心网点直接的距离
     */
    public void getAroundWdIdList(TopologicalWdAceeptVo topologicalWdAceeptVo, List<String> aroundWdIdList, HashMap<String,Double> wdDistMap){
        //1.根据经纬度 找到 周边网点Id
        // 以经纬度为中心，获取半径不超过最大距离的所有元素
        Point point = new Point(topologicalWdAceeptVo.getLng(),topologicalWdAceeptVo.getLat());

        //半径范围
        Distance distance = new Distance(topologicalWdAceeptVo.getRadius(), RedisGeoCommands.DistanceUnit.METERS);

        //圆
        Circle circle = new Circle(point, distance);
        RedisGeoCommands.GeoRadiusCommandArgs geoRadiusCommandArgs = RedisGeoCommands.GeoRadiusCommandArgs.newGeoRadiusArgs();
        geoRadiusCommandArgs.includeDistance(); //添加参数：返回距离
        GeoResults<RedisGeoCommands.GeoLocation<Object>> radius = redisTemplate.boundGeoOps(topologicalWdAceeptVo.getAddrCode().substring(0, 4)).radius(circle,geoRadiusCommandArgs);

        //解析周边网点Id
        radius.getContent().stream().forEach(item -> {
            //记录网点坐标及距离
            aroundWdIdList.add((String) item.getContent().getName());
            if (wdDistMap != null)
                wdDistMap.put((String) item.getContent().getName(), item.getDistance().getValue());
        });
    }

}
