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;
///
/// 基础服务实现
///
/// 实体类型
public class BaseService : IBaseService where T : class, new()
{
protected readonly IBaseRepository _repository;
protected readonly ILogger> _logger;
///
/// 构造函数
///
/// 基础仓储
/// 日志记录器
public BaseService(IBaseRepository repository, ILogger> logger)
{
_repository = repository;
_logger = logger;
}
///
/// 根据ID获取实体
///
/// 实体ID
/// 实体对象
public async Task 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;
}
///
/// 获取所有实体列表
///
/// 实体列表
public async Task> GetListAsync()
{
_logger.LogInformation("正在获取所有实体列表");
return await _repository.GetListAsync();
}
///
/// 获取分页列表
///
/// 分页查询参数
/// 分页数据
public async Task> 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);
}
///
/// 新增实体
///
/// 实体对象
/// 是否成功
public async Task 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;
}
///
/// 更新实体
///
/// 实体对象
/// 是否成功
public async Task 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;
}
///
/// 根据ID删除实体
///
/// 实体ID
/// 是否成功
public async Task 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;
}
///
/// 设置插入时的审计字段
///
/// 实体对象
private static void SetAuditFieldsOnInsert(T entity)
{
if (entity is BaseEntity baseEntity)
{
var now = DateTime.Now;
baseEntity.CreatedAt = now;
baseEntity.UpdatedAt = now;
baseEntity.CreatedBy = baseEntity.CreatedBy ?? "system";
baseEntity.UpdatedBy = baseEntity.UpdatedBy ?? "system";
baseEntity.IsDeleted = false;
}
}
private static void SetAuditFieldsOnUpdate(T entity)
{
if (entity is BaseEntity baseEntity)
{
baseEntity.UpdatedAt = DateTime.Now;
baseEntity.UpdatedBy = baseEntity.UpdatedBy ?? "system";
}
}
}