using System.Threading.Tasks;
using ZhonTai.Admin.Core.Dto;
using ZhonTai.Admin.Domain.Dictionary;
using ZhonTai.Admin.Services.Dictionary.Dto;
using ZhonTai.Admin.Domain.Dictionary.Dto;
using ZhonTai.DynamicApi;
using ZhonTai.DynamicApi.Attributes;
using Microsoft.AspNetCore.Mvc;
using ZhonTai.Admin.Core.Consts;
namespace ZhonTai.Admin.Services.Dictionary
{
///
/// 数据字典服务
///
[DynamicApi(Area = AdminConsts.AreaName)]
public class DictionaryService : BaseService, IDictionaryService, IDynamicApi
{
private readonly IDictionaryRepository _dictionaryRepository;
public DictionaryService(IDictionaryRepository dictionaryRepository)
{
_dictionaryRepository = dictionaryRepository;
}
///
/// 查询数据字典
///
///
///
public async Task GetAsync(long id)
{
var result = await _dictionaryRepository.GetAsync(id);
return ResultOutput.Ok(result);
}
///
/// 查询数据字典列表
///
///
///
[HttpPost]
public async Task GetPageAsync(PageInput input)
{
var key = input.Filter?.Name;
var dictionaryTypeId = input.Filter?.DictionaryTypeId;
var list = await _dictionaryRepository.Select
.WhereDynamicFilter(input.DynamicFilter)
.WhereIf(dictionaryTypeId.HasValue && dictionaryTypeId.Value > 0, a => a.DictionaryTypeId == dictionaryTypeId)
.WhereIf(key.NotNull(), a => a.Name.Contains(key) || a.Code.Contains(key))
.Count(out var total)
.OrderByDescending(true, c => c.Id)
.Page(input.CurrentPage, input.PageSize)
.ToListAsync();
var data = new PageOutput()
{
List = list,
Total = total
};
return ResultOutput.Ok(data);
}
///
/// 新增
///
///
///
public async Task AddAsync(DictionaryAddInput input)
{
var dictionary = Mapper.Map(input);
var id = (await _dictionaryRepository.InsertAsync(dictionary)).Id;
return ResultOutput.Result(id > 0);
}
///
/// 修改
///
///
///
public async Task UpdateAsync(DictionaryUpdateInput input)
{
if (!(input?.Id > 0))
{
return ResultOutput.NotOk();
}
var entity = await _dictionaryRepository.GetAsync(input.Id);
if (!(entity?.Id > 0))
{
return ResultOutput.NotOk("数据字典不存在!");
}
Mapper.Map(input, entity);
await _dictionaryRepository.UpdateAsync(entity);
return ResultOutput.Ok();
}
///
/// 彻底删除
///
///
///
public async Task DeleteAsync(long id)
{
var result = false;
if (id > 0)
{
result = (await _dictionaryRepository.DeleteAsync(m => m.Id == id)) > 0;
}
return ResultOutput.Result(result);
}
///
/// 删除
///
///
///
public async Task SoftDeleteAsync(long id)
{
var result = await _dictionaryRepository.SoftDeleteAsync(id);
return ResultOutput.Result(result);
}
///
/// 批量删除
///
///
///
public async Task BatchSoftDeleteAsync(long[] ids)
{
var result = await _dictionaryRepository.SoftDeleteAsync(ids);
return ResultOutput.Result(result);
}
}
}