Files
sbnews/server/app/adminapi/logic/community/CommunityPostLogic.php
T
2026-06-11 12:15:29 +08:00

96 lines
2.8 KiB
PHP

<?php
namespace app\adminapi\logic\community;
use app\common\logic\BaseLogic;
use app\common\model\community\CommunityPost;
use app\common\service\ai\KbSyncService;
use app\common\model\user\User;
class CommunityPostLogic extends BaseLogic
{
public static function detail($params): array
{
$post = CommunityPost::findOrEmpty($params['id'])->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'] ?? '';
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 delete(array $params)
{
CommunityPost::destroy($params['id']);
KbSyncService::enqueue('post', 'post', (int) $params['id'], 'delete', 20);
}
}