package com.ssm.controller.system;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.ssm.model.system.SysLog;
import com.ssm.model.system.SysUserLoginLog;
import com.ssm.service.system.SysLogService;
import com.ssm.service.system.SysUserLoginLogService;
import com.ssm.util.page.PagedResult;
import com.ssm.web.Const;
import com.ssm.web.RoleLimit;

/**
 * 日志
 * */
@RequestMapping("log")
@Controller
public class SysLogController {

	@Autowired
	SysLogService sysLogService;
	@Autowired
	SysUserLoginLogService userLoginLogService;
	
	@RoleLimit(pid = "log", mid = "sysLog")
	@RequestMapping(value="sysLog")
	public ModelAndView getLog(SysLog sysLog,Integer pageNo,Integer pageSize){
		ModelAndView view=new ModelAndView("log/system_log");
		return view;
	}
	
	@RoleLimit(pid = Const.SYSTEM_ROLE_LIMIT_PID, mid = Const.SYSTEM_ROLE_LIMIT_MID)
	@RequestMapping(value="loadSysLog")
	public String loadSysLog(SysLog sysLog,Integer pageNo,Integer pageSize, Model model) throws ParseException{
		String rangeDate = sysLog.getCd();
		if(StringUtils.isNotBlank(rangeDate)){
			String[] dates = rangeDate.split("-");
			if(dates!= null && dates.length == 2){
				SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
				String startDate = dates[0].trim() + " 00:00:00";
				String endDate = dates[1].trim()+ " 23:59:59";
				sysLog.setStartDate(sdf.parse(startDate));
				sysLog.setEndDate(sdf.parse(endDate));
			}
		}
		PagedResult<SysLog> pagedResult=sysLogService.selectlog(sysLog, pageNo, pageSize);
		model.addAttribute("list", pagedResult);
		return "log/sys_log_data";
	}
	
	/**
	 * 查看日志详细
	 * @return
	 */
	@RoleLimit(pid = Const.SYSTEM_ROLE_LIMIT_PID, mid = Const.SYSTEM_ROLE_LIMIT_MID)
	@RequestMapping(value="logDetail")
	public String logDetail(@RequestParam("logId") Integer id, Model model){
		SysLog log = sysLogService.findById(id);
		model.addAttribute("log", log);
		return "log/log_detail";
	}
	
	/**
	 * 用户登录日志
	 * @return
	 */
	@RoleLimit(pid = "log", mid = "userLoginLog")
	@RequestMapping(value="userLoginLog")
	public String userLoginLog(SysUserLoginLog loginLog){
		return "log/user_login_log";
	}
	/**
	 * 分页查询  用户 登录 日志
	 * @param loginLog
	 * @param pageNo
	 * @param pageSize
	 * @param model
	 * @return
	 * @throws ParseException
	 */
	@RoleLimit(pid = Const.SYSTEM_ROLE_LIMIT_PID, mid = Const.SYSTEM_ROLE_LIMIT_MID)
	@RequestMapping(value="loadUserLoginLog")
	public String loadUserLoginLog(SysUserLoginLog loginLog, Integer pageNo, Integer pageSize, Model model) throws ParseException{
		String rangeDate = loginLog.getCd();
		if(StringUtils.isNotBlank(rangeDate)){
			String[] dates = rangeDate.split("-");
			if(dates!= null && dates.length == 2){
				SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
				String startDate = dates[0].trim() + " 00:00:00";
				String endDate = dates[1].trim()+ " 23:59:59";
				loginLog.setStartDate(sdf.parse(startDate));
				loginLog.setEndDate(sdf.parse(endDate));
			}
		}
		PagedResult<SysUserLoginLog> pagedResult = userLoginLogService.selectlog(loginLog, pageNo, pageSize);
		model.addAttribute("list", pagedResult);
		return "log/user_login_log_data";
	}
	
}
