📄 articledata.cs
字号:
using System;
using System.Collections.Generic;
using System.Web;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Queries;
using Castle.Components.Validator;
using NHibernate.Expression;
using DNNLite.Entites.Modules;
using DNNLite.Service.Cache;
namespace DNNLite.DesktopModules.Article
{
/// <summary>
///为内容模块的模板提供内容
/// </summary>
public class ArticleData
{
private IDictionary<string, string> _settings;
public IDictionary<string, string> Settings { get { return _settings; } set {_settings=value ;} }
private PortalModuleBase _module;
public PortalModuleBase Module { get { return _module; } set { _module = value; } }
#region 文章相关
public Article GetArticle(object id)
{
int iid = int.Parse(id.ToString());
TabModuleCache cache = (TabModuleCache)CacheFactory.CacheService(_module.TabModuleInfo);
Article a =
cache.GetCache(true, "article", iid.ToString() ) as Article;
if (a == null)
{
try
{
a= Article.Find(iid);
}
catch (NotFoundException)
{
a = new Article();
a.Content = "内容没有找到!";
}
cache.AddCache(a, true, "article", iid.ToString());
return a;
}
else
{
return a;
}
}
public Article GetArticle()
{
if (!string.IsNullOrEmpty(HttpContext.Current.Request["articleid"]))
{
TabModuleCache cache = (TabModuleCache)CacheFactory.CacheService(_module.TabModuleInfo);
Article a =
cache.GetCache(true,"article", HttpContext.Current.Request["articleid"]) as Article ;
if (a == null)
{
int id = int.Parse(HttpContext.Current.Request["articleid"]);
try
{
a= Article.Find(id);
}
catch (NotFoundException)
{
a = new Article();
a.Content = "内容没有找到!";
}
cache.AddCache(a,true, "article", HttpContext.Current.Request["articleid"]);
return a;
}
else
{
return a;
}
}
return new Article();
}
public string GetEditTag(object id)
{
if ( _module.IsEditable() )
{
return "<a href='" + _module.EditUrl("Edit", "articleid=" + id.ToString())
+ "'>编辑</a>";
}
return "";
}
public string GetCommentLink(Article a, object commenttabid)
{
return DNNLite.DesktopModules.Comment.Tool.GetCommentLink(
int.Parse(commenttabid.ToString()),
a.CommentKey , _module.TabModuleInfo.TabModuleId
);
}
#endregion
#region 文章列表相关
#region 获得查询
private SimpleQuery<Article> GetQuery()
{
string c = string.Empty;
if (Settings.ContainsKey("LoadClass"))
{
c = Settings["LoadClass"];
}
if (c == string.Empty)
{
c = "-2";
}
//string[] cs = c.Split(',');
//string cls = "";
//for (int i = 0; i < cs.Length; i++)
//{
// if (cs[i] != "")
// {
// cls += cs[i] + ",";
// }
//}
//if (cls.Length > 0 && cls.Substring(cls.Length - 1, 1) == ",")
//{
// cls = cls.Substring(0, cls.Length - 1);
//}
//if (cls.Length == 0) { cls = "-2"; }
//改为使用原生Sql语句
// string hql = string.Empty;
// if (c.IndexOf("-1,") < 0)
// {
// hql = @"from Article a
// Where a in
// ( select b.ArticleId
// from ArticleClassRef b where b.ArticleClassId in (" + cls + @"))
// Order By a.TopSort Desc,a.CreateTime Desc,a.Id Desc";
// }
// else
// {
// hql = @"from Article a Order By a.TopSort Desc,a.CreateTime Desc,a.Id Desc
// ";
// }
string sql = string.Empty;
if (c != "-1")
{
sql = @"
select a.Id, a.TitleIntact, a.Subheading, a.Author, a.CopyFrom,
a.Keyword, a.IncludePic, a.TitleFontColor,
a.TitleFontType, a.ShowCommentLink, a.Intro,
a.PaginationType, a.MaxCharPerPage, '' as Content,
a.Stars, a.ArticleTabId, a.LinkUrl, a.AritcleKey,
a.CreateTime, a.UpdateTime, a.IsTop,a.[Img],
a.Hits from article a inner join articleclassref b on
a.id=b.articleid
where b.articleclassid = " + c + @"
order by a.IsTop Desc,b.sort desc,a.CreateTime Desc,
a.Id Desc
";
}
else
{
sql = @"select a.Id, a.TitleIntact, a.Subheading, a.Author,
a.CopyFrom, a.Keyword, a.IncludePic, a.TitleFontColor,
a.TitleFontType, a.ShowCommentLink, a.Intro, a.PaginationType,
a.MaxCharPerPage, '' as Content, a.Stars, a.ArticleTabId,
a.LinkUrl, a.AritcleKey, a.CreateTime, a.UpdateTime, a.IsTop,
a.Hits,a.Img from article a
order by a.IsTop Desc,a.CreateTime Desc,a.Id Desc";
}
//SimpleQuery<Article> q = new SimpleQuery<Article>(hql);
SimpleQuery<Article> q = new SimpleQuery<Article>(QueryLanguage.Sql, sql);
q.AddSqlReturnDefinition(typeof(Article), "a");
return q;
}
private CountQuery GetCountQuery()
{
string c = string.Empty;
if (Settings.ContainsKey("LoadClass"))
{
c = Settings["LoadClass"];
}
//string[] cs = c.Split(',');
//string cls = "";
//for (int i = 0; i < cs.Length; i++)
//{
// if (cs[i] != "")
// {
// cls += cs[i] + ",";
// }
//}
//if (cls.Length > 0 && cls.Substring(cls.Length - 1, 1) == ",")
//{
// cls = cls.Substring(0, cls.Length - 1);
//}
//if (cls.Length == 0) { cls = "-2"; }
if (c == string.Empty) { c = "-2"; }
string hql = string.Empty;
if (c != "-1")
{
hql = " Id in ( select b.ArticleId from ArticleClassRef b Where b.ArticleClassId=" + c + ") ";
}
else
{
hql = "1=1";
}
CountQuery q = new CountQuery(typeof(Article),hql );
return q;
}
#endregion
public Article[] GetArticleList(int recordsize,int pageindex)
{
TabModuleCache cache = (TabModuleCache)CacheFactory.CacheService(_module.TabModuleInfo);
Article[] value = (Article[])cache.GetCache("list",recordsize.ToString(), pageindex.ToString());
if (value == null)
{
SimpleQuery<Article> q = GetQuery();
q.SetQueryRange(recordsize * pageindex, recordsize);
value=q.Execute();
cache.AddCache(value ,"list", recordsize.ToString(), pageindex.ToString());
}
return value ;
}
public int GetCount()
{
TabModuleCache cache = (TabModuleCache)CacheFactory.CacheService(_module.TabModuleInfo);
object value = cache.GetCache("count");
if (value == null)
{
value=Article.ExecuteQuery(GetCountQuery());
cache.AddCache(value ,"count");
}
return (int)value;
}
#endregion
public string PageFlag
{
get
{
return "articlepageidx" + _module.TabModuleInfo.TabModuleId.ToString();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -