Exemplo n.º 1
0
        protected List <ArticleJson> ArticleListInfo(List <Article> list, string usernumber = "")
        {
            if (list == null)
            {
                return(new List <ArticleJson>());
            }
            if (list.Count == 0)
            {
                return(new List <ArticleJson>());
            }


            //文章编号集合
            var array        = list.Select(x => x.Number).ToArray();
            var articletypes = AppHelper.GetArticleType();
            var parts        = new SubSonic.Query.Select(provider).From <ArticlePart>().Where <ArticlePart>(x => x.Types == Enum_ArticlePart.Pic).And("ArticleNumber").In(array).OrderAsc("SortID").ExecuteTypedList <ArticlePart>();

            List <string> userids = new List <string>();

            list.ForEach(x =>
            {
                userids.Add(x.CreateUserNumber);
            });

            List <string> articleids = new List <string>();

            list.ForEach(x =>
            {
                articleids.Add(x.Number);
            });

            var users = new SubSonic.Query.Select(provider, "ID", "NickName", "Avatar", "Cover", "Signature", "Number", "IsPay").From <User>().Where("Number").In(userids.ToArray()).ExecuteTypedList <User>();

            var comments = new SubSonic.Query.Select(provider, "ID", "ArticleNumber", "ParentCommentNumber").From <Comment>().Where("ArticleNumber").In(articleids.ToArray()).And("ParentCommentNumber").IsEqualTo("").ExecuteTypedList <Comment>();

            //判断是否关注、判断是否点赞、判断是否收藏
            var fans  = new List <Fan>();
            var zans  = new List <ArticleZan>();
            var keeps = new List <Keep>();

            if (!string.IsNullOrWhiteSpace(usernumber))
            {
                fans  = db.Find <Fan>(x => x.CreateUserNumber == usernumber).ToList();
                zans  = db.Find <ArticleZan>(x => x.CreateUserNumber == usernumber).ToList();
                keeps = db.Find <Keep>(x => x.CreateUserNumber == usernumber).ToList();
            }

            var tags = GetTag();

            List <ArticleJson> newlist = new List <ArticleJson>();

            list.ForEach(x =>
            {
                var user = users.FirstOrDefault(y => y.Number == x.CreateUserNumber);
                if (user != null)
                {
                    ArticleJson model   = new ArticleJson();
                    var articletype     = articletypes.FirstOrDefault(y => y.ID == x.TypeID);
                    model.UserID        = user.ID;
                    model.NickName      = user.NickName;
                    model.Avatar        = user.Avatar;
                    model.UserCover     = user.Cover;
                    model.Signature     = user.Signature;
                    model.IsPay         = user.IsPay;
                    model.ArticleID     = x.ID;
                    model.ArticleNumber = x.Number;
                    model.Title         = x.Title;
                    model.Views         = x.Views;
                    model.Goods         = x.Goods;

                    //标签
                    model.TagList = new List <Tag>();
                    if (!string.IsNullOrWhiteSpace(x.Tag))
                    {
                        var tag = x.Tag.Split(',').ToList();
                        tag.ForEach(y =>
                        {
                            var id   = Tools.SafeInt(y);
                            var item = tags.FirstOrDefault(z => z.ID == id);
                            if (item != null)
                            {
                                model.TagList.Add(item);
                            }
                        });
                    }
                    model.Comments     = comments.Count(y => y.ArticleNumber == x.Number);
                    model.IsFollow     = fans.Count(y => y.ToUserNumber == x.CreateUserNumber);
                    model.IsZan        = zans.Count(y => y.ArticleNumber == x.Number);
                    model.IsKeep       = keeps.Count(y => y.ArticleNumber == x.Number);
                    model.UserNumber   = x.CreateUserNumber;
                    model.Cover        = x.Cover;
                    model.CreateDate   = FormatTime(x.CreateDate);
                    model.TypeName     = articletype == null ? "" : articletype.Name;
                    model.ArticlePart  = parts.Where(y => y.ArticleNumber == x.Number).OrderBy(y => y.ID).Take(3).ToList();
                    model.ArticlePower = x.ArticlePower;
                    model.Recommend    = x.Recommend;
                    model.Province     = x.Province;
                    model.City         = x.City;
                    model.Submission   = x.Submission;
                    newlist.Add(model);
                }
            });

            return(newlist);
        }
