using CallCenterApi.DB;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

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


        /// <summary>
		/// 增加一条数据
		/// </summary>
		public long Add(CallCenterApi.Model.T_Wo_WorkOrderItem_New model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Wo_WorkOrderItem_New(");
            strSql.Append("F_WoID,F_WoState,F_ItemType,F_OptType,F_OptContent,F_NextUser,F_NextDept,F_IsUsed,F_LimitTime,F_IsSMS,F_SMSToUser,F_SMSToPhone,F_SMSContent,F_CreateUser,F_CreateTime,F_LastTime,F_Type,F_Flag)");
            strSql.Append(" values (");
            strSql.Append("@F_WoID,@F_WoState,@F_ItemType,@F_OptType,@F_OptContent,@F_NextUser,@F_NextDept,@F_IsUsed,@F_LimitTime,@F_IsSMS,@F_SMSToUser,@F_SMSToPhone,@F_SMSContent,@F_CreateUser,@F_CreateTime,@F_LastTime,@F_Type,@F_Flag)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_WoID", SqlDbType.BigInt,8),
                    new SqlParameter("@F_WoState", SqlDbType.Int,4),
                    new SqlParameter("@F_ItemType", SqlDbType.Int,4),
                    new SqlParameter("@F_OptType", SqlDbType.Int,4),
                    new SqlParameter("@F_OptContent", SqlDbType.NVarChar,-1),
                    new SqlParameter("@F_NextUser", SqlDbType.VarChar,50),
                    new SqlParameter("@F_NextDept", SqlDbType.Int,4),
                    new SqlParameter("@F_IsUsed", SqlDbType.Int,4),
                    new SqlParameter("@F_LimitTime", SqlDbType.NVarChar,200),
                    new SqlParameter("@F_IsSMS", SqlDbType.Int,4),
                    new SqlParameter("@F_SMSToUser", SqlDbType.VarChar,50),
                    new SqlParameter("@F_SMSToPhone", SqlDbType.VarChar,50),
                    new SqlParameter("@F_SMSContent", SqlDbType.NText),
                    new SqlParameter("@F_CreateUser", SqlDbType.VarChar,50),
                    new SqlParameter("@F_CreateTime", SqlDbType.DateTime),
                    new SqlParameter("@F_LastTime", SqlDbType.DateTime),
                    new SqlParameter("@F_Type",  SqlDbType.Int,4),
                    new SqlParameter("@F_Flag",  SqlDbType.Int,4)
            };
            parameters[0].Value = model.F_WoID;
            parameters[1].Value = model.F_WoState;
            parameters[2].Value = model.F_ItemType;
            parameters[3].Value = model.F_OptType;
            parameters[4].Value = model.F_OptContent;
            parameters[5].Value = model.F_NextUser;
            parameters[6].Value = model.F_NextDept;
            parameters[7].Value = model.F_IsUsed;
            parameters[8].Value = model.F_LimitTime;
            parameters[9].Value = model.F_IsSMS;
            parameters[10].Value = model.F_SMSToUser;
            parameters[11].Value = model.F_SMSToPhone;
            parameters[12].Value = model.F_SMSContent;
            parameters[13].Value = model.F_CreateUser;
            parameters[14].Value = model.F_CreateTime;
            parameters[15].Value = model.F_LastTime;
            parameters[16].Value = model.F_Type;
            parameters[17].Value = model.F_Flag;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt64(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CallCenterApi.Model.T_Wo_WorkOrderItem_New model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Wo_WorkOrderItem_New set ");
            strSql.Append("F_WoID=@F_WoID,");
            strSql.Append("F_WoState=@F_WoState,");
            strSql.Append("F_ItemType=@F_ItemType,");
            strSql.Append("F_OptType=@F_OptType,");
            strSql.Append("F_OptContent=@F_OptContent,");
            strSql.Append("F_NextUser=@F_NextUser,");
            strSql.Append("F_NextDept=@F_NextDept,");
            strSql.Append("F_IsUsed=@F_IsUsed,");
            strSql.Append("F_LimitTime=@F_LimitTime,");
            strSql.Append("F_IsSMS=@F_IsSMS,");
            strSql.Append("F_SMSToUser=@F_SMSToUser,");
            strSql.Append("F_SMSToPhone=@F_SMSToPhone,");
            strSql.Append("F_SMSContent=@F_SMSContent,");
            strSql.Append("F_CreateUser=@F_CreateUser,");
            strSql.Append("F_CreateTime=@F_CreateTime,");
            strSql.Append("F_LastTime=@F_LastTime,");
            strSql.Append("F_Type=@F_Type,"); 
            strSql.Append("F_Flag=@F_Flag");
            strSql.Append(" where F_ID=@F_ID");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_WoID", SqlDbType.BigInt,8),
                    new SqlParameter("@F_WoState", SqlDbType.Int,4),
                    new SqlParameter("@F_ItemType", SqlDbType.Int,4),
                    new SqlParameter("@F_OptType", SqlDbType.Int,4),
                    new SqlParameter("@F_OptContent", SqlDbType.NVarChar,-1),
                    new SqlParameter("@F_NextUser", SqlDbType.VarChar,50),
                    new SqlParameter("@F_NextDept", SqlDbType.Int,4),
                    new SqlParameter("@F_IsUsed", SqlDbType.Int,4),
                    new SqlParameter("@F_LimitTime", SqlDbType.NVarChar,200),
                    new SqlParameter("@F_IsSMS", SqlDbType.Int,4),
                    new SqlParameter("@F_SMSToUser", SqlDbType.VarChar,50),
                    new SqlParameter("@F_SMSToPhone", SqlDbType.VarChar,50),
                    new SqlParameter("@F_SMSContent", SqlDbType.NText),
                    new SqlParameter("@F_CreateUser", SqlDbType.VarChar,50),
                    new SqlParameter("@F_CreateTime", SqlDbType.DateTime),
                    new SqlParameter("@F_LastTime", SqlDbType.DateTime),
                    new SqlParameter("@F_Type", SqlDbType.Int,4),
                    new SqlParameter("@F_Flag", SqlDbType.Int,4),
                    new SqlParameter("@F_ID", SqlDbType.BigInt,8)
       
                    
            };
            parameters[0].Value = model.F_WoID;
            parameters[1].Value = model.F_WoState;
            parameters[2].Value = model.F_ItemType;
            parameters[3].Value = model.F_OptType;
            parameters[4].Value = model.F_OptContent;
            parameters[5].Value = model.F_NextUser;
            parameters[6].Value = model.F_NextDept;
            parameters[7].Value = model.F_IsUsed;
            parameters[8].Value = model.F_LimitTime;
            parameters[9].Value = model.F_IsSMS;
            parameters[10].Value = model.F_SMSToUser;
            parameters[11].Value = model.F_SMSToPhone;
            parameters[12].Value = model.F_SMSContent;
            parameters[13].Value = model.F_CreateUser;
            parameters[14].Value = model.F_CreateTime;
            parameters[15].Value = model.F_LastTime;
            parameters[16].Value = model.F_Type; 
            parameters[17].Value = model.F_Flag;
            parameters[18].Value = model.F_ID;

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

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

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Wo_WorkOrderItem_New ");
            strSql.Append(" where F_ID=@F_ID");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_ID", SqlDbType.BigInt)
            };
            parameters[0].Value = F_ID;

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


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CallCenterApi.Model.T_Wo_WorkOrderItem_New GetModel(long F_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from T_Wo_WorkOrderItem_New ");
            strSql.Append(" where F_ID=@F_ID");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_ID", SqlDbType.BigInt)
            };
            parameters[0].Value = F_ID;

            CallCenterApi.Model.T_Wo_WorkOrderItem_New model = new CallCenterApi.Model.T_Wo_WorkOrderItem_New();
            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 CallCenterApi.Model.T_Wo_WorkOrderItem_New DataRowToModel(DataRow row)
        {
            CallCenterApi.Model.T_Wo_WorkOrderItem_New model = new CallCenterApi.Model.T_Wo_WorkOrderItem_New();
            if (row != null)
            {
                if (row["F_ID"] != null && row["F_ID"].ToString() != "")
                {
                    model.F_ID = long.Parse(row["F_ID"].ToString());
                }
                if (row["F_WoID"] != null && row["F_WoID"].ToString() != "")
                {
                    model.F_WoID = long.Parse(row["F_WoID"].ToString());
                }
                if (row["F_WoState"] != null && row["F_WoState"].ToString() != "")
                {
                    model.F_WoState = int.Parse(row["F_WoState"].ToString());
                }
                if (row["F_ItemType"] != null && row["F_ItemType"].ToString() != "")
                {
                    model.F_ItemType = int.Parse(row["F_ItemType"].ToString());
                }
                if (row["F_OptType"] != null && row["F_OptType"].ToString() != "")
                {
                    model.F_OptType = int.Parse(row["F_OptType"].ToString());
                }
                if (row["F_OptContent"] != null)
                {
                    model.F_OptContent = row["F_OptContent"].ToString();
                }
                if (row["F_NextUser"] != null)
                {
                    model.F_NextUser = row["F_NextUser"].ToString();
                }
                if (row["F_NextDept"] != null && row["F_NextDept"].ToString() != "")
                {
                    model.F_NextDept = int.Parse(row["F_NextDept"].ToString());
                }
                if (row["F_IsUsed"] != null && row["F_IsUsed"].ToString() != "")
                {
                    model.F_IsUsed = int.Parse(row["F_IsUsed"].ToString());
                }
                if (row["F_LimitTime"] != null)
                {
                    model.F_LimitTime = row["F_LimitTime"].ToString();
                }
                if (row["F_IsSMS"] != null && row["F_IsSMS"].ToString() != "")
                {
                    model.F_IsSMS = int.Parse(row["F_IsSMS"].ToString());
                }
                if (row["F_SMSToUser"] != null)
                {
                    model.F_SMSToUser = row["F_SMSToUser"].ToString();
                }
                if (row["F_SMSToPhone"] != null)
                {
                    model.F_SMSToPhone = row["F_SMSToPhone"].ToString();
                }
                if (row["F_SMSContent"] != null)
                {
                    model.F_SMSContent = row["F_SMSContent"].ToString();
                }
                if (row["F_CreateUser"] != null)
                {
                    model.F_CreateUser = row["F_CreateUser"].ToString();
                }
                if (row["F_CreateTime"] != null && row["F_CreateTime"].ToString() != "")
                {
                    model.F_CreateTime = DateTime.Parse(row["F_CreateTime"].ToString());
                }
                if (row["F_LastTime"] != null && row["F_LastTime"].ToString() != "")
                {
                    model.F_LastTime = DateTime.Parse(row["F_LastTime"].ToString());
                }
                if (row["F_Type"] != null && row["F_Type"].ToString() != "")
                {
                    model.F_Type = int.Parse(row["F_Type"].ToString());
                }
                if (row["F_Flag"] != null && row["F_Flag"].ToString() != "")
                {
                    model.F_Flag = int.Parse(row["F_Flag"].ToString());
                }

            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM T_Wo_WorkOrderItem_New ");
            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(" * ");
            strSql.Append(" FROM T_Wo_WorkOrderItem_New ");
            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_WorkOrderItem_New ");
            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_ID desc");
            }
            strSql.Append(")AS Row, T.*  from T_Wo_WorkOrderItem_New 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());
        }
        
        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}
