package zr.zrpower.web.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import zr.zrpower.entity.sys.BPIP_USER;
import zr.zrpower.entity.sys.BPIP_USER_PHOTO;
import zr.zrpower.web.bean.MessageBox;
import zr.zrpower.common.web.Upload;
import zr.zrpower.ejb.sys.UserService;

public class ActionUserData
    extends MessageBox {
  private static final String CONTENT_TYPE = "text/html; charset=GBK";

  //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(CONTENT_TYPE);
    String mAct = "";
    String userid = "";
    String unitid = "";

    if(request.getContentLength()>0){

      Upload upload = new Upload();
      upload.setSwitch(false);
      upload.setUpType(1);
      upload.setSuffix(".jpg.jpeg.gif");
      upload.setSize(1024*1024);

      upload.setSourceFile(request);

      Hashtable htFields = upload.getFields();
      Hashtable htPhoto = upload.getFieldStrem();

      if(htFields!=null){
        BPIP_USER mUser = new BPIP_USER();
        BPIP_USER_PHOTO mUserPhoto = null;
        UserService userMgr = null;
        try {
          userMgr = new UserService();

          mAct = (String) htFields.get("Act");
          userid = (String) htFields.get("USERID");
          unitid = (String) htFields.get("UNITID");

          mUser.fullDataFromHashtable(htFields);


          if (htPhoto != null) {

            mUserPhoto = new BPIP_USER_PHOTO();
            if (htPhoto.get("USERPHOTO") != null) {
              mUserPhoto.setUSERPHOTO( (byte[]) htPhoto.get("USERPHOTO"));
            }
            if (htPhoto.get("IDIOGRAPH") != null) {
              mUserPhoto.setIDIOGRAPH( (byte[]) htPhoto.get("IDIOGRAPH"));
            }
            mUser.setUserPhoto(mUserPhoto);
          }

          if (mAct.equals("add")) {

            userid = userMgr.AddUser(mUser);
            response.sendRedirect("zrsysmanage/UserEdit.jsp?SelectedItems="+userid+"&unitid="+unitid);
          }
          else if (mAct.equals("edit")) {
            userMgr.EditUser(mUser);

            response.sendRedirect("zrsysmanage/UserEdit.jsp?SelectedItems="+userid+"&unitid="+unitid);

          }
          else if (mAct.equals("sedit")){
            userMgr.EditUser(mUser);
            response.sendRedirect("zrsysmanage/MessageUserInfo.jsp");

          }

          userMgr = null;
          mUser = null;
          mUserPhoto = null;
        }
        catch (Exception ex) {
          this.Msg= "连接中间间服务器发生异常！\n详细错误信息："+ex.toString();
        }
      }else this.Msg= "分析上传数据失败，请稍后再试。如果问题依然存在请与系统管理员联系！";
    }
        this.out = response.getWriter();
        //this.GetBox();
  }

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

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