<%@page import="com.stfoa.domain.QuartzSchedule"%>
<%@page import="com.stfoa.dao.hibernate.QuartzScheduleDAO"%>
<%@ page language="java"  pageEncoding="UTF-8"%>
<%@page import="java.util.List"%>
<%@page import="com.myoa.common.util.FormatUtil"%>
<%@page import="com.myoa.common.util.ValidateUtil"%>
<%@page import="com.stfoa.domain.Employee"%>
<%@page import="com.myoa.common.util.SysConfUtil"%>
<%@page import="com.stfoa.dao.hibernate.DocumentInstanceDAO"%>
<%@page import="com.stfoa.domain.DocumentInstance"%>
<%@page import="com.myoa.common.util.DESUtil"%>
<%@page import="com.stfhr.dao.hibernate.DataDicItemDAO"%>
<%@page import="org.apache.commons.lang.StringEscapeUtils"%>
<%@ include file="/common/include/taglibs.jsp"%>
<HTML>
<HEAD>
<%@ include file="/common/include/meta.jsp"%>
<%@ include file="/common/include/list.jsp"%>
</HEAD>
<%
try{
Employee self = (Employee)session.getAttribute("session_employee");
int pageno = 1; // 当前页码
int countperpage = SysConfUtil.getInstance().getPageSize(); //每页条数
int totalcount = 0; //总条数

String queryType = request.getParameter("queryType");//查询方式 
DocumentInstanceDAO instancedao = new DocumentInstanceDAO();
String queryHQL = "from com.stfoa.domain.Document as doc "
    +"where doc.docType=1 and doc.oaTblEmployee.empId="+self.getEmpId()+" and docStatus=0 ";//最终查询语句
String orderby = " order by doc.docCreatTime desc";
if(queryType!=null){
 if("ALL".equals(queryType)){//查询全部
	queryHQL += orderby;
	queryHQL = DESUtil.doEncrypt(queryHQL);
 }else if("CUST".equals(queryType)){//自定义查询
	String mySelect = request.getParameter("mySelect");	
	if("title".equals(mySelect)){//按标题
		   String title = request.getParameter("title");
		   queryHQL += " and (doc.docTitle like '%"+title+"%')"+ orderby; 
	   }else if("sender".equals(mySelect)){//按发送人
		   String sender = request.getParameter("sender");
		   queryHQL += " and (doc.oaTblEmployee.empName like'%"+sender+"%')"+ orderby; 
	   }else if("senddate".equals(mySelect)){//按发送日期
		   String begDate = request.getParameter("begDate");
	       String endDate = request.getParameter("endDate");
		   queryHQL += " and (doc.docCreatTime>='"+begDate+" 00:00:00' and doc.docCreatTime<='"+endDate+" 23:59:59')" +orderby; 
	   }else if("content".equals(mySelect)){//按内容
		   String content = request.getParameter("content");
		   queryHQL += " and (doc.docContent like '%"+content+"%')"+ orderby; 
	   }else{
		 queryHQL += " and (doc."+mySelect+"="+request.getParameter(mySelect)+")"+orderby;
	   } 
	queryHQL = DESUtil.doEncrypt(queryHQL);
 }else if("PAGE".equals(queryType)){
    queryHQL = request.getParameter("queryHQL"); 
	pageno = Integer.valueOf(request.getParameter("pageno"));
	countperpage = Integer.parseInt(request.getParameter("countperpage"));
	if (pageno < 1) {
	  pageno = 1;
    } 
 }
}
if(!ValidateUtil.isNull(queryHQL)){
  String hql = DESUtil.doDecrypt(queryHQL);
  totalcount = instancedao.queryCountByHQL("select count(*)"+hql);
  //总页数
  int pagecount = ((totalcount + countperpage) - 1) / countperpage;
  if (pageno > pagecount) {
	 pageno = pagecount;
  }
  List<DocumentInstance> querylist = instancedao.queryByHQLForPagination(hql,(pageno - 1) * countperpage, countperpage);
  request.setAttribute("querylist",querylist);
}
DataDicItemDAO dicdao = new DataDicItemDAO();
QuartzScheduleDAO scheduledao = new QuartzScheduleDAO();
%>
<BODY leftmargin="0" topmargin="0">
 <table cellspacing="0" onClick="sortColumn(event)" width="100%" class="sort" id=mytable>       
   <%int num = 0; %>
 <logic:present name="querylist" >
   <thead>
    <tr>
     <td type="String" width="5%" align="center"><fmt:message key="common.query.Check"/></td>
    <td type="Number" width="5%"><fmt:message key="common.query.Code"/></td>
     <td type="String" width="59%"><fmt:message key="common.query.Subject"/></td>
     <td type="String" width="15%"><fmt:message key="common.query.SendTime"/></td>
     <td type="String" width="8%"><fmt:message key="common.query.SecretLevel"/></td>
     <td type="String" width="8%"><fmt:message key="common.query.UrgentLevel"/></td>
    </tr>
   </thead> 
   <tbody>      
   <logic:iterate id="doc" name="querylist" type="com.stfoa.domain.Document">
    <%
     num++;
     String docId = DESUtil.doEncrypt(doc.getDocId().toString());
     String timestamp = FormatUtil.formatDate("yyyyMMddHHmmss",doc.getDocCreatTime());
     String scheduleHQL = "from com.stfoa.domain.QuartzSchedule where relaObj='Document' and relaId='"+doc.getDocId().toString()+"' and status=0";
     List<QuartzSchedule> scheduleList = scheduledao.queryByHQL(scheduleHQL);
     QuartzSchedule schedule = null;
     String scheduleTitle = null;
     if(scheduleList.size()==1){
    	 schedule =  scheduleList.get(0);
    	 if(schedule.getScheduleType()==1){//每天
 			scheduleTitle = "每天于"+schedule.getTimes()+"定时发送";
 		}else if(schedule.getScheduleType()==2){//每周
 			scheduleTitle = "每周"+schedule.getWeeks()+"于"+schedule.getTimes()+"定时发送";	
 		}else if(schedule.getScheduleType()==3){//每月
 			scheduleTitle = schedule.getMonths()+"月于每月"+schedule.getDays()+"日"+schedule.getTimes()+"定时发送";	
 		}else if(schedule.getScheduleType()==4){//一次性
 			scheduleTitle = "一次性"+schedule.getTimes()+"定时发送";
 		}
     }
    %>
    <tr class="sort"  onMouseOver="this.style.cursor='hand';" 
     ondblclick="parent.location.href='${ctx}/jsp/collaborate/update/index.jsp?docId=<%=docId%>&timestamp=<%=timestamp%>'"
     onclick=showDetail(this,<%=num%>,'<%=docId%>','<%=doc.getHasAttach()%>','<%=timestamp%>') >
      <td class="sort"align="center">
       <input type="checkbox" name="myCheckBox" docId="<%=docId%>" isSchedule="<%=schedule!=null && schedule.getScheduleType()==4%>">
      </td>
      <td class="sort"><%=num%></td>
      <td class="sort" id=td_title_<%=num%>><%=StringEscapeUtils.escapeHtml(doc.getDocTitle())%>
          <%if(doc.getHasAttach()>0){%><img border=0  src='${ctx}/common/images/attachment.gif' width='16' height='16'  align='middle'><%}%>
          <%if(schedule !=null){ %>
            <font color="green">[<%=scheduleTitle%>]</font>
          <%}%>
          </td>
      <td class="sort" id=td_createtime_<%=num%>><%=FormatUtil.formatDate("yyyy-MM-dd HH:mm:ss",doc.getDocCreatTime())%></td>          
       <td class="sort"><%=dicdao.findById(doc.getDocSecretLevel()).getItemName()%></td>
      <td class="sort"><%=dicdao.findById(doc.getDocUrgentLevel()).getItemName()%></td>
	 </tr>
    </logic:iterate>
    </tbody>       
   </logic:present>
  </table>
  <form action='list.jsp' method=post name=pageform style="margin:0;">
   <input type=hidden name=queryHQL value="<%=queryHQL%>">
   <input type=hidden name=queryType value=PAGE>
   <div align="right" style="font-size:9pt;" class=noprint>
    <myoa:pageation pageno="<%=pageno%>" countperpage="<%=countperpage%>" totalcount="<%=totalcount%>"></myoa:pageation>
   </div>
  </form>
<script>
function showDetail(obj,num,docId,hasAttach,timestamp){
  SetFocusIt(mytable,obj);
  parent.viewframe.detailframe.location.href='../view/detail.jsp?docId='+docId+'&timestamp='+timestamp;
  parent.viewframe.viewbar.docId.value = docId;
  parent.viewframe.viewbar.enablebuttons();
  parent.viewframe.viewbar.span_title.innerHTML = document.getElementById('td_title_'+num).innerHTML;
  parent.viewframe.viewbar.span_sender.innerHTML = '<%=self.getEmpName()%>（'+document.getElementById('td_createtime_'+num).innerHTML+'）';
  parent.viewframe.attachframe.location.href="../view/attachment.jsp?docId="+docId+'&timestamp='+timestamp;
  if(hasAttach>0){
    parent.viewframe.viewdetail.cols ="*,0,160,0";
  }else{
    parent.viewframe.viewdetail.cols ="*,0,0";
  }
}
function clearview(){
  parent.viewframe.location.href='../view/index.jsp?viewType=ToSend&total=<%=totalcount%>';
}
clearview();
</script>
</BODY>
<%}catch(Exception ex){ex.printStackTrace();} %>
</HTML>

