PostService.php
9.8 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\portal\service;
use app\portal\model\PortalPostModel;
use think\db\Query;
class PostService
{
/**
* 文章查询
* @param $filter
* @return \think\Paginator
* @throws \think\exception\DbException
*/
public function adminArticleList($filter)
{
return $this->adminPostList($filter);
}
/**
* 页面文章列表
* @param $filter
* @return \think\Paginator
* @throws \think\exception\DbException
*/
public function adminPageList($filter)
{
return $this->adminPostList($filter, true);
}
/**
* 文章查询
* @param $filter
* @param bool $isPage
* @return \think\Paginator
* @throws \think\exception\DbException
*/
public function adminPostList($filter, $isPage = false)
{
$join = [
['__USER__ u', 'a.user_id = u.id']
];
$field = 'a.*,u.user_login,u.user_nickname,u.user_email';
$category = empty($filter['category']) ? 0 : intval($filter['category']);
if (!empty($category)) {
array_push($join, [
'__PORTAL_CATEGORY_POST__ b', 'a.id = b.post_id'
]);
$field = 'a.*,b.id AS post_category_id,b.list_order,b.category_id,u.user_login,u.user_nickname,u.user_email';
}
$portalPostModel = new PortalPostModel();
$articles = $portalPostModel->alias('a')->field($field)
->join($join)
->where('a.create_time', '>=', 0)
->where('a.delete_time', 0)
->where(function (Query $query) use ($filter, $isPage) {
$category = empty($filter['category']) ? 0 : intval($filter['category']);
if (!empty($category)) {
$query->where('b.category_id', $category);
}
//城市分类
$city_id = empty($filter['city_id']) ? 0 : intval($filter['city_id']);
if (!empty($city_id)) {
$query->where('a.city_id', $city_id);
}
//明星姓名
$full_name = empty($filter['full_name']) ? '' : $filter['full_name'];
if (!empty($full_name)) {
$query->where('a.full_name', 'like', "%$full_name%");
}
//标题
$post_title = empty($filter['post_title']) ? '' : $filter['post_title'];
if (!empty($post_title)) {
$query->where('a.post_title', 'like', "%$post_title%");
}
$startTime = empty($filter['start_time']) ? 0 : strtotime($filter['start_time']);
$endTime = empty($filter['end_time']) ? 0 : strtotime($filter['end_time']);
if (!empty($startTime)) {
$query->where('a.published_time', '>=', $startTime);
}
if (!empty($endTime)) {
$query->where('a.published_time', '<=', $endTime);
}
$keyword = empty($filter['keyword']) ? '' : $filter['keyword'];
if (!empty($keyword)) {
$query->where('a.post_title', 'like', "%$keyword%");
}
if ($isPage) {
$query->where('a.post_type', 2);
} else {
$query->where('a.post_type', 1);
}
})
->order('weigh', 'DESC')
->paginate(10);
return $articles;
}
/**
* 已发布文章查询
* @param int $postId 文章id
* @param int $categoryId 分类id
* @return array|string|\think\Model|null
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function publishedArticle($postId, $categoryId = 0)
{
$portalPostModel = new PortalPostModel();
if (empty($categoryId)) {
$where = [
'post.post_type' => 1,
'post.post_status' => 1,
'post.delete_time' => 0,
'post.id' => $postId
];
$article = $portalPostModel->alias('post')->field('post.*')
->where($where)
->where('post.published_time', ['< time', time()], ['> time', 0], 'and')
->find();
} else {
$where = [
'post.post_type' => 1,
'post.post_status' => 1,
'post.delete_time' => 0,
'relation.category_id' => $categoryId,
'relation.post_id' => $postId
];
$join = [
['__PORTAL_CATEGORY_POST__ relation', 'post.id = relation.post_id']
];
$article = $portalPostModel->alias('post')->field('post.*')
->join($join)
->where($where)
->where('post.published_time', ['< time', time()], ['> time', 0], 'and')
->find();
}
return $article;
}
/**
* 上一篇文章
* @param int $postId 文章id
* @param int $categoryId 分类id
* @return array|string|\think\Model|null
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function publishedPrevArticle($postId, $categoryId = 0)
{
$portalPostModel = new PortalPostModel();
if (empty($categoryId)) {
$where = [
'post.post_type' => 1,
'post.post_status' => 1,
'post.delete_time' => 0,
];
$article = $portalPostModel
->alias('post')
->field('post.*')
->where($where)
->where('post.id', '<', $postId)
->where('post.published_time', ['< time', time()], ['> time', 0], 'and')
->order('id', 'DESC')
->find();
} else {
$where = [
'post.post_type' => 1,
'post.post_status' => 1,
'post.delete_time' => 0,
'relation.category_id' => $categoryId,
];
$join = [
['__PORTAL_CATEGORY_POST__ relation', 'post.id = relation.post_id']
];
$article = $portalPostModel
->alias('post')
->field('post.*')
->join($join)
->where($where)
->where('relation.post_id', '<', $postId)
->where('post.published_time', ['< time', time()], ['> time', 0], 'and')
->order('id', 'DESC')
->find();
}
return $article;
}
/**
* 下一篇文章
* @param int $postId 文章id
* @param int $categoryId 分类id
* @return array|string|\think\Model|null
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function publishedNextArticle($postId, $categoryId = 0)
{
$portalPostModel = new PortalPostModel();
if (empty($categoryId)) {
$where = [
'post.post_type' => 1,
'post.post_status' => 1,
'post.delete_time' => 0,
];
$article = $portalPostModel->alias('post')->field('post.*')
->where($where)
->where('post.id', '>', $postId)
->where('post.published_time', ['< time', time()], ['> time', 0], 'and')
->order('id', 'ASC')
->find();
} else {
$where = [
'post.post_type' => 1,
'post.post_status' => 1,
'post.delete_time' => 0,
'relation.category_id' => $categoryId,
];
$join = [
['__PORTAL_CATEGORY_POST__ relation', 'post.id = relation.post_id']
];
$article = $portalPostModel->alias('post')->field('post.*')
->join($join)
->where($where)
->where('relation.post_id', '>', $postId)
->where('post.published_time', ['< time', time()], ['> time', 0], 'and')
->order('id', 'ASC')
->find();
}
return $article;
}
/**
* 页面管理查询
* @param int $pageId 文章id
* @return array|string|\think\Model|null
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function publishedPage($pageId)
{
$where = [
'post_type' => 2,
'post_status' => 1,
'delete_time' => 0,
'id' => $pageId
];
$portalPostModel = new PortalPostModel();
$page = $portalPostModel
->where($where)
->where('published_time', ['< time', time()], ['> time', 0], 'and')
->find();
return $page;
}
}