Files
QYZH.InteractiveMagazine/QYZH.InteractiveMagazine.Service/CommunityMessageService.cs
glz bdaa6a0dc8 refactor: 统一业务异常处理,标准化结果码和错误响应
1.  新增并完善ResultCode枚举,补充标准HTTP状态码对应的业务状态码
2.  重构BusinessException,新增基于ResultCode的构造函数和ThrowIf扩展方法
3.  替换所有硬编码的HTTP状态码为统一的ResultCode枚举
4.  优化全局异常中间件,根据业务状态码映射对应HTTP状态码并规范化JSON响应
5.  修复OssImageHelper和AutoDotCodeConsumer中的OSS文件处理逻辑
6.  新增用户答题快照实体类
7.  清理废弃的宠物模块迁移脚本
2026-06-29 16:34:26 +08:00

242 lines
9.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.Extensions.Logging;
using QYZH.InteractiveMagazine.Common.Extensions;
using QYZH.InteractiveMagazine.IService;
using QYZH.InteractiveMagazine.Models.Common;
using QYZH.InteractiveMagazine.Models.Dto;
using QYZH.InteractiveMagazine.Models.Entity;
using QYZH.InteractiveMagazine.Models.Enum;
using QYZH.InteractiveMagazine.Repository;
using SqlSugar;
namespace QYZH.InteractiveMagazine.Service;
/// <summary>
/// 社区消息后台管理服务实现
/// </summary>
public class CommunityMessageService(BaseRepository<CommunityMessage> messageRepository, ILogger<CommunityMessageService> logger)
: BaseRepository<CommunityMessage>, ICommunityMessageService
{
/// <summary>
/// 分页查询消息列表
/// </summary>
public async Task<PageListModel<AdminMessageDetailOutput>> GetListAsync(AdminMessageQueryInput input)
{
logger.LogInformation("正在查询社区消息列表,页码: {PageIndex}, 每页条数: {PageSize}", input.PageIndex, input.PageSize);
if (input.PageIndex <= 0) throw new BusinessException("页码必须大于0", ResultCode.BAD_REQUEST);
if (input.PageSize <= 0 || input.PageSize > 100) throw new BusinessException("每页条数必须在1-100之间", ResultCode.BAD_REQUEST);
RefAsync<int> totalNumber = 0;
var pageResult = await messageRepository.Queryable()
.WhereIF(input.JournalId.HasValue, m => m.JournalId == input.JournalId.Value)
.WhereIF(!string.IsNullOrWhiteSpace(input.Type), m => m.Type.ToString() == input.Type)
.WhereIF(input.Status.HasValue, m => m.Status == input.Status.Value)
.WhereIF(input.IsFeatured.HasValue, m => m.IsFeatured == input.IsFeatured.Value)
.WhereIF(input.IsActive.HasValue, m => m.IsActive == input.IsActive.Value)
.WhereIF(!string.IsNullOrWhiteSpace(input.KeyWord), m => m.Content.Contains(input.KeyWord))
.WhereIF(input.UserId.HasValue, m => m.UserId == input.UserId.Value)
.OrderByDescending(m => m.IsFeatured)
.OrderByDescending(m => m.CreatedAt)
.Select(m => new AdminMessageDetailOutput
{
Id = m.Id,
JournalId = m.JournalId,
UserId = m.UserId,
UserJournalId = m.UserJournalId,
JournalTaskId = m.JournalTaskId,
JournalTaskAnswerId = m.JournalTaskAnswerId,
Content = m.Content,
ImageUrl = m.ImageUrl,
SortOrder = m.SortOrder,
IsActive = m.IsActive,
Type = m.Type.ToString(),
LikeCount = m.LikeCount,
IsFeatured = m.IsFeatured,
Status = m.Status,
CreatedBy = m.CreatedBy,
CreatedAt = m.CreatedAt,
UpdatedBy = m.UpdatedBy,
UpdatedAt = m.UpdatedAt
}, true)
.ToPageListAsync(input.PageIndex, input.PageSize, totalNumber);
return new PageListModel<AdminMessageDetailOutput>(pageResult, input.PageIndex, input.PageSize, totalNumber);
}
/// <summary>
/// 查看消息详情
/// </summary>
public async Task<AdminMessageDetailOutput> GetDetailAsync(long id)
{
logger.LogInformation("正在获取社区消息详情ID: {Id}", id);
var message = await messageRepository.GetByIdAsync(id);
if (message == null)
{
logger.LogWarning("未找到社区消息ID: {Id}", id);
throw new BusinessException("消息不存在", ResultCode.NOT_FOUND);
}
return new AdminMessageDetailOutput
{
Id = message.Id,
JournalId = message.JournalId,
UserId = message.UserId,
UserJournalId = message.UserJournalId,
JournalTaskId = message.JournalTaskId,
JournalTaskAnswerId = message.JournalTaskAnswerId,
Content = message.Content,
ImageUrl = message.ImageUrl,
SortOrder = message.SortOrder,
IsActive = message.IsActive,
Type = message.Type.ToString(),
LikeCount = message.LikeCount,
IsFeatured = message.IsFeatured,
Status = message.Status,
CreatedBy = message.CreatedBy,
CreatedAt = message.CreatedAt,
UpdatedBy = message.UpdatedBy,
UpdatedAt = message.UpdatedAt
};
}
/// <summary>
/// 软删除消息
/// </summary>
public async Task DeleteAsync(long id)
{
logger.LogInformation("正在删除社区消息ID: {Id}", id);
var message = await messageRepository.GetByIdAsync(id);
if (message == null)
{
logger.LogWarning("未找到要删除的社区消息ID: {Id}", id);
throw new BusinessException("消息不存在", ResultCode.NOT_FOUND);
}
var result = await messageRepository.DeleteByIdAsync(id);
if (!result)
{
logger.LogError("社区消息删除失败ID: {Id}", id);
throw new BusinessException("删除消息失败", ResultCode.GLOBAL_ERROR);
}
logger.LogInformation("社区消息删除成功ID: {Id}", id);
}
/// <summary>
/// 冻结/解冻消息
/// </summary>
public async Task FreezeAsync(long id, int status)
{
logger.LogInformation("正在更新社区消息冻结状态ID: {Id}, Status: {Status}", id, status);
if (status != 1 && status != 2)
{
throw new BusinessException("状态值无效只能为1(解冻/通过)或2(冻结)", ResultCode.BAD_REQUEST);
}
var message = await messageRepository.GetByIdAsync(id);
if (message == null)
{
logger.LogWarning("未找到社区消息ID: {Id}", id);
throw new BusinessException("消息不存在", ResultCode.NOT_FOUND);
}
message.Status = status;
message.UpdatedBy = "System";
message.UpdatedAt = DateTime.Now;
var result = await messageRepository.UpdateAsync(message);
if (!result)
{
logger.LogError("社区消息冻结状态更新失败ID: {Id}", id);
throw new BusinessException("更新冻结状态失败", ResultCode.GLOBAL_ERROR);
}
logger.LogInformation("社区消息冻结状态更新成功ID: {Id}, Status: {Status}", id, status);
}
/// <summary>
/// 设置/取消精选
/// </summary>
public async Task SetFeaturedAsync(long id, int isFeatured)
{
logger.LogInformation("正在设置社区消息精选状态ID: {Id}, IsFeatured: {IsFeatured}", id, isFeatured);
if (isFeatured != 0 && isFeatured != 1)
{
throw new BusinessException("精选值无效只能为0(取消)或1(精选)", ResultCode.BAD_REQUEST);
}
var message = await messageRepository.GetByIdAsync(id);
if (message == null)
{
logger.LogWarning("未找到社区消息ID: {Id}", id);
throw new BusinessException("消息不存在", ResultCode.NOT_FOUND);
}
message.IsFeatured = isFeatured;
message.UpdatedBy = "System";
message.UpdatedAt = DateTime.Now;
var result = await messageRepository.UpdateAsync(message);
if (!result)
{
logger.LogError("社区消息精选设置失败ID: {Id}", id);
throw new BusinessException("设置精选失败", ResultCode.GLOBAL_ERROR);
}
logger.LogInformation("社区消息精选设置成功ID: {Id}, IsFeatured: {IsFeatured}", id, isFeatured);
}
/// <summary>
/// 设置排序权重
/// </summary>
public async Task SetSortOrderAsync(long id, int sortOrder)
{
logger.LogInformation("正在设置社区消息排序权重ID: {Id}, SortOrder: {SortOrder}", id, sortOrder);
var message = await messageRepository.GetByIdAsync(id);
if (message == null)
{
logger.LogWarning("未找到社区消息ID: {Id}", id);
throw new BusinessException("消息不存在", ResultCode.NOT_FOUND);
}
message.SortOrder = sortOrder;
message.UpdatedBy = "System";
message.UpdatedAt = DateTime.Now;
var result = await messageRepository.UpdateAsync(message);
if (!result)
{
logger.LogError("社区消息排序权重设置失败ID: {Id}", id);
throw new BusinessException("设置排序权重失败", ResultCode.GLOBAL_ERROR);
}
logger.LogInformation("社区消息排序权重设置成功ID: {Id}, SortOrder: {SortOrder}", id, sortOrder);
}
/// <summary>
/// 批量发布消息
/// </summary>
public async Task BatchPublishAsync(List<long> ids)
{
logger.LogInformation("正在批量发布社区消息,数量: {Count}", ids.Count);
if (ids == null || ids.Count == 0)
{
throw new BusinessException("消息ID列表不能为空", ResultCode.BAD_REQUEST);
}
var result = await Context.Updateable<CommunityMessage>()
.SetColumns(m => m.IsActive == true)
.Where(m => ids.Contains(m.Id))
.ExecuteCommandAsync();
logger.LogInformation("批量发布社区消息完成,影响行数: {Result}", result);
}
}