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

namespace CallCenterApi.DAL
{
    /// <summary>
    /// 数据访问类:T_Form_FormInfo
    /// </summary>
    public partial class T_Form_FormInfo
    {
        public T_Form_FormInfo()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int F_FormId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Form_FormInfo");
            strSql.Append(" where F_FormId=@F_FormId");
            SqlParameter[] parameters = {
					new SqlParameter("@F_FormId", SqlDbType.Int,4)
			};
            parameters[0].Value = F_FormId;

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


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(CallCenterApi.Model.T_Form_FormInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Form_FormInfo(");
            strSql.Append("F_FormName,F_FormTable,F_FormType,F_FormDes,F_FormUrl,F_EntityId,F_CreateOn,F_CreateBy,F_CreateName,F_ModifyOn,F_ModifyBy,F_ModifyName,F_DeleteFlag,F_ColumnCount)");
            strSql.Append(" values (");
            strSql.Append("@F_FormName,@F_FormTable,@F_FormType,@F_FormDes,@F_FormUrl,@F_EntityId,@F_CreateOn,@F_CreateBy,@F_CreateName,@F_ModifyOn,@F_ModifyBy,@F_ModifyName,@F_DeleteFlag,@F_ColumnCount)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@F_FormName", SqlDbType.NVarChar,50),
					new SqlParameter("@F_FormTable", SqlDbType.NVarChar,50),
					new SqlParameter("@F_FormType", SqlDbType.NVarChar,50),
					new SqlParameter("@F_FormDes", SqlDbType.NVarChar,500),
					new SqlParameter("@F_FormUrl", SqlDbType.NVarChar,200),
					new SqlParameter("@F_EntityId", SqlDbType.Int,4),
					new SqlParameter("@F_CreateOn", SqlDbType.DateTime),
					new SqlParameter("@F_CreateBy", SqlDbType.Int,4),
					new SqlParameter("@F_CreateName", SqlDbType.NVarChar,50),
					new SqlParameter("@F_ModifyOn", SqlDbType.DateTime),
					new SqlParameter("@F_ModifyBy", SqlDbType.Int,4),
					new SqlParameter("@F_ModifyName", SqlDbType.NVarChar,50),
					new SqlParameter("@F_DeleteFlag", SqlDbType.SmallInt,2),
					new SqlParameter("@F_ColumnCount", SqlDbType.Int,4)};
            parameters[0].Value = model.F_FormName;
            parameters[1].Value = model.F_FormTable;
            parameters[2].Value = model.F_FormType;
            parameters[3].Value = model.F_FormDes;
            parameters[4].Value = model.F_FormUrl;
            parameters[5].Value = model.F_EntityId;
            parameters[6].Value = model.F_CreateOn;
            parameters[7].Value = model.F_CreateBy;
            parameters[8].Value = model.F_CreateName;
            parameters[9].Value = model.F_ModifyOn;
            parameters[10].Value = model.F_ModifyBy;
            parameters[11].Value = model.F_ModifyName;
            parameters[12].Value = model.F_DeleteFlag;
            parameters[13].Value = model.F_ColumnCount;

            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_Form_FormInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Form_FormInfo set ");
            strSql.Append("F_FormName=@F_FormName,");
            strSql.Append("F_FormTable=@F_FormTable,");
            strSql.Append("F_FormType=@F_FormType,");
            strSql.Append("F_FormDes=@F_FormDes,");
            strSql.Append("F_FormUrl=@F_FormUrl,");
            strSql.Append("F_EntityId=@F_EntityId,");
            strSql.Append("F_CreateOn=@F_CreateOn,");
            strSql.Append("F_CreateBy=@F_CreateBy,");
            strSql.Append("F_CreateName=@F_CreateName,");
            strSql.Append("F_ModifyOn=@F_ModifyOn,");
            strSql.Append("F_ModifyBy=@F_ModifyBy,");
            strSql.Append("F_ModifyName=@F_ModifyName,");
            strSql.Append("F_DeleteFlag=@F_DeleteFlag,");
            strSql.Append("F_ColumnCount=@F_ColumnCount");
            strSql.Append(" where F_FormId=@F_FormId");
            SqlParameter[] parameters = {
					new SqlParameter("@F_FormName", SqlDbType.NVarChar,50),
					new SqlParameter("@F_FormTable", SqlDbType.NVarChar,50),
					new SqlParameter("@F_FormType", SqlDbType.NVarChar,50),
					new SqlParameter("@F_FormDes", SqlDbType.NVarChar,500),
					new SqlParameter("@F_FormUrl", SqlDbType.NVarChar,200),
					new SqlParameter("@F_EntityId", SqlDbType.Int,4),
					new SqlParameter("@F_CreateOn", SqlDbType.DateTime),
					new SqlParameter("@F_CreateBy", SqlDbType.Int,4),
					new SqlParameter("@F_CreateName", SqlDbType.NVarChar,50),
					new SqlParameter("@F_ModifyOn", SqlDbType.DateTime),
					new SqlParameter("@F_ModifyBy", SqlDbType.Int,4),
					new SqlParameter("@F_ModifyName", SqlDbType.NVarChar,50),
					new SqlParameter("@F_DeleteFlag", SqlDbType.SmallInt,2),
					new SqlParameter("@F_ColumnCount", SqlDbType.Int,4),
					new SqlParameter("@F_FormId", SqlDbType.Int,4)};
            parameters[0].Value = model.F_FormName;
            parameters[1].Value = model.F_FormTable;
            parameters[2].Value = model.F_FormType;
            parameters[3].Value = model.F_FormDes;
            parameters[4].Value = model.F_FormUrl;
            parameters[5].Value = model.F_EntityId;
            parameters[6].Value = model.F_CreateOn;
            parameters[7].Value = model.F_CreateBy;
            parameters[8].Value = model.F_CreateName;
            parameters[9].Value = model.F_ModifyOn;
            parameters[10].Value = model.F_ModifyBy;
            parameters[11].Value = model.F_ModifyName;
            parameters[12].Value = model.F_DeleteFlag;
            parameters[13].Value = model.F_ColumnCount;
            parameters[14].Value = model.F_FormId;

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

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

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

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


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

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

            CallCenterApi.Model.T_Form_FormInfo model = new CallCenterApi.Model.T_Form_FormInfo();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["F_FormId"] != null && ds.Tables[0].Rows[0]["F_FormId"].ToString() != "")
                {
                    model.F_FormId = int.Parse(ds.Tables[0].Rows[0]["F_FormId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["F_FormName"] != null && ds.Tables[0].Rows[0]["F_FormName"].ToString() != "")
                {
                    model.F_FormName = ds.Tables[0].Rows[0]["F_FormName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["F_FormTable"] != null && ds.Tables[0].Rows[0]["F_FormTable"].ToString() != "")
                {
                    model.F_FormTable = ds.Tables[0].Rows[0]["F_FormTable"].ToString();
                }
                if (ds.Tables[0].Rows[0]["F_FormType"] != null && ds.Tables[0].Rows[0]["F_FormType"].ToString() != "")
                {
                    model.F_FormType = ds.Tables[0].Rows[0]["F_FormType"].ToString();
                }
                if (ds.Tables[0].Rows[0]["F_FormDes"] != null && ds.Tables[0].Rows[0]["F_FormDes"].ToString() != "")
                {
                    model.F_FormDes = ds.Tables[0].Rows[0]["F_FormDes"].ToString();
                }
                if (ds.Tables[0].Rows[0]["F_FormUrl"] != null && ds.Tables[0].Rows[0]["F_FormUrl"].ToString() != "")
                {
                    model.F_FormUrl = ds.Tables[0].Rows[0]["F_FormUrl"].ToString();
                }
                if (ds.Tables[0].Rows[0]["F_EntityId"] != null && ds.Tables[0].Rows[0]["F_EntityId"].ToString() != "")
                {
                    model.F_EntityId = int.Parse(ds.Tables[0].Rows[0]["F_EntityId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["F_CreateOn"] != null && ds.Tables[0].Rows[0]["F_CreateOn"].ToString() != "")
                {
                    model.F_CreateOn = DateTime.Parse(ds.Tables[0].Rows[0]["F_CreateOn"].ToString());
                }
                if (ds.Tables[0].Rows[0]["F_CreateBy"] != null && ds.Tables[0].Rows[0]["F_CreateBy"].ToString() != "")
                {
                    model.F_CreateBy = int.Parse(ds.Tables[0].Rows[0]["F_CreateBy"].ToString());
                }
                if (ds.Tables[0].Rows[0]["F_CreateName"] != null && ds.Tables[0].Rows[0]["F_CreateName"].ToString() != "")
                {
                    model.F_CreateName = ds.Tables[0].Rows[0]["F_CreateName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["F_ModifyOn"] != null && ds.Tables[0].Rows[0]["F_ModifyOn"].ToString() != "")
                {
                    model.F_ModifyOn = DateTime.Parse(ds.Tables[0].Rows[0]["F_ModifyOn"].ToString());
                }
                if (ds.Tables[0].Rows[0]["F_ModifyBy"] != null && ds.Tables[0].Rows[0]["F_ModifyBy"].ToString() != "")
                {
                    model.F_ModifyBy = int.Parse(ds.Tables[0].Rows[0]["F_ModifyBy"].ToString());
                }
                if (ds.Tables[0].Rows[0]["F_ModifyName"] != null && ds.Tables[0].Rows[0]["F_ModifyName"].ToString() != "")
                {
                    model.F_ModifyName = ds.Tables[0].Rows[0]["F_ModifyName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["F_DeleteFlag"] != null && ds.Tables[0].Rows[0]["F_DeleteFlag"].ToString() != "")
                {
                    model.F_DeleteFlag = int.Parse(ds.Tables[0].Rows[0]["F_DeleteFlag"].ToString());
                }
                if (ds.Tables[0].Rows[0]["F_ColumnCount"] != null && ds.Tables[0].Rows[0]["F_ColumnCount"].ToString() != "")
                {
                    model.F_ColumnCount = int.Parse(ds.Tables[0].Rows[0]["F_ColumnCount"].ToString());
                }
                if (ds.Tables[0].Rows[0]["F_DBTableName"] != null && ds.Tables[0].Rows[0]["F_DBTableName"].ToString() != "")
                {
                    model.F_DBTableName = ds.Tables[0].Rows[0]["F_DBTableName"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM vw_Form_FormInfo ");
            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 vw_Form_FormInfo ");
            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 vw_Form_FormInfo ");
            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_FormId desc");
            }
            strSql.Append(")AS Row, T.*  from vw_Form_FormInfo 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_Form_FormInfo";
            parameters[1].Value = "F_FormId";
            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  Method
    }
}

