using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using CallCenterApi.DB;
namespace CallCenterApi.DAL
{
    /// <summary>
    /// 数据访问类:T_Web_WorkRelease
    /// </summary>
    public partial class T_Web_WorkRelease
    {
        public T_Web_WorkRelease()
        { }
        #region  Method

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string F_WorkOrderId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Web_WorkRelease");
            strSql.Append(" where F_WorkOrderId=@F_WorkOrderId ");
            SqlParameter[] parameters = {
					new SqlParameter("@F_WorkOrderId", SqlDbType.VarChar,50)};
            parameters[0].Value = F_WorkOrderId;

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


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(CallCenterApi.Model.T_Web_WorkRelease model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Web_WorkRelease(");
            strSql.Append("F_WorkOrderId,F_ReleaseState,F_EffectiveState,F_EffectiveTime,F_Releaser,F_ReleaseTime,F_WorkState,F_WorkSort,F_AcceptTime,F_FeedbackTime,F_Title,F_Content,F_Opinion,F_Department,F_Result,F_ViewCount,F_DeptID,F_InfoConType,F_BackDate,F_AssignedIdea)");
            strSql.Append(" values (");
            strSql.Append("@F_WorkOrderId,@F_ReleaseState,@F_EffectiveState,@F_EffectiveTime,@F_Releaser,@F_ReleaseTime,@F_WorkState,@F_WorkSort,@F_AcceptTime,@F_FeedbackTime,@F_Title,@F_Content,@F_Opinion,@F_Department,@F_Result,@F_ViewCount,@F_DeptID,@F_InfoConType,@F_BackDate,@F_AssignedIdea)");
            SqlParameter[] parameters = {
					new SqlParameter("@F_WorkOrderId", SqlDbType.VarChar,50),
					new SqlParameter("@F_ReleaseState", SqlDbType.Int,4),
					new SqlParameter("@F_EffectiveState", SqlDbType.Int,4),
					new SqlParameter("@F_EffectiveTime", SqlDbType.DateTime),
					new SqlParameter("@F_Releaser", SqlDbType.NVarChar,50),
					new SqlParameter("@F_ReleaseTime", SqlDbType.DateTime),
					new SqlParameter("@F_WorkState", SqlDbType.Int,4),
					new SqlParameter("@F_WorkSort", SqlDbType.Int,4),
					new SqlParameter("@F_AcceptTime", SqlDbType.DateTime),
					new SqlParameter("@F_FeedbackTime", SqlDbType.DateTime),
					new SqlParameter("@F_Title", SqlDbType.VarChar,200),
					new SqlParameter("@F_Content", SqlDbType.Text),
					new SqlParameter("@F_Opinion", SqlDbType.VarChar,500),
					new SqlParameter("@F_Department", SqlDbType.Int,4),
					new SqlParameter("@F_Result", SqlDbType.Text),
					new SqlParameter("@F_ViewCount", SqlDbType.Int,4),
					new SqlParameter("@F_DeptID", SqlDbType.Int,4),
					new SqlParameter("@F_InfoConType", SqlDbType.NVarChar,200),
					new SqlParameter("@F_BackDate", SqlDbType.DateTime),
					new SqlParameter("@F_AssignedIdea", SqlDbType.NVarChar)};
            parameters[0].Value = model.F_WorkOrderId;
            parameters[1].Value = model.F_ReleaseState;
            parameters[2].Value = model.F_EffectiveState;
            parameters[3].Value = model.F_EffectiveTime;
            parameters[4].Value = model.F_Releaser;
            parameters[5].Value = model.F_ReleaseTime;
            parameters[6].Value = model.F_WorkState;
            parameters[7].Value = model.F_WorkSort;
            parameters[8].Value = model.F_AcceptTime;
            parameters[9].Value = model.F_FeedbackTime;
            parameters[10].Value = model.F_Title;
            parameters[11].Value = model.F_Content;
            parameters[12].Value = model.F_Opinion;
            parameters[13].Value = model.F_Department;
            parameters[14].Value = model.F_Result;
            parameters[15].Value = model.F_ViewCount;
            parameters[16].Value = model.F_DeptID;
            parameters[17].Value = model.F_InfoConType;
            parameters[18].Value = model.F_BackDate;
            parameters[19].Value = model.F_AssignedIdea;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CallCenterApi.Model.T_Web_WorkRelease model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Web_WorkRelease set ");
            strSql.Append("F_ReleaseState=@F_ReleaseState,");
            strSql.Append("F_EffectiveState=@F_EffectiveState,");
            strSql.Append("F_EffectiveTime=@F_EffectiveTime,");
            strSql.Append("F_Releaser=@F_Releaser,");
            strSql.Append("F_ReleaseTime=@F_ReleaseTime,");
            strSql.Append("F_WorkState=@F_WorkState,");
            strSql.Append("F_WorkSort=@F_WorkSort,");
            strSql.Append("F_AcceptTime=@F_AcceptTime,");
            strSql.Append("F_FeedbackTime=@F_FeedbackTime,");
            strSql.Append("F_Title=@F_Title,");
            strSql.Append("F_Content=@F_Content,");
            strSql.Append("F_Opinion=@F_Opinion,");
            strSql.Append("F_Department=@F_Department,");
            strSql.Append("F_Result=@F_Result,");
            strSql.Append("F_ViewCount=@F_ViewCount,");
            strSql.Append("F_DeptID=@F_DeptID,");
            strSql.Append("F_InfoConType=@F_InfoConType,");
            strSql.Append("F_BackDate=@F_BackDate,");
            strSql.Append("F_AssignedIdea=@F_AssignedIdea");
            strSql.Append(" where F_WorkOrderId=@F_WorkOrderId ");
            SqlParameter[] parameters = {
					new SqlParameter("@F_ReleaseState", SqlDbType.Int,4),
					new SqlParameter("@F_EffectiveState", SqlDbType.Int,4),
					new SqlParameter("@F_EffectiveTime", SqlDbType.DateTime),
					new SqlParameter("@F_Releaser", SqlDbType.NVarChar,50),
					new SqlParameter("@F_ReleaseTime", SqlDbType.DateTime),
					new SqlParameter("@F_WorkState", SqlDbType.Int,4),
					new SqlParameter("@F_WorkSort", SqlDbType.Int,4),
					new SqlParameter("@F_AcceptTime", SqlDbType.DateTime),
					new SqlParameter("@F_FeedbackTime", SqlDbType.DateTime),
					new SqlParameter("@F_Title", SqlDbType.VarChar,200),
					new SqlParameter("@F_Content", SqlDbType.Text),
					new SqlParameter("@F_Opinion", SqlDbType.VarChar,500),
					new SqlParameter("@F_Department", SqlDbType.Int,4),
					new SqlParameter("@F_Result", SqlDbType.Text),
					new SqlParameter("@F_ViewCount", SqlDbType.Int,4),
					new SqlParameter("@F_DeptID", SqlDbType.Int,4),
					new SqlParameter("@F_InfoConType", SqlDbType.NVarChar,200),
					new SqlParameter("@F_BackDate", SqlDbType.DateTime),
					new SqlParameter("@F_AssignedIdea", SqlDbType.NVarChar),
					new SqlParameter("@F_WorkOrderId", SqlDbType.VarChar,20)};
            parameters[0].Value = model.F_ReleaseState;
            parameters[1].Value = model.F_EffectiveState;
            parameters[2].Value = model.F_EffectiveTime;
            parameters[3].Value = model.F_Releaser;
            parameters[4].Value = model.F_ReleaseTime;
            parameters[5].Value = model.F_WorkState;
            parameters[6].Value = model.F_WorkSort;
            parameters[7].Value = model.F_AcceptTime;
            parameters[8].Value = model.F_FeedbackTime;
            parameters[9].Value = model.F_Title;
            parameters[10].Value = model.F_Content;
            parameters[11].Value = model.F_Opinion;
            parameters[12].Value = model.F_Department;
            parameters[13].Value = model.F_Result;
            parameters[14].Value = model.F_ViewCount;
            parameters[15].Value = model.F_DeptID;
            parameters[16].Value = model.F_InfoConType;
            parameters[17].Value = model.F_BackDate;
            parameters[18].Value = model.F_AssignedIdea;
            parameters[19].Value = model.F_WorkOrderId;

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

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

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Web_WorkRelease ");
            strSql.Append(" where F_WorkOrderId=@F_WorkOrderId ");
            SqlParameter[] parameters = {
					new SqlParameter("@F_WorkOrderId", SqlDbType.VarChar,20)};
            parameters[0].Value = F_WorkOrderId;

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


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CallCenterApi.Model.T_Web_WorkRelease GetModel(string F_WorkOrderId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 F_WorkOrderId,F_ReleaseState,F_EffectiveState,F_EffectiveTime,F_Releaser,F_ReleaseTime,F_WorkState,F_WorkSort,F_AcceptTime,F_FeedbackTime,F_Title,F_Content,F_Opinion,F_Department,F_Result,F_ViewCount,F_DeptID,F_InfoConType,F_BackDate,F_AssignedIdea from T_Web_WorkRelease ");
            strSql.Append(" where F_WorkOrderId=@F_WorkOrderId ");
            SqlParameter[] parameters = {
					new SqlParameter("@F_WorkOrderId", SqlDbType.VarChar,50)};
            parameters[0].Value = F_WorkOrderId;

            CallCenterApi.Model.T_Web_WorkRelease model = new CallCenterApi.Model.T_Web_WorkRelease();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["F_WorkOrderId"] != null && ds.Tables[0].Rows[0]["F_WorkOrderId"].ToString() != "")
                {
                    model.F_WorkOrderId = ds.Tables[0].Rows[0]["F_WorkOrderId"].ToString();
                }
                if (ds.Tables[0].Rows[0]["F_ReleaseState"] != null && ds.Tables[0].Rows[0]["F_ReleaseState"].ToString() != "")
                {
                    model.F_ReleaseState = int.Parse(ds.Tables[0].Rows[0]["F_ReleaseState"].ToString());
                }
                if (ds.Tables[0].Rows[0]["F_EffectiveState"] != null && ds.Tables[0].Rows[0]["F_EffectiveState"].ToString() != "")
                {
                    model.F_EffectiveState = int.Parse(ds.Tables[0].Rows[0]["F_EffectiveState"].ToString());
                }
                if (ds.Tables[0].Rows[0]["F_EffectiveTime"] != null && ds.Tables[0].Rows[0]["F_EffectiveTime"].ToString() != "")
                {
                    model.F_EffectiveTime = DateTime.Parse(ds.Tables[0].Rows[0]["F_EffectiveTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["F_Releaser"] != null && ds.Tables[0].Rows[0]["F_Releaser"].ToString() != "")
                {
                    model.F_Releaser = ds.Tables[0].Rows[0]["F_Releaser"].ToString();
                }
                if (ds.Tables[0].Rows[0]["F_ReleaseTime"] != null && ds.Tables[0].Rows[0]["F_ReleaseTime"].ToString() != "")
                {
                    model.F_ReleaseTime = DateTime.Parse(ds.Tables[0].Rows[0]["F_ReleaseTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["F_WorkState"] != null && ds.Tables[0].Rows[0]["F_WorkState"].ToString() != "")
                {
                    model.F_WorkState = int.Parse(ds.Tables[0].Rows[0]["F_WorkState"].ToString());
                }
                if (ds.Tables[0].Rows[0]["F_WorkSort"] != null && ds.Tables[0].Rows[0]["F_WorkSort"].ToString() != "")
                {
                    model.F_WorkSort = int.Parse(ds.Tables[0].Rows[0]["F_WorkSort"].ToString());
                }
                if (ds.Tables[0].Rows[0]["F_AcceptTime"] != null && ds.Tables[0].Rows[0]["F_AcceptTime"].ToString() != "")
                {
                    model.F_AcceptTime = DateTime.Parse(ds.Tables[0].Rows[0]["F_AcceptTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["F_FeedbackTime"] != null && ds.Tables[0].Rows[0]["F_FeedbackTime"].ToString() != "")
                {
                    model.F_FeedbackTime = DateTime.Parse(ds.Tables[0].Rows[0]["F_FeedbackTime"].ToString());
                }
                if (ds.Tables[0].Rows[0]["F_Title"] != null && ds.Tables[0].Rows[0]["F_Title"].ToString() != "")
                {
                    model.F_Title = ds.Tables[0].Rows[0]["F_Title"].ToString();
                }
                if (ds.Tables[0].Rows[0]["F_Content"] != null && ds.Tables[0].Rows[0]["F_Content"].ToString() != "")
                {
                    model.F_Content = ds.Tables[0].Rows[0]["F_Content"].ToString();
                }
                if (ds.Tables[0].Rows[0]["F_Opinion"] != null && ds.Tables[0].Rows[0]["F_Opinion"].ToString() != "")
                {
                    model.F_Opinion = ds.Tables[0].Rows[0]["F_Opinion"].ToString();
                }
                if (ds.Tables[0].Rows[0]["F_Department"] != null && ds.Tables[0].Rows[0]["F_Department"].ToString() != "")
                {
                    model.F_Department = int.Parse(ds.Tables[0].Rows[0]["F_Department"].ToString());
                }
                if (ds.Tables[0].Rows[0]["F_Result"] != null && ds.Tables[0].Rows[0]["F_Result"].ToString() != "")
                {
                    model.F_Result = ds.Tables[0].Rows[0]["F_Result"].ToString();
                }
                if (ds.Tables[0].Rows[0]["F_ViewCount"] != null && ds.Tables[0].Rows[0]["F_ViewCount"].ToString() != "")
                {
                    model.F_ViewCount = int.Parse(ds.Tables[0].Rows[0]["F_ViewCount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["F_DeptID"] != null && ds.Tables[0].Rows[0]["F_DeptID"].ToString() != "")
                {
                    model.F_DeptID = int.Parse(ds.Tables[0].Rows[0]["F_DeptID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["F_InfoConType"] != null && ds.Tables[0].Rows[0]["F_InfoConType"].ToString() != "")
                {
                    model.F_InfoConType = ds.Tables[0].Rows[0]["F_InfoConType"].ToString();
                }
                if (ds.Tables[0].Rows[0]["F_BackDate"] != null && ds.Tables[0].Rows[0]["F_BackDate"].ToString() != "")
                {
                    model.F_BackDate = DateTime.Parse(ds.Tables[0].Rows[0]["F_BackDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["F_AssignedIdea"] != null && ds.Tables[0].Rows[0]["F_AssignedIdea"].ToString() != "")
                {
                    model.F_AssignedIdea = ds.Tables[0].Rows[0]["F_AssignedIdea"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select F_WorkOrderId,F_ReleaseState,F_EffectiveState,F_EffectiveTime,F_Releaser,F_ReleaseTime,F_WorkState,F_WorkSort,F_AcceptTime,F_FeedbackTime,F_Title,F_Content,F_Opinion,F_Department,F_Result,F_ViewCount,F_DeptID,F_InfoConType,F_BackDate,F_AssignedIdea ");
            strSql.Append(" FROM T_Web_WorkRelease ");
            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_WorkOrderId,F_ReleaseState,F_EffectiveState,F_EffectiveTime,F_Releaser,F_ReleaseTime,F_WorkState,F_WorkSort,F_AcceptTime,F_FeedbackTime,F_Title,F_Content,F_Opinion,F_Department,F_Result,F_ViewCount,F_DeptID,F_InfoConType,F_BackDate,F_AssignedIdea ");
            strSql.Append(" FROM T_Web_WorkRelease ");
            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_Web_WorkRelease ");
            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.F_WorkOrderId desc");
            }
            strSql.Append(")AS Row, T.*  from T_Web_WorkRelease 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_Web_WorkRelease";
            parameters[1].Value = "F_WorkOrderId";
            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  Method
        #region addMethod
        /// <summary>
        /// 更新发布状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public bool UpdateBy(string id, string state)
        {
            string strSql = "";
            strSql += "update T_Web_WorkRelease set F_ReleaseState=";
            strSql += state;
            strSql += " where F_WorkOrderId='" + id + "'";
            int row = DbHelperSQL.ExecuteSql(strSql);
            if (row > 0)
                return true;
            else
                return false;
        }
        #endregion
    }
}

