using System;
using System.Data;
using System.Data.OracleClient;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using Core.Mes.ServerFrameWork;
using Core.Mes.IBaseInterface;

namespace Core.Mes.PurviewManager
{
  class QuestionReply : IComponent
  {
    public ReturnObject getAllData(string[] sArgs)
    {
      StringBuilder strbuld = new StringBuilder();
      strbuld.Append("SELECT ID_,CREATOR,CREATEDEPARTMENT,QUESTION,DEPARTMENT,PRINCIPAL,CREATETIME,LASTANSWERER,LASTTIME,IPADDRESS,FLAG,PHONE,QUESTIONTYPE FROM CXUSER.PT_QUESTION ");
      string sWhere = "";
      if (sArgs[3] == "All")
        sWhere = "WHERE 1=1 ";
      else if (sArgs[3] == "NoReply")
        sWhere = "WHERE LASTANSWERER IS NULL ";
      else if (sArgs[3] == "NoComplete")
        sWhere = "WHERE FLAG='0' ";
      else if (sArgs[3] == "Complete")
        sWhere = "WHERE FLAG='1' ";

      if (sArgs[0] != "")
      {
        sWhere += "AND (CREATETIME BETWEEN TO_DATE('{0}','yyyy-mm-dd hh24:mi:ss') AND TO_DATE('{1}','yyyy-mm-dd hh24:mi:ss')) ";
      }

      if (sArgs[2] != "")
      {
        sWhere += "AND CREATOR LIKE '%{2}%' ";
      }
      sWhere = string.Format(sWhere, sArgs);

      strbuld.Append(sWhere);
      strbuld.Append("ORDER BY CREATETIME DESC");

      try
      {
        string err = "";
        DataSet dset = DBManager.ExecuteQuery(strbuld.ToString(), out err);
        if (err == "")
        {
          strbuld.Remove(0, strbuld.Length);
          strbuld.Append("SELECT ID_,XH,ANSWERER,REPLYINFO,TIME_,IPADDRESS,ISCOMPLETE,DEPARTMENT FROM CXUSER.PT_QUESTION_REPLY WHERE EXISTS(");
          strbuld.Append("SELECT 1 FROM CXUSER.PT_QUESTION ");
          strbuld.Append(sWhere);
          strbuld.Append("AND CXUSER.PT_QUESTION.ID_=CXUSER.PT_QUESTION_REPLY.ID_)");
          DataSet dsetTmp = DBManager.ExecuteQuery(strbuld.ToString(), out err);

          dset.Tables[0].TableName = "tab_m";
          dsetTmp.Tables[0].TableName = "tab_d";
          dset.Tables.Add(dsetTmp.Tables[0].Copy());
          dset.Relations.Add("Band 1", dset.Tables[0].Columns[0], dset.Tables[1].Columns[0]);

          return new ReturnObject(dset);
        }
        return new ReturnObject(null, err);
      }
      catch (Exception ee)
      {
        return new ReturnObject(null, ee.Message);
      }
    }

    public ReturnObject getReplyData(string sID)
    {
      StringBuilder strbuld = new StringBuilder();
      strbuld.Append("SELECT ID_,XH,ANSWERER,REPLYINFO,TIME_,IPADDRESS,ISCOMPLETE,DEPARTMENT FROM CXUSER.PT_QUESTION_REPLY WHERE ID_='{0}' ORDER BY XH");

      try
      {
        string err = "";
        DataSet dset = DBManager.ExecuteQuery(string.Format(strbuld.ToString(), sID), out err);
        return new ReturnObject(dset);
      }
      catch (Exception ee)
      {
        return new ReturnObject(null, ee.Message);
      }
    }

    //sArgs[0]：发布人； sArgs[1]：发布部门； sArgs[2]：问题； sArgs[3]：负责部门； sArgs[4]：负责人； sArgs[5]：IP； sArgs[6]：发布人联系电话； sArgs[7]：问题类别；
    public ReturnObject addQuestion(string[] sArgs)
    {
      return ExecProcWithNoCollParam("cxuser.pt_k.addQuestion", sArgs);
    }

    //sArgs[0]：问题ID； sArgs[1]：问题； sArgs[2]：负责部门； sArgs[3]：负责人； sArgs[4]：发布人联系电话； sArgs[5]：问题类别；
    public ReturnObject editQuestion(string[] sArgs)
    {
      return ExecProcWithNoCollParam("cxuser.pt_k.editQuestion", sArgs);
    }

    //sArgs[0]：问题ID；
    public ReturnObject delQuestion(string[] sArgs)
    {
      return ExecProcWithNoCollParam("cxuser.pt_k.delQuestion", sArgs);
    }

    //sArgs[0]：问题ID； sArgs[1]：回复人； sArgs[2]：回复部门； sArgs[3]：回复内容； sArgs[4]：处理完成； sArgs[5]：IP；
    public ReturnObject addReply(string[] sArgs)
    {
      return ExecProcWithNoCollParam("cxuser.pt_k.addReply", sArgs);
    }

    //sArgs[0]：问题ID； sArgs[1]：序号； 
    public ReturnObject delReply(string[] sArgs)
    {
      return ExecProcWithNoCollParam("cxuser.pt_k.delReply", sArgs);
    }

    ReturnObject ExecProcWithNoCollParam(string sProcName, object[] Args)
    {
      try
      {
        string err = "";
        OracleParameter[] pars = new OracleParameter[Args.Length + 2];
        object[] vals = new object[Args.Length + 2];
        ArrayList arylstOut = new ArrayList();

        for (int i = 0; i < Args.Length + 2; i++)
        {
          pars[i] = new OracleParameter();
          if (i < Args.Length)
          {
            pars[i].ParameterName = "v" + i.ToString();
            if (Args[i].GetType() == typeof(byte[]))
              pars[i].OracleType = OracleType.Blob;

            pars[i].Value = Args[i];
          }
          else if (i == Args.Length)
          {
            pars[i].ParameterName = "iRet";
            pars[i].Direction = ParameterDirection.Output;
            pars[i].DbType = DbType.Int32;
          }
          else if (i == Args.Length + 1)
          {
            pars[i].ParameterName = "sRet";
            pars[i].Direction = ParameterDirection.Output;
            pars[i].DbType = DbType.String;
            pars[i].Size = 500;
          }
        }

        for (int i = 0; i < Args.Length; i++)
        {
          vals[i] = Args[i];
        }

        int iRet = DBManager.ExecuteNonQuery(sProcName, CommandType.StoredProcedure, pars, vals, ref arylstOut, out err);
        if (err == "")
          return new ReturnObject(arylstOut[0], arylstOut[1].ToString());
        else
          return new ReturnObject(iRet, err);
      }
      catch (Exception ee)
      {
        return new ReturnObject(-1, ee.Message);
      }
    }
  }
}
