75 lines
2.4 KiB
PHP
75 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace app\adminapi\logic\community;
|
|
|
|
use app\common\logic\BaseLogic;
|
|
use app\common\model\article\Article;
|
|
use app\common\model\article\ArticleComment;
|
|
use app\common\model\community\CommunityComment;
|
|
use app\common\model\community\CommunityPost;
|
|
use think\facade\Db;
|
|
|
|
class CommunityCommentLogic extends BaseLogic
|
|
{
|
|
public static function updateStatus(array $params): bool
|
|
{
|
|
try {
|
|
if (($params['comment_type'] ?? '') === 'article') {
|
|
$comment = ArticleComment::findOrEmpty($params['id']);
|
|
if ($comment->isEmpty()) {
|
|
self::setError('评论不存在');
|
|
return false;
|
|
}
|
|
$comment->save([
|
|
'is_show' => $params['status'],
|
|
]);
|
|
} else {
|
|
$comment = CommunityComment::findOrEmpty($params['id']);
|
|
if ($comment->isEmpty()) {
|
|
self::setError('评论不存在');
|
|
return false;
|
|
}
|
|
$comment->save([
|
|
'status' => $params['status'],
|
|
]);
|
|
}
|
|
return true;
|
|
} catch (\Exception $e) {
|
|
self::setError($e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public static function delete(array $params): bool
|
|
{
|
|
Db::startTrans();
|
|
try {
|
|
if (($params['comment_type'] ?? '') === 'article') {
|
|
$comment = ArticleComment::findOrEmpty($params['id']);
|
|
if (!$comment->isEmpty()) {
|
|
Article::where('id', $comment->article_id)
|
|
->where('comment_count', '>', 0)
|
|
->dec('comment_count')
|
|
->update();
|
|
$comment->delete();
|
|
}
|
|
} else {
|
|
$comment = CommunityComment::findOrEmpty($params['id']);
|
|
if (!$comment->isEmpty()) {
|
|
CommunityPost::where('id', $comment->post_id)
|
|
->where('comment_count', '>', 0)
|
|
->dec('comment_count')
|
|
->update();
|
|
$comment->delete();
|
|
}
|
|
}
|
|
Db::commit();
|
|
return true;
|
|
} catch (\Throwable $e) {
|
|
Db::rollback();
|
|
self::setError($e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
}
|