package com.ylcm.sys.controller;

import java.io.ByteArrayOutputStream;
import java.util.List;

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

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ylcm.sys.common.Constants;
import com.ylcm.sys.common.ExcelUtil;
import com.ylcm.sys.common.Util;
import com.ylcm.sys.domain.Log;
import com.ylcm.sys.service.LogService;

import net.sf.json.JSONObject;

@Controller
public class LogController {
	
	private Logger logger = LoggerFactory.getLogger(LogController.class);
	
	@Autowired
	private LogService logService;
	
	/**
	 * 日志页面
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/log_list.do")
	public ModelAndView logList(ModelMap model){
        model.put("menu", "log_list");
        model.put("parentMenu", "admin");
		return new ModelAndView("log/log_list");
	}
	
	
	/**
	 * 获取日志列表
	 * 
	 * @param req
	 * @param resp
	 * @param type    日志类型
	 * @param startDate   开始日期
	 * @param endDate	  结束日期
	 * @param pageSize	  每页显示的数据
	 * @param pageNo	  第几页
	 * @return
	 */
	@RequestMapping(value="/get_log_list.do", produces="application/json;charset=utf-8")
	@ResponseBody
	public String getLogList(HttpServletRequest req, HttpServletResponse resp,String name, String startDate, String endDate,String pageSize, String pageNo){
		JSONObject result = new JSONObject();
		
		if(StringUtils.isNotBlank(startDate)){
			startDate = startDate + " 00:00:00";
		}
		if(StringUtils.isNotBlank(endDate)){
			endDate = endDate + " 23:59:59";
		}
		List<Log> logs = this.logService.getLogList(name, startDate, endDate, pageSize, pageNo);
		Integer total = this.logService.getLogCount(name, startDate, endDate);
		
		if(Util.isNotNull(logs)){
			result.put("code", Constants.CODE_SUCCESS);
			result.put("msg", Constants.MSG_SUCCESS);
			result.put("data", logs);
			result.put("total", total);
		}else{
			result.put("code", Constants.CODE_FAIL);
		}	
		return result.toString();
	}
	
	/**
	 * 导出日志
	 * 
	 * @param request
	 * @param response
	 * @param type
	 * @param startDate
	 * @param endDate
	 */
	@RequestMapping(value = "/export_excel_for_log.do")
	@ResponseBody
	private void exportExcelForLog(HttpServletRequest request, HttpServletResponse response,String name, String startDate, String endDate) {

		if(StringUtils.isNotBlank(startDate)){
			startDate = startDate + " 00:00:00";
		}
		if(StringUtils.isNotBlank(endDate)){
			endDate = endDate + " 23:59:59";
		}
		List<Log> logs = this.logService.getLogList(name, startDate, endDate, null, null);

		String fileName = "系统日志.xls";

		ByteArrayOutputStream os = new ByteArrayOutputStream();
		try {
			logService.createExcelForLog(logs).write(os);
			ExcelUtil.exportExcel(response, fileName, os);
		} catch (Exception e) {
			logger.error("导出excel失败:" , e.getMessage());
		}
	}
}
