using System;
using System.Collections.Generic;
using System.Text;
using NBear.Common;
using NBear.Data;
using Aspxcn.Entities;

namespace Aspxcn.ServiceImpls
{
    public class MessageService
    {
        #region 数据层操作代码

        //protected Gateway Gateway.Default = new Gateway("LocalSqlServer");    
        //获取网站列表内容
        public Aspxcn.Entities.Lists[] GetAllContent(Int32 pageSize,Int32 pageID,Int32 catalogName,Int32 className,string sortName)
        {
            OrderByClip sort=new OrderByClip(sortName.Trim());
            if (className == 0)
            {

                return Gateway.Default.From<Lists>().Where(Lists._.Catalog == catalogName).OrderBy(sort).ToArray<Lists>(pageSize, (pageID - 1) * pageSize);
            }
            else
            {
                return Gateway.Default.From<Lists>().Where(Lists._.Catalog == catalogName & Lists._.Class == className).OrderBy(sort).ToArray<Lists>(pageSize, (pageID - 1) * pageSize);
            }
        }

        //获取网站列表内容重载
        public Aspxcn.Entities.Lists[] GetAllContent(Int32 pageSize, Int32 pageID, Int32 catalogName, Int32 className, string SearchText,string typeName, string sortName)
        {
            OrderByClip sort = new OrderByClip(sortName.Trim());
            if (typeName == "topic")
            {
                if (catalogName == 0)
                {
                    return Gateway.Default.From<Lists>().Where(Lists._._Title.Like('%' + SearchText + '%')).OrderBy(sort).ToArray<Lists>(pageSize, (pageID - 1) * pageSize);
                }
                else
                {
                    return Gateway.Default.From<Lists>().Where(Lists._.Catalog == catalogName & Lists._._Title.Like('%' + SearchText + '%')).OrderBy(sort).ToArray<Lists>(pageSize, (pageID - 1) * pageSize);
                }
            }
            else
            {
                if (catalogName == 0)
                {
                    return Gateway.Default.From<Lists>().Where(Lists._.UserName == SearchText).OrderBy(sort).ToArray<Lists>(pageSize, (pageID - 1) * pageSize);
                
                }
                else
                {
                    return Gateway.Default.From<Lists>().Where(Lists._.Catalog == catalogName & Lists._.UserName==SearchText).OrderBy(sort).ToArray<Lists>(pageSize, (pageID - 1) * pageSize);
                }
            }
        }

        //获取用户即时信息
        public Aspxcn.Entities.Msg[] GetMsgContent(Int32 pageSize,string UserName)
        {
            return Gateway.Default.From<Msg>().Where(Msg._.To_userName == UserName & Msg._.ToIsDel==false).OrderBy(Msg._.Msg_DateTime.Desc).ToArray<Msg>(20,0);
        }

        //获取用户即时信息重载
        public Aspxcn.Entities.Msg GetMsgContent(string UserName,Int32 Msg_ID)
        {
            return Gateway.Default.Find<Msg>(Msg._.To_userName == UserName & Msg._.ID == Msg_ID);
        }

        //获取用户即时信息重载
        public Aspxcn.Entities.Msg GetMsgContent(string UserName, Int32 Msg_ID,string sendBox)
        {
            return Gateway.Default.Find<Msg>(Msg._.From_userName == UserName & Msg._.ID == Msg_ID);
        }

        //获取用户即时信息重载
        public Aspxcn.Entities.Msg[] GetMsgContent(Int32 pageSize, string UserName, string  sendBox)
        {
            return Gateway.Default.FindArray<Msg>(Msg._.From_userName == UserName & Msg._.FromIsDel == false, OrderByClip.Default);
        }

        //获取贴子具体内容
        public Aspxcn.Entities.Lists[] GetContentPage(Int32 pageSize, Int32 pageID, Int32 catalogName, Int32 className, string sortName)
        {
            OrderByClip sort = new OrderByClip(sortName.Trim());
            if (catalogName == 0)
            {
                return Gateway.Default.From<Lists>().Where(Lists._.Class == className).OrderBy(sort).ToArray<Lists>(pageSize, (pageID - 1) * pageSize);
            }
            else
            {
                return Gateway.Default.From<Lists>().Where(Lists._.Class == className & Lists._.Catalog == catalogName).OrderBy(sort).ToArray<Lists>(pageSize, (pageID - 1) * pageSize);
            }
        }

        //获取收藏夹内容
        public Aspxcn.Entities.fav[] GetFavContent(Int32 pageSize,string UserName)
        {
            return Gateway.Default.From<fav>().Where(fav._.UserName == UserName).OrderBy(fav._.Add_DateTime.Desc).ToArray<fav>(pageSize,0);
        }

