<%@ page contentType="text/html;charset=gbk" %>
<%@ 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.docEngine.*" %>
<%@ include file="/sessionInfo.jsp"%>

<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<link href="<%=strSkin_SESSION%>/css2007.css" rel="stylesheet" type="text/css">
<script language="javascript" src="../../share/js/commonTool.js"></script>
</head>
<%
	//获取模块ID
	String moduleId = SzConvert.toZeroStr(request.getParameter("moduleId"));
	//查询时间段
	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("");
	//获取对象信息moduleId
    DocStat docStat = new DocStat(moduleId);
	WdyqType wdyType = new WdyqType(moduleId);
	WdyqTypeInfo typeInfo = null;
	//获取分类信息
	ArrayList typeList = wdyType.getArrayList("1=1 ");
	typeInfo = new WdyqTypeInfo();
	typeInfo.setFLID(0);
	typeInfo.setFLMC("无分类");
	typeList.add(typeInfo);
	int typeSize = typeList!=null? typeList.size():0;
	
	//========获取部门信息=============
	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 XXBMID IN( " + strManageDepartID + ")");
			}
		 }
	 } else {
		 departStr.append(" and BMMC = '"+BMMC+"'");
	 }
	 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;
	//取得本人能管理的所有模块ID
    String strFLID_Manager = "0";
    strFLID_Manager = wdyType.getManagerFLID(iDLID_SESSION);
	sCond.append(" AND XXGSID=" + szCOMPANY_SESSION);
	sCond.append(" AND " + docStat.getCond(iBMID_SESSION, iDLID_SESSION, 
  						isBarManager_SESSION, strFLID_Manager, "0"));
	
	sCond.append(" AND XXID=YDXX");
	String searchStr = docStat.getStat_SQL(BMMC,YGXM, sSearchBegin, sSearchEnd, sCond.toString());
	
	//应读的文档
	Map xxMap = new HashMap();
	//已读的文档
	Map xxYDMap = docStat.getReceiveStatMapByCond("YDBMMC,YDDLXM,COUNT(1)", searchStr + " GROUP BY YDBMMC,YDDLXM");
	//System.out.println( searchStr + " GROUP BY XXBMMC,XXDLXM");
	//取得用户
	ArrayList loginInfoList = SzUserHelp.getUserStaticList();
	//根据姓名查询
	if (!YGXM.equals("")){
		szLoginInfo = (SzLoginInfo)szLogin.getInfoByCond(" DLXM='" + YGXM + "'");
		loginInfoList = new ArrayList();
		if (szLoginInfo != null){
			loginInfoList.add(szLoginInfo);
		}
	}
	
	
	boolean canAdd = true;	//表示当前用的ID有没有加到loginIDStr中
	boolean canSee = false; 	//表示可看的用户
	boolean isBarManger = false;	//表示是否是模块管理员
	int loginCount = loginInfoList != null? loginInfoList.size():0;
  	//用于存放所有可看员工的ID
	 StringBuffer loginIDStr = new StringBuffer();
	 //用于存放所有部门的员工
	 Map loginMap = new HashMap();
	 int departID = 0;
	 //用于存入某个部门的员工
	 ArrayList userList = new ArrayList();
	 int iDLID = 0;		//用户ID
	 int iDLBM = 0;		//部门ID
	 String condStr = "";
	 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);
			 iDLID = szLoginInfo.getDLID();
			 iDLBM = szLoginInfo.getDLBM();
			 //System.out.println(szLoginInfo.getDLBM()+":"+szLoginInfo.getDLXM());
			 if (isBarManager_SESSION && iDLBM == departID){
				 canSee = true;
				 userList.add(szLoginInfo);
				 loginIDStr.append(iDLID+",");
			 } else if (strManageDepartID.equals("0") && iDLID == iDLID_SESSION){
				 canSee = true;
				 userList.add(szLoginInfo);
				 if (canAdd){
				 	loginIDStr.append(iDLID+",");
				 	canAdd = false;
				 }
			 } else if (!strManageDepartID.equals("0") && iDLBM == departID){
				 canSee = true;
				 userList.add(szLoginInfo);
				 loginIDStr.append(iDLID+",");
			 } else {
				 canSee = false;
			 }
			 if (canSee){
				 isBarManger = szPopedom_SESSION.isModuleManager(iMKID_SESSION, iDLID);
				 strFLID_Manager = wdyType.getManagerFLID(iDLID);
				 condStr = " AND XXGSID=" + szCOMPANY_SESSION+" AND "+
				 		docStat.getCond(iDLBM, iDLID,isBarManger, strFLID_Manager, "0");
				 int count = docStat.getCountBySQL(docStat.getStat_SQL("","", sSearchBegin, sSearchEnd, condStr));
				 xxMap.put(departInfo.getBMMC()+":"+szLoginInfo.getDLXM(),count);
			 }
		 }
		 loginMap.put(departID,userList);
	 }
	 loginIDStr.append("-1");
	 
	 //列表的宽度
	 int width = (typeSize+3)*100;
