/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.lightinit.hsdatagateway.modules.gateway.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lightinit.hsdatagateway.common.utils.CacheUtils;
import com.lightinit.hsdatagateway.common.utils.SpringContextHolder;
import com.lightinit.hsdatagateway.modules.sys.utils.UserUtils;
import com.lightinit.hsdatagateway.modules.gateway.entity.BlackWhiteList;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.lightinit.hsdatagateway.common.config.Global;
import com.lightinit.hsdatagateway.common.persistence.Page;
import com.lightinit.hsdatagateway.common.web.BaseController;
import com.lightinit.hsdatagateway.common.utils.StringUtils;
import com.lightinit.hsdatagateway.modules.gateway.entity.BlackWhiteList;
import com.lightinit.hsdatagateway.modules.gateway.service.BlackWhiteListService;

import java.util.Date;
import java.util.List;

/**
 * 黑白名单
 * @Author: yangrui
 * @version 2019-03-05
 */
@Controller
@RequestMapping(value = "${adminPath}/blackWhite")
public class BlackWhiteListController extends BaseController {

	@Autowired
	private BlackWhiteListService blackWhiteListService;
	
	@ModelAttribute
	public BlackWhiteList get(@RequestParam(required=false) Long id) {
		BlackWhiteList entity = null;
		if (id != null){
			entity = blackWhiteListService.get(id);
		}
		if (entity == null){
			entity = new BlackWhiteList();
		}
		return entity;
	}
	
	@RequiresPermissions("blackWhite:blackWhiteList:view")
	@RequestMapping(value = {"list", ""})
	public String list(BlackWhiteList blackWhiteList, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<BlackWhiteList> page = blackWhiteListService.findPage(new Page<BlackWhiteList>(request, response), blackWhiteList); 
		model.addAttribute("page", page);
		return "modules/gateway/blackwhite/blackWhiteListList";
	}

	@RequiresPermissions("blackWhite:blackWhiteList:view")
	@RequestMapping(value = "form")
	public String form(BlackWhiteList blackWhiteList, Model model) {
		model.addAttribute("blackWhiteList", blackWhiteList);
		return "modules/gateway/blackwhite/blackWhiteListForm";
	}

	@RequiresPermissions("blackWhite:blackWhiteList:edit")
	@RequestMapping(value = "save")
	public String save(BlackWhiteList blackWhiteList, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, blackWhiteList)){
			return form(blackWhiteList, model);
		}
		if (blackWhiteList.getId() == null) {
			blackWhiteList.setCreatedBy(UserUtils.getUser().getName());
			blackWhiteList.setCreatedAt(new Date());
		}
		blackWhiteListService.save(blackWhiteList);
		addMessage(redirectAttributes, "保存黑白名单成功");
		return "redirect:"+Global.getAdminPath()+"/blackWhite/list/?repage";
	}
	
	@RequiresPermissions("blackWhite:blackWhiteList:edit")
	@RequestMapping(value = "delete")
	public String delete(BlackWhiteList blackWhiteList, RedirectAttributes redirectAttributes) {
		blackWhiteList.setDeletedBy(UserUtils.getUser().getName());
		blackWhiteListService.delete(blackWhiteList);
		addMessage(redirectAttributes, "删除黑白名单成功");
		return "redirect:"+Global.getAdminPath()+"/blackWhite/list/?repage";
	}

	/**
	 * 更新缓存
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "updateCache")
	public String updateCache(RedirectAttributes redirectAttributes, Model model) {

		BlackWhiteListService service = SpringContextHolder.getBean(BlackWhiteListService.class);

		List<String> blacks = service.findIpByType("黑名单");
		List<String> whites = service.findIpByType("白名单");

		CacheUtils.put("black", blacks);
		CacheUtils.put("whites", whites);

		addMessage(redirectAttributes, "更新缓存成功");
		return "redirect:"+Global.getAdminPath()+"/blackWhite/list/?repage";
	}

}