        //获取RSS内容
        public Aspxcn.Entities.Lists[] GetRssContent(Int32 pageSize,Int32 catalog)
        {
            if (catalog != 0)
            {
                return Gateway.Default.From<Lists>().Where(Lists._.Catalog == catalog).OrderBy(Lists._._Date.Desc).ToArray<Lists>(pageSize,0);
            }
            else
            {
                return Gateway.Default.From<Lists>().Where(WhereClip.All).OrderBy(Lists._._Date.Desc).ToArray<Lists>(pageSize, 0);
            }
        }

        //获取内容标题
        public Aspxcn.Entities.Lists[] GetTitle(Int32 Record)
        {
            return Gateway.Default.FindArray<Lists>(Lists._.ListId == Record, OrderByClip.Default);
        }

        //获取回复内容
        public Aspxcn.Entities.Reply[] GetReplyPage(Int32 Record,Int32 pageSize,Int32 pageID)
        {
            return Gateway.Default.From<Reply>().Where(Reply._.TopIc_ID == Record).OrderBy(Reply._._DateTime.Asc).ToArray<Reply>(pageSize, (pageID - 1) * pageSize);
        }

        //插入主题操作
        public void insert_Content(Aspxcn.Entities.Lists User_Content)
        {
            Gateway.Default.Save<Lists>(User_Content);
        }
        //插入回复操作
        public void insert_ReplyContent(Aspxcn.Entities.Reply User_Content)
        {
            Gateway.Default.Save<Reply>(User_Content);
        }

        //插入即时信息操作
        public void insert_MsgContent(Aspxcn.Entities.Msg User_Content)
        {
            Gateway.Default.Save<Msg>(User_Content);
        }
        //插入收藏夹操作
        public void insert_Fav(Aspxcn.Entities.fav Add_Fav)
        {
            Gateway.Default.Save<fav>(Add_Fav);
        }

        //更新主题内容
        public void Update_Content(string updateID, string updateTitle,string updateContent,string catalogID,string classID)
        {
            Lists lst = new Lists();
            lst.ListId = Int32.Parse(updateID);
            lst.Attach();
            lst._Title = updateTitle;
            lst._Content = updateContent;
            lst.Catalog = Int32.Parse(catalogID);
            lst.Class = Int32.Parse(classID);
            Gateway.Default.Save<Lists>(lst);
        }
        //更新主题内容重载
        public void Update_Content(string updateID,string classID)
        {
            Lists lst = new Lists();
            lst.ListId = Int32.Parse(updateID);
            lst.Attach();
            lst.Class = Int32.Parse(classID);
            Gateway.Default.Save<Lists>(lst);
        }
        //更新回复内容
        public void Update_Reply(Int32 updateID, string updateTitle, string updateContent)
        {
            Reply lst = new Reply();
            lst.id = updateID;
            lst.Attach();
            lst._Title = updateTitle;
            lst._Content = updateContent;
            Gateway.Default.Save<Reply>(lst);
        }
        //更新主题标题
        public void Update_Topic(Int32 updateID, string updateName)
        {
            switch (updateName)
            {
                case "hits":
                    Gateway.Default.Update<Lists>(new PropertyItem[] { Lists._.Hits }, new object[] { Lists._.Hits + 1 }, Lists._.ListId == updateID);
                    break;
                case "istop":
                    Gateway.Default.Update<Lists>(new PropertyItem[] { Lists._.IsTop }, new object[] { !Lists._.IsTop }, Lists._.ListId == updateID);
                    break;
                case "Reply":
                    Gateway.Default.Update<Lists>(new PropertyItem[] { Lists._.Reply }, new object[] {  Lists._.Reply + 1 }, Lists._.ListId == updateID);
                    break;
                case "delReply":
                    Gateway.Default.Update<Lists>(new PropertyItem[] { Lists._.Reply }, new object[] { Lists._.Reply - 1 }, Lists._.ListId == updateID);
                    break;
                default:
                    break;
            }
        }
        //更新即时信息
        public void Update_Msg(Int32 updateID, Int32 DelType)
        {
            Msg lst = new Msg();
            lst.ID = updateID;
            lst.Attach();
            if (DelType == 1)
            {
                lst.ToIsDel = true;
            }
            else
            {
                lst.FromIsDel = true;
            }
            Gateway.Default.Save<Msg>(lst);
        }
        //更新即时信息重载
        public void Update_Msg(Int32 updateID, bool isRead)
        {
            Msg lst = new Msg();
            lst.ID = updateID;
            lst.Attach();
            lst.IsRead = isRead;
            Gateway.Default.Save<Msg>(lst);
        }

