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_MaterialManage
	/// </summary>
	public partial class T_Wo_MaterialManage
    {
        public T_Wo_MaterialManage()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int F_Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Wo_MaterialManage");
            strSql.Append(" where F_Id=@F_Id");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_Id", SqlDbType.Int,4)
            };
            parameters[0].Value = F_Id;

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


        /// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(CallCenterApi.Model.T_Wo_MaterialManage model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Wo_MaterialManage(");
            strSql.Append("F_MaterialCode,F_MaterialName,F_Model,F_Specs,F_Level1,F_Level2,F_Level3,F_Brand,F_MaterialEffect,F_Pipeline,F_Craft,F_Remark,F_CreateBy,F_CreateOn,F_UpdateCount,F_UpdateBy,F_UpdateOn,F_IsDelete,F_DeleteBy,F_DeleteOn)");
            strSql.Append(" values (");
            strSql.Append("@F_MaterialCode,@F_MaterialName,@F_Model,@F_Specs,@F_Level1,@F_Level2,@F_Level3,@F_Brand,@F_MaterialEffect,@F_Pipeline,@F_Craft,@F_Remark,@F_CreateBy,@F_CreateOn,@F_UpdateCount,@F_UpdateBy,@F_UpdateOn,@F_IsDelete,@F_DeleteBy,@F_DeleteOn)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_MaterialCode", SqlDbType.NVarChar,11),
                    new SqlParameter("@F_MaterialName", SqlDbType.NVarChar,2000),
                    new SqlParameter("@F_Model", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_Specs", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_Level1", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_Level2", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_Level3", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_Brand", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_MaterialEffect", SqlDbType.NText),
                    new SqlParameter("@F_Pipeline", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_Craft", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_Remark", SqlDbType.NText),
                    new SqlParameter("@F_CreateBy", SqlDbType.VarChar,50),
                    new SqlParameter("@F_CreateOn", SqlDbType.DateTime),
                    new SqlParameter("@F_UpdateCount", SqlDbType.Int,4),
                    new SqlParameter("@F_UpdateBy", SqlDbType.VarChar,50),
                    new SqlParameter("@F_UpdateOn", SqlDbType.DateTime),
                    new SqlParameter("@F_IsDelete", SqlDbType.Int,4),
                    new SqlParameter("@F_DeleteBy", SqlDbType.VarChar,50),
                    new SqlParameter("@F_DeleteOn", SqlDbType.DateTime)};
            parameters[0].Value = model.F_MaterialCode;
            parameters[1].Value = model.F_MaterialName;
            parameters[2].Value = model.F_Model;
            parameters[3].Value = model.F_Specs;
            parameters[4].Value = model.F_Level1;
            parameters[5].Value = model.F_Level2;
            parameters[6].Value = model.F_Level3;
            parameters[7].Value = model.F_Brand;
            parameters[8].Value = model.F_MaterialEffect;
            parameters[9].Value = model.F_Pipeline;
            parameters[10].Value = model.F_Craft;
            parameters[11].Value = model.F_Remark;
            parameters[12].Value = model.F_CreateBy;
            parameters[13].Value = model.F_CreateOn;
            parameters[14].Value = model.F_UpdateCount;
            parameters[15].Value = model.F_UpdateBy;
            parameters[16].Value = model.F_UpdateOn;
            parameters[17].Value = model.F_IsDelete;
            parameters[18].Value = model.F_DeleteBy;
            parameters[19].Value = model.F_DeleteOn;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CallCenterApi.Model.T_Wo_MaterialManage model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Wo_MaterialManage set ");
            strSql.Append("F_MaterialCode=@F_MaterialCode,");
            strSql.Append("F_MaterialName=@F_MaterialName,");
            strSql.Append("F_Model=@F_Model,");
            strSql.Append("F_Specs=@F_Specs,");
            strSql.Append("F_Level1=@F_Level1,");
            strSql.Append("F_Level2=@F_Level2,");
            strSql.Append("F_Level3=@F_Level3,");
            strSql.Append("F_Brand=@F_Brand,");
            strSql.Append("F_MaterialEffect=@F_MaterialEffect,");
            strSql.Append("F_Pipeline=@F_Pipeline,");
            strSql.Append("F_Craft=@F_Craft,");
            strSql.Append("F_Remark=@F_Remark,");
            strSql.Append("F_CreateBy=@F_CreateBy,");
            strSql.Append("F_CreateOn=@F_CreateOn,");
            strSql.Append("F_UpdateCount=@F_UpdateCount,");
            strSql.Append("F_UpdateBy=@F_UpdateBy,");
            strSql.Append("F_UpdateOn=@F_UpdateOn,");
            strSql.Append("F_IsDelete=@F_IsDelete,");
            strSql.Append("F_DeleteBy=@F_DeleteBy,");
            strSql.Append("F_DeleteOn=@F_DeleteOn");
            strSql.Append(" where F_Id=@F_Id");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_MaterialCode", SqlDbType.NVarChar,11),
                    new SqlParameter("@F_MaterialName", SqlDbType.NVarChar,2000),
                    new SqlParameter("@F_Model", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_Specs", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_Level1", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_Level2", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_Level3", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_Brand", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_MaterialEffect", SqlDbType.NText),
                    new SqlParameter("@F_Pipeline", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_Craft", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_Remark", SqlDbType.NText),
                    new SqlParameter("@F_CreateBy", SqlDbType.VarChar,50),
                    new SqlParameter("@F_CreateOn", SqlDbType.DateTime),
                    new SqlParameter("@F_UpdateCount", SqlDbType.Int,4),
                    new SqlParameter("@F_UpdateBy", SqlDbType.VarChar,50),
                    new SqlParameter("@F_UpdateOn", SqlDbType.DateTime),
                    new SqlParameter("@F_IsDelete", SqlDbType.Int,4),
                    new SqlParameter("@F_DeleteBy", SqlDbType.VarChar,50),
                    new SqlParameter("@F_DeleteOn", SqlDbType.DateTime),
                    new SqlParameter("@F_Id", SqlDbType.Int,4)};
            parameters[0].Value = model.F_MaterialCode;
            parameters[1].Value = model.F_MaterialName;
            parameters[2].Value = model.F_Model;
            parameters[3].Value = model.F_Specs;
            parameters[4].Value = model.F_Level1;
            parameters[5].Value = model.F_Level2;
            parameters[6].Value = model.F_Level3;
            parameters[7].Value = model.F_Brand;
            parameters[8].Value = model.F_MaterialEffect;
            parameters[9].Value = model.F_Pipeline;
            parameters[10].Value = model.F_Craft;
            parameters[11].Value = model.F_Remark;
            parameters[12].Value = model.F_CreateBy;
            parameters[13].Value = model.F_CreateOn;
            parameters[14].Value = model.F_UpdateCount;
            parameters[15].Value = model.F_UpdateBy;
            parameters[16].Value = model.F_UpdateOn;
            parameters[17].Value = model.F_IsDelete;
            parameters[18].Value = model.F_DeleteBy;
            parameters[19].Value = model.F_DeleteOn;
            parameters[20].Value = model.F_Id;

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

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int F_Id, string usercode, DateTime deltime)
        {

            StringBuilder strSql = new StringBuilder();
            //strSql.Append("delete from T_Wo_MaterialManage ");
            strSql.Append("update T_Wo_MaterialManage set F_IsDelete=1,");
            strSql.Append("F_DeleteBy=@F_DeleteBy,");
            strSql.Append("F_DeleteOn=@F_DeleteOn");
            strSql.Append(" where F_Id=@F_Id");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_DeleteBy", SqlDbType.VarChar,50),
                    new SqlParameter("@F_DeleteOn", SqlDbType.DateTime),
                    new SqlParameter("@F_Id", SqlDbType.Int,4)
            };
            parameters[0].Value = usercode;
            parameters[1].Value = deltime;
            parameters[2].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, string usercode, DateTime deltime)
        {
            StringBuilder strSql = new StringBuilder();
            //strSql.Append("delete from T_Wo_MaterialManage ");
            strSql.Append("update T_Wo_MaterialManage set F_IsDelete=1,");
            strSql.Append("F_DeleteBy='"+ usercode + "',");
            strSql.Append("F_DeleteOn='"+ deltime + "'");
            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_MaterialManage GetModel(string F_MaterialCode)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from T_Wo_MaterialManage ");
            strSql.Append(" where F_MaterialCode=@F_MaterialCode and isnull(F_IsDelete,0)=0");
           SqlParameter[] parameters = {
                    new SqlParameter("@F_MaterialCode", SqlDbType.VarChar,50)         };
            parameters[0].Value = F_MaterialCode;

            CallCenterApi.Model.T_Wo_MaterialManage model = new CallCenterApi.Model.T_Wo_MaterialManage();
            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_MaterialManage GetModel(int F_Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 * from T_Wo_MaterialManage ");
            strSql.Append(" where F_Id=@F_Id");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_Id", SqlDbType.Int,4)
            };
            parameters[0].Value = F_Id;

            CallCenterApi.Model.T_Wo_MaterialManage model = new CallCenterApi.Model.T_Wo_MaterialManage();
            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_MaterialManage DataRowToModel(DataRow row)
        {
            CallCenterApi.Model.T_Wo_MaterialManage model = new CallCenterApi.Model.T_Wo_MaterialManage();
            if (row != null)
            {
                if (row["F_Id"] != null && row["F_Id"].ToString() != "")
                {
                    model.F_Id = int.Parse(row["F_Id"].ToString());
                }
                if (row["F_MaterialCode"] != null)
                {
                    model.F_MaterialCode = row["F_MaterialCode"].ToString();
                }
                if (row["F_MaterialName"] != null)
                {
                    model.F_MaterialName = row["F_MaterialName"].ToString();
                }
                if (row["F_Model"] != null)
                {
                    model.F_Model = row["F_Model"].ToString();
                }
                if (row["F_Specs"] != null)
                {
                    model.F_Specs = row["F_Specs"].ToString();
                }
                if (row["F_Level1"] != null)
                {
                    model.F_Level1 = row["F_Level1"].ToString();
                }
                if (row["F_Level2"] != null)
                {
                    model.F_Level2 = row["F_Level2"].ToString();
                }
                if (row["F_Level3"] != null)
                {
                    model.F_Level3 = row["F_Level3"].ToString();
                }
                if (row["F_Brand"] != null)
                {
                    model.F_Brand = row["F_Brand"].ToString();
                }
                if (row["F_MaterialEffect"] != null)
                {
                    model.F_MaterialEffect = row["F_MaterialEffect"].ToString();
                }
                if (row["F_Pipeline"] != null)
                {
                    model.F_Pipeline = row["F_Pipeline"].ToString();
                }
                if (row["F_Craft"] != null)
                {
                    model.F_Craft = row["F_Craft"].ToString();
                }
                if (row["F_Remark"] != null)
                {
                    model.F_Remark = row["F_Remark"].ToString();
                }
                if (row["F_CreateBy"] != null)
                {
                    model.F_CreateBy = row["F_CreateBy"].ToString();
                }
                if (row["F_CreateOn"] != null && row["F_CreateOn"].ToString() != "")
                {
                    model.F_CreateOn = DateTime.Parse(row["F_CreateOn"].ToString());
                }
                if (row["F_UpdateCount"] != null && row["F_UpdateCount"].ToString() != "")
                {
                    model.F_UpdateCount = int.Parse(row["F_UpdateCount"].ToString());
                }
                if (row["F_UpdateBy"] != null)
                {
                    model.F_UpdateBy = row["F_UpdateBy"].ToString();
                }
                if (row["F_UpdateOn"] != null && row["F_UpdateOn"].ToString() != "")
                {
                    model.F_UpdateOn = DateTime.Parse(row["F_UpdateOn"].ToString());
                }
                if (row["F_IsDelete"] != null && row["F_IsDelete"].ToString() != "")
                {
                    model.F_IsDelete = int.Parse(row["F_IsDelete"].ToString());
                }
                if (row["F_DeleteBy"] != null)
                {
                    model.F_DeleteBy = row["F_DeleteBy"].ToString();
                }
                if (row["F_DeleteOn"] != null && row["F_DeleteOn"].ToString() != "")
                {
                    model.F_DeleteOn = DateTime.Parse(row["F_DeleteOn"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM T_Wo_MaterialManage ");
            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_MaterialManage ");
            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_MaterialManage ");
            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_MaterialManage 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
    }
}
