using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using RMYY_CallCenter_Api.DB;

namespace RMYY_CallCenter_Api.Dal
{
    /// <summary>
    /// 数据访问类:T_Sys_Menu
    /// </summary>
    public partial class T_Sys_Menu
    {
        public T_Sys_Menu()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string F_MenuCode)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Sys_Menu");
            strSql.Append(" where F_MenuCode=@F_MenuCode");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_MenuCode", SqlDbType.VarChar,50)
            };
            parameters[0].Value = F_MenuCode;

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


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(RMYY_CallCenter_Api.Model.T_Sys_Menu model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Sys_Menu(");
            strSql.Append("F_MenuCode,F_MenuName,F_Type,F_Url,F_Icon,F_Action,F_Sort,F_Remark,F_State,F_CreateUser,F_CreateTime,F_ParentId,F_MenuType,F_WorkState)");
            strSql.Append(" values (");
            strSql.Append("@F_MenuCode,@F_MenuName,@F_Type,@F_Url,@F_Icon,@F_Action,@F_Sort,@F_Remark,@F_State,@F_CreateUser,@F_CreateTime,@F_ParentId,@F_MenuType,@F_WorkState)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_MenuCode", SqlDbType.VarChar,50),
                    new SqlParameter("@F_MenuName", SqlDbType.VarChar,50),
                    new SqlParameter("@F_Type", SqlDbType.Int,4),
                    new SqlParameter("@F_Url", SqlDbType.VarChar,500),
                    new SqlParameter("@F_Icon", SqlDbType.VarChar,255),
                    new SqlParameter("@F_Action", SqlDbType.VarChar,255),
                    new SqlParameter("@F_Sort", SqlDbType.Int,4),
                    new SqlParameter("@F_Remark", SqlDbType.NVarChar,255),
                    new SqlParameter("@F_State", SqlDbType.Int,4),
                    new SqlParameter("@F_CreateUser", SqlDbType.VarChar,255),
                    new SqlParameter("@F_CreateTime", SqlDbType.DateTime),
                    new SqlParameter("@F_ParentId", SqlDbType.Int,4),
                    new SqlParameter("@F_MenuType", SqlDbType.Int,4),
                    new SqlParameter("@F_WorkState", SqlDbType.VarChar,50)};
            parameters[0].Value = model.F_MenuCode;
            parameters[1].Value = model.F_MenuName;
            parameters[2].Value = model.F_Type;
            parameters[3].Value = model.F_Url;
            parameters[4].Value = model.F_Icon;
            parameters[5].Value = model.F_Action;
            parameters[6].Value = model.F_Sort;
            parameters[7].Value = model.F_Remark;
            parameters[8].Value = model.F_State;
            parameters[9].Value = model.F_CreateUser;
            parameters[10].Value = model.F_CreateTime;
            parameters[11].Value = model.F_ParentId;
            parameters[12].Value = model.F_MenuType;
            parameters[13].Value = model.F_WorkState;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(RMYY_CallCenter_Api.Model.T_Sys_Menu model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Sys_Menu set ");
            strSql.Append("F_MenuCode=@F_MenuCode,");
            strSql.Append("F_MenuName=@F_MenuName,");
            strSql.Append("F_Type=@F_Type,");
            strSql.Append("F_Url=@F_Url,");
            strSql.Append("F_Icon=@F_Icon,");
            strSql.Append("F_Action=@F_Action,");
            strSql.Append("F_Sort=@F_Sort,");
            strSql.Append("F_Remark=@F_Remark,");
            strSql.Append("F_State=@F_State,");
            strSql.Append("F_CreateUser=@F_CreateUser,");
            strSql.Append("F_CreateTime=@F_CreateTime,"); 
            strSql.Append("F_ParentId=@F_ParentId,");
            strSql.Append("F_WorkState=@F_WorkState,");


            strSql.Append("F_MenuType=@F_MenuType");
            strSql.Append(" where F_MenuId=@F_MenuId");
          //  strSql.Append("  and  F_WorkState=@F_WorkState");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_MenuCode", SqlDbType.VarChar,50),
                    new SqlParameter("@F_MenuName", SqlDbType.VarChar,50),
                    new SqlParameter("@F_Type", SqlDbType.Int,4),
                    new SqlParameter("@F_Url", SqlDbType.VarChar,500),
                    new SqlParameter("@F_Icon", SqlDbType.VarChar,255),
                    new SqlParameter("@F_Action", SqlDbType.VarChar,255),
                    new SqlParameter("@F_Sort", SqlDbType.Int,4),
                    new SqlParameter("@F_Remark", SqlDbType.NVarChar,255),
                    new SqlParameter("@F_State", SqlDbType.Int,4),
                    new SqlParameter("@F_CreateUser", SqlDbType.VarChar,255),
                    new SqlParameter("@F_CreateTime", SqlDbType.DateTime),
                    new SqlParameter("@F_ParentId", SqlDbType.Int,4),



                    new SqlParameter("@F_MenuType", SqlDbType.Int,4),
                    new SqlParameter("@F_MenuId", SqlDbType.Int,4),
                    new SqlParameter("@F_WorkState", SqlDbType.VarChar,50)};
            parameters[0].Value = model.F_MenuCode;
            parameters[1].Value = model.F_MenuName;
            parameters[2].Value = model.F_Type;
            parameters[3].Value = model.F_Url;
            parameters[4].Value = model.F_Icon;
            parameters[5].Value = model.F_Action;
            parameters[6].Value = model.F_Sort;
            parameters[7].Value = model.F_Remark;
            parameters[8].Value = model.F_State;
            parameters[9].Value = model.F_CreateUser;
            parameters[10].Value = model.F_CreateTime;
            parameters[11].Value = model.F_ParentId;
            parameters[12].Value = model.F_MenuType;
            parameters[13].Value = model.F_MenuId;
            parameters[14].Value = model.F_WorkState;

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

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

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

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


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public RMYY_CallCenter_Api.Model.T_Sys_Menu GetModel(string F_MenuCode)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 F_MenuId,F_ParentId,F_MenuCode,F_MenuName,F_Type,F_Url,F_Icon,F_Action,F_Sort,F_Remark,F_State,F_CreateUser,F_CreateTime,F_MenuType,F_WorkState from T_Sys_Menu with(nolock)");
            strSql.Append(" where F_MenuCode=@F_MenuCode");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_MenuCode", SqlDbType.VarChar,50)
            };
            parameters[0].Value = F_MenuCode;

            RMYY_CallCenter_Api.Model.T_Sys_Menu model = new RMYY_CallCenter_Api.Model.T_Sys_Menu();
            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 RMYY_CallCenter_Api.Model.T_Sys_Menu GetModel(int F_MenuId)
        {

            StringBuilder strSql = new StringBuilder();
          strSql.Append("select  top 1 F_MenuId,F_MenuCode,F_MenuName,F_Type,F_Url,F_Icon,F_Action,F_Sort,F_Remark,F_State,F_CreateUser,F_CreateTime,F_ParentId,F_MenuType,F_WorkState from T_Sys_Menu with(nolock)");
            strSql.Append(" where F_MenuId=@F_MenuId");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_MenuId", SqlDbType.Int,4)
            };
            parameters[0].Value = F_MenuId;

            RMYY_CallCenter_Api.Model.T_Sys_Menu model = new RMYY_CallCenter_Api.Model.T_Sys_Menu();
            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 RMYY_CallCenter_Api.Model.T_Sys_Menu DataRowToModel(DataRow row)
        {
            RMYY_CallCenter_Api.Model.T_Sys_Menu model = new RMYY_CallCenter_Api.Model.T_Sys_Menu();
            if (row != null)
            {
                if (row["F_MenuId"] != null && row["F_MenuId"].ToString() != "")
                {
                    model.F_MenuId = int.Parse(row["F_MenuId"].ToString());
                }
                if (row["F_MenuCode"] != null)
                {
                    model.F_MenuCode = row["F_MenuCode"].ToString();
                }
                if (row["F_MenuName"] != null)
                {
                    model.F_MenuName = row["F_MenuName"].ToString();
                }
                if (row["F_Type"] != null && row["F_Type"].ToString() != "")
                {
                    model.F_Type = int.Parse(row["F_Type"].ToString());
                }
                if (row["F_Url"] != null)
                {
                    model.F_Url = row["F_Url"].ToString();
                }
                if (row["F_Icon"] != null)
                {
                    model.F_Icon = row["F_Icon"].ToString();
                }
                if (row["F_Action"] != null)
                {
                    model.F_Action = row["F_Action"].ToString();
                }
                if (row["F_Sort"] != null && row["F_Sort"].ToString() != "")
                {
                    model.F_Sort = int.Parse(row["F_Sort"].ToString());
                }
                if (row["F_Remark"] != null)
                {
                    model.F_Remark = row["F_Remark"].ToString();
                }
                if (row["F_State"] != null && row["F_State"].ToString() != "")
                {
                    model.F_State = int.Parse(row["F_State"].ToString());
                }
                if (row["F_CreateUser"] != null)
                {
                    model.F_CreateUser = row["F_CreateUser"].ToString();
                }
                if (row["F_CreateTime"] != null && row["F_CreateTime"].ToString() != "")
                {
                    model.F_CreateTime = DateTime.Parse(row["F_CreateTime"].ToString());
                }
                if (row["F_ParentId"] != null && row["F_ParentId"].ToString() != "")
                {
                    model.F_ParentId = int.Parse(row["F_ParentId"].ToString());
                }
                if (row["F_MenuType"] != null && row["F_MenuType"].ToString() != "")
                {
                    model.F_MenuType = int.Parse(row["F_MenuType"].ToString());
                }
                if (row["F_WorkState"] != null && row["F_WorkState"].ToString() != "")
                {
                    model.F_WorkState = row["F_WorkState"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select F_MenuId,F_MenuCode,F_MenuName,F_Type,F_Url,F_Icon,F_Action,F_Sort,F_Remark,F_State,F_CreateUser,F_CreateTime,F_ParentId,F_MenuType,F_WorkState ");
            strSql.Append(" FROM T_Sys_Menu with(nolock)");
            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_MenuId,F_MenuCode,F_MenuName,F_Type,F_Url,F_Icon,F_Action,F_Sort,F_Remark,F_State,F_CreateUser,F_CreateTime,F_ParentId,F_MenuType,F_WorkState ");
            strSql.Append(" FROM T_Sys_Menu with(nolock)");
            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_Menu with(nolock)");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        #endregion  BasicMethod
    }
}

