-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy patharticle.service.ts
More file actions
257 lines (238 loc) · 9.63 KB
/
article.service.ts
File metadata and controls
257 lines (238 loc) · 9.63 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import {Injectable} from '@nestjs/common';
import {InjectRepository} from '@nestjs/typeorm';
import {Article} from '@libs/db/entity/article.entity';
import {Repository} from 'typeorm';
import {CustomException} from '@common/common/common/http.decoration';
@Injectable()
export class ArticleService {
constructor(
@InjectRepository(Article)
private readonly articleRepo: Repository<Article>,
) {
}
async getArticleDetail(params): Promise<any> {
const sqlQuery = await this.articleRepo.query(`
select A.id, A.artTitle, A.abstract, A.artDiscuss,A.artType,
(SELECT categoryname FROM category where status = 1 and FIND_IN_SET(A.category, id) ) as category,
GROUP_CONCAT(T.tagname) as tag,
A.thumbnail, A.pv,
(SELECT COUNT(*) FROM comment where artId = A.id and isChecked = 1 ) as discuss,
A.content,
FROM_UNIXTIME(A.cdate/1000,'%Y-%m-%d %H:%i') as cdate
from article as A
left join tag as T
on FIND_IN_SET(T.id , A.tag)
where A.status = 1 and A.id = ${params.id} and A.authorId = 0
group by A.id
`);
if (sqlQuery.length === 0){
throw new CustomException('您查看的内容暂未公开或已删除', 404);
}
const articleDetail = sqlQuery[0];
if (articleDetail.pv >= 0) {
const newPv = articleDetail.pv + 1;
articleDetail.pv = newPv;
return await this.articleRepo.update(articleDetail.id, {
pv: newPv,
}).then( (res) => {
return articleDetail;
}).catch( err => {
});
}
}
async getHotArticleList(): Promise<any> {
const artList = await this.articleRepo.createQueryBuilder('article')
.where('article.status = :status', {status: 1})
.take(10)
.orderBy('article.pv', 'DESC')
.getMany();
return artList;
}
async getArchive(): Promise<any> {
const archiveList = await this.articleRepo.query(`
select A.id, A.artTitle, A.abstract,
(SELECT categoryname FROM category where status = 1 and FIND_IN_SET(A.category, id) ) as category,
GROUP_CONCAT(T.tagname) as tag,
A.thumbnail,
A.pv,
(SELECT COUNT(*) FROM comment where artId = A.id and isChecked = 1 ) as discuss,
A.content,
FROM_UNIXTIME(A.cdate/1000,'%Y-%m-%d') as cdate
FROM article as A
left join tag as T
on FIND_IN_SET(T.id , A.tag)
WHERE A.status = 1 and A.authorId = 0
group by A.id
ORDER BY A.cdate DESC;
`);
const archiveCount = await this.articleRepo.createQueryBuilder('article')
.where('article.status = :status', {status: 1})
.getCount();
function getDateArr(arr) {
const yearObj = {};
const monthObj = {};
for (let i = 0, len = arr.length; i < len; i++) {
const YearIndex = arr[i].cdate.indexOf('-');
const yearDate = arr[i].cdate.substr(0, YearIndex);
if (!yearObj[yearDate]) {
yearObj[yearDate] = [];
yearObj[yearDate].push(arr[i]);
} else {
yearObj[yearDate].push(arr[i]);
}
}
return yearObj;
}
const result = {
list: getDateArr(archiveList),
total: archiveCount,
};
return result;
}
async getArticleListAll(params): Promise<any> {
const artList = await this.articleRepo.query(`
select
A.id, A.artTitle, A.abstract,A.artType,
(SELECT categoryname FROM category where status = 1 and FIND_IN_SET(A.category, id) ) as category,
GROUP_CONCAT(T.tagname) as tag,
A.thumbnail, A.pv,
(SELECT COUNT(*) FROM comment where artId = A.id and isChecked = 1) as discuss,
A.content,
FROM_UNIXTIME(A.cdate/1000,'%Y-%m-%d %H:%i') as cdate
from article as A
left join tag as T
on FIND_IN_SET(T.id , A.tag)
where A.status = 1 and A.authorId = 0
group by A.id
ORDER BY A.cdate desc
limit ${(params.currentPage - 1) * params.limit}, ${params.limit};
`);
const artCount = await this.articleRepo.createQueryBuilder('article')
.where('article.status = :status', {status: 1})
.getCount();
const result = {
list: artList,
total: artCount,
};
return result;
}
async getArtListByTag(params): Promise<any> {
const artListByTag = await this.articleRepo.query(`
select
A.id, A.artTitle,
(SELECT categoryname FROM category where status = 1 and FIND_IN_SET(A.category, id) ) as category,
GROUP_CONCAT(T.tagname) as tag,
FROM_UNIXTIME(A.cdate/1000, '%Y-%m-%d %H:%i') as cdate,
A.abstract, A.thumbnail,
(SELECT COUNT(*) FROM comment where artId = A.id and isChecked = 1 ) as discuss,
A.pv
from article as A
left join tag as T
on FIND_IN_SET(T.id, A.tag)
where A.status = 1 and A.authorId = 0
AND FIND_IN_SET('${params.tagid}', A.tag)
group by A.id
ORDER BY A.cdate DESC
limit ${(params.currentPage - 1) * params.limit}, ${params.limit};
`);
const artCount = await this.articleRepo.query(`
SELECT COUNT(*) as total FROM article where status = 1 and FIND_IN_SET('${params.tagid}', tag);
`);
const result = {
list: artListByTag,
total: Number(artCount[0].total),
};
return result;
}
async getArtListByCategory(params): Promise<any> {
const artListByCategory = await this.articleRepo.query(`
select
A.id, A.artTitle,
(SELECT categoryname FROM category where status = 1 and FIND_IN_SET(A.category, id) ) as category,
GROUP_CONCAT(T.tagname) as tag,
FROM_UNIXTIME(A.cdate/1000, '%Y-%m-%d %H:%i') as cdate,
A.abstract, A.thumbnail, A.pv,
(SELECT COUNT(*) FROM comment where artId = A.id and isChecked = 1) as discuss
from article as A
left join tag as T
on FIND_IN_SET(T.id, A.tag)
where A.status = 1 and A.authorId = 0
AND FIND_IN_SET('${params.categoryid}', A.category)
group by A.id
ORDER BY A.cdate DESC
limit ${(params.currentPage - 1) * params.limit}, ${params.limit};
`);
const artCount = await this.articleRepo.query(`
SELECT COUNT(*) as total FROM article where status = 1 and FIND_IN_SET('${params.categoryid}', category);
`);
const result = {
list: artListByCategory,
total: Number(artCount[0].total),
};
return result;
}
async getArtListByType(params): Promise<any> {
const artListByType = await this.articleRepo.query(`
select
A.id, A.artTitle,
(SELECT categoryname FROM category where status = 1 and FIND_IN_SET(A.category, id) ) as category,
GROUP_CONCAT(T.tagname) as tag,
FROM_UNIXTIME(A.cdate/1000, '%Y-%m-%d %H:%i') as cdate,
A.abstract, A.thumbnail, A.pv,
(SELECT COUNT(*) FROM comment where artId = A.id and isChecked = 1) as discuss
from article as A
left join tag as T
on FIND_IN_SET(T.id, A.tag)
where A.artType = "${params.artType}"
AND A.status = 1 and A.authorId = 0
group by A.id
ORDER BY A.cdate DESC
limit ${(params.currentPage - 1) * params.limit}, ${params.limit};
`);
const artCount = await this.articleRepo.query(`
SELECT COUNT(*) as total FROM article where status = 1 and artType = "${params.artType}";
`);
const result = {
list: artListByType,
total: Number(artCount[0].total),
};
return result;
}
async getArtListByKeywords(params): Promise<any> {
const artListByCategory = await this.articleRepo.query(`
select
A.id, A.artTitle,
(SELECT categoryname FROM category where status = 1 and FIND_IN_SET(A.category, id) ) as category,
GROUP_CONCAT(T.tagname) as tag,
FROM_UNIXTIME(A.cdate/1000, '%Y-%m-%d %H:%i') as cdate,
A.abstract, A.thumbnail, A.pv,
(SELECT COUNT(*) FROM comment where artId = A.id and isChecked = 1) as discuss
from article as A
left join tag as T
on FIND_IN_SET(T.id, A.tag)
where (
A.artTitle like "%${params.keyword}%"
or
A.abstract like "%${params.keyword}%"
)
AND A.status = 1 and A.authorId = 0
group by A.id
ORDER BY A.cdate DESC
limit ${(params.currentPage - 1) * params.limit}, ${params.limit};
`);
const artCount = await this.articleRepo.query(`
SELECT COUNT(*) as total FROM article where
(
artTitle like "%${params.keyword}%"
or
abstract like "%${params.keyword}%"
)
and status = 1 ;
`);
const result = {
list: artListByCategory,
total: Number(artCount[0].total),
};
return result;
}
}