/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.newspager.struts.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.newspager.dao.HistoryNumberDAO;
import com.newspager.dao.impl.HistoryNumberDAOImpl;
import com.newspager.struts.form.HistoryNumberForm;
import com.newspager.util.DateUtilly;
public class HistoryNumberAction extends BaseAction {
public ActionForward unspecified(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return null;
}
public ActionForward listinput(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
HistoryNumberForm f = (HistoryNumberForm)form;
HistoryNumberDAO hndao = new HistoryNumberDAOImpl();
String pdate=f.getPdate();
if(pdate==null || pdate=="" ){
pdate=DateUtilly.today("yyyy-MM-dd");
}
request.setAttribute("user", hndao.getUserName(pdate));
request.setAttribute("pdate", pdate);
return mapping.findForward("history_list");
}
public ActionForward list(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
HistoryNumberForm f = (HistoryNumberForm)form;
String pdate =f.getPdate();
if(pdate==null || pdate=="" ){
pdate=DateUtilly.today("yyyy-MM-dd");
}
if(f.getNextDate()!=0){
pdate=DateUtilly.getNextDate(pdate, f.getNextDate());
}
int offset = 0;
try {
offset = Integer.parseInt(request.getParameter("pager.offset"));
} catch (Exception ignore) {
}
int pageSize;
try {
pageSize = Integer.parseInt(request.getParameter("pageSize"));
} catch (Exception e) {
pageSize = 41*2;
}
HistoryNumberDAO hndao = new HistoryNumberDAOImpl();
String userName=hndao.getUserNameById(f.getUserId(), pdate);
if(userName!=null){
userName=userName+" "+pdate+" "+DateUtilly.getWeekName(pdate);
}
request.setAttribute("userName", userName);
request.setAttribute("total", hndao.getRsCount(f.getUserId(), pdate));
request.setAttribute("pageSize", pageSize);
request.setAttribute("pdate", pdate);
request.setAttribute("userId", f.getUserId());
request.setAttribute("data", hndao.getUserNumberByDate(f.getUserId(), pdate,pageSize,offset));
request.setAttribute("user", hndao.getUserName(pdate));
return mapping.findForward("history_list");
}
public ActionForward addtologinput(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
HistoryNumberForm f = (HistoryNumberForm)form;
if(f.getPdate()==null || f.getPdate()==""){
request.setAttribute("pdate", DateUtilly.getNextDate(1));
}
return mapping.findForward("savelog_input");
}
public ActionForward addtolog(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
HistoryNumberForm f = (HistoryNumberForm)form;
HistoryNumberDAO logdao = new HistoryNumberDAOImpl();
String pdate = f.getPdate();
request.setAttribute("pdate", pdate);
//如果重新添加,则先删除
if(f.getDel()!=null && f.getDel().equals("yes")){
if(!logdao.delLogByDate(pdate)){
request.setAttribute("message", "删除失败,无法重新添加!");
return addtologinput(mapping, form,request, response);
}
}else{
//如果已存在pdate参数值的日期记录,设置提示信息返回给用户
int c = logdao.isExistDateLog(f.getPdate());
String week = DateUtilly.getWeekName(pdate);
if(c>0){
request.setAttribute("msg_confirm",pdate+week+"已存在"+c+"个用户记录,是否删除后重新添加!");
return addtologinput(mapping, form,request, response);
}
}
if(logdao.addToHistory(pdate)){
request.setAttribute("message", "添加成功!");
}else{
request.setAttribute("message", "添加失败!");
}
request.setAttribute("cd", "addtolog");
return addtologinput(mapping, form,request, response);
}
public ActionForward exportinput(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
HistoryNumberForm f = (HistoryNumberForm)form;
if(f.getPdate()==null || f.getPdate()==""){
request.setAttribute("pdate", DateUtilly.getNextDate(1));
}
return mapping.findForward("history_export");
}
public ActionForward export(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
HistoryNumberForm f = (HistoryNumberForm)form;
HistoryNumberDAO logdao = new HistoryNumberDAOImpl();
String pdate = f.getPdate();
if(logdao.getExportCount(pdate)>0){
//取得项目的根目录(绝对路径)
ServletContext context = request.getSession().getServletContext();
String path = context.getRealPath(".");
String outputFile = path+"";
File tempFile = new File(outputFile);
InputStream is = null;
FileOutputStream fOut = new FileOutputStream(outputFile);
if(logdao.exportToExcel(pdate,fOut)){
request.setAttribute("message", "报表输出成功!");
}else{
request.setAttribute("message", "报表输出失败!");
}
fOut.flush();// 操作结束,关闭文件
fOut.close();
is = new FileInputStream(tempFile);
is.close();
}else{
request.setAttribute("message", "没有数据可输出!");
}
request.setAttribute("pdate", pdate);
request.setAttribute("cd", "export");
return mapping.findForward("history_export");
}
// public ActionForward dowload(ActionMapping mapping, ActionForm form,
// HttpServletRequest request, HttpServletResponse response)
// throws Exception {
// HistoryNumberForm f = (HistoryNumberForm)form;
// try{
// String fname = "test123";//excel文件名
// OutputStream os = response.getOutputStream();//取得输出流
// response.reset();//清空输出流
// response.setHeader("content-disposition", "attachment; filename=" + fname + ".xls");//设定输出文件头
// response.setContentType("application/msexcel");//定义输出类型
// HistoryNumberDAO hndao = new HistoryNumberDAOImpl();
// hndao.exportToExcel(f.getPdate(), os);
// os.flush();
// os.close();
// }catch(Exception e){
// System.out.println("==="+e.getMessage());
// }
// return null;
//// return exportinput(mapping, form,request, response);
// }
}
阅读(588) | 评论(0) | 转发(0) |