using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using HySoft.DBUtility;

namespace HySoft.BaseCallCenter.DAL
{
    //T_Fax_RecvFax
    public partial class T_Fax_RecvFax
    {

        public bool Exists(int FaxID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Fax_RecvFax");
            strSql.Append(" where ");
            strSql.Append(" FaxID = @FaxID  ");
            SqlParameter[] parameters = {
					new SqlParameter("@FaxID", SqlDbType.Int,4)
			};
            parameters[0].Value = FaxID;

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



        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(Model.T_Fax_RecvFax model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Fax_RecvFax(");
            strSql.Append("FileState,State,F_UserID,F_CustomerID,F_Name,F_FaxCode,TelNum,TifFilePath,FilePath,FileSize,FileType,Remark,RecvTime,Info");
            strSql.Append(") values (");
            strSql.Append("@FileState,@State,@F_UserID,@F_CustomerID,@F_Name,@F_FaxCode,@TelNum,@TifFilePath,@FilePath,@FileSize,@FileType,@Remark,@RecvTime,@Info");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
			            new SqlParameter("@FileState", SqlDbType.Int,4) ,            
                        new SqlParameter("@State", SqlDbType.Int,4) ,            
                        new SqlParameter("@F_UserID", SqlDbType.Int,4) ,            
                        new SqlParameter("@F_CustomerID", SqlDbType.Int,4) ,            
                        new SqlParameter("@F_Name", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@F_FaxCode", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@TelNum", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@TifFilePath", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@FilePath", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@FileSize", SqlDbType.Int,4) ,            
                        new SqlParameter("@FileType", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@Remark", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@RecvTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@Info", SqlDbType.VarChar,200)             
              
            };

            parameters[0].Value = model.FileState;
            parameters[1].Value = model.State;
            parameters[2].Value = model.F_UserID;
            parameters[3].Value = model.F_CustomerID;
            parameters[4].Value = model.F_Name;
            parameters[5].Value = model.F_FaxCode;
            parameters[6].Value = model.TelNum;
            parameters[7].Value = model.TifFilePath;
            parameters[8].Value = model.FilePath;
            parameters[9].Value = model.FileSize;
            parameters[10].Value = model.FileType;
            parameters[11].Value = model.Remark;
            parameters[12].Value = model.RecvTime;
            parameters[13].Value = model.Info;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {

                return Convert.ToInt32(obj);

            }

        }


        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.T_Fax_RecvFax model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Fax_RecvFax set ");

            strSql.Append(" FileState = @FileState , ");
            strSql.Append(" State = @State , ");
            strSql.Append(" F_UserID = @F_UserID , ");
            strSql.Append(" F_CustomerID = @F_CustomerID , ");
            strSql.Append(" F_Name = @F_Name , ");
            strSql.Append(" F_FaxCode = @F_FaxCode , ");
            strSql.Append(" TelNum = @TelNum , ");
            strSql.Append(" TifFilePath = @TifFilePath , ");
            strSql.Append(" FilePath = @FilePath , ");
            strSql.Append(" FileSize = @FileSize , ");
            strSql.Append(" FileType = @FileType , ");
            strSql.Append(" Remark = @Remark , ");
            strSql.Append(" RecvTime = @RecvTime , ");
            strSql.Append(" Info = @Info  ");
            strSql.Append(" where FaxID=@FaxID ");

            SqlParameter[] parameters = {
			            new SqlParameter("@FaxID", SqlDbType.Int,4) ,            
                        new SqlParameter("@FileState", SqlDbType.Int,4) ,            
                        new SqlParameter("@State", SqlDbType.Int,4) ,            
                        new SqlParameter("@F_UserID", SqlDbType.Int,4) ,            
                        new SqlParameter("@F_CustomerID", SqlDbType.Int,4) ,            
                        new SqlParameter("@F_Name", SqlDbType.NVarChar,50) ,            
                        new SqlParameter("@F_FaxCode", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@TelNum", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@TifFilePath", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@FilePath", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@FileSize", SqlDbType.Int,4) ,            
                        new SqlParameter("@FileType", SqlDbType.VarChar,20) ,            
                        new SqlParameter("@Remark", SqlDbType.VarChar,200) ,            
                        new SqlParameter("@RecvTime", SqlDbType.DateTime) ,            
                        new SqlParameter("@Info", SqlDbType.VarChar,200)             
              
            };

            parameters[0].Value = model.FaxID;
            parameters[1].Value = model.FileState;
            parameters[2].Value = model.State;
            parameters[3].Value = model.F_UserID;
            parameters[4].Value = model.F_CustomerID;
            parameters[5].Value = model.F_Name;
            parameters[6].Value = model.F_FaxCode;
            parameters[7].Value = model.TelNum;
            parameters[8].Value = model.TifFilePath;
            parameters[9].Value = model.FilePath;
            parameters[10].Value = model.FileSize;
            parameters[11].Value = model.FileType;
            parameters[12].Value = model.Remark;
            parameters[13].Value = model.RecvTime;
            parameters[14].Value = model.Info;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新查看状态
        /// </summary>
        /// <param name="FaxID"></param>
        /// <param name="State"></param>
        /// <returns></returns>
        public int UpdateState(int FaxID, int State)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Fax_RecvFax set ");
            strSql.Append("State=@State");
            strSql.Append(" where FaxID=@FaxID ");
            SqlParameter[] parameters = {
					new SqlParameter("@FaxID", SqlDbType.Int,4),
					new SqlParameter("@State", SqlDbType.SmallInt,2)};
            parameters[0].Value = FaxID;
            parameters[1].Value = State;

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

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int UpdateWebFilePath(Model.T_Fax_RecvFax model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Fax_RecvFax set ");
            strSql.Append("FilePath=@FilePath");
            strSql.Append(" where FaxID=@FaxID ");
            SqlParameter[] parameters = {
					new SqlParameter("@FaxID", SqlDbType.Int,4),
					new SqlParameter("@FilePath", SqlDbType.VarChar,200)};
            parameters[0].Value = model.FaxID;
            parameters[1].Value = model.FilePath;

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

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

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


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

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


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

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select FaxID, FileState, State, F_UserID, F_CustomerID, F_Name, F_FaxCode, TelNum, TifFilePath, FilePath, FileSize, FileType, Remark, RecvTime, Info  ");
            strSql.Append("  from T_Fax_RecvFax ");
            strSql.Append(" where FaxID=@FaxID");
            SqlParameter[] parameters = {
					new SqlParameter("@FaxID", SqlDbType.Int,4)
			};
            parameters[0].Value = FaxID;


            Model.T_Fax_RecvFax model = new Model.T_Fax_RecvFax();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["FaxID"].ToString() != "")
                {
                    model.FaxID = int.Parse(ds.Tables[0].Rows[0]["FaxID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["FileState"].ToString() != "")
                {
                    model.FileState = int.Parse(ds.Tables[0].Rows[0]["FileState"].ToString());
                }
                if (ds.Tables[0].Rows[0]["State"].ToString() != "")
                {
                    model.State = int.Parse(ds.Tables[0].Rows[0]["State"].ToString());
                }
                if (ds.Tables[0].Rows[0]["F_UserID"].ToString() != "")
                {
                    model.F_UserID = int.Parse(ds.Tables[0].Rows[0]["F_UserID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["F_CustomerID"].ToString() != "")
                {
                    model.F_CustomerID = int.Parse(ds.Tables[0].Rows[0]["F_CustomerID"].ToString());
                }
                model.F_Name = ds.Tables[0].Rows[0]["F_Name"].ToString();
                model.F_FaxCode = ds.Tables[0].Rows[0]["F_FaxCode"].ToString();
                model.TelNum = ds.Tables[0].Rows[0]["TelNum"].ToString();
                model.TifFilePath = ds.Tables[0].Rows[0]["TifFilePath"].ToString();
                model.FilePath = ds.Tables[0].Rows[0]["FilePath"].ToString();
                if (ds.Tables[0].Rows[0]["FileSize"].ToString() != "")
                {
                    model.FileSize = int.Parse(ds.Tables[0].Rows[0]["FileSize"].ToString());
                }
                model.FileType = ds.Tables[0].Rows[0]["FileType"].ToString();
                model.Remark = ds.Tables[0].Rows[0]["Remark"].ToString();
                if (ds.Tables[0].Rows[0]["RecvTime"].ToString() != "")
                {
                    model.RecvTime = DateTime.Parse(ds.Tables[0].Rows[0]["RecvTime"].ToString());
                }
                model.Info = ds.Tables[0].Rows[0]["Info"].ToString();

                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM T_Fax_RecvFax ");
            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 T_Fax_RecvFax ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }


    }
}
