www.pudn.com > MyShop.rar > Article.cs
using System;
using System.Collections.Generic;
using System.Data;
using MyShop.DALFactory;
using MyShop.IDAL;
using MyShop.Model;
namespace MyShop.BLL
{
public class Article
{
ConfigInfo configInfo = new ConfigInfo();
Config config = new Config();
private int _id;
private IArticle dal = DataAccess.CreateArticle();
public Article()
{
configInfo = config.GetModel();
}
private int _articleId;
public Article(int articleId)
{
this._id = articleId;
}
#region IArticle
///
///
///
///
///
protected int Add(ArticleInfo model)
{
if (model == null)
{
return 0;
}
return dal.Add(model);
}
protected int Delete(string filter)
{
if (string.IsNullOrEmpty(filter))
return 0;
return dal.Delete(filter);
}
public bool Exist(string filter)
{
filter = filter.Trim();
if (string.IsNullOrEmpty(filter))
return false;
return dal.Exist(filter);
}
public DataSet GetDataSet()
{
return dal.GetDataSet();
}
public DataSet GetDataSet(string filter)
{
filter = filter.Trim();
if (string.IsNullOrEmpty(filter))
return null;
return dal.GetDataSet(filter);
}
public ArticleInfo GetModel(DataRow dr)
{
if (dr == null)
return null;
return dal.GetModel(dr);
}
private DataSet Query(string sql)
{
sql = sql.Trim();
if (string.IsNullOrEmpty(sql))
return null;
return dal.Query(sql);
}
public int Update(ArticleInfo model, string filter)
{
if (model == null)
return 0;
filter = filter.Trim();
if (string.IsNullOrEmpty(filter))
return 0;
return dal.Update(model, filter);
}
#endregion
#region common
///
///
///
///
///
///
public int Add(ArticleInfo model, out string msg)
{
msg = "";
if (model == null)
{
msg = msg + "数据不能为空";
return 0;
}
bool isErr = false;
if (isErr)
return 0;
int count = 0;
count = Add(model);
if (count == 0)
msg = "系统发生错误,请重新添加!";
if (count == 1)
msg = "添加成功!";
return count;
}
///
/// 真正从数据库中删除,注意使用时要分清是不是只放到回收站
///
///
///
public int Delete(int articleId)
{
ArticleInfo model = new ArticleInfo();
model = GetModel(articleId);
if (model == null)
return 0;
string filer;
filer = " articleId =" + articleId;
return Delete(filer);
}
public int Update(ArticleInfo model)
{
if (model == null)
{
return 0;
}
string filter;
filter = " articleId=" + model.ArticleID;
return Update(model, filter);
}
public ArticleInfo GetModel(int articleId)
{
DataSet dataset = new DataSet();
dataset = GetDataSet(" articleId=" + articleId);
if (dataset != null && dataset.Tables[0].Rows.Count > 0)
return GetModel(dataset.Tables[0].Rows[0]);
return null;
}
public ArticleInfo GetModel(string title)
{
title = Utils.ReplaceBadSQL(title.Trim());
if (string.IsNullOrEmpty(title.ToString()))
return null;
DataSet dataset = new DataSet();
dataset = GetDataSet(" title='" + title + "'");
if (dataset != null && dataset.Tables[0].Rows.Count > 0)
return GetModel(dataset.Tables[0].Rows[0]);
return null;
}
#endregion
#region 得到文章列表
///
/// 得到文章列表(前台用)
///
/// 频道ID。如果是所有频道,参数值应为-1
/// 栏目ID。如果是所有栏目,参数值应为-1
///
public DataSet GetArticlList(int channelId, int classId)
{
bool status = true;
bool deleted = false;
int isDeleted = deleted ? 1 : 0;
int _status = (status == true) ? 1 : 0;
string filter = "";
filter = (channelId == -1) ? filter : filter + " and [channelId] = " + channelId;
filter = (classId == -1) ? filter : filter + " and [classId] = " + classId;
filter = filter + " and [Status] = " + _status + " and [deleted] =" + isDeleted;
filter = " [articleId] > 0 " + filter + " order by onTop desc, articleId desc ";
return GetDataSet(filter);
}
///
/// 得到文章列表
///
/// 文章频道ID
///
public DataSet GetDataSetByChannelId(int channelId)
{
return GetDataSet(channelId,-1);
}
///
/// 得到文章列表
///
/// 文章栏目ID
///
public DataSet GetDataSetByClassId(int classId)
{
return GetDataSet(-1,classId);
}
///
/// 得到文章列表
///
/// 频道ID。如果是所有频道,参数值应为-1
/// 栏目ID。如果是所有栏目,参数值应为-1
///
public DataSet GetDataSet(int channelId, int classId)
{
string filter = "";
filter = (channelId == -1) ? filter : filter + " and [channelId] = " + channelId;
filter = (classId == -1) ? filter : filter + " and [classId] = " + classId;
filter = " [articleId] > 0 " + filter + " order by articleId desc ";
return GetDataSet(filter);
}
//================================
///
/// 得到文章列表
///
/// 是否审核通过
/// 是否已删除
///
public DataSet GetDataSet(bool status, bool deleted)
{
return GetDataSet(-1,-1,status,deleted);
}
///
/// 得到文章列表
///
/// 频道ID。如果是所有频道,参数值应为-1
/// 是否审核通过
/// 是否已删除
///
public DataSet GetDataSetByChannelId(int channelId,bool status, bool deleted)
{
return GetDataSet(channelId,-1,status,deleted);
}
///
/// 得到文章列表
///
/// 栏目ID。如果是所有栏目,参数值应为-1
/// 是否审核通过
/// 是否已删除
///
public DataSet GetDataSetByClassId(int classId,bool status, bool deleted)
{
return GetDataSet(-1,classId,status,deleted);
}
///
/// 得到文章列表
///
/// 频道ID。如果是所有频道,参数值应为-1
/// 栏目ID。如果是所有栏目,参数值应为-1
/// 是否审核通过
/// 是否已删除
///
public DataSet GetDataSet(int channelId, int classId, bool status, bool deleted)
{
int isDeleted = deleted ? 1 : 0;
int _status = (status == true) ? 1 : 0;
string filter = "";
filter = (channelId == -1) ? filter : filter + " and [channelId] = " + channelId;
filter = (classId == -1) ? filter : filter + " and [classId] = " + classId;
filter = filter + " and [Status] = " + _status + " and [deleted] =" + isDeleted;
filter = " [articleId] > 0 " + filter + " order by articleId desc ";
return GetDataSet(filter);
}
//=======================================
///
/// 得到文章列表
///
/// 频道ID。如果是所有频道,参数值应为-1
/// 是否已放入回收站
///
public DataSet GetDataSetByChannelId(int channelId, bool deleted)
{
return GetDataSet(channelId, -1, string.Empty, deleted);
}
///
/// 得到文章列表
///
/// 栏目ID。如果是所有栏目,参数值应为-1
/// 是否已放入回收站
///
public DataSet GetDataSetByClassId(int classId, bool deleted)
{
return GetDataSet(-1, classId, string.Empty, deleted);
}
///
/// 返回所有未放入回收站的文章
///
///
public DataSet GetDataSetUndeleted()
{
return GetDataSetUndeleted(-1);
}
///
/// 返回未放入回收站的文章
///
/// 频道ID。如果是所有频道,参数值应为-1
///
public DataSet GetDataSetUndeleted(int channelId)
{
return GetDataSetUndeleted(channelId, string.Empty);
}
///
/// 返回未放入回收站的文章
///
/// 频道ID。如果是所有频道,参数值应为-1
/// 文章选项。无选定项时值应为空
///
public DataSet GetDataSetUndeleted(int channelId, string articlOptionFilter)
{
return GetDataSet(channelId, -1, articlOptionFilter, false);
}
///
/// 返回所有放入回收站的文章
///
///
public DataSet GetDataSetDeleted()
{
return GetDataSetDeleted(-1);
}
///
/// 返回放入回收站的文章
///
/// 频道ID。如果是所有频道,参数值应为-1
///
public DataSet GetDataSetDeleted(int channelId)
{
return GetDataSet(channelId, -1, string.Empty, true);
}
///
///返回放入回收站的文章
///
/// 频道ID。如果是所有频道,参数值应为-1
/// 文章选项。无选定项时值应为空
///
public DataSet GetDataSetDeleted(int channelId, string articlOptionFilter)
{
return GetDataSet(channelId, -1, articlOptionFilter, true);
}
///
/// 返回文章列表
///
/// 频道ID。如果是所有频道,参数值应为-1
/// 栏目ID。如果是所有栏目,参数值应为-1
/// 文章选项。无选定项时值应为空
/// 是否已放入回收站
///
public DataSet GetDataSet(int channelId, int classId, string articlOptionFilter, bool deleted)
{
int isDeleted = deleted ? 1 : 0;
string filter = "";
filter = (channelId == -1) ? filter : filter + " and [channelId] = " + channelId;
filter = (classId == -1) ? filter : filter + " and [classId] = " + classId;
filter = (string.IsNullOrEmpty(articlOptionFilter.Trim())) ? filter : filter + " and " + articlOptionFilter;
filter = filter + " and [Deleted] = " + isDeleted;
filter = " [articleId] > 0 " + filter + " order by articleId desc ";
return GetDataSet(filter);
}
#endregion
#region 前台模块
///
/// 最新文章
///
/// 返回文章个数
///
public DataSet GetLastArticles( int count)
{
if (count == 0)
return null;
return dal.GetLastArticles(count);
}
///
/// 热点文章
///
/// 返回文章个数
///
public DataSet GetHotArticles( int count)
{
if (count == 0)
return null;
return dal.GetHotArticles(count);
}
///
/// 推荐文章
///
/// 返回文章个数
///
public DataSet GetRecommendedArticles( int count )
{
if (count == 0)
return null;
return dal.GetRecommendedArticles(count);
}
public DataSet GetDataSetByTitle(string title)
{
if (string.IsNullOrEmpty(title))
return null;
return GetDataSet(" title like '%" + Utils.ReplaceBadSQL(title) + "%'");
}
#endregion
#region 以下过程要用Article article = new Article(id)实例化后才能进行调用
///
/// 增加点击数
///
/// 要增加或减少的点击数,加用正数,减用负数
public void AddHit( int num)
{
ArticleInfo model = new ArticleInfo();
model = Model;
model.Hits += num;
Model = model;
}
///
/// 增加评论数
///
/// 要增加或减少的点击数,加用正数,减用负数
public void AddCommentCount( int num)
{
ArticleInfo model = new ArticleInfo();
model = Model;
model.CommentCount += num;
Model = model;
}
#region property
///
/// 获取当前项目ID
///
public int ID
{
get
{
return this._id;
}
}
///
/// 获取调查项目的实体
///
public ArticleInfo Model
{
get
{
return GetModel(this._id);
}
set
{
Update(value);
}
}
///
/// 获取当前对象是否存在(数据库中有没有这条记录)
///
public bool IsExist
{
get
{
return Model == null ? false : true;
}
}
#endregion
#endregion
#region 后台管理
///
/// 删除Article
///
///
///
///
public int Delete(int channelId, int classId)
{
string filer;
filer = " [channelId]=" + channelId + " and [classId] = " + classId;
return Delete(filer);
}
///
/// 快速搜索
///
/// 文章频道ID
/// 文章栏目ID
/// 文章选项。无选定项时值应为空
/// true:返回放到回收站的文章;false:返回未放到回收站的文章
///
public DataSet QuickSearch(int channelId, int classId, string articlOptionFilter, bool deleted)
{
return dal.QuickSearch(channelId, classId,articlOptionFilter, deleted);
}
///
/// 高级查询
///
/// 文章频道ID
/// 文章栏目ID
/// 文章选项。无选定项时值应为空
///
///
/// true:返回放到回收站的文章;false:返回未放到回收站的文章
///
public DataSet KeywordsSearch(int channelId, int classId, string articlOptionFilter, string field, string keywords, bool deleted)
{
field = Utils.ReplaceBadSQL(field.Trim().ToLower());
keywords = Utils.ReplaceBadSQL(keywords.ToLower().Trim());
if (string.IsNullOrEmpty(field) || string.IsNullOrEmpty(keywords))
return null;
return dal.KeywordsSearch(channelId,classId,articlOptionFilter, field, keywords,deleted);
}
///
/// 判断该文章是不是热点
///
///
/// 无该文章时返回false
public bool IsHot(int articleId)
{
ArticleInfo model = new ArticleInfo();
model = GetModel(articleId);
if (model == null)
return false;
if (model.IsHot == 1)
return true;
else
return false;
}
///
/// 取消或设置该文章为热点
///
///
/// true 为设置 false为取消
///
public int IsHot(int articleId, bool action)
{
ArticleInfo model = new ArticleInfo();
model = GetModel(articleId);
if (model == null)
return 0;
if (action)
{
model.IsHot = 1;
}
else
{
model.IsHot = 0;
}
return Update(model);
}
///
/// 判断该文章是不是固顶
///
///
/// 无该文章时返回false
public bool OnTop(int articleId)
{
ArticleInfo model = new ArticleInfo();
model = GetModel(articleId);
if (model == null)
return false;
if (model.OnTop == 1)
return true;
else
return false;
}
///
/// 取消或设置该文章为固顶
///
///
/// true 为设置 false为取消
///
public int OnTop(int articleId, bool action)
{
ArticleInfo model = new ArticleInfo();
model = GetModel(articleId);
if (model == null)
return 0;
if (action )
{
model.OnTop = 1;
}
else
{
model.OnTop = 0;
}
return Update(model);
}
///
/// 判断该文章是不是推荐
///
///
/// 无该文章时返回false
public bool IsElite(int articleId)
{
ArticleInfo model = new ArticleInfo();
model = GetModel(articleId);
if (model == null)
return false;
if (model.IsElite == 1)
return true;
else
return false;
}
///
/// 取消或设置该文章为推荐
///
///
/// true 为设置 false为取消
///
public int IsElite(int articleId, bool action)
{
ArticleInfo model = new ArticleInfo();
model = GetModel(articleId);
if (model == null)
return 0;
if (action)
{
model.IsElite = 1;
}
else
{
model.IsElite = 0;
}
return Update(model);
}
///
/// 判断该文章是不是审核通过
///
///
/// 无该文章时返回false
public bool Pass(int articleId)
{
ArticleInfo model = new ArticleInfo();
model = GetModel(articleId);
if (model == null)
return false;
if (model.Status == 1)
return true;
else
return false;
}
///
/// 取消或设置该文章状态
///
///
/// true false为取消审核通过
///
public int Pass(int articleId, bool action)
{
ArticleInfo model = new ArticleInfo();
model = GetModel(articleId);
if (model == null)
return 0;
if (action)
{
model.Status = 1;
}
else
{
model.Status = 0;
}
return Update(model);
}
///
/// 判断该文章有没有首页图片
///
///
/// 无该文章时返回false
public bool HasDefaultPic(int articleId)
{
ArticleInfo model = new ArticleInfo();
model = GetModel(articleId);
if (model == null)
return false;
if (model.DefaultPicUrl.Length >= 3)
return true;
else
return false;
}
///
/// 判断该文章标题是不是已经存在
///
///
///
public bool ExistTitle(string title)
{
title = title.Trim();
if(string.IsNullOrEmpty(title))
return false;
return Exist(" title ='" + Utils.ReplaceBadSQL(title) + "'");
}
///
/// 将文章放到回收站
///
///
///
public int MoveToRecycle(int articleId)
{
ArticleInfo model = new ArticleInfo();
model = GetModel(articleId);
if (model != null)
{
model.Deleted = 1;
return Update(model);
}
return 0;
}
///
/// 将文章从回收站还原
///
///
///
public int RestoreFromRecycle(int articleId)
{
ArticleInfo model = new ArticleInfo();
model = GetModel(articleId);
if (model != null)
{
model.Deleted = 0;
return Update(model);
}
return 0;
}
///
/// 将文章从回收站清空
///
///
public int DeleteAllFromRecycle()
{
string filter = " Deleted = 1 ";
return Delete(filter);
}
public int RestoreAllFromRecycle()
{
string filter = " Deleted = 1 ";
DataSet dataset = new DataSet();
dataset = GetDataSet(filter);
ArticleInfo model = new ArticleInfo();
int i = 0;
foreach (DataRow dr in dataset.Tables[0].Rows)
{
model = GetModel(dr);
if(model != null)
RestoreFromRecycle(model.ArticleID);
i++;
}
return i;
}
#endregion
///
/// 显示classId栏目的文章列表
///
/// 栏目ID
/// 要显示的数目
/// 样式类名
/// 显示的最大长度
///
public string ShowArticleList(int classId, int count, string cssClass, int titleMaxLength)
{
DataSet dataset = new DataSet();
Class cls = new Class();
ClassInfo clsModel = new ClassInfo();
clsModel = cls.GetModel(classId);
if (clsModel == null)
return "";
string classtitle = clsModel.ClassName;
dataset = GetArticlList(-1, classId);
ArticleInfo model = new ArticleInfo();
string content = "";
string title = "";
int listCount = Math.Min(count, dataset.Tables[0].Rows.Count);
for (int i = 0; i < listCount; i++)
{
model = GetModel(dataset.Tables[0].Rows[i]);
title = (model.Title.Length > titleMaxLength) ? model.Title.Substring(0, titleMaxLength) + "…" : model.Title;
content = content + "" + title + "\r\n";
}
content = "\r\n";
return "\r\n" + "
" + classtitle + " >>\r\n" + content
+ "
更多>>
\r\n";
}
}
}