/**
 * 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.modules.sys.utils.UserUtils;
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.Auth;
import com.lightinit.hsdatagateway.modules.gateway.service.AuthService;

/**
 * 鉴权管理Controller
 * @Author: yangrui
 * @version 2019-03-05
 */
@Controller
@RequestMapping(value = "${adminPath}/gateway/auth")
public class AuthController extends BaseController {

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

	@RequiresPermissions("gateway:auth:view")
	@RequestMapping(value = "form")
	public String form(Auth auth, Model model) {
		model.addAttribute("auth", auth);
		return "modules/gateway/auth/authForm";
	}

	@RequiresPermissions("gateway:auth:edit")
	@RequestMapping(value = "save")
	public String save(Auth auth, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, auth)){
			return form(auth, model);
		}
		if (auth.getId() == null) {
			auth.setCreatedBy(UserUtils.getUser().getName());
		}
		String message = "路由ID、应用ID、用户ID重复，操作失败";
		if (authService.isRepetition(auth)) {
			authService.save(auth);
			message = "保存鉴权成功";
		}
		addMessage(redirectAttributes, message);
		return "redirect:"+Global.getAdminPath()+"/gateway/auth/?repage";
	}
	
	@RequiresPermissions("gateway:auth:edit")
	@RequestMapping(value = "delete")
	public String delete(Auth auth, RedirectAttributes redirectAttributes) {
		auth.setDeletedBy(UserUtils.getUser().getName());
		authService.delete(auth);
		addMessage(redirectAttributes, "删除鉴权成功");
		return "redirect:"+Global.getAdminPath()+"/gateway/auth/?repage";
	}

}