package com.gaowj.system.back;



import java.sql.SQLException;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;







import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import com.gaowj.business.SystemOpeBusiness;
import com.gaowj.business.action.BasicAction;
import com.gaowj.business.comp.BusinessManager;
import com.gaowj.business.exception.BusinessException;
import com.gaowj.business.util.Page;
import com.gaowj.business.util.RequestUtils;


public class LogAction extends BasicAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 3824623016480456824L;	


	public String log() throws BusinessException, SQLException, ParseException {
		SystemOpeBusiness systemOpeBusiness = BusinessManager.getBusiness(SystemOpeBusiness.class);
		Map<String,Object> query = RequestUtils.getUpdateFieldMap(request, RequestUtils.UPDATEDATAFIELDPREFIX);
		query = StringToDate(query);
		Page<Map<String, Object>> listGrid = systemOpeBusiness.listLogInfo(pageNo, pageSize, query);
		setListdata(listGrid.getItems());
		setPageCount((listGrid.getCount() - 1)/pageSize + 1);
		//
//		java.util.Enumeration names = request.getHeaderNames();
//        System.out.println("===================================================================");
//        while(names.hasMoreElements()){
//        String name = (String) names.nextElement();
//            System.out.println(name + ":" + request.getHeader(name));
//        }
//        System.out.println("now service sessionid :"+request.getSession().getId());
//        System.out.println("===================================================================");
		//
        
       // systemOpeBusiness.insert_logBusiness("log", "查看登陆日志记录:"+listGrid.getItems(),"查看了第"+pageNo+"页日志,共"+getPageCount()+"页,每页显示"+pageSize+"条","见详细","","C_T_SYS_LOGINLOG");
	    return "log";
	}
	
	public String log4j() throws BusinessException, SQLException, ParseException {
		SystemOpeBusiness systemOpeBusiness = BusinessManager.getBusiness(SystemOpeBusiness.class);
		Map<String,Object> query = RequestUtils.getUpdateFieldMap(request, RequestUtils.UPDATEDATAFIELDPREFIX);
		query = StringToDate(query);
		Page<Map<String, Object>> listGrid = systemOpeBusiness.listLog4jInfo(pageNo, pageSize, query);
		setListdata(listGrid.getItems());
		setPageCount((listGrid.getCount() - 1)/pageSize + 1);
	    return "log4j";
	}
	
	public String log4j_update() throws BusinessException {
		String ls_myzd_id=RequestUtils.getString(request,"myzd_id");
		SystemOpeBusiness systemOpeBusiness = BusinessManager.getBusiness(SystemOpeBusiness.class);
		if(StringUtils.isNotEmpty(ls_myzd_id)){
			Map<String,Object> query = new HashMap<String,Object>();
			query.put("ID", ls_myzd_id);
			editdata = systemOpeBusiness.listLog4jInfo(query).get(0);
		}
	    return "log4j_update";
	}
	
	public String logBusiness() throws BusinessException, SQLException, ParseException {
		SystemOpeBusiness systemOpeBusiness = BusinessManager.getBusiness(SystemOpeBusiness.class);
		Map<String,Object> query = RequestUtils.getUpdateFieldMap(request, RequestUtils.UPDATEDATAFIELDPREFIX);
		query = StringToDate(query);
		Page<Map<String, Object>> listGrid = systemOpeBusiness.listLogBusiness(pageNo, pageSize, query);
		setListdata(listGrid.getItems());
		setPageCount((listGrid.getCount() - 1)/pageSize + 1);
	    return "logBusiness";
	}
	
	public String logBusiness_detail() throws BusinessException {
		String ls_myzd_id=RequestUtils.getString(request,"myzd_id");
		SystemOpeBusiness systemOpeBusiness = BusinessManager.getBusiness(SystemOpeBusiness.class);
		if(StringUtils.isNotEmpty(ls_myzd_id)){
			Map<String,Object> query = new HashMap<String,Object>();
			query.put("UUID", ls_myzd_id);
			editdata = systemOpeBusiness.listLogBusiness(query).get(0);
		}
	    return "logBusiness_detail";
	}
	
	private Map<String,Object> StringToDate(Map<String,Object> map) throws SQLException, ParseException{
		Set<String> key = map.keySet();
		for (Iterator it = key.iterator(); it.hasNext();) {
		   String s = (String) it.next();
		   if(map.get(s)!=null && map.get(s).toString().length()>0){
		     if(StringUtils.contains(map.get(s).toString(),"DATE_")){
			   if(map.get(s).toString().split("_").length>=2 && map.get(s).toString().split("_")[1].length()>0){
				 String tempDate = DateFormatUtils.format(new Date(Long.parseLong(map.get(s).toString().split("_")[1])), "yyyy-MM-dd");
			     map.put(s,DateUtils.parseDate(tempDate, "yyyy-MM-dd"));
			   }
		     } 
		   }
		}
		return map;
	}
	
	private List<Map<String,Object>> StringToDate(List<Map<String,Object>> list) throws SQLException, ParseException{
		for(Map<String,Object> map:list){
			map = StringToDate(map);
		}
		return list;
	}

}
