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_Sys_ModuleFunctions
    /// </summary>
    public partial class T_Sys_ModuleInfo
    {
        public T_Sys_ModuleInfo()
        { }
        #region  BasicMethod

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

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

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


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.T_Sys_ModuleInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Sys_ModuleInfo(");
            strSql.Append("F_ModuleName,F_ModuleCode,F_Remark,F_Sort,F_StateFlag,F_OptUrl,F_ImgUrl,F_Target,F_IsMenu,F_Button,F_CreateTime,F_LastModifyTime,F_DeleteTime,F_CreateUID,F_LastModifyUID,F_DeleteUID,F_ParentID)");
            strSql.Append(" values (");
            strSql.Append("@F_ModuleName,@F_ModuleCode,@F_Remark,@F_Sort,@F_StateFlag,@F_OptUrl,@F_ImgUrl,@F_Target,@F_IsMenu,@F_Button,@F_CreateTime,@F_LastModifyTime,@F_DeleteTime,@F_CreateUID,@F_LastModifyUID,@F_DeleteUID,@F_ParentID)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_ModuleName", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_ModuleCode", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_Remark", SqlDbType.NVarChar,200),
                    new SqlParameter("@F_Sort", SqlDbType.Int,4),
                    new SqlParameter("@F_StateFlag", SqlDbType.Int,4),
                    new SqlParameter("@F_OptUrl", SqlDbType.VarChar,200),
                    new SqlParameter("@F_ImgUrl", SqlDbType.VarChar,200),
                    new SqlParameter("@F_Target", SqlDbType.VarChar,50),
                    new SqlParameter("@F_IsMenu", SqlDbType.Int,4),
                    new SqlParameter("@F_Button", SqlDbType.VarChar,50),
                    new SqlParameter("@F_CreateTime", SqlDbType.DateTime),
                    new SqlParameter("@F_LastModifyTime", SqlDbType.DateTime),
                    new SqlParameter("@F_DeleteTime", SqlDbType.DateTime),
                    new SqlParameter("@F_CreateUID", SqlDbType.Int,4),
                    new SqlParameter("@F_LastModifyUID", SqlDbType.Int,4),
                    new SqlParameter("@F_DeleteUID", SqlDbType.Int,4),
                    new SqlParameter("@F_ParentID", SqlDbType.Int,4)};
            parameters[0].Value = model.F_ModuleName;
            parameters[1].Value = model.F_ModuleCode;
            parameters[2].Value = model.F_Remark;
            parameters[3].Value = model.F_Sort;
            parameters[4].Value = model.F_StateFlag;
            parameters[5].Value = model.F_OptUrl;
            parameters[6].Value = model.F_ImgUrl;
            parameters[7].Value = model.F_Target;
            parameters[8].Value = model.F_IsMenu;
            parameters[9].Value = model.F_Button;
            parameters[10].Value = model.F_CreateTime;
            parameters[11].Value = model.F_LastModifyTime;
            parameters[12].Value = model.F_DeleteTime;
            parameters[13].Value = model.F_CreateUID;
            parameters[14].Value = model.F_LastModifyUID;
            parameters[15].Value = model.F_DeleteUID;
            parameters[16].Value = model.F_ParentID;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.T_Sys_ModuleInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Sys_ModuleInfo set ");
            strSql.Append("F_ModuleName=@F_ModuleName,");
            strSql.Append("F_ModuleCode=@F_ModuleCode,");
            strSql.Append("F_Remark=@F_Remark,");
            strSql.Append("F_Sort=@F_Sort,");
            strSql.Append("F_StateFlag=@F_StateFlag,");
            strSql.Append("F_OptUrl=@F_OptUrl,");
            strSql.Append("F_ImgUrl=@F_ImgUrl,");
            strSql.Append("F_Target=@F_Target,");
            strSql.Append("F_IsMenu=@F_IsMenu,");
            strSql.Append("F_Button=@F_Button,");
            strSql.Append("F_CreateTime=@F_CreateTime,");
            strSql.Append("F_LastModifyTime=@F_LastModifyTime,");
            strSql.Append("F_DeleteTime=@F_DeleteTime,");
            strSql.Append("F_CreateUID=@F_CreateUID,");
            strSql.Append("F_LastModifyUID=@F_LastModifyUID,");
            strSql.Append("F_DeleteUID=@F_DeleteUID,");
            strSql.Append("F_ParentID=@F_ParentID");
            strSql.Append(" where F_ModuleId=@F_ModuleId");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_ModuleName", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_ModuleCode", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_Remark", SqlDbType.NVarChar,200),
                    new SqlParameter("@F_Sort", SqlDbType.Int,4),
                    new SqlParameter("@F_StateFlag", SqlDbType.Int,4),
                    new SqlParameter("@F_OptUrl", SqlDbType.VarChar,200),
                    new SqlParameter("@F_ImgUrl", SqlDbType.VarChar,200),
                    new SqlParameter("@F_Target", SqlDbType.VarChar,50),
                    new SqlParameter("@F_IsMenu", SqlDbType.Int,4),
                    new SqlParameter("@F_Button", SqlDbType.VarChar,50),
                    new SqlParameter("@F_CreateTime", SqlDbType.DateTime),
                    new SqlParameter("@F_LastModifyTime", SqlDbType.DateTime),
                    new SqlParameter("@F_DeleteTime", SqlDbType.DateTime),
                    new SqlParameter("@F_CreateUID", SqlDbType.Int,4),
                    new SqlParameter("@F_LastModifyUID", SqlDbType.Int,4),
                    new SqlParameter("@F_DeleteUID", SqlDbType.Int,4),
                    new SqlParameter("@F_ParentID", SqlDbType.Int,4),
                    new SqlParameter("@F_ModuleId", SqlDbType.Int,4)};
            parameters[0].Value = model.F_ModuleName;
            parameters[1].Value = model.F_ModuleCode;
            parameters[2].Value = model.F_Remark;
            parameters[3].Value = model.F_Sort;
            parameters[4].Value = model.F_StateFlag;
            parameters[5].Value = model.F_OptUrl;
            parameters[6].Value = model.F_ImgUrl;
            parameters[7].Value = model.F_Target;
            parameters[8].Value = model.F_IsMenu;
            parameters[9].Value = model.F_Button;
            parameters[10].Value = model.F_CreateTime;
            parameters[11].Value = model.F_LastModifyTime;
            parameters[12].Value = model.F_DeleteTime;
            parameters[13].Value = model.F_CreateUID;
            parameters[14].Value = model.F_LastModifyUID;
            parameters[15].Value = model.F_DeleteUID;
            parameters[16].Value = model.F_ParentID;
            parameters[17].Value = model.F_ModuleId;

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

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

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

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


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

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 F_ModuleId,F_ModuleName,F_ModuleCode,F_Remark,F_Sort,F_StateFlag,F_OptUrl,F_ImgUrl,F_Target,F_IsMenu,F_Button,F_CreateTime,F_LastModifyTime,F_DeleteTime,F_CreateUID,F_LastModifyUID,F_DeleteUID,F_ParentID from T_Sys_ModuleInfo ");
            strSql.Append(" where F_ModuleId=@F_ModuleId");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_ModuleId", SqlDbType.Int,4)
            };
            parameters[0].Value = F_ModuleId;

            Model.T_Sys_ModuleInfo model = new Model.T_Sys_ModuleInfo();
            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_Sys_ModuleInfo DataRowToModel(DataRow row)
        {
            Model.T_Sys_ModuleInfo model = new Model.T_Sys_ModuleInfo();
            if (row != null)
            {
                if (row["F_ModuleId"] != null && row["F_ModuleId"].ToString() != "")
                {
                    model.F_ModuleId = int.Parse(row["F_ModuleId"].ToString());
                }
                if (row["F_ModuleName"] != null)
                {
                    model.F_ModuleName = row["F_ModuleName"].ToString();
                }
                if (row["F_ModuleCode"] != null)
                {
                    model.F_ModuleCode = row["F_ModuleCode"].ToString();
                }
                if (row["F_Remark"] != null)
                {
                    model.F_Remark = row["F_Remark"].ToString();
                }
                if (row["F_Sort"] != null && row["F_Sort"].ToString() != "")
                {
                    model.F_Sort = int.Parse(row["F_Sort"].ToString());
                }
                if (row["F_StateFlag"] != null && row["F_StateFlag"].ToString() != "")
                {
                    model.F_StateFlag = int.Parse(row["F_StateFlag"].ToString());
                }
                if (row["F_OptUrl"] != null)
                {
                    model.F_OptUrl = row["F_OptUrl"].ToString();
                }
                if (row["F_ImgUrl"] != null)
                {
                    model.F_ImgUrl = row["F_ImgUrl"].ToString();
                }
                if (row["F_Target"] != null)
                {
                    model.F_Target = row["F_Target"].ToString();
                }
                if (row["F_IsMenu"] != null && row["F_IsMenu"].ToString() != "")
                {
                    model.F_IsMenu = int.Parse(row["F_IsMenu"].ToString());
                }
                if (row["F_Button"] != null)
                {
                    model.F_Button = row["F_Button"].ToString();
                }
                if (row["F_CreateTime"] != null && row["F_CreateTime"].ToString() != "")
                {
                    model.F_CreateTime = DateTime.Parse(row["F_CreateTime"].ToString());
                }
                if (row["F_LastModifyTime"] != null && row["F_LastModifyTime"].ToString() != "")
                {
                    model.F_LastModifyTime = DateTime.Parse(row["F_LastModifyTime"].ToString());
                }
                if (row["F_DeleteTime"] != null && row["F_DeleteTime"].ToString() != "")
                {
                    model.F_DeleteTime = DateTime.Parse(row["F_DeleteTime"].ToString());
                }
                if (row["F_CreateUID"] != null && row["F_CreateUID"].ToString() != "")
                {
                    model.F_CreateUID = int.Parse(row["F_CreateUID"].ToString());
                }
                if (row["F_LastModifyUID"] != null && row["F_LastModifyUID"].ToString() != "")
                {
                    model.F_LastModifyUID = int.Parse(row["F_LastModifyUID"].ToString());
                }
                if (row["F_DeleteUID"] != null && row["F_DeleteUID"].ToString() != "")
                {
                    model.F_DeleteUID = int.Parse(row["F_DeleteUID"].ToString());
                }
                if (row["F_ParentID"] != null && row["F_ParentID"].ToString() != "")
                {
                    model.F_ParentID = int.Parse(row["F_ParentID"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select F_ModuleId,F_ModuleName,F_ModuleCode,F_Remark,F_Sort,F_StateFlag,F_OptUrl,F_ImgUrl,F_Target,F_IsMenu,F_Button,F_CreateTime,F_LastModifyTime,F_DeleteTime,F_CreateUID,F_LastModifyUID,F_DeleteUID,F_ParentID ");
            strSql.Append(" FROM T_Sys_ModuleInfo ");
            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_ModuleId,F_ModuleName,F_ModuleCode,F_Remark,F_Sort,F_StateFlag,F_OptUrl,F_ImgUrl,F_Target,F_IsMenu,F_Button,F_CreateTime,F_LastModifyTime,F_DeleteTime,F_CreateUID,F_LastModifyUID,F_DeleteUID,F_ParentID ");
            strSql.Append(" FROM T_Sys_ModuleInfo ");
            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_Sys_ModuleInfo ");
            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_ModuleId desc");
            }
            strSql.Append(")AS Row, T.*  from T_Sys_ModuleInfo 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_Sys_ModuleInfo";
			parameters[1].Value = "F_ModuleId";
			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
    }
}
