<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="java.util.*" %>
<%@ page import="net.sz600.tool.SzDateHelp"%>
<%@ page import="net.sz600.basicdata.*" %>
<%@ page import="net.sz600.tool.SzConvert" %>
<%@ page import="net.sz600.database.SzPageInfo" %>
<%@ page import="net.sz600.system.*" %>
<%@ page import="net.sz600.mail.*" %>
<%@ include file="/sessionInfo.jsp"%>

<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="<%=strSkin_SESSION%>/css2007.css" rel="stylesheet" type="text/css">
<link href="<%=strSkin_SESSION%>/css2007detail.css" rel="stylesheet" type="text/css">
<script language="javascript" src="../share/js/commonTool.js"></script>
</head>
<%
	
	//查询时间段
	String sSearchBegin = SzConvert.strWriteData(request.getParameter("txtSearchBegin"));
	String sSearchEnd = SzConvert.strWriteData(request.getParameter("txtSearchEnd"));
	//获取选择的部门信息
	String BMID = SzConvert.toZeroStr(request.getParameter("BMID"));
	String BMMC = SzConvert.strWriteData(request.getParameter("BMMC"));
	//员工姓名
	String YGXM = SzConvert.strWriteData(request.getParameter("YGXM"));
	//如果开始时间和结束时间都为空，就默认为当前月
	Date curDate = null;
	if(sSearchBegin.equals("")&&sSearchEnd.equals("")){
		curDate = SzDateHelp.getFirstDateMonth();
		sSearchBegin = SzDateHelp.dateToYMD(SzDateHelp.getFirstDateMonth());
		sSearchEnd = SzDateHelp.dateToYMD(SzDateHelp.getLastDateMonth());
	}
	StringBuffer sCond = new StringBuffer("");
	//获取对象信息
    YjStat yjStat = new YjStat();
	
	//========获取部门信息=============
	SzLogin szLogin = new SzLogin();
	SzLoginInfo szLoginInfo = (SzLoginInfo)szLogin.getInfoByCond(" DLID = " + iDLID_SESSION);
	SzDepart depart = new SzDepart();
	StringBuffer departStr = new StringBuffer(" 1=1 ");
	String strManageDepartID = "";
	
	ArrayList loginList = new ArrayList();
	//是管理员时查询所有部门，否则查询当前用户的部门/选择的部门
	if (BMMC.equals("")){
		 if (!isBarManager_SESSION){
			SzUserHelp szUserHelp = new SzUserHelp(); 
			strManageDepartID = SzConvert.toZeroStr(szUserHelp.getManageDepartID(iDLID_SESSION));			
			if (strManageDepartID.equals("0")){
				departStr.append(" and BMID = "+szLoginInfo.getDLBM());
			} else {
				departStr.append(" and BMID IN( " + strManageDepartID + ")");
				sCond.append(" and BMID IN( " + strManageDepartID + ")");
			}
		 }
	 } else {
		 departStr.append(" and BMMC = '"+BMMC+"'");
	 }
	 //2015-01增加排序
	 departStr.append(" ORDER BY BMBH ASC,BMMC ASC");	
	 ArrayList departList = depart.getArrayList(departStr.toString());
	 SzDepartInfo departInfo = null;
	 //如果是管理员，还要显示没有部门的员工
	 if(BMMC.equals("")&&isBarManager_SESSION){
		 departInfo = new SzDepartInfo();
		 departInfo.setBMID(0);
		 departInfo.setBMMC("其他");
		 departList.add(departInfo);
	 }
	 int departSize = departList!=null?departList.size():0;
	 
	String searchStr = yjStat.getStat_SQL(BMMC,YGXM, sSearchBegin, sSearchEnd, sCond.toString());

	//取得用户
	ArrayList loginInfoList = SzUserHelp.getUserStaticList();
	//根据姓名查询
	if (!YGXM.equals("")){
		szLoginInfo = (SzLoginInfo)szLogin.getInfoByCond(" DLXM='" + YGXM + "'");
		loginInfoList = new ArrayList();
		if (szLoginInfo != null){
			loginInfoList.add(szLoginInfo);
		}
	}
	int loginCount = loginInfoList != null? loginInfoList.size():0;
  	//用于存放所有可看员工的ID
	 StringBuffer loginIDStr = new StringBuffer();
	 //用于存放所有部门的员工
	 Map loginMap = new HashMap();
	 int departID = 0;
	 //用于存入某个部门的员工
	 ArrayList userList = new ArrayList();
	 for (int i=0;i < departSize;i++ ){
		 userList = new ArrayList();
		 departInfo = (SzDepartInfo)departList.get(i);
		 departID = departInfo.getBMID();
		 userList = new ArrayList();
		 for (int j = 0;j<loginCount;j++){
			 szLoginInfo = (SzLoginInfo)loginInfoList.get(j);
			 //System.out.println(szLoginInfo.getDLBM()+":"+szLoginInfo.getDLXM());
			 if (isBarManager_SESSION && szLoginInfo.getDLBM() == departID){
				 userList.add(szLoginInfo);
				 loginIDStr.append(szLoginInfo.getDLID()+",");
			 } else if (strManageDepartID.equals("0") && szLoginInfo.getDLID() == iDLID_SESSION){
				 userList.add(szLoginInfo);
				 loginIDStr.append(szLoginInfo.getDLID()+",");
			 } else if (!strManageDepartID.equals("0") && szLoginInfo.getDLBM() == departID){
				 userList.add(szLoginInfo);
				 loginIDStr.append(szLoginInfo.getDLID()+",");
			 }
			 
		 }
		 loginMap.put(departID,userList);
	 }
	 loginIDStr.append("-1");
	 

	Map YjSendCountMap = yjStat.getYjSendCount(loginIDStr.toString(), searchStr);
	Map YjReceiveCountMap = yjStat.getYjReceiveCount(loginIDStr.toString(), searchStr);
	Map YjNotReadCountMap = yjStat.getYjNotReadCount(loginIDStr.toString(), searchStr);
	Map YjSendAnnexCountMap = yjStat.getYjSendAnnexCount(loginIDStr.toString(), searchStr);
	Map YjReceiveAnnexCountMap = yjStat.getYjReceiveAnnexCount(loginIDStr.toString(), searchStr);
	Map YjSendSizeMap = yjStat.getYjSendSize(loginIDStr.toString(), searchStr);
	Map YjReceiveSizeMap = yjStat.getYjReceiveSize(loginIDStr.toString(), searchStr);
	Map[] allMap = new Map[]{YjReceiveCountMap,YjReceiveAnnexCountMap,YjNotReadCountMap,
			YjReceiveSizeMap,YjSendCountMap,YjSendAnnexCountMap,YjSendSizeMap};
	int mapSize = allMap.length;
	
