using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using ZXDT.DBUtility;//Please add references
namespace ZXDT.CallCenter.DAL
{
    /// <summary>
    /// 数据访问类:T_Wo_WorkOrderHistory
    /// </summary>
    public partial class T_Wo_WorkOrderHistory
    {
        public T_Wo_WorkOrderHistory()
        { }
        #region  Method



        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ZXDT.CallCenter.Model.T_Wo_WorkOrderHistory model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Wo_WorkOrderHistory(");
            strSql.Append("F_WORKORDERSTATEID,F_OPTUSERID,F_OPTDATE,F_ELAPSEDTIME,F_NEXTOWNERARRID,F_REMARK,F_OPTBTNID,F_INSTANCEID,F_TASKFLAG)");
            strSql.Append(" values (");
            strSql.Append("@F_WORKORDERSTATEID,@F_OPTUSERID,@F_OPTDATE,@F_ELAPSEDTIME,@F_NEXTOWNERARRID,@F_REMARK,@F_OPTBTNID,@F_INSTANCEID,@F_TASKFLAG)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@F_WORKORDERSTATEID", SqlDbType.Int,4),
					new SqlParameter("@F_OPTUSERID", SqlDbType.Int,4),
					new SqlParameter("@F_OPTDATE", SqlDbType.DateTime),
					new SqlParameter("@F_ELAPSEDTIME", SqlDbType.Int,4),
					new SqlParameter("@F_NEXTOWNERARRID", SqlDbType.NVarChar,200),
					new SqlParameter("@F_REMARK", SqlDbType.NVarChar,800),
					new SqlParameter("@F_OPTBTNID", SqlDbType.Int,4),
					new SqlParameter("@F_INSTANCEID", SqlDbType.Int,4),
					new SqlParameter("@F_TASKFLAG", SqlDbType.Int,4)};
            parameters[0].Value = model.F_WORKORDERSTATEID;
            parameters[1].Value = model.F_OPTUSERID;
            parameters[2].Value = model.F_OPTDATE;
            parameters[3].Value = model.F_ELAPSEDTIME;
            parameters[4].Value = model.F_NEXTOWNERARRID;
            parameters[5].Value = model.F_REMARK;
            parameters[6].Value = model.F_OPTBTNID;
            parameters[7].Value = model.F_INSTANCEID;
            parameters[8].Value = model.F_TASKFLAG;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(ZXDT.CallCenter.Model.T_Wo_WorkOrderHistory model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Wo_WorkOrderHistory set ");
            strSql.Append("F_WORKORDERSTATEID=@F_WORKORDERSTATEID,");
            strSql.Append("F_OPTUSERID=@F_OPTUSERID,");
            strSql.Append("F_OPTDATE=@F_OPTDATE,");
            strSql.Append("F_ELAPSEDTIME=@F_ELAPSEDTIME,");
            strSql.Append("F_NEXTOWNERARRID=@F_NEXTOWNERARRID,");
            strSql.Append("F_REMARK=@F_REMARK,");
            strSql.Append("F_OPTBTNID=@F_OPTBTNID,");
            strSql.Append("F_INSTANCEID=@F_INSTANCEID,");
            strSql.Append("F_TASKFLAG=@F_TASKFLAG,");
            strSql.Append("ReplayTime=@ReplayTime,");
            strSql.Append("F_StateName=@F_StateName");
            strSql.Append(" where F_HISTORYID=@F_HISTORYID");
            SqlParameter[] parameters = {
					new SqlParameter("@F_WORKORDERSTATEID", SqlDbType.Int,4),
					new SqlParameter("@F_OPTUSERID", SqlDbType.Int,4),
					new SqlParameter("@F_OPTDATE", SqlDbType.DateTime),
					new SqlParameter("@F_ELAPSEDTIME", SqlDbType.Int,4),
					new SqlParameter("@F_NEXTOWNERARRID", SqlDbType.NVarChar,200),
					new SqlParameter("@F_REMARK", SqlDbType.NVarChar,800),
					new SqlParameter("@F_OPTBTNID", SqlDbType.Int,4),
					new SqlParameter("@F_INSTANCEID", SqlDbType.Int,4),
					new SqlParameter("@F_TASKFLAG", SqlDbType.Int,4),
					new SqlParameter("@F_HISTORYID", SqlDbType.Int,4),
                    new SqlParameter("@F_StateName", SqlDbType.VarChar,50),
                    new SqlParameter("@ReplayTime", SqlDbType.DateTime)

            };
            parameters[0].Value = model.F_WORKORDERSTATEID;
            parameters[1].Value = model.F_OPTUSERID;
            parameters[2].Value = model.F_OPTDATE;
            parameters[3].Value = model.F_ELAPSEDTIME;
            parameters[4].Value = model.F_NEXTOWNERARRID;
            parameters[5].Value = model.F_REMARK;
            parameters[6].Value = model.F_OPTBTNID;
            parameters[7].Value = model.F_INSTANCEID;
            parameters[8].Value = model.F_TASKFLAG;
            parameters[9].Value = model.F_HISTORYID;
            parameters[10].Value = model.F_StateName;
            parameters[11].Value = model.ReplayTime;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int F_HISTORYID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Wo_WorkOrderHistory ");
            strSql.Append(" where F_HISTORYID=@F_HISTORYID");
            SqlParameter[] parameters = {
					new SqlParameter("@F_HISTORYID", SqlDbType.Int,4)
};
            parameters[0].Value = F_HISTORYID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string F_HISTORYIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Wo_WorkOrderHistory ");
            strSql.Append(" where F_HISTORYID in (" + F_HISTORYIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ZXDT.CallCenter.Model.T_Wo_WorkOrderHistory GetModel(int F_HISTORYID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 F_HISTORYID,F_WORKORDERSTATEID,F_OPTUSERID,F_OPTDATE,F_ELAPSEDTIME,F_NEXTOWNERARRID,F_REMARK,F_OPTBTNID,F_INSTANCEID,F_TASKFLAG from T_Wo_WorkOrderHistory ");
            strSql.Append(" where F_HISTORYID=@F_HISTORYID");
            SqlParameter[] parameters = {
					new SqlParameter("@F_HISTORYID", SqlDbType.Int,4)
};
            parameters[0].Value = F_HISTORYID;

            ZXDT.CallCenter.Model.T_Wo_WorkOrderHistory model = new ZXDT.CallCenter.Model.T_Wo_WorkOrderHistory();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["F_HISTORYID"] != null && ds.Tables[0].Rows[0]["F_HISTORYID"].ToString() != "")
                {
                    model.F_HISTORYID = int.Parse(ds.Tables[0].Rows[0]["F_HISTORYID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["F_WORKORDERSTATEID"] != null && ds.Tables[0].Rows[0]["F_WORKORDERSTATEID"].ToString() != "")
                {
                    model.F_WORKORDERSTATEID = int.Parse(ds.Tables[0].Rows[0]["F_WORKORDERSTATEID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["F_OPTUSERID"] != null && ds.Tables[0].Rows[0]["F_OPTUSERID"].ToString() != "")
                {
                    model.F_OPTUSERID = int.Parse(ds.Tables[0].Rows[0]["F_OPTUSERID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["F_OPTDATE"] != null && ds.Tables[0].Rows[0]["F_OPTDATE"].ToString() != "")
                {
                    model.F_OPTDATE = DateTime.Parse(ds.Tables[0].Rows[0]["F_OPTDATE"].ToString());
                }
                if (ds.Tables[0].Rows[0]["F_ELAPSEDTIME"] != null && ds.Tables[0].Rows[0]["F_ELAPSEDTIME"].ToString() != "")
                {
                    model.F_ELAPSEDTIME = int.Parse(ds.Tables[0].Rows[0]["F_ELAPSEDTIME"].ToString());
                }
                if (ds.Tables[0].Rows[0]["F_NEXTOWNERARRID"] != null && ds.Tables[0].Rows[0]["F_NEXTOWNERARRID"].ToString() != "")
                {
                    model.F_NEXTOWNERARRID = ds.Tables[0].Rows[0]["F_NEXTOWNERARRID"].ToString();
                }
                if (ds.Tables[0].Rows[0]["F_REMARK"] != null && ds.Tables[0].Rows[0]["F_REMARK"].ToString() != "")
                {
                    model.F_REMARK = ds.Tables[0].Rows[0]["F_REMARK"].ToString();
                }
                if (ds.Tables[0].Rows[0]["F_OPTBTNID"] != null && ds.Tables[0].Rows[0]["F_OPTBTNID"].ToString() != "")
                {
                    model.F_OPTBTNID = int.Parse(ds.Tables[0].Rows[0]["F_OPTBTNID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["F_INSTANCEID"] != null && ds.Tables[0].Rows[0]["F_INSTANCEID"].ToString() != "")
                {
                    model.F_INSTANCEID = int.Parse(ds.Tables[0].Rows[0]["F_INSTANCEID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["F_TASKFLAG"] != null && ds.Tables[0].Rows[0]["F_TASKFLAG"].ToString() != "")
                {
                    model.F_TASKFLAG = int.Parse(ds.Tables[0].Rows[0]["F_TASKFLAG"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM T_Wo_WorkOrderHistory ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append("    order by F_OPTDATE asc");
          
            return DbHelperSQL.Query(strSql.ToString());
        }

        public DataSet GetListView(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM V_WorkOrderHistory ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" F_HISTORYID,F_WORKORDERSTATEID,F_OPTUSERID,F_OPTDATE,F_ELAPSEDTIME,F_NEXTOWNERARRID,F_REMARK,F_OPTBTNID,F_INSTANCEID,F_TASKFLAG ");
            strSql.Append(" FROM T_Wo_WorkOrderHistory ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM T_Wo_WorkOrderHistory ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.T_Wo_WorkOrderHistory DataRowToModelDetail(DataRow row)
        {
            Model.T_Wo_WorkOrderHistory model = new Model.T_Wo_WorkOrderHistory();
            if (row != null)
            {
                if (row["F_HISTORYID"] != null && row["F_HISTORYID"].ToString() != "")
                {
                    model.F_HISTORYID = int.Parse(row["F_HISTORYID"].ToString());
                }
                if (row["F_INSTANCEID"] != null)
                {
                    model.F_INSTANCEID = int.Parse(row["F_INSTANCEID"].ToString());
                }
                if (row["F_WORKORDERSTATEID"] != null && row["F_WORKORDERSTATEID"].ToString() != "")
                {
                    model.F_WORKORDERSTATEID = int.Parse(row["F_WORKORDERSTATEID"].ToString());
                }
                if (row["F_OPTUSERID"] != null && row["F_OPTUSERID"].ToString() != "")
                {
                    model.F_OPTUSERID = int.Parse(row["F_OPTUSERID"].ToString());
                }
                if (row["F_OPTDATE"] != null && row["F_OPTDATE"].ToString() != "")
                {
                    model.F_OPTDATE = DateTime.Parse(row["F_OPTDATE"].ToString());
                }
                if (row["F_ELAPSEDTIME"] != null && row["F_ELAPSEDTIME"].ToString() != "")
                {
                    model.F_ELAPSEDTIME = int.Parse(row["F_ELAPSEDTIME"].ToString());
                }
                if (row["F_NEXTOWNERARRID"] != null)
                {
                    model.F_NEXTOWNERARRID = row["F_NEXTOWNERARRID"].ToString();
                }
                if (row["F_REMARK"] != null)
                {
                    model.F_REMARK = row["F_REMARK"].ToString();
                }
                if (row["F_OPTBTNID"] != null && row["F_OPTBTNID"].ToString() != "")
                {
                    model.F_OPTBTNID = int.Parse(row["F_OPTBTNID"].ToString());
                }
                if (row["F_TASKFLAG"] != null && row["F_TASKFLAG"].ToString() != "")
                {
                    model.F_TASKFLAG = int.Parse(row["F_TASKFLAG"].ToString());
                }

                if (row["F_UserName"] != null)
                {
                    model.F_UserName = row["F_UserName"].ToString();
                }
                if (row["F_Name"] != null)
                {
                    model.F_Name = row["F_Name"].ToString();
                }
                if (row["F_StateName"] != null)
                {
                    model.F_StateName = row["F_StateName"].ToString();
                }
            }
            return model;
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.F_HISTORYID desc");
            }
            strSql.Append(")AS Row, T.*  from T_Wo_WorkOrderHistory T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取订单的相关责任部门
        /// </summary>
        public DataSet GetListDZ(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select distinct F_OPTBTNID,F_NEXTOWNERARRID ");
            strSql.Append(" FROM T_Wo_WorkOrderHistory ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append("    order by F_OPTBTNID asc");

            return DbHelperSQL.Query(strSql.ToString());
        }
        #endregion  Method

    }
}

