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

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(CallCenterApi.Model.T_Vis_Files model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Vis_Files(");
            strSql.Append("F_TypeGroup,F_FileOldName,F_FileNewName,F_FilePath,F_FileFix,F_FileType,F_FileSize,F_InputUserCode,F_InputUserID,F_InputDate)");
            strSql.Append(" values (");
            strSql.Append("@F_TypeGroup,@F_FileOldName,@F_FileNewName,@F_FilePath,@F_FileFix,@F_FileType,@F_FileSize,@F_InputUserCode,@F_InputUserID,@F_InputDate)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_TypeGroup", SqlDbType.Int,4),
                    new SqlParameter("@F_FileOldName", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_FileNewName", SqlDbType.NVarChar,500),
                    new SqlParameter("@F_FilePath", SqlDbType.NText),
                    new SqlParameter("@F_FileFix", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_FileType", SqlDbType.Int,4),
                    new SqlParameter("@F_FileSize", SqlDbType.Int,4),
                    new SqlParameter("@F_InputUserCode", SqlDbType.NVarChar,50),
                    new SqlParameter("@F_InputUserID", SqlDbType.Int,4),
                    new SqlParameter("@F_InputDate", SqlDbType.DateTime)};
            parameters[0].Value = model.F_TypeGroup;
            parameters[1].Value = model.F_FileOldName;
            parameters[2].Value = model.F_FileNewName;
            parameters[3].Value = model.F_FilePath;
            parameters[4].Value = model.F_FileFix;
            parameters[5].Value = model.F_FileType;
            parameters[6].Value = model.F_FileSize;
            parameters[7].Value = model.F_InputUserCode;
            parameters[8].Value = model.F_InputUserID;
            parameters[9].Value = model.F_InputDate;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int F_FileID)
        {

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

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


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

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 F_FileID,F_TypeGroup,F_FileOldName,F_FileNewName,F_FilePath,F_FileFix,F_FileType,F_FileSize,F_InputUserCode,F_InputUserID,F_InputDate from T_Vis_Files ");
            strSql.Append(" where F_FileID=@F_FileID");
            SqlParameter[] parameters = {
                    new SqlParameter("@F_FileID", SqlDbType.Int,4)
            };
            parameters[0].Value = F_FileID;

            CallCenterApi.Model.T_Vis_Files model = new CallCenterApi.Model.T_Vis_Files();
            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_Vis_Files DataRowToModel(DataRow row)
        {
            CallCenterApi.Model.T_Vis_Files model = new CallCenterApi.Model.T_Vis_Files();
            if (row != null)
            {
                if (row["F_FileID"] != null && row["F_FileID"].ToString() != "")
                {
                    model.F_FileID = int.Parse(row["F_FileID"].ToString());
                }
                if (row["F_TypeGroup"] != null && row["F_TypeGroup"].ToString() != "")
                {
                    model.F_TypeGroup = int.Parse(row["F_TypeGroup"].ToString());
                }
                if (row["F_FileOldName"] != null)
                {
                    model.F_FileOldName = row["F_FileOldName"].ToString();
                }
                if (row["F_FileNewName"] != null)
                {
                    model.F_FileNewName = row["F_FileNewName"].ToString();
                }
                if (row["F_FilePath"] != null)
                {
                    model.F_FilePath = row["F_FilePath"].ToString();
                }
                if (row["F_FileFix"] != null)
                {
                    model.F_FileFix = row["F_FileFix"].ToString();
                }
                if (row["F_FileType"] != null && row["F_FileType"].ToString() != "")
                {
                    model.F_FileType = int.Parse(row["F_FileType"].ToString());
                }
                if (row["F_FileSize"] != null && row["F_FileSize"].ToString() != "")
                {
                    model.F_FileSize = int.Parse(row["F_FileSize"].ToString());
                }
                if (row["F_InputUserCode"] != null)
                {
                    model.F_InputUserCode = row["F_InputUserCode"].ToString();
                }
                if (row["F_InputUserID"] != null && row["F_InputUserID"].ToString() != "")
                {
                    model.F_InputUserID = int.Parse(row["F_InputUserID"].ToString());
                }
                if (row["F_InputDate"] != null && row["F_InputDate"].ToString() != "")
                {
                    model.F_InputDate = DateTime.Parse(row["F_InputDate"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select F_FileID,F_TypeGroup,F_FileOldName,F_FileNewName,F_FilePath,F_FileFix,F_FileType,F_FileSize,F_InputUserCode,F_InputUserID,F_InputDate ");
            strSql.Append(" FROM T_Vis_Files ");
            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_FileID,F_TypeGroup,F_FileOldName,F_FileNewName,F_FilePath,F_FileFix,F_FileType,F_FileSize,F_InputUserCode,F_InputUserID,F_InputDate ");
            strSql.Append(" FROM T_Vis_Files ");
            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_Vis_Files ");
            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_FileID desc");
            }
            strSql.Append(")AS Row, T.*  from T_Vis_Files 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
    }
}