%>
<!-----上面的工具栏------>
<%@ include file="/share/title.jsp"%>
<table width="100%">
  <form name="formBar" method="post">
  <input type="hidden" name="moduleId" value="<%=moduleId%>">
  <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%>">
      姓名<input type="text" name="YGXM" class="txtClassSearch" value="<%=YGXM%>">
      开始<input type="text" name="txtSearchBegin" class="txtClassSearch" value="<%=sSearchBegin%>">
      <%=SzPageHelp.calendar2Select2("formBar.txtSearchBegin")%>
      结束<input type="text" name="txtSearchEnd" class="txtClassSearch" value="<%=sSearchEnd%>">
      <%=SzPageHelp.calendar2Select2("formBar.txtSearchEnd")%>
      <%=SzPageHelp.button("查询", "javascript:FormSubmit(formBar,'stat_user_receive.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>发布部门</td>
    <td>姓名</td>
    <td>需要查看的数量</td>
    <td>未查看的数量</td>
  </tr>
<%
	
	int count = 0;
	int uCont = 0;
	int departCount = 0;	//部门中应查看总数
	int departYDCount = 0;	//部门中已看总数
	int sum = 0;			//应查看总数	
	int sumYD = 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;
    	departYDCount = 0;
    	
    	for(int j = 0;j<loginSize;j++){
    		szLoginInfo = (SzLoginInfo)loginList.get(j);
 			if (szLoginInfo == null){
 				continue;
 			}
 			Lid = szLoginInfo.getDLID();
 			Lxm = szLoginInfo.getDLXM();
 			out.println("<tr title=\"["+sBMMC+"]"+Lxm+"\" align=\"center\">");
    		if (j == 0){
    	    	out.println("<td rowspan=\""+ loginSize +"\">"+ sBMMC +"&nbsp;</td>");
    		}
    		out.println("<td>"+ Lxm +"&nbsp;</td>");
           	obj = xxMap.get(sBMMC+ ":" + Lxm);
           	uCont = obj != null? Integer.valueOf(obj.toString()):0;
           	out.println("<td>"+ uCont +"&nbsp;</td>");
           	
           	obj = xxYDMap.get(sBMMC+ ":" + Lxm);
           	count = obj != null? Integer.valueOf(obj.toString()):0;
           	out.println("<td style=\"color: "+((uCont-count)>0? "red":"block")+";\">"+ (uCont-count) +"&nbsp;</td>");
           	out.println("</tr>");
    		departCount += uCont;
    		departYDCount += count;
 		}
    	if (loginSize > 0){
	    	out.println("<tr class=\"trClassTitle2\" title=\""+sBMMC+"--合计\" align=\"center\">");
	        out.println("<td style=\"color: blue;\">部门合计</td>");
	        out.println("<td>&nbsp;</td>");
	        out.println("<td style=\"color: blue;\">"+ departCount +"&nbsp;</td>");
	        out.println("<td style=\"color: "+((departCount-departYDCount)>0? "red":"blue")+";\">"+ (departCount-departYDCount) +"&nbsp;</td>");
	        out.println("<tr>");
    	}
    	sum += departCount;
    	sumYD += departYDCount;
    }
    out.println("<tr class=\"trClassTitle2\" title=\"合计\" align=\"center\">");
    out.println("<td style=\"color: blue;\">合计</td>");
    out.println("<td>&nbsp;</td>");
    out.println("<td style=\"color: blue;\">"+ sum +"&nbsp;</td>");
    out.println("<td style=\"color: "+((sum-sumYD)>0? "red":"blue")+";\">"+ (sum-sumYD) +"&nbsp;</td>");
    out.println("<tr>");
    
    xxMap = null;
    xxYDMap = null;
    loginMap = null;
%>

  </form>
</table>

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

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