%>
<!-----上面的工具栏------>
<%@ include file="/share/title.jsp"%>
<table width="100%">
  <form name="formBar" method="post">
  <tr>
  	<td valign="bottom" style="padding-bottom: 5px;padding-top: 5px;" align="center">
  		<h4 style="display: inline;">邮件统计</h4>
  	</td>
  </tr>
  <tr>
    <td>
      部门<input type="text" name="BMMC" class="txtClassSearch" value="<%=BMMC%>" OnDblClick="javascript:OpenSingleDepartLevel('../','','formBar.BMMC');">
      姓名<input type="text" name="YGXM" class="txtClassSearch" value="<%=YGXM%>" OnDblClick="javascript:OpenWin32('../','','formBar.YGXM');">
      开始<input type="text" name="txtSearchBegin" class="txtClassSearch" value="<%=sSearchBegin%>">
      <%=SzPageHelp.calendar2Select1("formBar.txtSearchBegin")%>
      结束<input type="text" name="txtSearchEnd" class="txtClassSearch" value="<%=sSearchEnd%>">
      <%=SzPageHelp.calendar2Select1("formBar.txtSearchEnd")%>
      <%=SzPageHelp.button("查询", "javascript:FormSubmit(formBar,'stat.jsp');")%>
    </td>
  </tr>
  </form>
</table>

<!-----信息列表----->
<table width="100%" border="1" bordercolorlight="#C0C0C0" bordercolordark="#FFFFFF" class="tableClass" cellpadding="1" cellspacing="0">
  <form name="formList" method="post">
  <tr class="trClassTitle_c">
    <td rowspan="2">发布部门</td>
    <td rowspan="2">姓名</td>
    <td colspan="4">收件</td>
    <td colspan="3">发件</td>
  </tr>
  <tr class="trClassTitle_c">
    <td>收件数量</td>
    <td>附件数量</td>
    <td>未读数量</td>
    <td>总大小</td>
    
    <td>发件数量</td>
    <td>附件数量</td>
    <td>总大小</td>
  </tr>
