package com.ylx.web.controller.massage;

import com.ylx.common.core.domain.R;
import com.ylx.massage.domain.Location;
import com.ylx.massage.domain.TWxUser;
import com.ylx.massage.utils.LocationUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.geo.*;
import org.springframework.data.redis.connection.RedisGeoCommands;
import org.springframework.data.redis.core.BoundGeoOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@RestController
@Api(tags = "redis", description = "redis控制")
@RequestMapping("/geo")
@AllArgsConstructor
public class RedisGeoController {

    private static final String GEO_STAGE = "cities";

    private final RedisTemplate<String, String> redisTemplate;

    @Resource
    private LocationUtil locationUtil;
    /**
     * 初始化数据可以将职位id和经纬度存入redis，
     * 添加职业时增加位置数据
     * 当用户点击附近是，传入经纬度。返回id获得职位信息推送给用户
     */
    @GetMapping("/init")
    @ApiOperation("初始化")
    public void init() {
        // 清理缓存
        redisTemplate.delete(GEO_STAGE);
        Map<String, Point> points = new HashMap<>();
        points.put("shijiazhuang", new Point(114.48, 38.03));
        points.put("xingtang", new Point(114.54, 38.42));
        points.put("guangcheng", new Point(114.84, 38.03));
        points.put("gaoyi", new Point(114.58, 37.62));
        points.put("zhaoxian", new Point(114.78, 37.76));
        points.put("jinxing", new Point(114.13, 38.03));
        points.put("luquan", new Point(114.03, 38.08));
        points.put("xinle", new Point(114.67, 38.33));
        points.put("zhengding", new Point(114.56, 38.13));
        // 添加地理信息
        redisTemplate.boundGeoOps(GEO_STAGE).add(points);
    }

    @PostMapping("/city")
    @ApiOperation("获得附近技师")
    public R<GeoResults<RedisGeoCommands.GeoLocation<String>>> dis(@RequestBody Location locationBo) {
        //设置当前位置
        Point point = new Point(locationBo.getLongitude(), locationBo.getLatitude());
        //设置半径范围
        Metric metric = RedisGeoCommands.DistanceUnit.METERS;
        Distance distance = new Distance(locationBo.getRadius(), metric);
        Circle circle = new Circle(point, distance);
        //设置参数 包括距离、坐标、条数
        RedisGeoCommands.GeoRadiusCommandArgs args = RedisGeoCommands
                .GeoRadiusCommandArgs
                .newGeoRadiusArgs()
                .includeDistance()
                .includeCoordinates()
                .sortAscending()
                .limit(locationBo.getLimit());
        BoundGeoOperations<String, String> stringStringBoundGeoOperations = redisTemplate.boundGeoOps(GEO_STAGE);
        GeoResults<RedisGeoCommands.GeoLocation<String>> radius = stringStringBoundGeoOperations.radius(circle, args);
        return R.ok(radius);
    }

    @ApiOperation("测试")
    @RequestMapping(value = "test", method = RequestMethod.GET)
    public List<TWxUser> test() {
        locationUtil.geoAdd(LocationUtil.GEO_KEY_USER,"o-HEJ6VP2YNi0HPeLaIHCQsQD69s", 112.521465, 37.857166);
        locationUtil.geoAdd(LocationUtil.GEO_KEY_USER,"o-HEJ6RAjBDjFvuZcAdHRX8mIZXw", 116.396151, 39.939414);

        Map<String, Double> shijiazhuang1 = locationUtil.distanceInclude("shijiazhuang1", 30000);

        locationUtil.getDistance("o-HEJ6VP2YNi0HPeLaIHCQsQD69s","o-HEJ6RAjBDjFvuZcAdHRX8mIZXw");
        log.error(shijiazhuang1.toString());

        return null;
    }

}


