using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using ZXDT.DBUtility;

namespace ZXDT.CallCenter.DAL
{
    /// <summary>
    /// 数据访问类:T_Wo_WorkOrderNotice
    /// </summary>
    public partial class T_Wo_WorkOrderNotice
    {
        public T_Wo_WorkOrderNotice()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int NoticeId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Wo_WorkOrderNotice");
            strSql.Append(" where NoticeId=@NoticeId");
            SqlParameter[] parameters = {
                    new SqlParameter("@NoticeId", SqlDbType.Int,4)
            };
            parameters[0].Value = NoticeId;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(ZXDT.CallCenter.Model.T_Wo_WorkOrderNotice model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Wo_WorkOrderNotice(");
            strSql.Append("OrderId,HistoryID,NoticeUserid,NoticeUsername,NoticeDeptId,NoticeDeptName,NoticeDeptContent,NoticeDeptAdvise,Class1,Class2,Class3,ClassId1,ClassId2,ClassId3,NoticeType,Userid,Username,DealAdvise,DealResult,NoticeState,Addtime,Remark1,Remark2,Remark3,Remark4,Remark5,DZNumber)");
            strSql.Append(" values (");
            strSql.Append("@OrderId,@HistoryID,@NoticeUserid,@NoticeUsername,@NoticeDeptId,@NoticeDeptName,@NoticeDeptContent,@NoticeDeptAdvise,@Class1,@Class2,@Class3,@ClassId1,@ClassId2,@ClassId3,@NoticeType,@Userid,@Username,@DealAdvise,@DealResult,@NoticeState,@Addtime,@Remark1,@Remark2,@Remark3,@Remark4,@Remark5,@DZNumber)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@OrderId", SqlDbType.Int,4),
                    new SqlParameter("@HistoryID", SqlDbType.Int,4),
                    new SqlParameter("@NoticeUserid", SqlDbType.Int,4),
                    new SqlParameter("@NoticeUsername", SqlDbType.VarChar,50),
                    new SqlParameter("@NoticeDeptId", SqlDbType.Int,4),
                    new SqlParameter("@NoticeDeptName", SqlDbType.VarChar,50),
                    new SqlParameter("@NoticeDeptContent", SqlDbType.VarChar,500),
                    new SqlParameter("@NoticeDeptAdvise", SqlDbType.VarChar,500),
                    new SqlParameter("@Class1", SqlDbType.VarChar,50),
                    new SqlParameter("@Class2", SqlDbType.VarChar,50),
                    new SqlParameter("@Class3", SqlDbType.VarChar,50),
                    new SqlParameter("@ClassId1", SqlDbType.Int,4),
                    new SqlParameter("@ClassId2", SqlDbType.Int,4),
                    new SqlParameter("@ClassId3", SqlDbType.Int,4),
                    new SqlParameter("@NoticeType", SqlDbType.Int,4),
                    new SqlParameter("@Userid", SqlDbType.Int,4),
                    new SqlParameter("@Username", SqlDbType.VarChar,50),
                    new SqlParameter("@DealAdvise", SqlDbType.VarChar,500),
                    new SqlParameter("@DealResult", SqlDbType.VarChar,500),
                    new SqlParameter("@NoticeState", SqlDbType.Int,4),
                    new SqlParameter("@Addtime", SqlDbType.DateTime),
                    new SqlParameter("@Remark1", SqlDbType.VarChar,500),
                    new SqlParameter("@Remark2", SqlDbType.VarChar,500),
                    new SqlParameter("@Remark3", SqlDbType.VarChar,500),
                    new SqlParameter("@Remark4", SqlDbType.VarChar,500),
                    new SqlParameter("@Remark5", SqlDbType.VarChar,500),
                    new SqlParameter("@DZNumber", SqlDbType.Int,4)};
            parameters[0].Value = model.OrderId;
            parameters[1].Value = model.HistoryID;
            parameters[2].Value = model.NoticeUserid;
            parameters[3].Value = model.NoticeUsername;
            parameters[4].Value = model.NoticeDeptId;
            parameters[5].Value = model.NoticeDeptName;
            parameters[6].Value = model.NoticeDeptContent;
            parameters[7].Value = model.NoticeDeptAdvise;
            parameters[8].Value = model.Class1;
            parameters[9].Value = model.Class2;
            parameters[10].Value = model.Class3;
            parameters[11].Value = model.ClassId1;
            parameters[12].Value = model.ClassId2;
            parameters[13].Value = model.ClassId3;
            parameters[14].Value = model.NoticeType;
            parameters[15].Value = model.Userid;
            parameters[16].Value = model.Username;
            parameters[17].Value = model.DealAdvise;
            parameters[18].Value = model.DealResult;
            parameters[19].Value = model.NoticeState;
            parameters[20].Value = model.Addtime;
            parameters[21].Value = model.Remark1;
            parameters[22].Value = model.Remark2;
            parameters[23].Value = model.Remark3;
            parameters[24].Value = model.Remark4;
            parameters[25].Value = model.Remark5;
            parameters[26].Value = model.DZNumber;

            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_WorkOrderNotice model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Wo_WorkOrderNotice set ");
            strSql.Append("OrderId=@OrderId,");
            strSql.Append("HistoryID=@HistoryID,");
            strSql.Append("NoticeUserid=@NoticeUserid,");
            strSql.Append("NoticeUsername=@NoticeUsername,");
            strSql.Append("NoticeDeptId=@NoticeDeptId,");
            strSql.Append("NoticeDeptName=@NoticeDeptName,");
            strSql.Append("NoticeDeptContent=@NoticeDeptContent,");
            strSql.Append("NoticeDeptAdvise=@NoticeDeptAdvise,");
            strSql.Append("Class1=@Class1,");
            strSql.Append("Class2=@Class2,");
            strSql.Append("Class3=@Class3,");
            strSql.Append("ClassId1=@ClassId1,");
            strSql.Append("ClassId2=@ClassId2,");
            strSql.Append("ClassId3=@ClassId3,");
            strSql.Append("NoticeType=@NoticeType,");
            strSql.Append("Userid=@Userid,");
            strSql.Append("Username=@Username,");
            strSql.Append("DealAdvise=@DealAdvise,");
            strSql.Append("DealResult=@DealResult,");
            strSql.Append("NoticeState=@NoticeState,");
            strSql.Append("Addtime=@Addtime,");
            strSql.Append("Remark1=@Remark1,");
            strSql.Append("Remark2=@Remark2,");
            strSql.Append("Remark3=@Remark3,");
            strSql.Append("Remark4=@Remark4,");
            strSql.Append("Remark5=@Remark5,");
            strSql.Append("DZNumber=@DZNumber");
            strSql.Append(" where NoticeId=@NoticeId");
            SqlParameter[] parameters = {
                    new SqlParameter("@OrderId", SqlDbType.Int,4),
                    new SqlParameter("@HistoryID", SqlDbType.Int,4),
                    new SqlParameter("@NoticeUserid", SqlDbType.Int,4),
                    new SqlParameter("@NoticeUsername", SqlDbType.VarChar,50),
                    new SqlParameter("@NoticeDeptId", SqlDbType.Int,4),
                    new SqlParameter("@NoticeDeptName", SqlDbType.VarChar,50),
                    new SqlParameter("@NoticeDeptContent", SqlDbType.VarChar,500),
                    new SqlParameter("@NoticeDeptAdvise", SqlDbType.VarChar,500),
                    new SqlParameter("@Class1", SqlDbType.VarChar,50),
                    new SqlParameter("@Class2", SqlDbType.VarChar,50),
                    new SqlParameter("@Class3", SqlDbType.VarChar,50),
                    new SqlParameter("@ClassId1", SqlDbType.Int,4),
                    new SqlParameter("@ClassId2", SqlDbType.Int,4),
                    new SqlParameter("@ClassId3", SqlDbType.Int,4),
                    new SqlParameter("@NoticeType", SqlDbType.Int,4),
                    new SqlParameter("@Userid", SqlDbType.Int,4),
                    new SqlParameter("@Username", SqlDbType.VarChar,50),
                    new SqlParameter("@DealAdvise", SqlDbType.VarChar,500),
                    new SqlParameter("@DealResult", SqlDbType.VarChar,500),
                    new SqlParameter("@NoticeState", SqlDbType.Int,4),
                    new SqlParameter("@Addtime", SqlDbType.DateTime),
                    new SqlParameter("@Remark1", SqlDbType.VarChar,500),
                    new SqlParameter("@Remark2", SqlDbType.VarChar,500),
                    new SqlParameter("@Remark3", SqlDbType.VarChar,500),
                    new SqlParameter("@Remark4", SqlDbType.VarChar,500),
                    new SqlParameter("@Remark5", SqlDbType.VarChar,500),
                    new SqlParameter("@DZNumber", SqlDbType.Int,4),
                    new SqlParameter("@NoticeId", SqlDbType.Int,4)};
            parameters[0].Value = model.OrderId;
            parameters[1].Value = model.HistoryID;
            parameters[2].Value = model.NoticeUserid;
            parameters[3].Value = model.NoticeUsername;
            parameters[4].Value = model.NoticeDeptId;
            parameters[5].Value = model.NoticeDeptName;
            parameters[6].Value = model.NoticeDeptContent;
            parameters[7].Value = model.NoticeDeptAdvise;
            parameters[8].Value = model.Class1;
            parameters[9].Value = model.Class2;
            parameters[10].Value = model.Class3;
            parameters[11].Value = model.ClassId1;
            parameters[12].Value = model.ClassId2;
            parameters[13].Value = model.ClassId3;
            parameters[14].Value = model.NoticeType;
            parameters[15].Value = model.Userid;
            parameters[16].Value = model.Username;
            parameters[17].Value = model.DealAdvise;
            parameters[18].Value = model.DealResult;
            parameters[19].Value = model.NoticeState;
            parameters[20].Value = model.Addtime;
            parameters[21].Value = model.Remark1;
            parameters[22].Value = model.Remark2;
            parameters[23].Value = model.Remark3;
            parameters[24].Value = model.Remark4;
            parameters[25].Value = model.Remark5;
            parameters[26].Value = model.DZNumber;
            parameters[27].Value = model.NoticeId;

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

        /// <summary>
        /// 更新定责次数
        /// </summary>
        public int updatenum(int orderid)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append(" update T_Wo_WorkOrderNotice ");
            strSql.Append(" set NoticeState=0  ");
            strSql.Append(" where OrderId=@OrderId;");
            SqlParameter[] parameters = {
                    new SqlParameter("@OrderId",orderid)
            };

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            strSql.Clear();
            strSql.Append(" select top 1 DZNumber from  T_Wo_WorkOrderNotice ");
            strSql.Append(" where OrderId=@OrderId Order by DZNumber desc; ");
            int rows = DbHelperSQL.GetSingle(strSql.ToString(), parameters).ToInt32();
            return rows;
        }

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

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

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


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

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 NoticeId,OrderId,HistoryID,NoticeUserid,NoticeUsername,NoticeDeptId,NoticeDeptName,NoticeDeptContent,NoticeDeptAdvise,Class1,Class2,Class3,ClassId1,ClassId2,ClassId3,NoticeType,Userid,Username,DealAdvise,DealResult,NoticeState,Addtime,Remark1,Remark2,Remark3,Remark4,Remark5,DZNumber from T_Wo_WorkOrderNotice ");
            strSql.Append(" where NoticeId=@NoticeId");
            SqlParameter[] parameters = {
                    new SqlParameter("@NoticeId", SqlDbType.Int,4)
            };
            parameters[0].Value = NoticeId;

