|
|
@@ -388,12 +388,12 @@ class NewsService implements NewsServiceInterface
|
|
|
*/
|
|
|
public function getArticleList(array $data): array
|
|
|
{
|
|
|
- $startTime = microtime(true);
|
|
|
//判断是否是管理员'1:个人会员 2:政务会员 3:企业会员 4:调研员 10000:管理员 20000:游客(小程序)'
|
|
|
$type_id = $data['type_id'];
|
|
|
unset($data['type_id']);
|
|
|
$user_id = $data['user_id'];
|
|
|
unset($data['user_id']);
|
|
|
+
|
|
|
$where = [];
|
|
|
$status1 = [];
|
|
|
if (isset($data['id']) && $data['id']) {
|
|
|
@@ -426,65 +426,63 @@ class NewsService implements NewsServiceInterface
|
|
|
if ($type_id != 10000) {
|
|
|
$where[] = ['article.admin_user_id', '=', $user_id];
|
|
|
}
|
|
|
-
|
|
|
- // $rep = Article::where($where)
|
|
|
- // ->whereNotIn('article.status', [404])
|
|
|
- // ->when($status1, function ($query) use ($status1) {
|
|
|
- // if (isset($status1) && $status1) {
|
|
|
- // $query->whereIn('article.status', $status1);
|
|
|
- // }
|
|
|
- // })
|
|
|
- // ->leftJoin('category', 'article.catid', 'category.id')
|
|
|
- // ->leftJoin('website_category', function ($join) {
|
|
|
- // $join->on('article.web_site_id', '=', 'website_category.website_id')
|
|
|
- // ->on('article.catid', '=', 'website_category.category_id');
|
|
|
- // })
|
|
|
- // ->when($type_id != 10000 && !empty($data['website_id']), function ($query) use ($data) {
|
|
|
- // $query->leftJoin('article_ignore', function ($join) use ($data) {
|
|
|
- // $join->on('article.id', '=', 'article_ignore.article_id')
|
|
|
- // ->where('article_ignore.website_id', $data['website_id']);
|
|
|
- // });
|
|
|
- // })
|
|
|
- // ->select("article.*", "category.name as category_name", "website_category.alias",
|
|
|
- // Db::raw('IFNULL(article_ignore.c_show_time, "") as c_show_time'))
|
|
|
- // ->orderBy("article.updated_at", "desc")
|
|
|
- // ->limit($data['pageSize'])
|
|
|
- // ->offset(($data['page'] - 1) * $data['pageSize'])->get();
|
|
|
- //
|
|
|
- $query = Article::where($where)
|
|
|
- ->whereNotIn('article.status', [404])
|
|
|
- ->when($status1, function ($query) use ($status1) {
|
|
|
- if (isset($status1) && $status1) {
|
|
|
+ $startTime = microtime(true);
|
|
|
+ $rep = Article::query()
|
|
|
+ // 预加载相关关联
|
|
|
+ ->with([
|
|
|
+ 'category' => function ($query) {
|
|
|
+ $query->select('id', 'name');
|
|
|
+ },
|
|
|
+ 'websiteCategory' => function ($query) {
|
|
|
+ $query->select('website_id', 'category_id', 'alias');
|
|
|
+ }
|
|
|
+ ])
|
|
|
+ // 构建where条件
|
|
|
+ ->when(isset($data['id']) && $data['id'], function ($query) use ($data) {
|
|
|
+ $query->where('article.id', '=', $data['id']);
|
|
|
+ })
|
|
|
+ ->when(isset($data['title']) && $data['title'], function ($query) use ($data) {
|
|
|
+ $query->where('article.title', 'like', '%' . $data['title'] . '%');
|
|
|
+ })
|
|
|
+ ->when(isset($data['category_name']) && $data['category_name'], function ($query) use ($data) {
|
|
|
+ // 使用子查询替代左连接进行模糊搜索
|
|
|
+ $query->whereHas('category', function ($subQuery) use ($data) {
|
|
|
+ $subQuery->where('name', 'like', '%' . $data['category_name'] . '%');
|
|
|
+ });
|
|
|
+ })
|
|
|
+ ->when(isset($data['catid']) && $data['catid'], function ($query) use ($data) {
|
|
|
+ $query->where('article.catid', '=', $data['catid']);
|
|
|
+ })
|
|
|
+ ->when(isset($data['author']) && $data['author'], function ($query) use ($data) {
|
|
|
+ $query->where('article.author', '=', $data['author']);
|
|
|
+ })
|
|
|
+ ->when(isset($data['islink']) && $data['islink'] !== "", function ($query) use ($data) {
|
|
|
+ $query->where('article.islink', '=', $data['islink']);
|
|
|
+ })
|
|
|
+ ->when(isset($data['status']) && $data['status'] !== "", function ($query) use ($data) {
|
|
|
+ $query->where('article.status', '=', $data['status']);
|
|
|
+ })
|
|
|
+ ->when(isset($data['status1']) && $data['status1'], function ($query) use ($data) {
|
|
|
+ // 安全地处理JSON字符串
|
|
|
+ $status1 = is_array($data['status1']) ? $data['status1'] : json_decode($data['status1'], true) ?? [];
|
|
|
+ if (!empty($status1)) {
|
|
|
$query->whereIn('article.status', $status1);
|
|
|
}
|
|
|
})
|
|
|
- ->leftJoin('category', 'article.catid', 'category.id')
|
|
|
- ->leftJoin('website_category', function ($join) {
|
|
|
- $join->on('article.web_site_id', '=', 'website_category.website_id')
|
|
|
- ->on('article.catid', '=', 'website_category.category_id');
|
|
|
- });
|
|
|
-
|
|
|
- if ($type_id != 10000 && !empty($data['website_id'])) {
|
|
|
- $query = $query
|
|
|
- ->leftJoin('article_ignore', function ($join) use ($data) {
|
|
|
- $join->on('article.id', '=', 'article_ignore.article_id')
|
|
|
- ->where('article_ignore.website_id', $data['website_id']);
|
|
|
- })
|
|
|
- ->select("article.*", "category.name as category_name", "website_category.alias", 'article_ignore.c_show_time');
|
|
|
- } else {
|
|
|
- $query = $query
|
|
|
- ->select("article.*", "category.name as category_name", "website_category.alias");
|
|
|
- }
|
|
|
+ // 非管理员限制
|
|
|
+ ->when($type_id != 10000, function ($query) use ($user_id) {
|
|
|
+ $query->where('article.admin_user_id', '=', $user_id);
|
|
|
+ })
|
|
|
+ // 基础条件
|
|
|
+ ->whereNotIn('article.status', [404])
|
|
|
+ // 排序和分页
|
|
|
+ ->orderBy('article.updated_at', 'desc')
|
|
|
+ ->paginate($data['pageSize'], ["article.*"]);
|
|
|
|
|
|
- $count = $query->count();
|
|
|
- $rep = $query
|
|
|
- ->orderBy("article.updated_at", "desc")
|
|
|
- ->limit($data['pageSize'])
|
|
|
- ->offset(($data['page'] - 1) * $data['pageSize'])
|
|
|
- ->get();
|
|
|
+ // 获取结果并格式化
|
|
|
$data = [
|
|
|
- 'rows' => $rep->toArray(),
|
|
|
- 'count' => $count,
|
|
|
+ 'rows' => $rep->items(),
|
|
|
+ 'count' => $rep->total(),
|
|
|
];
|
|
|
// 执行查询
|
|
|
$endTime = microtime(true);
|