        //处理删除记录
        public void Delete_Content(int delID)
        {
            Gateway.Default.Delete<Lists>(delID);
            Gateway.Default.Delete<fav>(fav._.favID == delID); 
        }
        //处理删除收藏夹
        public void Delete_Fav(int delID)
        {
            Gateway.Default.Delete<fav>(fav._.favID == delID);

        }
        //处理删除回复
        public void Delete_Reply(int delID,int listid)
        {
            Update_Topic(listid, "delReply");
            Gateway.Default.Delete<Reply>(delID);

        }
        //处理删除信息
        public void Delete_Msg()
        {
            Gateway.Default.Delete<Msg>(Msg._.FromIsDel==true & Msg._.ToIsDel==true);

        }
        //返回列表页数
        public int GetMaxPage(Int32 catalogName,Int32 className)
        {
            if (className == 0)
            {
                return Gateway.Default.Count<Lists>(Lists._.Catalog == catalogName);
            }
            else
            {
                return Gateway.Default.Count<Lists>(Lists._.Class == className & Lists._.Catalog == catalogName);
            }
            
        }
        //返回列表页数重载
        public int GetMaxPage(Int32 catalogName, Int32 className, string searchText,string typeName)
        {
            if (typeName == "topic")
            {
                if (catalogName == 0)
                {
                    return Gateway.Default.Count<Lists>(Lists._._Title.Like('%' + searchText + '%'));
                }
                else
                {
                    return Gateway.Default.Count<Lists>(Lists._._Title.Like('%' + searchText + '%') & Lists._.Catalog == catalogName);
                }
            }
            else
            {
                if (catalogName == 0)
                {
                    return Gateway.Default.Count<Lists>(Lists._.UserName==searchText);
                }
                else
                {
                    return Gateway.Default.Count<Lists>(Lists._.UserName == searchText & Lists._.Catalog == catalogName);
                }
            }
            
        }
        //返回列表页数重载
        public int GetMaxPage(Int32 listID)
        {
            return Gateway.Default.Count<Reply>(Reply._.TopIc_ID == listID);
        }

        //返回用户发表的主题
        public int GetUserPost(string userName)
        {
            return Gateway.Default.Count<Lists>(Lists._.UserName == userName);
        }
        //返回收藏夹页数
        public int GetFavMaxPage(string userName)
        {
            return Gateway.Default.Count<fav>(fav._.UserName == userName);
        }
        //返回用户收藏夹列表
        public int GetFavPost(string userName)
        {
            return Gateway.Default.Count<fav>(fav._.UserName == userName);
        }
        //返回即时信息列表
        public int GetMsgPost(string userName,string type)
        {
            if (type == "TO")
            {
                return Gateway.Default.Count<Msg>(Msg._.To_userName==userName & Msg._.ToIsDel==false);
            }
            else
            {
                return Gateway.Default.Count<Msg>(Msg._.From_userName==userName & Msg._.FromIsDel == false) ;
            }
        }

        //返回即时信息已读列表
        public int GetMsgRead(string userName)
        {
            return Gateway.Default.Count<Msg>(Msg._.To_userName == userName & Msg._.ToIsDel == false&Msg._.IsRead==false);
        }
        //返回指定贴子
        public Aspxcn.Entities.Lists GetTopic(int TopicID)
        {
            return Gateway.Default.Find<Lists>(TopicID);
        }
        //返回指定回复
        public Aspxcn.Entities.Reply GetReply(int ReplyID)
        {
            return Gateway.Default.Find<Reply>(ReplyID);
        }
        //返回分类
        public Aspxcn.Entities.Class[] GetClass(int Catalogid)
        {
            return Gateway.Default.From<Class>().Where(Class._.Catalogid == Catalogid).ToArray<Class>();
        }

        //返回分类重载
        public Aspxcn.Entities.Class[] GetClass()
        {
            return Gateway.Default.From<Class>().ToArray<Class>();
        }

        //返回主题列表
        public Aspxcn.Entities.Catalog[] GetCatalog(string user)
        {
            if (user == "admin")
            {
                return Gateway.Default.From<Catalog>().ToArray<Catalog>();
            }
            else
            {
                return Gateway.Default.From<Catalog>().Where(Catalog._.Show==true).ToArray<Catalog>();
            }
        }
        
        //返回指定主题名称
        public string GetCatalogName(int CatalogID)
        {
            return Gateway.Default.Find<Catalog>(CatalogID).Name;
        }

        #endregion
    }
}