移动系统liao
2025-02-17 557c2711a3e103ebc3d0492344eca9730d5e92b2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/***********************************************************************
 *            Project: baifenBinfa
 *        ProjectName: 百分兵法管理系统                               
 *                Web: http://chuanyin.com                     
 *             Author:                                        
 *              Email:                               
 *         CreateTime: 202403/02   
 *        Description: 暂无
 ***********************************************************************/
 
 
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using CoreCms.Net.IRepository;
using CoreCms.Net.IRepository.UnitOfWork;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.ViewModels.Basics;
using CoreCms.Net.Utility.Helper;
using SqlSugar;
 
namespace CoreCms.Net.Repository
{
    /// <summary>
    /// 文章表 接口实现
    /// </summary>
    public class CoreCmsArticleRepository : BaseRepository<CoreCmsArticle>, ICoreCmsArticleRepository
    {
        public CoreCmsArticleRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
        {
        }
 
 
        #region  获取指定id 的文章详情
        /// <summary>
        /// 获取指定id 的文章详情
        /// </summary>
        /// <param name="id">序列</param>
        public async Task<CoreCmsArticle> ArticleDetail(int id)
        {
            var article = await DbClient.Queryable<CoreCmsArticle>().Where(p => p.id == id && p.isPub == true)
                .FirstAsync();
            if (article == null)
            {
                return null;
            }
            article.contentBody = CommonHelper.ClearHtml(article.contentBody, new[] { "width", "height" });
            article.contentBody = article.contentBody.Replace("<img", "<img style='max-width: 100%'");
            article.contentBody = article.contentBody.Replace("oembed url=", "video  width=\"100%\"  controls=\"controls\" src=");
            article.contentBody = article.contentBody.Replace("/oembed", "/video");
 
            if (article.typeId > 0)
            {
                article.articleType = await DbClient.Queryable<CoreCmsArticleType>().InSingleAsync(article.typeId);
            }
            //上一篇
            article.upArticle = await DbClient.Queryable<CoreCmsArticle>().Where(p => p.id < article.id && p.isPub == true).Select(p => new CoreCmsArticle
            {
                id = p.id,
                title = p.title,
                brief = p.brief,
                coverImage = p.coverImage,
                typeId = p.typeId,
                sort = p.sort,
                isPub = p.isPub,
                isDel = p.isDel,
                pv = p.pv,
                createTime = p.createTime,
                updateTime = p.updateTime
            }).FirstAsync();
            //下一篇
            article.downArticle = await DbClient.Queryable<CoreCmsArticle>().Where(p => p.id > article.id && p.isPub == true).Select(p => new CoreCmsArticle
            {
                id = p.id,
                title = p.title,
                brief = p.brief,
                coverImage = p.coverImage,
                typeId = p.typeId,
                sort = p.sort,
                isPub = p.isPub,
                isDel = p.isDel,
                pv = p.pv,
                createTime = p.createTime,
                updateTime = p.updateTime
            }).FirstAsync();
 
            await DbClient.Updateable<CoreCmsArticle>().SetColumns(p => p.pv == (p.pv + 1)).Where(p => p.id == article.id).ExecuteCommandAsync();
 
            return article;
        }
 
        #endregion
 
        #region 重写根据条件查询分页数据
        /// <summary>
        ///     重写根据条件查询分页数据
        /// </summary>
        /// <param name="predicate">判断集合</param>
        /// <param name="orderByType">排序方式</param>
        /// <param name="pageIndex">当前页面索引</param>
        /// <param name="pageSize">分布大小</param>
        /// <param name="orderByExpression"></param>
        /// <returns></returns>
        public async Task<IPageList<CoreCmsArticle>> QueryPageAsync(Expression<Func<CoreCmsArticle, bool>> predicate,
            Expression<Func<CoreCmsArticle, object>> orderByExpression, OrderByType orderByType, int pageIndex = 1,
            int pageSize = 20)
        {
            RefAsync<int> totalCount = 0;
            var page = await DbClient.Queryable<CoreCmsArticle>()
                .OrderByIF(orderByExpression != null, orderByExpression, orderByType)
                .WhereIF(predicate != null, predicate).Select(p => new CoreCmsArticle
                {
                    id = p.id,
                    title = p.title,
                    brief = p.brief,
                    coverImage = p.coverImage,
                    typeId = p.typeId,
                    sort = p.sort,
                    isPub = p.isPub,
                    isDel = p.isDel,
                    pv = p.pv,
                    createTime = p.createTime,
                    updateTime = p.updateTime
                }).ToPageListAsync(pageIndex, pageSize, totalCount);
            var list = new PageList<CoreCmsArticle>(page, pageIndex, pageSize, totalCount);
            return list;
        }
 
        #endregion
 
 
        #region 重写根据条件查询分页数据
 
        /// <summary>
        ///     重写根据条件查询分页数据
        /// </summary>
        /// <param name="predicate">判断集合</param>
        /// <param name="orderByType">排序方式</param>
        /// <param name="orderByExpression"></param>
        /// <param name="take"></param>
        /// <returns></returns>
        public async Task<List<CoreCmsArticle>> QueryListAsync(Expression<Func<CoreCmsArticle, bool>> predicate,
            Expression<Func<CoreCmsArticle, object>> orderByExpression, OrderByType orderByType,int take)
        {
            var list = await DbClient.Queryable<CoreCmsArticle>()
                .OrderByIF(orderByExpression != null, orderByExpression, orderByType)
                .WhereIF(predicate != null, predicate).Select(p => new CoreCmsArticle
                {
                    id = p.id,
                    title = p.title,
                    brief = p.brief,
                    coverImage = p.coverImage,
                    typeId = p.typeId,
                    sort = p.sort,
                    isPub = p.isPub,
                    isDel = p.isDel,
                    pv = p.pv,
                    createTime = p.createTime,
                    updateTime = p.updateTime
                }).Take(take).ToListAsync();
            return list;
        }
 
        #endregion
 
 
    }
}