using YD_Event.Application.Config.Dtos;
namespace YD_Event.Application.Config;
///
/// 自定义配置项
///
public class CustomConfigItemService : BaseService
{
private readonly ISqlSugarRepository _repository;
private readonly IEasyCachingProvider _easyCachingProvider;
public CustomConfigItemService(ISqlSugarRepository repository,
IEasyCachingProvider easyCachingProvider) : base(repository)
{
_repository = repository;
_easyCachingProvider = easyCachingProvider;
}
///
/// 自定义配置项分页列表
///
///
///
[HttpGet]
[DisplayName("自定义配置项分页列表")]
public async Task> Page([FromQuery] CustomConfigItemQueryInput dto)
{
var result = await _repository.AsQueryable().Where(x => x.ConfigId == dto.Id)
.Select(x => new { x.Id, x.Json, x.Status, x.CreatedTime }).ToPagedListAsync(dto);
var list = result.Rows.Select(x =>
{
var o = JObject.Parse(x.Json);
o["__Id"] = x.Id;
o["__Status"] = (int)x.Status;
o["__CreatedTime"] = x.CreatedTime;
return o;
}).ToList();
return new PageResult()
{
Rows = list,
PageNo = result.PageNo,
PageSize = result.PageSize,
Pages = result.Pages,
Total = result.Total
};
}
///
/// 添加自定义配置子项
///
///
///
[DisplayName("添加自定义配置子项")]
[HttpPost("add")]
public async Task AddItem(AddCustomConfigItemInput dto)
{
var item = dto.Adapt();
await _repository.InsertAsync(item);
await ClearCache();
}
///
/// 修改自定义配置子项
///
///
///
[DisplayName("修改自定义配子置项")]
[HttpPut("edit")]
public async Task UpdateItem(UpdateCustomConfigItemInput dto)
{
var item = await _repository.GetByIdAsync(dto.Id);
if (item == null) throw Oops.Bah("无效参数");
dto.Adapt(item);
await _repository.UpdateAsync(item);
await ClearCache();
}
internal override Task ClearCache()
{
return _easyCachingProvider.RemoveByPrefixAsync(CacheConst.ConfigCacheKey);
}
}