package com.gci.boot.modules.demo.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gci.boot.common.util.RedisUtil;
import com.gci.boot.common.vo.Result;
import com.gci.boot.modules.system.entity.SysUser;
import com.gci.boot.modules.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author chenzhiwei
 * @since 2023-08-14
 */
@Slf4j
@RestController
@RequestMapping("/demo/redis")
public class RedisDemoController {

	@Autowired
	private ISysUserService sysUserService;

	/**
	 * 注解方式使用缓存，value是缓存名称，key使用基于参数的spEL
	 *
	 * 获取缓存
	 * @param id
	 * @return
	 */
	@Cacheable(value = "gci:cache:sysUser", key = "#id")
	@GetMapping("/getById")
	public Result<?> getById(String id){
		return Result.ok(sysUserService.getById(id));
	}

	/**
	 * 注解方式使用缓存，value是缓存名称，key使用基于参数的spEL
	 * 缓存清除
	 * @param user
	 * @return
	 */
	@CacheEvict(value = "gci:cache:sysUser", key = "#user.id")
	@DeleteMapping("/delete")
	public Result<?> delete(SysUser user){
		sysUserService.removeById(user.getId());
		return Result.ok();
	}



	@GetMapping("/testRedisUtil")
	public Result<?> testRedisUtil(){
		//设置string，永不过期
		RedisUtil.set("test", "123");
		//设置string，60秒过期
		RedisUtil.set("test", "123", 60);
		//删除，可以传多个key或数组进行批量删除
		RedisUtil.del("test");
		RedisUtil.del("test1", "test2");
		RedisUtil.del(new String[]{"test1", "test2"});
		//判断key是否存在
		RedisUtil.hasKey("test");
		//设置过期时间
		RedisUtil.expire("test", 60);
		//自增2
		RedisUtil.incr("test", 2);
		//自减2
		RedisUtil.decr("test", 2);


		List<String> list = new ArrayList<>();
		//单个元素加入队列，永不过期
		RedisUtil.lSet("test", "a");
		//单个元素加入队列，60秒过期
		RedisUtil.lSet("test", "a", 60);
		//批量入队，永不过期
		RedisUtil.lSet("test", list);
		//批量入队，60秒过期
		RedisUtil.lSet("test", list, 60);
		//获取队列坐标0的元素
		RedisUtil.lGetIndex("test", 0);
		//获取队列坐标从0到2的元素
		RedisUtil.lGet("test", 0,2);
		//获取队列的大小
		RedisUtil.lGetListSize("test");

		return Result.ok();
	}
}
