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;
using System.Linq;
namespace ZhonTai.Admin.Services.Dictionary;
///
/// 数据字典服务
///
[Order(60)]
[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 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 data;
}
///
/// 新增
///
///
///
public async Task AddAsync(DictionaryAddInput input)
{
if (await _dictionaryRepository.Select.AnyAsync(a => a.DictionaryTypeId == input.DictionaryTypeId && a.Name == input.Name))
{
throw ResultOutput.Exception($"字典已存在");
}
if (input.Code.NotNull() && await _dictionaryRepository.Select.AnyAsync(a => a.DictionaryTypeId == input.DictionaryTypeId && a.Code == input.Code))
{
throw ResultOutput.Exception($"字典编码已存在");
}
if (input.Code.NotNull() && await _dictionaryRepository.Select.AnyAsync(a => a.DictionaryTypeId == input.DictionaryTypeId && a.Code == input.Code))
{
throw ResultOutput.Exception($"字典编码已存在");
}
if (input.Value.NotNull() && await _dictionaryRepository.Select.AnyAsync(a => a.DictionaryTypeId == input.DictionaryTypeId && a.Value == input.Value))
{
throw ResultOutput.Exception($"字典值已存在");
}
var dictionary = Mapper.Map(input);
await _dictionaryRepository.InsertAsync(dictionary);
return dictionary.Id;
}
///
/// 修改
///
///
///
public async Task UpdateAsync(DictionaryUpdateInput input)
{
var entity = await _dictionaryRepository.GetAsync(input.Id);
if (!(entity?.Id > 0))
{
throw ResultOutput.Exception("字典不存在");
}
if (await _dictionaryRepository.Select.AnyAsync(a => a.Id != input.Id && a.DictionaryTypeId == input.DictionaryTypeId && a.Name == input.Name))
{
throw ResultOutput.Exception($"字典已存在");
}
if (input.Code.NotNull() && await _dictionaryRepository.Select.AnyAsync(a => a.Id != input.Id && a.DictionaryTypeId == input.DictionaryTypeId && a.Code == input.Code))
{
throw ResultOutput.Exception($"字典编码已存在");
}
if (input.Value.NotNull() && await _dictionaryRepository.Select.AnyAsync(a => a.Id != input.Id && a.DictionaryTypeId == input.DictionaryTypeId && a.Value == input.Value))
{
throw ResultOutput.Exception($"字典值已存在");
}
Mapper.Map(input, entity);
await _dictionaryRepository.UpdateAsync(entity);
}
///
/// 彻底删除
///
///
///
public async Task DeleteAsync(long id)
{
await _dictionaryRepository.DeleteAsync(m => m.Id == id);
}
///
/// 批量彻底删除
///
///
///
public async Task BatchDeleteAsync(long[] ids)
{
await _dictionaryRepository.DeleteAsync(a => ids.Contains(a.Id));
}
///
/// 删除
///
///
///
public async Task SoftDeleteAsync(long id)
{
await _dictionaryRepository.SoftDeleteAsync(id);
}
///
/// 批量删除
///
///
///
public async Task BatchSoftDeleteAsync(long[] ids)
{
await _dictionaryRepository.SoftDeleteAsync(ids);
}
}