feat: add community post categories and channels
This commit is contained in:
@@ -3,9 +3,11 @@
|
||||
namespace app\adminapi\logic\community;
|
||||
|
||||
use app\common\logic\BaseLogic;
|
||||
use app\common\model\community\CommunityCategory;
|
||||
use app\common\model\community\CommunityPost;
|
||||
use app\common\service\ai\KbSyncService;
|
||||
use app\common\model\user\User;
|
||||
use app\common\service\ai\KbSyncService;
|
||||
use think\facade\Db;
|
||||
|
||||
class CommunityPostLogic extends BaseLogic
|
||||
{
|
||||
@@ -21,6 +23,9 @@ class CommunityPostLogic extends BaseLogic
|
||||
$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;
|
||||
}
|
||||
|
||||
@@ -87,9 +92,65 @@ class CommunityPostLogic extends BaseLogic
|
||||
}
|
||||
}
|
||||
|
||||
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(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user