package org.springblade.flow.base.controller;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.springblade.flow.base.ActionResult;
import org.springblade.flow.base.Page;
import org.springblade.flow.base.model.cachemanage.CacheManageInfoVO;
import org.springblade.flow.base.model.cachemanage.CacheManageListVO;
import org.springblade.flow.base.vo.ListVO;
import org.springblade.flow.util.DateUtil;
import org.springblade.flow.util.RedisUtil;
import org.springblade.flow.util.StringUtil;
import org.springblade.flow.util.UserProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 缓存管理
 *
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司
 * @date 2019年9月27日 上午9:18
 */
@Api(tags = "缓存管理", value = "CacheManage")
@RestController
@RequestMapping("/paas/system/CacheManage")
public class CacheManageController {

	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private UserProvider userProvider;

	@ApiOperation("获取缓存列表")
	@GetMapping
	public ActionResult<ListVO<CacheManageListVO>> getList(Page page) {
		String tenantId = userProvider.getTenantId();
		List<CacheManageListVO> list = new ArrayList<>();
		Set<String> data = redisUtil.getAllKeys();
		for (String key : data) {
			if (!StringUtil.isEmpty(tenantId) && key.contains(tenantId)) {
				CacheManageListVO model = new CacheManageListVO();
				model.setName(key);
				model.setOverdueTime(
						new Date((DateUtil.getTime(new Date()) + redisUtil.getLiveTime(key)) * 1000).getTime());
				list.add(model);
			} else if (StringUtil.isEmpty(tenantId)) {
				CacheManageListVO model = new CacheManageListVO();
				model.setName(key);
				model.setOverdueTime(
						new Date((DateUtil.getTime(new Date()) + redisUtil.getLiveTime(key)) * 1000).getTime());
				list.add(model);
			}
		}
		list = list.stream().sorted(Comparator.comparing(CacheManageListVO::getOverdueTime))
				.collect(Collectors.toList());
		if (StringUtil.isNotEmpty(page.getKeyword())) {
			list = list.stream().filter(t -> t.getName().contains(page.getKeyword())).collect(Collectors.toList());
		}
		ListVO vo = new ListVO();
		vo.setList(list);
		return ActionResult.success(vo);
	}

	/**
	 * 获取缓存信息
	 *
	 * @param name 主键值
	 * @return
	 */
	@ApiOperation("获取缓存信息")
	@GetMapping("/{name}")
	public ActionResult info(@PathVariable("name") String name) {
		String json = String.valueOf(redisUtil.getString(name));
		CacheManageInfoVO vo = new CacheManageInfoVO();
		vo.setName(name);
		vo.setValue(json);
		return ActionResult.success(vo);
	}

	/**
	 * 清空所有缓存
	 *
	 * @return
	 */
	@ApiOperation("清空所有缓存")
	@PostMapping("/Actions/ClearAll")
	public ActionResult clearAll() {
		String tenantId = userProvider.getTenantId();
		if ("".equals(tenantId)) {
			Set<String> keys = redisUtil.getAllKeys();
			for (String key : keys) {
				redisUtil.remove(key);
			}
		} else {
			Set<String> data = redisUtil.getAllKeys();
			String clientKey = UserProvider.getToken();
			System.out.println(clientKey);
			for (String key : data) {
				if (key.contains(tenantId)) {
					redisUtil.remove(key);
				}
			}
		}
		return ActionResult.success("清理成功");
	}

	/**
	 * 清空单个缓存
	 *
	 * @param name 主键值
	 * @return
	 */
	@ApiOperation("清空单个缓存")
	@DeleteMapping("/{name}")
	public ActionResult clear(@PathVariable("name") String name) {
		redisUtil.remove(name);
		return ActionResult.success("清空成功");
	}
}
