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

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


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.T_Sys_ModuleFunctions model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Sys_ModuleFunctions(");
            strSql.Append("F_FunctionCode,F_ParentId,F_ParentCode,F_Name,F_Sort,F_Layer,F_StateFlag,F_Type,F_OptUrl,F_Remark,F_SysFlag,F_ImgUrl,F_ModuleId,F_ModuleCode,F_Controller,F_Action)");
            strSql.Append(" values (");
            strSql.Append("@F_FunctionCode,@F_ParentId,@F_ParentCode,@F_Name,@F_Sort,@F_Layer,@F_StateFlag,@F_Type,@F_OptUrl,@F_Remark,@F_SysFlag,@F_ImgUrl,@F_ModuleId,@F_ModuleCode,@F_Controller,@F_Action)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_FunctionCode", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_ParentId", SqlDbType.Int,4),
                    new SqlParameter("@F_ParentCode", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_Name", SqlDbType.VarChar,50),
                    new SqlParameter("@F_Sort", SqlDbType.Int,4),
                    new SqlParameter("@F_Layer", SqlDbType.Int,4),
                    new SqlParameter("@F_StateFlag", SqlDbType.Int,4),
                    new SqlParameter("@F_Type", SqlDbType.Int,4),
                    new SqlParameter("@F_OptUrl", SqlDbType.VarChar,200),
                    new SqlParameter("@F_Remark", SqlDbType.VarChar,200),
                    new SqlParameter("@F_SysFlag", SqlDbType.Bit,1),
                    new SqlParameter("@F_ImgUrl", SqlDbType.VarChar,200),
                    new SqlParameter("@F_ModuleId", SqlDbType.Int,4),
                    new SqlParameter("@F_ModuleCode", SqlDbType.VarChar,50),
                    new SqlParameter("@F_Controller", SqlDbType.VarChar,300),
                    new SqlParameter("@F_Action", SqlDbType.VarChar,300)
            };
            parameters[0].Value = model.F_FunctionCode;
            parameters[1].Value = model.F_ParentId;
            parameters[2].Value = model.F_ParentCode;
            parameters[3].Value = model.F_Name;
            parameters[4].Value = model.F_Sort;
            parameters[5].Value = model.F_Layer;
            parameters[6].Value = model.F_StateFlag;
            parameters[7].Value = model.F_Type;
            parameters[8].Value = model.F_OptUrl;
            parameters[9].Value = model.F_Remark;
            parameters[10].Value = model.F_SysFlag;
            parameters[11].Value = model.F_ImgUrl;
            parameters[12].Value = model.F_ModuleId;
            parameters[13].Value = model.F_ModuleCode;
            parameters[14].Value = model.F_Controller;
            parameters[15].Value = model.F_Action;

            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_ModuleFunctions model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Sys_ModuleFunctions set ");
            strSql.Append("F_FunctionCode=@F_FunctionCode,");
            strSql.Append("F_ParentId=@F_ParentId,");
            strSql.Append("F_ParentCode=@F_ParentCode,");
            strSql.Append("F_Name=@F_Name,");
            strSql.Append("F_Sort=@F_Sort,");
            strSql.Append("F_Layer=@F_Layer,");
            strSql.Append("F_StateFlag=@F_StateFlag,");
            strSql.Append("F_Type=@F_Type,");
            strSql.Append("F_OptUrl=@F_OptUrl,");
            strSql.Append("F_Remark=@F_Remark,");
            strSql.Append("F_SysFlag=@F_SysFlag,");
            strSql.Append("F_ImgUrl=@F_ImgUrl,");
            strSql.Append("F_ModuleId=@F_ModuleId,");
            strSql.Append("F_ModuleCode=@F_ModuleCode,");
            strSql.Append("F_Controller=@F_Controller,");
            strSql.Append("F_Action=@F_Action");
            strSql.Append(" where F_FunctionId=@F_FunctionId");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_FunctionCode", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_ParentId", SqlDbType.Int,4),
                    new SqlParameter("@F_ParentCode", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_Name", SqlDbType.VarChar,50),
                    new SqlParameter("@F_Sort", SqlDbType.Int,4),
                    new SqlParameter("@F_Layer", SqlDbType.Int,4),
                    new SqlParameter("@F_StateFlag", SqlDbType.Int,4),
                    new SqlParameter("@F_Type", SqlDbType.Int,4),
                    new SqlParameter("@F_OptUrl", SqlDbType.VarChar,200),
                    new SqlParameter("@F_Remark", SqlDbType.VarChar,200),
                    new SqlParameter("@F_SysFlag", SqlDbType.Bit,1),
                    new SqlParameter("@F_ImgUrl", SqlDbType.VarChar,200),
                    new SqlParameter("@F_ModuleId", SqlDbType.Int,4),
                    new SqlParameter("@F_ModuleCode", SqlDbType.VarChar,50),
                    new SqlParameter("@F_FunctionId", SqlDbType.Int,4),
                    new SqlParameter("@F_Controller", SqlDbType.VarChar,300),
                    new SqlParameter("@F_Action", SqlDbType.VarChar,300)
            };
            parameters[0].Value = model.F_FunctionCode;
            parameters[1].Value = model.F_ParentId;
            parameters[2].Value = model.F_ParentCode;
            parameters[3].Value = model.F_Name;
            parameters[4].Value = model.F_Sort;
            parameters[5].Value = model.F_Layer;
            parameters[6].Value = model.F_StateFlag;
            parameters[7].Value = model.F_Type;
            parameters[8].Value = model.F_OptUrl;
            parameters[9].Value = model.F_Remark;
            parameters[10].Value = model.F_SysFlag;
            parameters[11].Value = model.F_ImgUrl;
            parameters[12].Value = model.F_ModuleId;
            parameters[13].Value = model.F_ModuleCode;
            parameters[14].Value = model.F_FunctionId;
            parameters[15].Value = model.F_Controller;
            parameters[16].Value = model.F_Action;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

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

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

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


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

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 F_FunctionId,F_FunctionCode,F_ParentId,F_ParentCode,F_Name,F_Sort,F_Layer,F_StateFlag,F_Type,F_OptUrl,F_Remark,F_SysFlag,F_ImgUrl,F_ModuleId,F_ModuleCode,F_Controller,F_Action from T_Sys_ModuleFunctions ");
            strSql.Append(" where F_FunctionId=@F_FunctionId");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_FunctionId", SqlDbType.Int,4)
            };
            parameters[0].Value = F_FunctionId;

            Model.T_Sys_ModuleFunctions model = new Model.T_Sys_ModuleFunctions();
            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_ModuleFunctions GetModel(string controller, string action)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 F_FunctionId,F_FunctionCode,F_ParentId,F_ParentCode,F_Name,F_Sort,F_Layer,F_StateFlag,F_Type,F_OptUrl,F_Remark,F_SysFlag,F_ImgUrl,F_ModuleId,F_ModuleCode,F_Controller,F_Action from T_Sys_ModuleFunctions ");
            strSql.Append(" where F_Controller=@controller and F_Action=@action");
            SqlParameter[] parameters = {
                    new SqlParameter("@controller", SqlDbType.VarChar,300),
                    new SqlParameter("@action", SqlDbType.VarChar,300)
            };
            parameters[0].Value = controller;
            parameters[1].Value = action;

            Model.T_Sys_ModuleFunctions model = new Model.T_Sys_ModuleFunctions();
            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_ModuleFunctions GetModel(string url)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 F_FunctionId,F_FunctionCode,F_ParentId,F_ParentCode,F_Name,F_Sort,F_Layer,F_StateFlag,F_Type,F_OptUrl,F_Remark,F_SysFlag,F_ImgUrl,F_ModuleId,F_ModuleCode,F_Controller,F_Action from T_Sys_ModuleFunctions ");
            strSql.Append(" where F_OptUrl=@F_OptUrl");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_OptUrl",url)
            };

            Model.T_Sys_ModuleFunctions model = new Model.T_Sys_ModuleFunctions();
            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_ModuleFunctions DataRowToModel(DataRow row)
        {
            Model.T_Sys_ModuleFunctions model = new Model.T_Sys_ModuleFunctions();
            if (row != null)
            {
                if (row["F_FunctionId"] != null && row["F_FunctionId"].ToString() != "")
                {
                    model.F_FunctionId = int.Parse(row["F_FunctionId"].ToString());
                }
                if (row["F_FunctionCode"] != null)
                {
                    model.F_FunctionCode = row["F_FunctionCode"].ToString();
                }
                if (row["F_ParentId"] != null && row["F_ParentId"].ToString() != "")
                {
                    model.F_ParentId = int.Parse(row["F_ParentId"].ToString());
                }
                if (row["F_ParentCode"] != null)
                {
                    model.F_ParentCode = row["F_ParentCode"].ToString();
                }
                if (row["F_Name"] != null)
                {
                    model.F_Name = row["F_Name"].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_StateFlag"] != null && row["F_StateFlag"].ToString() != "")
                {
                    model.F_StateFlag = int.Parse(row["F_StateFlag"].ToString());
                }
                if (row["F_Type"] != null && row["F_Type"].ToString() != "")
                {
                    model.F_Type = int.Parse(row["F_Type"].ToString());
                }
                if (row["F_OptUrl"] != null)
                {
                    model.F_OptUrl = row["F_OptUrl"].ToString();
                }
                if (row["F_Remark"] != null)
                {
                    model.F_Remark = row["F_Remark"].ToString();
                }
                if (row["F_SysFlag"] != null && row["F_SysFlag"].ToString() != "")
                {
                    if ((row["F_SysFlag"].ToString() == "1") || (row["F_SysFlag"].ToString().ToLower() == "true"))
                    {
                        model.F_SysFlag = true;
                    }
                    else
                    {
                        model.F_SysFlag = false;
                    }
                }
                if (row["F_ImgUrl"] != null)
                {
                    model.F_ImgUrl = row["F_ImgUrl"].ToString();
                }
                if (row["F_ModuleId"] != null && row["F_ModuleId"].ToString() != "")
                {
                    model.F_ModuleId = int.Parse(row["F_ModuleId"].ToString());
                }
                if (row["F_ModuleCode"] != null)
                {
                    model.F_ModuleCode = row["F_ModuleCode"].ToString();
                }
                if (row["F_Controller"] != null)
                {
                    model.F_Controller = row["F_Controller"].ToString();
                }
                if (row["F_Action"] != null)
                {
                    model.F_Action = row["F_Action"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select F_FunctionId,F_FunctionCode,F_ParentId,F_ParentCode,F_Name,F_Sort,F_Layer,F_StateFlag,F_Type,F_OptUrl,F_Remark,F_SysFlag,F_ImgUrl,F_ModuleId,F_ModuleCode,F_Controller,F_Action");
            strSql.Append(" FROM T_Sys_ModuleFunctions ");
            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_FunctionId,F_FunctionCode,F_ParentId,F_ParentCode,F_Name,F_Sort,F_Layer,F_StateFlag,F_Type,F_OptUrl,F_Remark,F_SysFlag,F_ImgUrl,F_ModuleId,F_ModuleCode ,F_Controller,F_Action");
            strSql.Append(" FROM T_Sys_ModuleFunctions ");
            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_ModuleFunctions ");
            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_FunctionId desc");
            }
            strSql.Append(")AS Row, T.*  from T_Sys_ModuleFunctions 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
    }
}
