package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.redis.RedisUtil;
import com.ruoyi.system.mapper.YtjMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * @program: springbootdemo
 * @Date: 2019/1/25 15:03
 * @Author: Mr.Zheng
 * @Description:
 */
@Slf4j
@RequestMapping("/redis")
@RestController
public class RedisController {

    private static int ExpireTime = 60*60*24;   // redis中存储的过期时间60s

    @Resource
    private RedisUtil redisUtil;
    @Autowired
    private YtjMapper ytjMapper;

    @RequestMapping("set")
    public AjaxResult redisset() {

        Long i=0L;
        if(!redisUtil.hasKey("批发和零售业:-a-kang-")) {
            List<Map> map = ytjMapper.setRedis();
            for (Map map1 : map) {
                String key = (String) map1.get("HangYe") + ":" + (String) map1.get("pinyin");
                if(!redisUtil.set(key, map1.get("Zwzh"))){
                   System.err.println(key+"--------------"+map1.get("Zwzh"));
                }
                i++;
            }
        }
        return AjaxResult.success(i);
//        return redisUtil.set(key,value);
    }

    @RequestMapping("get")
    public Object redisget(String key) {
        return redisUtil.get(key);

    }


    /**
     * 指定缓存失效时间
     * @param key 键
     * @return
     */
    @RequestMapping("expire")
    public boolean expire(String key) {
        return redisUtil.expire(key, ExpireTime);
    }
    /**
     * 根据key 获取过期时间
     * @param key 键 不能为null
     * @return 时间(秒) 返回0代表为永久有效
     */
@RequestMapping("getExpire")
    public long getExpire(String key) {
        return redisUtil.getExpire(key);
    }
    /**
     * 判断key是否存在
     * @param key 键
     * @return true 存在 false不存在
     */
  @RequestMapping("hasKey")
    public boolean hasKey(String key) {
        return redisUtil.hasKey(key);
    }
}
