package com.css.simulation.resource.common.adapter.controller;

import api.common.pojo.common.ResponseBodyVO;
import api.common.pojo.constants.DictConstants;
import api.common.pojo.param.RedisParameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Duration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * https://blog.csdn.net/lzb348110175/article/details/105349109/
 */
@RestController
@RequestMapping("/redis")
public class RedisController {

    @Autowired
    StringRedisTemplate redisTemplate;


    @PostMapping("/get")
    public ResponseBodyVO<String> get(@RequestBody @Validated RedisParameter redisParameter) {
        //1 获取 key 对应的值
        String value = redisTemplate.opsForValue().get(redisParameter.getKey());

        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, "请求成功！", value);
    }

    @PostMapping("/set")
    public ResponseBodyVO<String> set(@RequestBody @Validated RedisParameter redisParameter) {
        if (redisParameter.getMinutes() == 0) { //2 存储键值对，永不过期。
            redisTemplate.opsForValue().set(redisParameter.getKey(), redisParameter.getValue());
        } else { //2 存储键值对并设置时间。
            redisTemplate.opsForValue().set(redisParameter.getKey(), redisParameter.getValue(), Duration.ofMinutes(redisParameter.getMinutes()));
        }


        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
    }

    @PostMapping("/getExpire")
    public ResponseBodyVO<Long> getExpire(@RequestBody @Validated RedisParameter redisParameter) {
        //3 获取指定 key 的剩余时间，单位秒。
        Long time3 = redisTemplate.getExpire(redisParameter.getKey());
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, time3);
    }


    @PostMapping("/delete")
    public ResponseBodyVO<String> delete(@RequestBody @Validated RedisParameter redisParameter) {
        //4 根据 key 删除
        redisTemplate.delete(redisParameter.getKey());
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
    }

    @PostMapping("/setDictLists")
    public ResponseBodyVO<String> setDictLists(@RequestBody List<RedisParameter> redisParameterList) {
        if (redisParameterList != null && redisParameterList.size() > 0) {
            for (RedisParameter parameter : redisParameterList) {
                redisTemplate.opsForValue().set(DictConstants.BASE_KEY + DictConstants.LIST_KEY + parameter.getKey().toUpperCase(), parameter.getValue());
            }
        }
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
    }

    @PostMapping("/setDictMaps")
    public ResponseBodyVO<String> setDictMaps(@RequestBody List<RedisParameter> redisParameterList) {
        if (redisParameterList != null && redisParameterList.size() > 0) {
            for (RedisParameter parameter : redisParameterList) {
                redisTemplate.opsForValue().set(DictConstants.BASE_KEY + DictConstants.MAP_KEY + parameter.getKey().toUpperCase(), parameter.getValue());
            }
        }
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS);
    }

    @PostMapping("/getDictLists")
    public ResponseBodyVO<Map<String, String>> getDictLists(@RequestBody List<String> keyList) {
        Map<String, String> map = new HashMap<>();
        if (keyList != null && keyList.size() > 0) {
            for (String key : keyList) {
                String value = redisTemplate.opsForValue().get(DictConstants.BASE_KEY + DictConstants.LIST_KEY + key.toUpperCase());
                map.put(key, value);
            }
        }
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, map);
    }

    @PostMapping("/getDictMaps")
    public ResponseBodyVO<Map<String, String>> getDictMaps(@RequestBody List<String> keyList) {
        Map<String, String> map = new HashMap<>();
        if (keyList != null && keyList.size() > 0) {
            for (String key : keyList) {
                String value = redisTemplate.opsForValue().get(DictConstants.BASE_KEY + DictConstants.MAP_KEY + key.toUpperCase());
                map.put(key, value);
            }
        }
        return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, map);
    }

}
