using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Stat.DBUtility;
namespace Stat.SQLServerDAL
{
    /// <summary>
    /// 
    /// </summary>
    class Company : IDAL.ICompany
    {
        private const string PARM_ID = "@ID";
        private const string PARM_TITLE = "@title";
        private const string PARM_AUTHOR = "@Author";
        private const string PARM_CONTENT = "@content";
        private const string PARM_SOURCE = "@Source";
        private const string PARM_ISPASS = "@IsPass";
        private const string PARM_ISTOP = "@IsTop";

        #region ：Methods
        public bool Insert(Model.Company model)
        {
            SqlParameter[] parms = SetSqlParmeter(model);
            int rInt = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_CompanyInsert", parms);

            return rInt > 0;
        }


        public bool Update(Model.Company model)
        {
            SqlParameter[] parms = SetSqlParmeter(model);
            int rInt = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_CompanyUpdate", parms);
            return rInt > 0;
        }
        public bool Delete(int id)
        {
            SqlParameter parm = new SqlParameter(PARM_ID, SqlDbType.Int);
            parm.Value = id;
            int rInt = SqlHelper.ExecuteNonQuery(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_CompanyDelete", parm);

            return rInt > 0;
        }
        public IList<Model.Company> Get()
        {
            return GetList(SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringMaster, CommandType.StoredProcedure, "Stat_CompanyLoadAll", null));
        }

        #endregion

        #region ：Utilities
        /// <summary>
        /// 获取实体
        /// </summary>
        ///<param name="row"></param>
        /// <returns></returns>
        private Model.Company GetModel(System.Data.DataRow row)
        {
            Model.Company model = new Model.Company();
            if (row != null)
            {
                if (row["ID"] != DBNull.Value)
                    model.ID = Convert.ToInt32(row["ID"]);
                if (row["title"] != DBNull.Value)
                    model.About = row["title"].ToString();
                if (row["Author"] != DBNull.Value)
                    model.Product = row["Author"].ToString();
                if (row["content"] != DBNull.Value)
                    model.Contacts = row["content"].ToString();
                if (row["Source"] != DBNull.Value)
                    model.Advertise = row["Source"].ToString();
                if (row["IsPass"] != DBNull.Value)
                    model.IsPass = Convert.ToInt32(row["IsPass"]);
                if (row["IsTop"] != DBNull.Value)
                    model.IsTop = Convert.ToInt32(row["IsTop"]);
                if (row["AddTime"] != DBNull.Value)
                    model.AddTime = Convert.ToDateTime(row["AddTime"]);
            }
            return model;
        }
        /// <summary>
        /// 获取 列表
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        private IList<Model.Company> GetList(System.Data.DataTable table)
        {
            List<Model.Company> list = new List<Model.Company>();
            if (table != null)
            {
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    list.Add(GetModel(table.Rows[i]));
                }
            }
            return list;
        }
        #endregion

        #region ：Set SqlParmeter
        private static SqlParameter[] SetSqlParmeter(Model.Company model)
        {
            SqlParameter[] parms = GetSqlParmeter();
            parms[0].Value = model.ID;
            parms[1].Value = model.About;
            parms[2].Value = model.Product;
            parms[3].Value = model.Contacts;
            parms[4].Value = model.Advertise;
            parms[5].Value = model.IsPass;
            parms[6].Value = model.IsTop;
            return parms;
        }

        private static SqlParameter[] GetSqlParmeter()
        {
            SqlParameter[] parms = new SqlParameter[]{
            new SqlParameter(PARM_ID, SqlDbType.Int),
            new SqlParameter(PARM_TITLE, SqlDbType.NVarChar),
            new SqlParameter(PARM_AUTHOR, SqlDbType.NVarChar),
            new SqlParameter(PARM_CONTENT, SqlDbType.NVarChar),
            new SqlParameter(PARM_SOURCE, SqlDbType.NVarChar),
            new SqlParameter(PARM_ISPASS, SqlDbType.Int),
            new SqlParameter(PARM_ISTOP, SqlDbType.Int)
            };
            return parms;
        }
        #endregion
    }
}
