/// <summary> /// 更新文章状态 /// </summary> /// <param name="articleId">文章Id</param> /// <param name="status">状态值</param> /// <returns></returns> public async Task <Article> UpdateArticleStatus(int articleId, ArticleStatus status) { var updateArticle = await discoveryDbContext.Article.FirstOrDefaultAsync(article => article.PKID == articleId); if (updateArticle == null) { throw new Exception("文章不存在"); } updateArticle.Status = status.ToString(); updateArticle.LastUpdateDateTime = DateTime.Now; //如果将文章撤回,并且当前文章为置顶状态,则设置为非置顶 if (status == ArticleStatus.Withdrew && updateArticle.IsTopMost.HasValue && updateArticle.IsTopMost.Value == true) { updateArticle.IsTopMost = false; } await discoveryDbContext.SaveChangesAsync(); if (status == ArticleStatus.Withdrew) { await InsertDataChangeRecord(updateArticle, DataOperationEnum.Withdrew); } return(updateArticle); }
public static bool UpdateArticleStatus(SqlConnection conn, int articleId, ArticleStatus status, int isShow) { using (conn) { const string sql = @"UPDATE Marketing..tbl_Article SET Status=@Status,IsShow=@IsShow,LastUpdateDateTime=GETDATE() WHERE PKID=@PKID"; var parameter = new SqlParameter[] { new SqlParameter("@PKID", articleId), new SqlParameter("@Status", status.ToString()), new SqlParameter("@IsShow", isShow) }; return(SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sql, parameter) > 0); } }
/// <summary> /// 搜索文章 /// </summary> /// <param name="status">文章状态</param> /// <param name="articleTitle">文章标题</param> /// <param name="startDateTime">创建开始时间</param> /// <param name="endDateTime">创建结束时间</param> /// <param name="pager">分页模型</param> /// <returns>文章列表</returns> public Task <List <Article> > SearchArticle(SqlConnection conn, ArticleStatus status, PagerModel pager, DateTime?startCreateDateTime, DateTime?endCreateDateTime, string pkid, string keyword = "", int type = 5) { List <Article> result = new List <Article>(); const string selectValues = @"PKID , Catalog , Image , ShowImages , ShowType , IsDescribe , SmallImage , SmallTitle , BigTitle , TitleColor , Brief , Content , ContentUrl , Source , PublishDateTime , CreateDateTime , LastUpdateDateTime , ClickCount , RedirectUrl , Vote , Category , CategoryTags , Heat , CommentIsActive , IsRead , ArticleBanner , SmallBanner , Bestla , HotArticles , Type , IsShow , ShareWX , SharePYQ , CreatorID , CreatorInfo , VehicleImage , LikeCountNum , ReadCountNum , IsDelete , IsHasBestAnswer , RelatedArticleId , CoverMode , CoverImage , Status , ShareCountNum , CommentCountNum , CoverTag , IsTopMost , Ranking , ContentHTML , IsShowTouTiao , QRCodeImg"; List <string> conditions = new List <string>(); DynamicParameters parameters = new DynamicParameters(); if (!string.IsNullOrEmpty(pkid)) { int id = Convert.ToInt32(pkid); conditions.Add("PKID = @Id"); parameters.Add("@Id", id); } else { string sqlStatus = string.Empty; if (status == ArticleStatus.Published) { conditions.Add("Status = @Status"); conditions.Add("PublishDateTime < GETDATE()"); sqlStatus = ArticleStatus.Published.ToString(); } else if (status == ArticleStatus.PrePublish) { conditions.Add("Status = @Status"); conditions.Add("PublishDateTime > GETDATE()"); sqlStatus = ArticleStatus.Published.ToString(); } else if (status != ArticleStatus.All) { conditions.Add("Status = @Status"); sqlStatus = status.ToString(); } parameters.Add("@Status", sqlStatus); if (!string.IsNullOrEmpty(keyword)) { conditions.Add("SmallTitle LIKE @Keyword"); parameters.Add("@Keyword", $"%{keyword}%"); } if (startCreateDateTime.HasValue) { conditions.Add("PublishDateTime > @StartCreateDateTime"); parameters.Add("@StartCreateDateTime", startCreateDateTime.Value); } if (endCreateDateTime.HasValue) { conditions.Add("PublishDateTime < @EndCreateDateTime"); parameters.Add("@EndCreateDateTime", endCreateDateTime.Value); } } parameters.Add("@Type", type); conditions.Add("Type = @Type"); conditions.Add("IsDelete = 0"); string countSql = $"Select Count(1) From Marketing..tbl_Article WITH(NOLOCK) Where {string.Join(" AND ", conditions)}"; string selectSql = $"Select {selectValues} From Marketing..tbl_Article WITH(NOLOCK) Where {string.Join(" AND ", conditions)} Order By IsTopMost desc, PublishDateTime desc OFFSET @Skip ROWS FETCH NEXT @PageSize ROWS ONLY "; int skip = (pager.CurrentPage - 1) * pager.PageSize; int pageSize = pager.PageSize; parameters.Add("@Skip", skip); parameters.Add("@PageSize", pageSize); using (conn) { pager.TotalItem = conn.ExecuteScalar <int>(countSql, commandType: CommandType.Text, param: parameters); result = conn.Query <Article>(selectSql, commandType: CommandType.Text, param: parameters).ToList(); } return(Task.FromResult(result)); }