package zr.zrpower.analyseengine.web.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import zr.zrpower.entity.sys.SessionUser;
import zr.zrpower.analyseengine.service.StatisticsControl;
import zr.zrpower.analyseengine.mode.ANALYSE_STATISTICS_MAIN;

public class StatisticsCompute
    extends HttpServlet {


  //Initialize global variables
  public void init() throws ServletException {
  }

  //Process the HTTP Get request
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, IOException {
    response.setContentType("text/html; charset=GBK");
    request.setCharacterEncoding("GBK");

    String ID = request.getParameter("ID");
    if (ID == null) {
      ID = "";
    }
    String P1 = request.getParameter("P1");
    if (P1 == null) {
       P1 ="";
    }
    String P2 = request.getParameter("P2");
    if (P2 == null) {
       P1 ="";
    }
    String P3 = request.getParameter("P3");
    if (P3 == null) {
       P3 ="";
    }
    String P4 = request.getParameter("P4");
    if (P4 == null) {
       P4 ="";
    }
    String P5 = request.getParameter("P5");
    if (P5 == null) {
       P5 ="";
    }
    String CONN = request.getParameter("CONN");
    if (CONN == null) {
       CONN ="";
    }

    //默认单位ID，为空时统计当前单位，有值时统计指定单位
    String UNITID = request.getParameter("UNITID");
    if (UNITID == null){
        UNITID ="";
    }

    try {
      StatisticsControl SControl = new StatisticsControl();
      HttpSession session = request.getSession(true);
      SessionUser user = (SessionUser) session.getAttribute("userinfo");
      StringBuffer sb = new StringBuffer();

      ANALYSE_STATISTICS_MAIN Smain = null;
      Smain = SControl.GetSmain(ID);

      //在网页上显示的表格模板
      String strTIMETEMPLATE = Smain.getTIMETEMPLATE();
      if (strTIMETEMPLATE==null){strTIMETEMPLATE="";}




         //计算报表
         SControl.GetCompute(ID,P1,P2,P3,P4,P5,user,CONN,UNITID);

                sb.append("<HTML>");
                sb.append("<HEAD>");
                sb.append("<TITLE></TITLE>");
                sb.append("<META HTTP-EQUIV=\"Pragma\" CONTENT=\"no-cache\">");
                sb.append("<META http-equiv=\"Content-Type\" content=\"text/html; charset=GBK\">");
                sb.append("</HEAD>");
                sb.append("<BODY class=\"BodyMain\">");
                sb.append("<FORM name=\"recordfrm\" method=\"post\" Action=\"\">");
                sb.append("</FORM>");
                sb.append("<script language=\"javascript\">");
                sb.append("setTimeout('show();',100);");
                sb.append("function show(){\r\n");

                    if (strTIMETEMPLATE.trim().length()>0)//有网页显示模板
                    {
                           sb.append("parent.document.data.location.href='ZrAnalyseEngine/StatisticsShow/datashowtable.jsp?ID="+ID+"&P1="+P1+"&P2="+P2+"&P3="+P3+"&P4="+P4+"&P5="+P5+"';\r\n");

                    }else
                    {
                           sb.append("parent.document.data.location.href='ZrAnalyseEngine/StatisticsShow/message_ts.jsp';\r\n");
                    }

                sb.append("parent.document.all('isexp').value='1';\r\n");
                sb.append("}\r\n");
                sb.append(" </script>");
                sb.append("</BODY>");
                sb.append("</HTML>");

                response.getWriter().print(sb.toString());




      SControl = null;
      user = null;
      Smain = null;

    }
    catch (Exception ex) {
      response.getWriter().print(
          "<p>发生异常！</p><p>详细错误信息:</p><p>" +
          ex.toString() + "</p>");
    }

  }

  //Process the HTTP Post request
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, IOException {
    doGet(request, response);
  }

  //Clean up resources
  public void destroy() {
  }
}
