toArray(); if (!empty($post['create_time']) && is_numeric($post['create_time'])) { $post['create_time'] = date('Y-m-d H:i:s', $post['create_time']); } if (!empty($post['update_time']) && is_numeric($post['update_time'])) { $post['update_time'] = date('Y-m-d H:i:s', $post['update_time']); } $user = User::field('nickname,avatar')->findOrEmpty($post['user_id'] ?? 0)->toArray(); $post['nickname'] = $user['nickname'] ?? '-'; $post['avatar'] = $user['avatar'] ?? ''; $post['category_name'] = (int) ($post['category_id'] ?? 0) > 0 ? (string) CommunityCategory::where('id', $post['category_id'])->value('name') : ''; return $post; } public static function updateStatus(array $params): bool { try { CommunityPost::update([ 'id' => $params['id'], 'status' => $params['status'], ]); KbSyncService::enqueue( 'post', 'post', (int) $params['id'], (int) $params['status'] === 1 ? 'upsert' : 'delete', 30 ); return true; } catch (\Exception $e) { self::setError($e->getMessage()); return false; } } public static function setTop(array $params): bool { try { CommunityPost::update([ 'id' => $params['id'], 'is_top' => $params['is_top'], ]); return true; } catch (\Exception $e) { self::setError($e->getMessage()); return false; } } public static function setHot(array $params): bool { try { CommunityPost::update([ 'id' => $params['id'], 'is_hot' => $params['is_hot'], ]); return true; } catch (\Exception $e) { self::setError($e->getMessage()); return false; } } public static function setRecommend(array $params): bool { try { CommunityPost::update([ 'id' => $params['id'], 'is_recommend' => $params['is_recommend'], ]); return true; } catch (\Exception $e) { self::setError($e->getMessage()); return false; } } public static function setTopic(array $params): bool { try { CommunityPost::update([ 'id' => $params['id'], 'is_topic' => $params['is_topic'], ]); return true; } catch (\Exception $e) { self::setError($e->getMessage()); return false; } } public static function setCategory(array $params): bool { $post = CommunityPost::findOrEmpty($params['id']); if ($post->isEmpty()) { self::setError('帖子不存在'); return false; } $oldCategoryId = (int) $post->category_id; $newCategoryId = (int) $params['category_id']; if ($oldCategoryId === $newCategoryId) { return true; } Db::startTrans(); try { $post->category_id = $newCategoryId; $post->save(); self::refreshCategoryCount($oldCategoryId); self::refreshCategoryCount($newCategoryId); Db::commit(); return true; } catch (\Exception $e) { Db::rollback(); self::setError($e->getMessage()); return false; } } public static function delete(array $params) { $categoryId = (int) CommunityPost::where('id', $params['id'])->value('category_id'); CommunityPost::destroy($params['id']); self::refreshCategoryCount($categoryId); KbSyncService::enqueue('post', 'post', (int) $params['id'], 'delete', 20); } private static function refreshCategoryCount(int $categoryId): void { if ($categoryId <= 0) { return; } CommunityCategory::where('id', $categoryId)->update([ 'post_count' => CommunityPost::where('category_id', $categoryId)->count(), 'update_time' => time(), ]); } }