package zr.zrpower.web.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import zr.zrpower.web.bean.MessageBox;
import zr.zrpower.dbmanage.BPIP_FIELD;
import zr.zrpower.dbmanage.service.FieldService;
import zr.zrpower.common.util.FunctionMessage;
import zr.zrpower.entity.sys.BPIP_OPERATE_TABLE;
import zr.zrpower.entity.sys.CODE_YWXT;
import zr.zrpower.ejb.sys.OperateTableService;
import zr.zrpower.ejb.sys.UdictService;


public class ActionField
    extends MessageBox {

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

  //Process the HTTP Get request
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, IOException {
    request.setCharacterEncoding("GBK");
    response.setContentType("text/html; charset=GBK");
    String strAction = request.getParameter("Act");
    if (!strAction.equals(null)) {
      if (strAction.equals("edit")) {
        EditTable(request, response);
      }

      if (strAction.equals("del")) {
        DeleteTable(request, response);
      }

      if (strAction.equals("add")) {
        AddTable(request, response);
      }
//=========================================================
//以下为字典的代码
      if (strAction.equals("edit1")) {
              EditDict(request, response);
            }

            if (strAction.equals("del1")) {
              DeleteDict(request, response);
            }

            if (strAction.equals("add1")) {
              AddDict(request, response);
      }


    }

  }

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

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

  //修改字段
  private void EditTable(HttpServletRequest request,
                        HttpServletResponse response) throws IOException {
    FunctionMessage fm = new FunctionMessage(1);
    BPIP_FIELD FIELDID = new BPIP_FIELD();
    FIELDID.fullDataFromRequest(request);

//=====

    BPIP_OPERATE_TABLE field = new BPIP_OPERATE_TABLE();
   field.TableName = request.getParameter("TableName");
   field.FieldName = request.getParameter("FIELDNAME");
   field.FieldType = request.getParameter("FIELDTYPETEXT");
   field.DefaultValue = request.getParameter("DefaultValue");
   field.OldFieldName = request.getParameter("OLDFIELDNAME");
   if(request.getParameter("FIELDLENGTH").length()>0)
           {
           field.Length = Integer.parseInt(request.getParameter("FIELDLENGTH"));
           }
          else
          {
          field.Length = 0;
          }
if (request.getParameter("ISNULL_VALUE").equals("1")) {
      field.IsNull = true;
       }
   else{
        field.IsNull = false;
    }


if (request.getParameter("FIELDTAG_VALUE").equals("1")) {
        field.IsPrimaryKey = true;
        FIELDID.setISKEY("1");
    }else
    {
       FIELDID.setISKEY("0");
    }


//====
    try {

	  FieldService Field = new FieldService();
      fm = Field.EditTC(FIELDID);
//=====
      OperateTableService TableService = new OperateTableService();
      TableService.ModifyField(field);
//=====
      if (fm.getResult()) {
        Field = null;
        TableService = null;
        response.sendRedirect( "zrsysmanage/DbManage/CloseWindow1.jsp");
      }
      else {
        this.Msg = fm.getMessage();
        this.isOk = fm.getResult();
      }
    }
    catch (Exception ex) {
      this.Msg = "文件管理中间件服务失败！<br>详细错误信息：" + ex.toString();
    }

    this.out = response.getWriter();
    this.returnPath = "zrsysmanage/DbManage/CloseWindow1.jsp";
    //this.GetBox();
  }

  //删除字段
  private void DeleteTable(HttpServletRequest request,
                          HttpServletResponse response) throws IOException {

    try {
      FunctionMessage fm = new FunctionMessage(1);
      FieldService Field = new FieldService();
      fm = Field.DeleteTC(request.getParameter("FIELDID"));
//=======

      BPIP_OPERATE_TABLE field = new BPIP_OPERATE_TABLE();
  field.TableName = request.getParameter("TableName");
  field.FieldName = request.getParameter("FIELDNAME");
  field.FieldType = request.getParameter("FIELDTYPETEXT");
  field.DefaultValue = request.getParameter("DefaultValue");
  if(request.getParameter("FIELDLENGTH").length()>0)
 {
   field.Length = Integer.parseInt(request.getParameter("FIELDLENGTH"));
 }
 else
  {
    field.Length = 0;
  }

   if (request.getParameter("ISNULL_VALUE").equals("1")) {
       field.IsNull = true;
   }
   else{
       field.IsNull = false;
   }


 if (request.getParameter("FIELDTAG_VALUE").equals("1")) {
        field.IsPrimaryKey = true;
   }


  OperateTableService TableService = new OperateTableService();
      TableService.DeleteField(field);
 //============
      if (fm.getResult()) {
        this.Msg = fm.getMessage();
        this.isOk = fm.getResult();
      }
      else {
        this.Msg = fm.getMessage();
        this.isOk = fm.getResult();
      }
      Field = null;
      TableService = null;
      response.sendRedirect("zrsysmanage/DbManage/CloseWindow1.jsp");
    }
    catch (Exception ex) {
      this.Msg = "连接文件管理中间件服务失败！<br>详细错误信息：" + ex.toString();
    }
    this.out = response.getWriter();
    this.returnPath = "zrsysmanage/DbManage/CloseWindow1.jsp";
    //this.GetBox();
  }

  //添加字段
  private void AddTable(HttpServletRequest request,
                        HttpServletResponse response) throws
      IOException {
    FunctionMessage fm = new FunctionMessage(1);
    BPIP_FIELD FIELDID = new BPIP_FIELD();
//=====
    String TableID = request.getParameter("TABLEID");
    BPIP_OPERATE_TABLE field = new BPIP_OPERATE_TABLE();
    field.TableName = request.getParameter("TableName");
    field.TableSpace = request.getParameter("TABLESPACENAME");
    field.FieldName = request.getParameter("FIELDNAME");
    field.FieldType = request.getParameter("FIELDTYPETEXT");
    field.DefaultValue = request.getParameter("DefaultValue");
    if(request.getParameter("FIELDLENGTH").length()>0)
   {
     field.Length = Integer.parseInt(request.getParameter("FIELDLENGTH"));
   }
   else
    {
      field.Length = 0;
    }
   if (request.getParameter("ISNULL_VALUE").equals("1")) {
      field.IsNull = true;
  }
  else{
      field.IsNull = false;
  }


    if (request.getParameter("FIELDTAG_VALUE").equals("1")) {
       field.IsPrimaryKey = true;
       FIELDID.setISKEY("1");
     }else
       {
          FIELDID.setISKEY("0");
       }


//=======
   try {
	  FieldService Fd = new FieldService();
      FIELDID.fullDataFromRequest(request);
      fm = Fd.AddTC(FIELDID);
//====
      OperateTableService TableService = new OperateTableService();
      TableService.AddField(field);
//======
      if (fm.getResult()) {
        Fd = null;
        TableService = null;
        response.sendRedirect( "zrsysmanage/DbManage/CloseWindow1.jsp");
      }
      else {
        this.Msg = fm.getMessage();
        this.isOk = fm.getResult();
      }
    }
    catch (Exception ex2) {
      this.Msg = "连接客户端文件中间件服务失败！<br>详细错误信息：<br>   " + ex2.toString();
    }

    this.out = response.getWriter();
    this.returnPath = "zrsysmanage/DbManage/CloseWindow1.jsp";
    //this.GetBox();

  }


//=================================================================================
  //以下为字典的代码
  //修改字典信息
 private void EditDict(HttpServletRequest request,
                     HttpServletResponse response) throws IOException {

   FunctionMessage fm = new FunctionMessage(1);

   CODE_YWXT CODE = new CODE_YWXT();
   CODE.fullDataFromRequest(request);
   String strtableid1 = request.getParameter("TABLEID");
   try {
     UdictService Udict = new UdictService();
     String strtableid = request.getParameter("TABLEID");
     String strid = request.getParameter("ID");
      fm = Udict.EditDC(CODE,strtableid,strid);
     if (fm.getResult()) {
       CODE = null;
       Udict = null;
       response.sendRedirect("zrsysmanage/Dict/UDictList.jsp?TABLEID="+strtableid);
     }
     else {
       this.Msg = fm.getMessage();
       this.isOk = fm.getResult();
     }
   }
   catch (Exception ex) {
     this.Msg = "文件管理中间件服务失败！<br>详细错误信息：" + ex.toString();
   }

   this.out = response.getWriter();
   this.returnPath ="zrsysmanage/Dict/UDictList.jsp?TABLEID="+strtableid1;
   //this.GetBox();
 }

 //删除字典信息
 private void DeleteDict(HttpServletRequest request,
                         HttpServletResponse response) throws IOException {
  String strtableid1 = request.getParameter("TABLEID");
   try {
     FunctionMessage fm = new FunctionMessage(1);
      UdictService Udict = new UdictService();
       String strtableid = request.getParameter("TABLEID");
      fm = Udict.DeleteDC(request.getParameter("CODE"),strtableid);
     if (fm.getResult()) {
       this.Msg = fm.getMessage();
       this.isOk = fm.getResult();
     }
     else {
       this.Msg = fm.getMessage();
       this.isOk = fm.getResult();
     }
     Udict = null;

     response.sendRedirect("zrsysmanage/Dict/UDictList.jsp?TABLEID="+strtableid);
   }
   catch (Exception ex) {
     this.Msg = "连接文件管理中间件服务失败！<br>详细错误信息：" + ex.toString();
   }
   this.out = response.getWriter();
   this.returnPath = "zrsysmanage/Dict/UDictList.jsp?TABLEID="+strtableid1;
   //this.GetBox();
 }

 //添加字典信息
 private void AddDict(HttpServletRequest request, HttpServletResponse response) throws
     IOException {
   FunctionMessage fm = new FunctionMessage(1);
   CODE_YWXT CODE = new CODE_YWXT();
   String strtableid1 = request.getParameter("TABLEID");
   try {
     UdictService Udict = new UdictService();
     CODE.fullDataFromRequest(request);
    String strtableid = request.getParameter("TABLEID");
     fm = Udict.AddDC(CODE,strtableid);
     if (fm.getResult()) {
       CODE = null;
       Udict = null;
       response.sendRedirect("zrsysmanage/Dict/UDictList.jsp?TABLEID="+strtableid);
     }
     else {
       this.Msg = fm.getMessage();

     }
   }
   catch (Exception ex2) {
     this.Msg = "连接客户端文件中间件服务失败！<br>详细错误信息：<br>   " + ex2.toString();
   }

   this.out = response.getWriter();
   this.returnPath = "zrsysmanage/Dict/UDictList.jsp?TABLEID="+strtableid1;
   //this.GetBox();

 }




}
