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>
    /// 区域项目，楼号
    /// </summary>
    public partial class T_Cus_RegionCategory
    {
        public T_Cus_RegionCategory()
        { }
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("F_RegionId", "T_Cus_RegionCategory");
        }

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

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


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.T_Cus_RegionCategory model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Cus_RegionCategory(");
            strSql.Append("F_AreaId,F_RegionNumber,F_RegionName,F_Property,F_City,F_Sort,F_Layer,F_ParentId,F_CreateBy,F_CreateOn,F_ModifyBy,F_ModifyOn,F_DeleteFlag)");
            strSql.Append(" values (");
            strSql.Append("@F_AreaId,@F_RegionNumber,@F_RegionName,@F_Property,@F_City,@F_Sort,@F_Layer,@F_ParentId,@F_CreateBy,@F_CreateOn,@F_ModifyBy,@F_ModifyOn,@F_DeleteFlag)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_AreaId", SqlDbType.Int,4),
                    new SqlParameter("@F_RegionNumber", SqlDbType.NVarChar,100),
                    new SqlParameter("@F_RegionName", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_Property", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_City", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_Sort", SqlDbType.Int,4),
                    new SqlParameter("@F_Layer", SqlDbType.Int,4),
                    new SqlParameter("@F_ParentId", SqlDbType.Int,4),
                    new SqlParameter("@F_CreateBy", SqlDbType.Int,4),
                    new SqlParameter("@F_CreateOn", SqlDbType.DateTime),
                    new SqlParameter("@F_ModifyBy", SqlDbType.Int,4),
                    new SqlParameter("@F_ModifyOn", SqlDbType.DateTime),
                    new SqlParameter("@F_DeleteFlag", SqlDbType.SmallInt,2)};
            parameters[0].Value = model.F_AreaId;
            parameters[1].Value = model.F_RegionNumber;
            parameters[2].Value = model.F_RegionName;
            parameters[3].Value = model.F_Property;
            parameters[4].Value = model.F_City;
            parameters[5].Value = model.F_Sort;
            parameters[6].Value = model.F_Layer;
            parameters[7].Value = model.F_ParentId;
            parameters[8].Value = model.F_CreateBy;
            parameters[9].Value = model.F_CreateOn;
            parameters[10].Value = model.F_ModifyBy;
            parameters[11].Value = model.F_ModifyOn;
            parameters[12].Value = model.F_DeleteFlag;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.T_Cus_RegionCategory model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Cus_RegionCategory set ");
            strSql.Append("F_AreaId=@F_AreaId,");
            strSql.Append("F_RegionNumber=@F_RegionNumber,");
            strSql.Append("F_RegionName=@F_RegionName,");
            strSql.Append("F_Property=@F_Property,");
            strSql.Append("F_City=@F_City,");
            strSql.Append("F_Sort=@F_Sort,");
            strSql.Append("F_Layer=@F_Layer,");
            strSql.Append("F_ParentId=@F_ParentId,");
            strSql.Append("F_CreateBy=@F_CreateBy,");
            strSql.Append("F_CreateOn=@F_CreateOn,");
            strSql.Append("F_ModifyBy=@F_ModifyBy,");
            strSql.Append("F_ModifyOn=@F_ModifyOn,");
            strSql.Append("F_DeleteFlag=@F_DeleteFlag");
            strSql.Append(" where F_RegionId=@F_RegionId");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_AreaId", SqlDbType.Int,4),
                    new SqlParameter("@F_RegionNumber", SqlDbType.NVarChar,100),
                    new SqlParameter("@F_RegionName", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_Property", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_City", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_Sort", SqlDbType.Int,4),
                    new SqlParameter("@F_Layer", SqlDbType.Int,4),
                    new SqlParameter("@F_ParentId", SqlDbType.Int,4),
                    new SqlParameter("@F_CreateBy", SqlDbType.Int,4),
                    new SqlParameter("@F_CreateOn", SqlDbType.DateTime),
                    new SqlParameter("@F_ModifyBy", SqlDbType.Int,4),
                    new SqlParameter("@F_ModifyOn", SqlDbType.DateTime),
                    new SqlParameter("@F_DeleteFlag", SqlDbType.SmallInt,2),
                    new SqlParameter("@F_RegionId", SqlDbType.Int,4)};
            parameters[0].Value = model.F_AreaId;
            parameters[1].Value = model.F_RegionNumber;
            parameters[2].Value = model.F_RegionName;
            parameters[3].Value = model.F_Property;
            parameters[4].Value = model.F_City;
            parameters[5].Value = model.F_Sort;
            parameters[6].Value = model.F_Layer;
            parameters[7].Value = model.F_ParentId;
            parameters[8].Value = model.F_CreateBy;
            parameters[9].Value = model.F_CreateOn;
            parameters[10].Value = model.F_ModifyBy;
            parameters[11].Value = model.F_ModifyOn;
            parameters[12].Value = model.F_DeleteFlag;
            parameters[13].Value = model.F_RegionId;

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

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

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

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


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.T_Cus_RegionCategory GetModel(int F_RegionId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 F_RegionId,F_AreaId,F_RegionNumber,F_RegionName,F_Property,F_City,F_Sort,F_Layer,F_ParentId,F_CreateBy,F_CreateOn,F_ModifyBy,F_ModifyOn,F_DeleteFlag from T_Cus_RegionCategory ");
            strSql.Append(" where F_RegionId=@F_RegionId");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_RegionId", SqlDbType.Int,4)
            };
            parameters[0].Value = F_RegionId;

            Model.T_Cus_RegionCategory model = new Model.T_Cus_RegionCategory();
            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 Model.T_Cus_RegionCategory DataRowToModel(DataRow row)
        {
            Model.T_Cus_RegionCategory model = new Model.T_Cus_RegionCategory();
            if (row != null)
            {
                if (row["F_RegionId"] != null && row["F_RegionId"].ToString() != "")
                {
                    model.F_RegionId = int.Parse(row["F_RegionId"].ToString());
                }
                if (row["F_AreaId"] != null && row["F_AreaId"].ToString() != "")
                {
                    model.F_AreaId = int.Parse(row["F_AreaId"].ToString());
                }
                if (row["F_RegionNumber"] != null)
                {
                    model.F_RegionNumber = row["F_RegionNumber"].ToString();
                }
                if (row["F_RegionName"] != null)
                {
                    model.F_RegionName = row["F_RegionName"].ToString();
                }
                if (row["F_Property"] != null)
                {
                    model.F_Property = row["F_Property"].ToString();
                }
                if (row["F_City"] != null)
                {
                    model.F_City = row["F_City"].ToString();
                }
                if (row["F_Sort"] != null && row["F_Sort"].ToString() != "")
                {
                    model.F_Sort = int.Parse(row["F_Sort"].ToString());
                }
                if (row["F_Layer"] != null && row["F_Layer"].ToString() != "")
                {
                    model.F_Layer = int.Parse(row["F_Layer"].ToString());
                }
                if (row["F_ParentId"] != null && row["F_ParentId"].ToString() != "")
                {
                    model.F_ParentId = int.Parse(row["F_ParentId"].ToString());
                }
                if (row["F_CreateBy"] != null && row["F_CreateBy"].ToString() != "")
                {
                    model.F_CreateBy = int.Parse(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_ModifyBy"] != null && row["F_ModifyBy"].ToString() != "")
                {
                    model.F_ModifyBy = int.Parse(row["F_ModifyBy"].ToString());
                }
                if (row["F_ModifyOn"] != null && row["F_ModifyOn"].ToString() != "")
                {
                    model.F_ModifyOn = DateTime.Parse(row["F_ModifyOn"].ToString());
                }
                if (row["F_DeleteFlag"] != null && row["F_DeleteFlag"].ToString() != "")
                {
                    model.F_DeleteFlag = int.Parse(row["F_DeleteFlag"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select F_RegionId,F_AreaId,F_RegionNumber,F_RegionName,F_Property,F_City,F_Sort,F_Layer,F_ParentId,F_CreateBy,F_CreateOn,F_ModifyBy,F_ModifyOn,F_DeleteFlag ");
            strSql.Append(" FROM T_Cus_RegionCategory ");
            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_RegionId,F_AreaId,F_RegionNumber,F_RegionName,F_Property,F_City,F_Sort,F_Layer,F_ParentId,F_CreateBy,F_CreateOn,F_ModifyBy,F_ModifyOn,F_DeleteFlag ");
            strSql.Append(" FROM T_Cus_RegionCategory ");
            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_Cus_RegionCategory ");
            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_RegionId desc");
            }
            strSql.Append(")AS Row, T.*  from T_Cus_RegionCategory 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_Cus_RegionCategory";
			parameters[1].Value = "F_RegionId";
			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
    }
}
