package prj.charging.controller;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import prj.charging.service.OutStoreSvc;
import prj.websys.dto.JfEmpDto;
import prj.websys.util.JavaUtil;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;
public class ChukuAction extends Action{
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException, SmartUploadException, RowsExceededException, WriteException, ParseException{
//获取页面参数
HashMap<String , Object> paraMap = new HashMap<String , Object>();
OutStoreSvc svc = new OutStoreSvc();
//调用jspsmart组件进行文件上传操作
SmartUpload smart = new SmartUpload();
ServletConfig config=this.getServlet().getServletConfig();
smart.initialize(config,request,response);//初始化
smart.upload();//开始上传
System.out.print("开始上传文件..");
smart.save("/upload");//save(destPathName)作用等同于save(destPathName,SAVE_AUTO)。
System.out.println("上传文件成功");
Request req=smart.getRequest();
String p_smfid=JavaUtil.getTrimStr(req.getParameter("p_smfid"));
String p_projno=JavaUtil.getTrimStr(req.getParameter("p_projno"));
String p_receivedate=JavaUtil.getTrimStr(req.getParameter("p_receivedate"));
String p_receiveper=JavaUtil.getTrimStr(req.getParameter("p_receiveper"));
String p_date=JavaUtil.getTrimStr(req.getParameter("p_date"));
String p_sendper=JavaUtil.getTrimStr(req.getParameter("p_sendper"));
String p_trans=JavaUtil.getTrimStr(req.getParameter("p_trans"));
paraMap.put("p_smfid", p_smfid);
paraMap.put("p_projno", p_projno);
paraMap.put("p_receivedate", p_receivedate);
paraMap.put("p_receiveper", p_receiveper);
paraMap.put("p_date", p_date);
paraMap.put("p_sendper", p_sendper);
paraMap.put("p_trans", p_trans);
int q=0,w=0;//q 出库成功数 w出库失败数
jxl.write.WritableWorkbook wwb=null;
jxl.write.WritableSheet ws=null;
String fileName = "";
Date date=new Date();
SimpleDateFormat format1 = new SimpleDateFormat("yyyyMMddHHmmss");
SimpleDateFormat format2 = new SimpleDateFormat("yyyyMMddHHmmss");
try{
//创建一个日志文件
fileName = System.getProperty("java.io.tmpdir")+"\\xls"+format2.format(date)+"出库导入日志.xls";
wwb = Workbook.createWorkbook(new File(fileName));
ws = wwb.createSheet("Sheet 1", 0);
ws.addCell(new jxl.write.Label(0, 0, "出库成功数"));//添加列头
ws.addCell(new jxl.write.Label(1, 0, "出库失败数"));
ws.addCell(new jxl.write.Label(2, 0, "操作人"));
ws.setColumnView(0, 20);//设置列宽
ws.setColumnView(1, 20);
ws.setColumnView(2, 20);
HttpSession session=request.getSession();
JfEmpDto empDto = (JfEmpDto)session.getAttribute("empDto");
String name=svc.getempname(empDto.getId());//获得操作人员的姓名 并记录日志中
ws.addCell(new jxl.write.Label(2, 1, name));
ws.addCell(new jxl.write.Label(0, 2, "表身号"));
int hanghao=3;//从第4行开始写入数据
//构建Workbook对象, 只读Workbook对象
//直接从本地文件创建Workbook
//从输入流创建Workbook
//String sourcefile=request.getParameter("myfile"); 文件路径
//System.out.println(sourcefile);
Files file=smart.getFiles();
String filename=file.getFile(0).getFileName();
// 15.19 D:\\Tomcat 6.0\\webapps\\websys\\upload\\
// 220 E:\\Tomcat 6.0\\webapps\\websys\\upload\\
//localhost C:\\Program Files\\Apache Software Foundation\\Tomcat 5.0\\webapps\\websys\\upload\\
InputStream is = new FileInputStream("E:\\Tomcat 6.0\\webapps\\websys\\upload\\"+filename);
jxl.Workbook rwb = Workbook.getWorkbook(is);//构建工作薄对象
Sheet[] shedts=rwb.getSheets();
for(int i=0;i<shedts.length;i++){
jxl.Sheet sh=rwb.getSheet(i);//获取第几页
int row=sh.getRows();//获取所有行
for(int j=1;j<row;j++){
Cell cell0=sh.getCell(0, j);//j代表行号 从第一行开始获取第1列
String biaoshenhao=cell0.getContents();//获取表身号
ws.addCell(new jxl.write.Label(0, hanghao, biaoshenhao));
//开始执行出库过程
if(!("").equals(biaoshenhao)){
paraMap.put("p_smno", biaoshenhao);
paraMap.put("p_emno", biaoshenhao);
String ids=svc.outStorePro(paraMap);
String message=ids.substring(0,1);//出库当前excel中的所有水表
if("e".equals(message)){
w++;//出库失败数量
//如果出库失败,记录表身号,忽律当前表身号继续执行下只水表出库操作,当前表身号不再进行配表
System.out.println("失败的当前表身号:"+biaoshenhao);
ws.addCell(new jxl.write.Label(1, hanghao, "出库失败"));
svc.recorditem(p_projno,biaoshenhao,"",name,"0","1","","");
}
else{
q++;//出库成功数量
svc.recorditem(p_projno,biaoshenhao,"",name,"0","0","","");
}
}
hanghao++;
}
ws.addCell(new jxl.write.Label(0, hanghao, "工程编号"));
ws.addCell(new jxl.write.Label(1, hanghao, p_projno));
}
}
catch (Exception e) {
e.printStackTrace();
}
finally{
ws.addCell(new jxl.write.Label(0, 1, String.valueOf(q)));
ws.addCell(new jxl.write.Label(1, 1, String.valueOf(w)));
wwb.write();
wwb.close();
response.reset();
response.setContentType("application/x-msdownload;charset=GBK");
response.setCharacterEncoding("UTF-8");
String docName=format1.format(date);
docName=docName+"出库导入日志.xls";
docName=java.net.URLEncoder.encode(docName,"UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=" +new String(docName.getBytes("UTF-8"),"GBK"));
BufferedInputStream br = new BufferedInputStream(new FileInputStream(fileName));
byte[] buf = new byte[1024];
int len = 0;
OutputStream out = response.getOutputStream();
while((len = br.read(buf)) >0)
out.write(buf,0,len);
out.close();
br.close();
}
return null;
}
}