package com.ruoyi.benyun.controller;

import com.ruoyi.benyun.entity.vo.TopologicalWdAceeptVo;
import com.ruoyi.benyun.service.TopologicalCommonService;
import com.ruoyi.benyun.service.WdInfoService;
import com.ruoyi.benyun.constant.RedisContant;
import com.ruoyi.benyun.entity.WdInfo;
import com.ruoyi.common.core.domain.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/poi/common")
public class TopologicalCommonController {
    @Autowired
    TopologicalCommonService topologicalCommonService;

    @Autowired
    WdInfoService wdInfoService;


    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping("/prefixSearch")
    public R prefixSearch(String searchText){
        List<WdInfo> wdInfos = topologicalCommonService.prefixSearch(searchText);
        return R.ok(wdInfos);
    }
    @RequestMapping("/range")
    public R range(){
        HashMap hashMap = new HashMap<>();
        hashMap.put("500m",500);
        hashMap.put("1km",1000);
        hashMap.put("2km",2000);
        return R.ok(hashMap);
    }

    @RequestMapping("/map")
    public R map(TopologicalWdAceeptVo topologicalWdAceeptVo){
        String md5 = topologicalWdAceeptVo.getHash();
        //2.查看redis中是否存在有缓存
        List<WdInfo> wdCount = (List<WdInfo>) redisTemplate.boundHashOps(RedisContant.TOPOLOGICAL_COMMON_MAP).get(md5);
        if (wdCount != null) {
            return R.ok(wdCount);
        }

        List<WdInfo> map = topologicalCommonService.map(topologicalWdAceeptVo);
        redisTemplate.boundHashOps(RedisContant.TOPOLOGICAL_COMMON_MAP).put(md5,map);
        redisTemplate.expire(RedisContant.TOPOLOGICAL_COMMON_MAP,RedisContant.TOPOLOGICAL_COMMON_MAP_TIME, TimeUnit.MINUTES); //30分钟

        return R.ok(map);
    }

}