<%
	Map<Integer,Integer> xxCountOfType = new HashMap<Integer,Integer>();
	Map<Integer,Integer> xxCountOfBM = new HashMap<Integer,Integer>();
	StringBuffer outSB = new StringBuffer();
	int rowCount = 5;
	int count = 0;
	int uCont = 0;
	int departCount = 0;
	int sum = 0;
	Object obj = null;
	int sBMID = 0;
	String sBMMC = "";
	int loginSize = 0;
	int Lid = 0;	//用户ID
	String Lxm = "";	//用户姓名
    for (int i=0;i<departSize;i++){
    	departInfo = (SzDepartInfo)departList.get(i);
 		sBMID = departInfo.getBMID();
 		sBMMC = departInfo.getBMMC();
 		loginList = (ArrayList)loginMap.get(sBMID);
 		loginSize = loginList == null? 0:loginList.size();
    	departCount = 0;
    	xxCountOfType = new HashMap<Integer,Integer>();
    	
    	for(int j = 0;j<loginSize;j++){
    		szLoginInfo = (SzLoginInfo)loginList.get(j);
 			if (szLoginInfo == null){
 				continue;
 			}
 			outSB = new StringBuffer();
 			uCont = 0;
 			Lid = szLoginInfo.getDLID();
 			Lxm = szLoginInfo.getDLXM();
 			out.println("<tr align=\"center\">");
    		if (j == 0){
    	    	out.println("<td rowspan=\""+ loginSize +"\">"+ sBMMC +"&nbsp;</td>");
    		}
    		out.println("<td>"+ Lxm +"&nbsp;</td>");
    		for(int m =0;m<mapSize ;m++){ 
            	obj = allMap[m].get(sBMMC+ ":" + Lxm);
            	
            	count = obj != null? Integer.valueOf(obj.toString()):0;
            	outSB.append("<td style=\"color: "+((m==2)&&count>0? "red":"block")+";\">"+ (m==3||m==6? SzConvert.formatFloat2Str(count*1.0/1000/1000)+"M":count) +"&nbsp;</td>");
            	
            	uCont += count;
            	if (xxCountOfType.containsKey(m)){
            		int value = xxCountOfType.get(m);
            		xxCountOfType.put(m,count+value);
            	} else {
            		xxCountOfType.put(m,count);
            	}
        	}
        	//out.println("<td align=\"center\">"+ uCont +"&nbsp;</td>");
    		out.println(outSB.toString());
    		outSB.append("</tr>");
    		departCount += uCont;
 		}
    	if (loginSize > 0){
	    	out.println("<tr class=\"trClassTitle2\" align=\"center\">");
	        out.println("<td style=\"color: blue;\">部门合计</td>");
	        out.println("<td>&nbsp;</td>");
	        //out.println("<td style=\"color: blue;\" align=\"center\">"+ departCount +"&nbsp;</td>");
	        for(int j =0;j<mapSize ;j++){ 
	        	int bmCount = xxCountOfType.get(j);
	        	
	        	out.println("<td style=\"color: "+((j==2)&&bmCount>0? "red":"blue")+";\">"+ (j==3||j==6? SzConvert.formatFloat2Str(bmCount*1.0/1000/1000)+"M":bmCount) +"&nbsp;</td>");
	        	if (xxCountOfBM.containsKey(j)){
            		int value = xxCountOfBM.get(j);
            		xxCountOfBM.put(j,bmCount+value);
            	} else {
            		xxCountOfBM.put(j,bmCount);
            	}
	        }
	        out.println("<tr>");
    	}
    	sum += departCount;
    }
    out.println("<tr class=\"trClassTitle2\" align=\"center\">");
    out.println("<td style=\"color: red;\">合计</td>");
    out.println("<td>&nbsp;</td>");
    //out.println("<td style=\"color: red;\" align=\"center\">"+ sum +"&nbsp;</td>");
    for(int j =0;j<mapSize ;j++){ 
    	obj = xxCountOfBM.get(j);
    	int bmCount = obj != null? Integer.valueOf(obj.toString()):0;
    	out.println("<td style=\"color: "+((j==2)&&bmCount>0? "red":"blue")+";\">"+ (j==3||j==6? SzConvert.formatFloat2Str(bmCount*1.0/1000/1000)+"M":bmCount) +"&nbsp;</td>");
    }
    out.println("<tr>");
    
    loginMap = null;
    YjReceiveCountMap = null;
    YjReceiveAnnexCountMap = null;
    YjNotReadCountMap = null;
    YjReceiveSizeMap = null;
    YjSendCountMap = null;
    YjSendAnnexCountMap = null;
    YjSendSizeMap = null;
%>

  </form>
</table>

<!-----下面的工具栏------>
<table width="100%">
  <form name="formPage" method="post">
  </form>
</table>

<!-----这里包含要提交的内容--->
<form name="formHidden" method="post">
</form>
<%
    ////如果要使用日历控件，必须输出这个calendarFrame
    out.println(SzPageHelp.calendar2Frame1());
%>