Exemplo n.º 2
0
        public string SubComment()
        {
            ApiResult result = new ApiResult();

            try
            {
                var pager  = new Pager();
                var Number = ZNRequest.GetString("Number");
                if (string.IsNullOrWhiteSpace(Number))
                {
                    result.message = "参数异常";
                    return(JsonConvert.SerializeObject(result));
                }
                var UserNumber = ZNRequest.GetString("UserNumber");

                var query       = new SubSonic.Query.Select(provider).From <Comment>().Where <Comment>(x => x.ParentCommentNumber == Number);
                var recordCount = query.GetRecordCount();
                if (recordCount == 0)
                {
                    result.result  = true;
                    result.message = new { records = recordCount, totalpage = 1 };
                    return(JsonConvert.SerializeObject(result));
                }

                var totalPage = recordCount % pager.Size == 0 ? recordCount / pager.Size : recordCount / pager.Size + 1;

                var list                   = query.Paged(pager.Index, pager.Size).OrderAsc("ID").ExecuteTypedList <Comment>();
                var users                  = new SubSonic.Query.Select(provider, "ID", "NickName", "Avatar", "Number").From <User>().Where("Number").In(list.Select(x => x.CreateUserNumber).Distinct().ToArray()).ExecuteTypedList <User>();
                var parentComments         = new SubSonic.Query.Select(provider, "ID", "ParentCommentNumber", "Number", "CreateUserNumber").From <Comment>().Where("ParentCommentNumber").In(list.Select(x => x.Number).ToArray()).ExecuteTypedList <Comment>();
                var zans                   = db.Find <CommentZan>(x => x.CreateUserNumber == UserNumber).ToList();
                List <CommentJson> newlist = new List <CommentJson>();
                list.ForEach(x =>
                {
                    CommentJson model = new CommentJson();
                    var user          = users.FirstOrDefault(y => y.Number == x.CreateUserNumber);
                    if (user == null)
                    {
                        return;
                    }
                    model.ID             = x.ID;
                    model.Summary        = x.Summary;
                    model.Goods          = x.Goods;
                    model.Number         = x.Number;
                    model.CreateDateText = x.CreateDate.ToString("yyyy-MM-dd");
                    model.ShowPosition   = x.ShowPosition;
                    model.City           = x.City;
                    if (string.IsNullOrWhiteSpace(model.City))
                    {
                        model.ShowPosition = 0;
                    }
                    model.UserID          = user.ID;
                    model.UserNumber      = user.Number;
                    model.NickName        = user.NickName;
                    model.Avatar          = user.Avatar;
                    model.SubCommentCount = parentComments.Count(y => y.ParentCommentNumber == x.Number);
                    if (model.SubCommentCount == 1)
                    {
                        var subuser = db.Single <User>(y => y.Number == parentComments[0].CreateUserNumber);
                        var comment = db.Single <Comment>(y => y.Number == parentComments[0].Number);
                        if (subuser == null && comment == null)
                        {
                            model.SubCommentCount = 0;
                        }
                        if (subuser != null)
                        {
                            model.SubUserName = subuser.NickName;
                        }
                        if (comment != null)
                        {
                            model.SubSummary = comment.Summary;
                        }
                    }
                    model.ArticleNumber = x.ArticleNumber;
                    model.IsZan         = zans.Count(y => y.CommentNumber == x.Number);
                    newlist.Add(model);
                });
                result.result  = true;
                result.message = new
                {
                    currpage  = pager.Index,
                    records   = recordCount,
                    totalpage = totalPage,
                    list      = newlist
                };
            }
            catch (Exception ex)
            {
                LogHelper.ErrorLoger.Error("Api_Comment_SubComment:" + ex.Message);
                result.message = ex.Message;
            }
            return(JsonConvert.SerializeObject(result));
        }