Files
sbnews/server/app/adminapi/logic/community/CommunityPostLogic.php
T

288 lines
9.8 KiB
PHP

<?php
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\model\community\CommunityTag;
use app\common\model\user\User;
use app\common\service\ai\KbSyncService;
use think\facade\Db;
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'] ?? '';
$post['category_name'] = (int) ($post['category_id'] ?? 0) > 0
? (string) CommunityCategory::where('id', $post['category_id'])->value('name')
: '';
$post['tag_ids'] = array_map('intval', Db::name('community_post_tag')
->where('post_id', $params['id'])
->column('tag_id'));
if (is_array($post['ext'] ?? null)) {
$post['ext'] = json_encode($post['ext'], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
} elseif (is_string($post['ext'] ?? null) && trim($post['ext']) !== '') {
$decodedExt = json_decode($post['ext'], true);
$post['ext'] = json_last_error() === JSON_ERROR_NONE
? json_encode($decodedExt, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)
: $post['ext'];
} else {
$post['ext'] = '';
}
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 edit(array $params): bool
{
$post = CommunityPost::findOrEmpty($params['id']);
if ($post->isEmpty()) {
self::setError('帖子不存在');
return false;
}
$oldCategoryId = (int) $post->category_id;
$oldTagIds = array_map('intval', Db::name('community_post_tag')
->where('post_id', $params['id'])
->column('tag_id'));
$newCategoryId = (int) $params['category_id'];
$newTagIds = array_values(array_unique(array_filter(
array_map('intval', $params['tag_ids'] ?? []),
static fn (int $tagId): bool => $tagId > 0
)));
$isPaid = (int) $params['is_paid'] === 1;
$extValue = $params['ext'] ?? '';
$ext = is_array($extValue)
? json_encode($extValue, JSON_UNESCAPED_UNICODE)
: trim((string) $extValue);
Db::startTrans();
try {
$post->save([
'origin_id' => trim((string) ($params['origin_id'] ?? '')) ?: null,
'user_id' => (int) $params['user_id'],
'content' => (string) ($params['content'] ?? ''),
'images' => array_values($params['images'] ?? []),
'post_type' => (int) $params['post_type'],
'category_id' => $newCategoryId,
'match_id' => (int) $params['match_id'],
'is_paid' => $isPaid ? 1 : 0,
'price_points' => $isPaid ? (int) $params['price_points'] : 0,
'free_content_len' => $isPaid ? (int) $params['free_content_len'] : 100,
'paid_count' => (int) $params['paid_count'],
'view_count' => (int) $params['view_count'],
'like_count' => (int) $params['like_count'],
'comment_count' => (int) $params['comment_count'],
'share_count' => (int) $params['share_count'],
'ext' => $ext === '' ? null : $ext,
'is_top' => (int) $params['is_top'],
'is_hot' => (int) $params['is_hot'],
'is_recommend' => (int) $params['is_recommend'],
'is_topic' => (int) $params['is_topic'],
'sort' => (int) $params['sort'],
'status' => (int) $params['status'],
'create_time' => strtotime((string) $params['create_time']),
'update_time' => time(),
]);
Db::name('community_post_tag')->where('post_id', $post->id)->delete();
if ($newTagIds) {
Db::name('community_post_tag')->insertAll(array_map(
static fn (int $tagId): array => [
'post_id' => (int) $post->id,
'tag_id' => $tagId,
],
$newTagIds
));
}
self::refreshCategoryCount($oldCategoryId);
self::refreshCategoryCount($newCategoryId);
self::refreshTagCounts(array_merge($oldTagIds, $newTagIds));
Db::commit();
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
KbSyncService::enqueue(
'post',
'post',
(int) $post->id,
(int) $post->status === 1 ? 'upsert' : 'delete',
30
);
return true;
}
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 setSort(array $params): bool
{
try {
CommunityPost::update([
'id' => $params['id'],
'sort' => (int) $params['sort'],
]);
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(),
]);
}
private static function refreshTagCounts(array $tagIds): void
{
$tagIds = array_values(array_unique(array_filter(
array_map('intval', $tagIds),
static fn (int $tagId): bool => $tagId > 0
)));
foreach ($tagIds as $tagId) {
$postCount = Db::name('community_post_tag')
->alias('post_tag')
->join('community_post post', 'post.id = post_tag.post_id')
->where('post_tag.tag_id', $tagId)
->whereNull('post.delete_time')
->count();
CommunityTag::where('id', $tagId)->update([
'post_count' => $postCount,
'update_time' => time(),
]);
}
}
}