            ZXDT.CallCenter.Model.T_Wo_WorkOrderNotice model = new ZXDT.CallCenter.Model.T_Wo_WorkOrderNotice();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public ZXDT.CallCenter.Model.T_Wo_WorkOrderNotice DataRowToModel(DataRow row)
        {
            ZXDT.CallCenter.Model.T_Wo_WorkOrderNotice model = new ZXDT.CallCenter.Model.T_Wo_WorkOrderNotice();
            if (row != null)
            {
                if (row["NoticeId"] != null && row["NoticeId"].ToString() != "")
                {
                    model.NoticeId = int.Parse(row["NoticeId"].ToString());
                }
                if (row["OrderId"] != null && row["OrderId"].ToString() != "")
                {
                    model.OrderId = int.Parse(row["OrderId"].ToString());
                }
                if (row["HistoryID"] != null && row["HistoryID"].ToString() != "")
                {
                    model.HistoryID = int.Parse(row["HistoryID"].ToString());
                }
                if (row["NoticeUserid"] != null && row["NoticeUserid"].ToString() != "")
                {
                    model.NoticeUserid = int.Parse(row["NoticeUserid"].ToString());
                }
                if (row["NoticeUsername"] != null)
                {
                    model.NoticeUsername = row["NoticeUsername"].ToString();
                }
                if (row["NoticeDeptId"] != null && row["NoticeDeptId"].ToString() != "")
                {
                    model.NoticeDeptId = int.Parse(row["NoticeDeptId"].ToString());
                }
                if (row["NoticeDeptName"] != null)
                {
                    model.NoticeDeptName = row["NoticeDeptName"].ToString();
                }
                if (row["NoticeDeptContent"] != null)
                {
                    model.NoticeDeptContent = row["NoticeDeptContent"].ToString();
                }
                if (row["NoticeDeptAdvise"] != null)
                {
                    model.NoticeDeptAdvise = row["NoticeDeptAdvise"].ToString();
                }
                if (row["Class1"] != null)
                {
                    model.Class1 = row["Class1"].ToString();
                }
                if (row["Class2"] != null)
                {
                    model.Class2 = row["Class2"].ToString();
                }
                if (row["Class3"] != null)
                {
                    model.Class3 = row["Class3"].ToString();
                }
                if (row["ClassId1"] != null && row["ClassId1"].ToString() != "")
                {
                    model.ClassId1 = int.Parse(row["ClassId1"].ToString());
                }
                if (row["ClassId2"] != null && row["ClassId2"].ToString() != "")
                {
                    model.ClassId2 = int.Parse(row["ClassId2"].ToString());
                }
                if (row["ClassId3"] != null && row["ClassId3"].ToString() != "")
                {
                    model.ClassId3 = int.Parse(row["ClassId3"].ToString());
                }
                if (row["NoticeType"] != null && row["NoticeType"].ToString() != "")
                {
                    model.NoticeType = int.Parse(row["NoticeType"].ToString());
                }
                if (row["Userid"] != null && row["Userid"].ToString() != "")
                {
                    model.Userid = int.Parse(row["Userid"].ToString());
                }
                if (row["Username"] != null)
                {
                    model.Username = row["Username"].ToString();
                }
                if (row["DealAdvise"] != null)
                {
                    model.DealAdvise = row["DealAdvise"].ToString();
                }
                if (row["DealResult"] != null)
                {
                    model.DealResult = row["DealResult"].ToString();
                }
                if (row["NoticeState"] != null && row["NoticeState"].ToString() != "")
                {
                    model.NoticeState = int.Parse(row["NoticeState"].ToString());
                }
                if (row["Addtime"] != null && row["Addtime"].ToString() != "")
                {
                    model.Addtime = DateTime.Parse(row["Addtime"].ToString());
                }
                if (row["Remark1"] != null)
                {
                    model.Remark1 = row["Remark1"].ToString();
                }
                if (row["Remark2"] != null)
                {
                    model.Remark2 = row["Remark2"].ToString();
                }
                if (row["Remark3"] != null)
                {
                    model.Remark3 = row["Remark3"].ToString();
                }
                if (row["Remark4"] != null)
                {
                    model.Remark4 = row["Remark4"].ToString();
                }
                if (row["Remark5"] != null)
                {
                    model.Remark5 = row["Remark5"].ToString();
                }
                if (row["DZNumber"] != null && row["DZNumber"].ToString() != "")
                {
                    model.DZNumber = int.Parse(row["DZNumber"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select NoticeId,OrderId,HistoryID,NoticeUserid,NoticeUsername,NoticeDeptId,NoticeDeptName,NoticeDeptContent,NoticeDeptAdvise,Class1,Class2,Class3,ClassId1,ClassId2,ClassId3,NoticeType,Userid,Username,DealAdvise,DealResult,NoticeState,Addtime,Remark1,Remark2,Remark3,Remark4,Remark5,DZNumber ");
            strSql.Append(" FROM T_Wo_WorkOrderNotice ");
            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(" NoticeId,OrderId,HistoryID,NoticeUserid,NoticeUsername,NoticeDeptId,NoticeDeptName,NoticeDeptContent,NoticeDeptAdvise,Class1,Class2,Class3,ClassId1,ClassId2,ClassId3,NoticeType,Userid,Username,DealAdvise,DealResult,NoticeState,Addtime,Remark1,Remark2,Remark3,Remark4,Remark5,DZNumber ");
            strSql.Append(" FROM T_Wo_WorkOrderNotice ");
            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_WorkOrderNotice ");
            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 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.NoticeId desc");
            }
            strSql.Append(")AS Row, T.*  from T_Wo_WorkOrderNotice 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 GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "T_Wo_WorkOrderNotice";
			parameters[1].Value = "NoticeId";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

