Files
QYZH.InteractiveMagazine/QYZH.InteractiveMagazine.Service/BaseService.cs

206 lines
5.8 KiB
C#
Raw Normal View History

2026-06-01 13:42:40 +08:00
using Microsoft.Extensions.Logging;
using QYZH.InteractiveMagazine.IService;
using QYZH.InteractiveMagazine.Models.Common;
using QYZH.InteractiveMagazine.Models.Dto;
using QYZH.InteractiveMagazine.Models.Entity;
using QYZH.InteractiveMagazine.Repository;
namespace QYZH.InteractiveMagazine.Service;
/// <summary>
/// 基础服务实现
/// </summary>
/// <typeparam name="T">实体类型</typeparam>
public class BaseService<T> : IBaseService<T> where T : class, new()
{
protected readonly IBaseRepository<T> _repository;
protected readonly ILogger<BaseService<T>> _logger;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="repository">基础仓储</param>
/// <param name="logger">日志记录器</param>
public BaseService(IBaseRepository<T> repository, ILogger<BaseService<T>> logger)
{
_repository = repository;
_logger = logger;
}
/// <summary>
/// 根据ID获取实体
/// </summary>
/// <param name="id">实体ID</param>
/// <returns>实体对象</returns>
public async Task<T?> GetByIdAsync(long id)
{
_logger.LogInformation("正在获取实体ID: {Id}", id);
var entity = await _repository.GetByIdAsync(id);
if (entity == null)
{
_logger.LogWarning("未找到实体ID: {Id}", id);
throw new BusinessException($"未找到ID为{id}的记录", 404);
}
return entity;
}
/// <summary>
/// 获取所有实体列表
/// </summary>
/// <returns>实体列表</returns>
public async Task<List<T>> GetListAsync()
{
_logger.LogInformation("正在获取所有实体列表");
return await _repository.GetListAsync();
}
/// <summary>
/// 获取分页列表
/// </summary>
/// <param name="pageQuery">分页查询参数</param>
/// <returns>分页数据</returns>
public async Task<PageListModel<T>> GetPageListAsync(PageQueryModel pageQuery)
{
_logger.LogInformation("正在获取分页列表,页码: {PageIndex}, 每页条数: {PageSize}", pageQuery.PageIndex, pageQuery.PageSize);
if (pageQuery.PageIndex <= 0)
{
throw new BusinessException("页码必须大于0", 400);
}
if (pageQuery.PageSize <= 0 || pageQuery.PageSize > 100)
{
throw new BusinessException("每页条数必须在1-100之间", 400);
}
return await _repository.GetPageListAsync(x => true, pageQuery);
}
/// <summary>
/// 新增实体
/// </summary>
/// <param name="entity">实体对象</param>
/// <returns>是否成功</returns>
public async Task<bool> InsertAsync(T entity)
{
if (entity == null)
{
throw new BusinessException("实体对象不能为空", 400);
}
_logger.LogInformation("正在新增实体,类型: {EntityType}", typeof(T).Name);
SetAuditFieldsOnInsert(entity);
var result = await _repository.InsertAsync(entity);
if (result)
{
_logger.LogInformation("实体新增成功");
}
else
{
_logger.LogError("实体新增失败");
throw new BusinessException("新增记录失败", 500);
}
return result;
}
/// <summary>
/// 更新实体
/// </summary>
/// <param name="entity">实体对象</param>
/// <returns>是否成功</returns>
public async Task<bool> UpdateAsync(T entity)
{
if (entity == null)
{
throw new BusinessException("实体对象不能为空", 400);
}
_logger.LogInformation("正在更新实体,类型: {EntityType}", typeof(T).Name);
SetAuditFieldsOnUpdate(entity);
var result = await _repository.UpdateAsync(entity);
if (result)
{
_logger.LogInformation("实体更新成功");
}
else
{
_logger.LogError("实体更新失败");
throw new BusinessException("更新记录失败", 500);
}
return result;
}
/// <summary>
/// 根据ID删除实体
/// </summary>
/// <param name="id">实体ID</param>
/// <returns>是否成功</returns>
public async Task<bool> DeleteByIdAsync(long id)
{
_logger.LogInformation("正在删除实体ID: {Id}", id);
var entity = await _repository.GetByIdAsync(id);
if (entity == null)
{
_logger.LogWarning("未找到要删除的实体ID: {Id}", id);
throw new BusinessException($"未找到ID为{id}的记录", 404);
}
var result = await _repository.DeleteByIdAsync(id);
if (result)
{
_logger.LogInformation("实体删除成功ID: {Id}", id);
}
else
{
_logger.LogError("实体删除失败ID: {Id}", id);
throw new BusinessException("删除记录失败", 500);
}
return result;
}
/// <summary>
/// 设置插入时的审计字段
/// </summary>
/// <param name="entity">实体对象</param>
private static void SetAuditFieldsOnInsert(T entity)
{
if (entity is BaseEntity baseEntity)
{
var now = DateTime.Now;
baseEntity.CreatedTime = now;
baseEntity.UpdatedTime = now;
baseEntity.CreatedBy = baseEntity.CreatedBy ?? "system";
baseEntity.UpdatedBy = baseEntity.UpdatedBy ?? "system";
baseEntity.IsDeleted = false;
}
}
/// <summary>
/// 设置更新时的审计字段
/// </summary>
/// <param name="entity">实体对象</param>
private static void SetAuditFieldsOnUpdate(T entity)
{
if (entity is BaseEntity baseEntity)
{
baseEntity.UpdatedTime = DateTime.Now;
baseEntity.UpdatedBy = baseEntity.UpdatedBy ?? "system";
}
}
}