package com.mosty.wzzx.controller;

import com.mosty.base.model.entity.wzzx.TbWzXfwz;
import com.mosty.base.utils.Constant;
import com.mosty.common.base.domain.ResponseResult;
import com.mosty.common.redis.service.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 位置-巡防报备控制层
 *
 * @author
 * @since 2022-07-14 15:37:38
 */
@AllArgsConstructor
@RestController
@Api(tags = "巡防位置相关接口")
@RequestMapping("/gxga/xfbb")
public class TbWzXfbbController {

    private final RedisService redisService;
    private final RedisTemplate redisTemplate;

    @GetMapping("/remove")
    @ApiImplicitParam(name = "bbid", value = "报备ID", dataTypeClass = String.class, required = true, paramType = "path")
    @ApiOperation(value = "清除redis数据")
    public ResponseResult<Boolean> locationReceive(String bbid) {
        String redisKey = Constant.WZ_XFWZ + bbid;
        redisService.deleteObject(redisKey);
        return ResponseResult.success(true);
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取报备redis数据")
    public ResponseResult<List<TbWzXfwz>> locationReceive() {
        List<TbWzXfwz> array = new ArrayList<>();
        String key = Constant.WZ_XFWZ + "*";
        Set<String> sets = redisTemplate.keys(key);
        if (!CollectionUtils.isEmpty(sets)) {
            List<TbWzXfwz> list = redisTemplate.opsForValue().multiGet(sets);
            if (!CollectionUtils.isEmpty(list)) {
                list.forEach(item -> {
                    item.setZb(null);
                    array.add(item);
                });
            }
        }
        return ResponseResult.success(array);
    }

    @GetMapping("/getXfgj")
    @ApiOperation(value = "获取redis中的巡组的巡防轨迹信息")
    public ResponseResult<TbWzXfwz> getXfgj(String bbid) {
        if (StringUtils.isEmpty(bbid)) return null;
        String key = Constant.WZ_XFWZ + bbid;
        TbWzXfwz xfwz = redisService.getCacheObject(key);
        return ResponseResult.success(xfwz);
    }

}


