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

namespace CallCenterApi.DAL
{
    /// <summary>
    /// 数据访问类:T_RegionCategory
    /// </summary>
    public partial class T_RegionCategory
    {
        public T_RegionCategory()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int F_RegionId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_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(CallCenterApi.Model.T_RegionCategory model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_RegionCategory(");
            strSql.Append("F_AreaId,F_RegionNumber,F_RegionName,F_RegionEName,F_Sort,F_Layer,F_ParentId,F_CreateBy,F_CreateOn,F_ModifyBy,F_ModifyOn,F_DeleteFlag,F_ZipCode)");
            strSql.Append(" values (");
            strSql.Append("@F_AreaId,@F_RegionNumber,@F_RegionName,@F_RegionEName,@F_Sort,@F_Layer,@F_ParentId,@F_CreateBy,@F_CreateOn,@F_ModifyBy,@F_ModifyOn,@F_DeleteFlag,@F_ZipCode)");
            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_RegionEName", SqlDbType.NVarChar,200),
					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_ZipCode", SqlDbType.NVarChar,50),
                    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_RegionEName;
            parameters[4].Value = model.F_Sort;
            parameters[5].Value = model.F_Layer;
            parameters[6].Value = model.F_ParentId;
            parameters[7].Value = model.F_CreateBy;
            parameters[8].Value = model.F_CreateOn;
            parameters[9].Value = model.F_ModifyBy;
            parameters[10].Value = model.F_ModifyOn;
            parameters[11].Value = model.F_ZipCode;
            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(CallCenterApi.Model.T_RegionCategory model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_RegionCategory set ");
            strSql.Append("F_AreaId=@F_AreaId,");
            strSql.Append("F_RegionNumber=@F_RegionNumber,");
            strSql.Append("F_RegionName=@F_RegionName,");
            strSql.Append("F_RegionEName=@F_RegionEName,");
            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_ZipCode=@F_ZipCode,");
            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_RegionEName", SqlDbType.NVarChar,200),
					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_ZipCode", SqlDbType.NVarChar,50),
                    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_RegionEName;
            parameters[4].Value = model.F_Sort;
            parameters[5].Value = model.F_Layer;
            parameters[6].Value = model.F_ParentId;
            parameters[7].Value = model.F_CreateBy;
            parameters[8].Value = model.F_CreateOn;
            parameters[9].Value = model.F_ModifyBy;
            parameters[10].Value = model.F_ModifyOn;
            parameters[11].Value = model.F_ZipCode;
            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_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_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 CallCenterApi.Model.T_RegionCategory GetModel(string F_RegionName)
        {

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

            CallCenterApi.Model.T_RegionCategory model = new CallCenterApi.Model.T_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 CallCenterApi.Model.T_RegionCategory GetModel(int F_RegionId)
        {

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

            CallCenterApi.Model.T_RegionCategory model = new CallCenterApi.Model.T_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 CallCenterApi.Model.T_RegionCategory DataRowToModel(DataRow row)
        {
            CallCenterApi.Model.T_RegionCategory model = new CallCenterApi.Model.T_RegionCategory();
//            StringBuilder strSql = new StringBuilder();
//            strSql.Append("select  top 1 F_RegionId,F_AreaId,F_RegionNumber,F_RegionName,F_RegionEName,F_Sort,F_Layer,F_ParentId,F_CreateBy,F_CreateOn,F_ModifyBy,F_ModifyOn,F_DeleteFlag from T_RegionCategory ");
//            strSql.Append(" where F_RegionId=@F_RegionId");
//            SqlParameter[] parameters = {
//					new SqlParameter("@F_RegionId", SqlDbType.Int,4)
//};
//            parameters[0].Value = F_RegionId;

//            CallCenterApi.Model.T_RegionCategory model = new CallCenterApi.Model.T_RegionCategory();
//            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            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 && row["F_RegionNumber"].ToString() != "")
                {
                    model.F_RegionNumber = row["F_RegionNumber"].ToString();
                }
                if (row["F_RegionName"] != null && row["F_RegionName"].ToString() != "")
                {
                    model.F_RegionName = row["F_RegionName"].ToString();
                }
                if (row["F_RegionEName"] != null && row["F_RegionEName"].ToString() != "")
                {
                    model.F_RegionEName = row["F_RegionEName"].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());
                }
                if (row["F_ZipCode"] != null && row["F_ZipCode"].ToString() != "")
                {
                    model.F_ZipCode = row["F_ZipCode"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM T_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(" * ");
            strSql.Append(" FROM T_RegionCategory ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        #endregion  Method
    }
}