<%@ 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.stat.*" %>
<%@ 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">
<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 YPMC = SzConvert.strWriteData(request.getParameter("YPMC"));
	//员工姓名
	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("");
	
	//========获取部门信息=============
	SzLogin szLogin = new SzLogin();
	SzLoginInfo szLoginInfo = (SzLoginInfo)szLogin.getInfoByCond(" DLID = " + iDLID_SESSION);
	SzDepart depart = new SzDepart();
	StringBuffer departStr = new StringBuffer(" 1=1 ");
	String strManageDepartID = "-1";
	
	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+"'");
	 }
	 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;
	
	//取得用户
	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");
	
	//获取对象信息
    OfficeSupplies officeSupplies = new OfficeSupplies();
	
	String searchType = " AND "+officeSupplies.getStat_SQLOfType("","","YP.XXMC3",YPMC,"");
	String searchStr = officeSupplies.getStat_SQL("U.XXMC6",BMMC,"","","U.XXMC4", sSearchBegin, sSearchEnd,searchType);

	Map DepartUseMap = officeSupplies.getDepartUseMap("",searchStr);
	searchStr = officeSupplies.getStat_SQL("U.XXMC6",BMMC,"","","U.XXMC4", sSearchBegin, sSearchEnd,"");
	ArrayList lingYongList = officeSupplies.getYongPingList(searchStr);
	int lingYongSize = lingYongList != null? lingYongList.size():0;
		
%>
<!-----上面的工具栏------>
<%@ 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%>">
      用品<input type="text" name="YPMC" class="txtClassSearch" value="<%=YPMC%>">
      开始<input type="text" name="txtSearchBegin" class="txtClassSearch" value="<%=sSearchBegin%>">
      <%=SzPageHelp.calendar2Select("../../../","formBar.txtSearchBegin")%>
      结束<input type="text" name="txtSearchEnd" class="txtClassSearch" value="<%=sSearchEnd%>">
      <%=SzPageHelp.calendar2Select("../../../","formBar.txtSearchEnd")%>
      <%=SzPageHelp.button("查询", "javascript:FormSubmit(formBar,'stat_use_depart.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="2">领用统计</td>
  </tr>
  <tr class="trClassTitle_c">
    <td>领用数量</td>
    <td>金额</td>
  </tr>
<%
	int rowSpanCount = 0;
	double count = 0;
	double uCount = 0;
	double flSum = 0;
	double sum = 0;
	Object obj = null;
	String sBMMC = "";
	
	ArrayList spList = null;
	int spSize = 0;
	String spName = "";
	Object[] value = null;
	int valueSize = 0;
	StringBuffer outSB = new StringBuffer();
    for (int i=0;i<departSize;i++){
    	rowSpanCount = 0;
    	count = 0;
    	flSum = 0;
    	departInfo = (SzDepartInfo)departList.get(i);
 		sBMMC = departInfo.getBMMC();
 		outSB = new StringBuffer();
 		for (int j = 0;j<lingYongSize;j++) {
 			spName = lingYongList.get(j).toString();

 			if (DepartUseMap.containsKey(sBMMC+":"+spName)){
 				value = (Object[])DepartUseMap.get(sBMMC+":"+spName);
 				double value0 = Double.valueOf(value[0].toString());
    			double value1 = Double.valueOf(value[1].toString());
    			count = count + value0;
    			flSum = flSum + value1;
 				if (rowSpanCount > 0){
 					outSB.append("<tr align=\"center\">");
 				}
 				rowSpanCount++;
 				outSB.append("<td>"+ spName +"&nbsp;</td>");
 				outSB.append("<td>"+ SzConvert.formatFloat2Str(value0) +"&nbsp;</td>");
 				outSB.append("<td>"+ SzConvert.formatFloat2Str(value1) +"&nbsp;</td>");
 				outSB.append("</tr>");
 			}
 		}
 		if (rowSpanCount > 0){
 			out.println("<tr align=\"center\">");
 	    	out.println("<td rowspan=\""+ rowSpanCount +"\">"+ sBMMC +"&nbsp;</td>");
 	    	out.println(outSB.toString());
 	    	
 	    	out.println("<tr class=\"trClassTitle2\" align=\"center\">");
 	    	out.println("<td colspan=\"2\">部门合计</td>");
 	    	out.println("<td>"+ SzConvert.formatFloat2Str(count) +"&nbsp;</td>");
 	    	out.println("<td>"+ SzConvert.formatFloat2Str(flSum) +"&nbsp;</td>");
 	    	out.println("<tr>");
 	    	
 	    	uCount = uCount + count;
 	    	sum = sum + flSum;
 		}
 		
    }
    out.println("<tr class=\"trClassTitle2\" align=\"center\">");
    out.println("<td colspan=\"2\" style=\"color: red;\">合计</td>");
    out.println("<td>"+ SzConvert.formatFloat2Str(uCount) +"&nbsp;</td>");
	out.println("<td>"+ SzConvert.formatFloat2Str(sum) +"&nbsp;</td>");
    out.println("<tr>");
    
    
%>

  </form>
</table>

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

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