using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Linq;
using System.Data.Linq;
using Maticsoft.Common;
namespace Maticsoft.BLL.NewsManage
{
    public class News
    {
        private readonly Maticsoft.DAL.NewsManage.News dal = new Maticsoft.DAL.NewsManage.News();
       
        #region  成员方法
     

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add(Maticsoft.Model.News model)
        {
            dal.Add(model);
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public void Update(Maticsoft.Model.News model)
        {
            dal.Update(model);
        }


        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int NewsId)
        {
            dal.Delete(NewsId);
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Maticsoft.Model.News GetModel(int NewsId)
        {
            return dal.GetModel(NewsId);
        }

        /// <summary>
        /// 得到一个对象实体,从缓存中
        /// </summary>
        public Maticsoft.Model.News GetModelByCache(int NewsId)
        {
            string CacheKey = "NewsModel-" + NewsId;
            object objModel = DataCache.GetCache(CacheKey);
            if (objModel == null)
            {
                try
                {
                    objModel = dal.GetModel(NewsId);
                    if (objModel != null)
                    {
                        int AdContentCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
                        DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(AdContentCache), TimeSpan.Zero);
                    }
                }
                catch
                { }
            }
            return (Maticsoft.Model.News)objModel;
        }       
  
        #endregion

        #region  查询

        /// <summary>
        /// 获得记录总数
        /// </summary>
        public int GetCount(string strWhere)
        {
            return dal.GetCount(strWhere);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }

        /// <summary>
        /// 得到某类别的最新新闻
        /// </summary>
        /// <param name="top">返回行数</param>
        /// <param name="ClassId">类别</param>
        /// <param name="Dormancy">发布状态</param>       
        /// <returns></returns>
        public DataSet GetNewsList(int top, int ClassId, bool Dormancy)
        {
            return dal.GetNewsList(top,ClassId, Dormancy);
        }

        /// <summary>
        /// 得到最新几条新闻
        /// </summary>
        /// <param name="top"></param>
        /// <returns></returns>
        public List<Maticsoft.Model.News> GetTopScroll(int top)
        {
            DataSet ds= dal.GetTopScroll(top);
            List<Maticsoft.Model.News> modellist = DataTableToList(ds.Tables[0]);
            return modellist;
        }

        #endregion


        #region   查询对象列表GetModelList
        /// <summary>
        /// 转换数据列表
        /// </summary>
        public List<Maticsoft.Model.News> DataTableToList(DataTable dt)
        {
            List<Maticsoft.Model.News> modelList = new List<Maticsoft.Model.News>();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                Maticsoft.Model.News model;
                for (int n = 0; n < rowsCount; n++)
                {
                    model = new Maticsoft.Model.News();
                    if (dt.Rows[n]["NewsId"].ToString() != "")
                    {
                        model.NewsId = int.Parse(dt.Rows[n]["NewsId"].ToString());
                    }
                    if (dt.Rows[n]["UserId"].ToString() != "")
                    {
                        model.UserId = int.Parse(dt.Rows[n]["UserId"].ToString());
                    }
                    model.Heading = dt.Rows[n]["Heading"].ToString();
                    model.Content = dt.Rows[n]["Content"].ToString();
                    if (dt.Rows[n]["Frequency"].ToString() != "")
                    {
                        model.Frequency = int.Parse(dt.Rows[n]["Frequency"].ToString());
                    }
                    if (dt.Rows[n]["IssueDate"].ToString() != "")
                    {
                        model.IssueDate = DateTime.Parse(dt.Rows[n]["IssueDate"].ToString());
                    }
                    model.Dormancy = dt.Rows[n]["Dormancy"].ToString();
                    if (dt.Rows[n]["ClassId"].ToString() != "")
                    {
                        model.ClassId = int.Parse(dt.Rows[n]["ClassId"].ToString());
                    }
                    model.Focus = dt.Rows[n]["Focus"].ToString();
                    if (dt.Rows[n]["Priority"].ToString() != "")
                    {
                        model.Priority = int.Parse(dt.Rows[n]["Priority"].ToString());
                    }
                    if (dt.Rows[n]["IsTop"].ToString() != "")
                    {
                        model.IsTop = int.Parse(dt.Rows[n]["IsTop"].ToString());
                    }
                    modelList.Add(model);
                }
            }
            return modelList;
        }

        /// <summary>
        /// 分页获取对象列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public IEnumerable<Maticsoft.Model.News> GetModelList(int page, int size)
        {
            DataSet ds = dal.GetList(size, page, "");
            List<Maticsoft.Model.News> modellist = DataTableToList(ds.Tables[0]);
            return modellist;
        }
        #endregion


        #region Linq查询

        /// <summary>
        /// 查询表对象的集合
        /// </summary>
        /// <returns></returns>
        public Table<Maticsoft.Model.News> GetTableModelList()
        {
            return dal.GetTableModelList();
        }
        /// <summary>
        /// 查询条件对象的集合
        /// </summary>
        /// <returns></returns>
        public IQueryable<Maticsoft.Model.News> GetQueryModelList()
        {
            return dal.GetQueryModelList();
        }

        /// <summary>
        /// 直接执行 SQL 查询 (LINQ to SQL)
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public IEnumerable<Maticsoft.Model.News> GetQueryModelList(string strWhere)
        {
            return dal.GetQueryModelList(strWhere);
        }

        #endregion
      

    }
}
