package com.lightinit.hsdataplatformresdir.controller;

import com.lightinit.hsdataplatformresdir.json.pojo.JsonResult;
import com.lightinit.hsdataplatformresdir.json.pojo.ListResult;
import com.lightinit.hsdataplatformresdir.entity.Cacheserver;
import com.lightinit.hsdataplatformresdir.service.ICacheServerService;
import com.lightinit.hsdataplatformresdir.service.IRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

/**
 * Created by Mr.Yao on 2017/4/13.
 */
@Controller
@RequestMapping("/cache")
public class CacheController {
    @Autowired
    private IRedisService redisService;

    @Autowired
    private ICacheServerService cacheServerService;

    @RequestMapping(value ="view/profile.action",method = RequestMethod.GET)
    public String print_view_cache_list(){
        return "cache/cacheserver";
    }
    @ResponseBody
    @RequestMapping(value = "json/list.action",method = RequestMethod.GET)
    public ListResult<Cacheserver> print_json_cache_server_list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int rows){
        ListResult<Cacheserver> result=new ListResult<Cacheserver>();
        result.setTotal(cacheServerService.QueryCount(null,""));
        result.setRows(cacheServerService.QueryList(null,"",page,rows));
        return result;
    }
    @ResponseBody
    @RequestMapping(value = "json/cache.action",method = RequestMethod.GET)
    public JsonResult print_json_cache_value(@RequestParam String ip, @RequestParam(defaultValue = "0") int type, @RequestParam String key){
        JsonResult result=new JsonResult();
        switch (type){
            case 0:
                Jedis jedis = new Jedis(ip);
                if(jedis.exists(key))
                    result.setMessage(jedis.get(key));
                else
                    result.setMessage("当前键值缓存不存在！");
                break;
            case 1:
                break;
            case 2:
                break;
        }
        result.setStatus(1);
        return result;
    }
}
