/**
 * 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 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.modules.gateway.entity.CallLog;
import com.lightinit.hsdatagateway.modules.gateway.service.CallLogService;

/**
 * 日志记录Controller
 * @Author yangrui
 * @version 2019-03-07
 */
@Controller
@RequestMapping(value = "${adminPath}/callLog")
public class CallLogController extends BaseController {

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

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

	@RequiresPermissions("gateway:callLog:edit")
	@RequestMapping(value = "save")
	public String save(CallLog callLog, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, callLog)){
			return form(callLog, model);
		}
		callLogService.save(callLog);
		addMessage(redirectAttributes, "保存日志记录成功");
		return "redirect:"+Global.getAdminPath()+"/gateway/callLog/?repage";
	}
	
	@RequiresPermissions("gateway:callLog:edit")
	@RequestMapping(value = "delete")
	public String delete(CallLog callLog, RedirectAttributes redirectAttributes) {
		callLogService.delete(callLog);
		addMessage(redirectAttributes, "删除日志记录成功");
		return "redirect:"+Global.getAdminPath()+"/gateway/callLog/?repage";
	}

}