using System.Threading.Tasks; using ZhonTai.Admin.Core.Dto; using ZhonTai.Admin.Domain.Dict; using ZhonTai.Admin.Services.Dict.Dto; using ZhonTai.Admin.Domain.Dict.Dto; using ZhonTai.DynamicApi; using ZhonTai.DynamicApi.Attributes; using Microsoft.AspNetCore.Mvc; using ZhonTai.Admin.Core.Consts; using System.Linq; using System.Collections.Generic; using Microsoft.AspNetCore.Authorization; namespace ZhonTai.Admin.Services.Dict; /// /// 数据字典服务 /// [Order(60)] [DynamicApi(Area = AdminConsts.AreaName)] public class DictService : BaseService, IDictService, IDynamicApi { private readonly IDictRepository _dictRepository; public DictService(IDictRepository dictRepository) { _dictRepository = dictRepository; } /// /// 查询 /// /// /// public async Task GetAsync(long id) { var result = await _dictRepository.GetAsync(id); return result; } /// /// 查询分页 /// /// /// [HttpPost] public async Task> GetPageAsync(PageInput input) { var key = input.Filter?.Name; var dictTypeId = input.Filter?.DictTypeId; var list = await _dictRepository.Select .WhereDynamicFilter(input.DynamicFilter) .WhereIf(dictTypeId.HasValue && dictTypeId.Value > 0, a => a.DictTypeId == dictTypeId) .WhereIf(key.NotNull(), a => a.Name.Contains(key) || a.Code.Contains(key)) .Count(out var total) .OrderByDescending(a => a.Sort) .Page(input.CurrentPage, input.PageSize) .ToListAsync(); var data = new PageOutput() { List = list, Total = total }; return data; } /// /// 查询列表 /// /// 编码列表 /// [AllowAnonymous] [HttpPost] public async Task>> GetListAsync(string[] codes) { var list = await _dictRepository.Select .Where(a => codes.Contains(a.DictType.Code) && a.DictType.Enabled == true && a.Enabled == true) .OrderBy(a => a.Sort) .ToListAsync(a => new DictGetListDto { DictTypeCode = a.DictType.Code }); var dicts = new Dictionary>(); foreach (var code in codes) { if (code.NotNull()) dicts[code] = list.Where(a => a.DictTypeCode == code).ToList(); } return dicts; } /// /// 新增 /// /// /// public async Task AddAsync(DictAddInput input) { if (await _dictRepository.Select.AnyAsync(a => a.DictTypeId == input.DictTypeId && a.Name == input.Name)) { throw ResultOutput.Exception($"字典已存在"); } if (input.Code.NotNull() && await _dictRepository.Select.AnyAsync(a => a.DictTypeId == input.DictTypeId && a.Code == input.Code)) { throw ResultOutput.Exception($"字典编码已存在"); } if (input.Code.NotNull() && await _dictRepository.Select.AnyAsync(a => a.DictTypeId == input.DictTypeId && a.Code == input.Code)) { throw ResultOutput.Exception($"字典编码已存在"); } if (input.Value.NotNull() && await _dictRepository.Select.AnyAsync(a => a.DictTypeId == input.DictTypeId && a.Value == input.Value)) { throw ResultOutput.Exception($"字典值已存在"); } var entity = Mapper.Map(input); if (entity.Sort == 0) { var sort = await _dictRepository.Select.Where(a => a.DictTypeId == input.DictTypeId).MaxAsync(a => a.Sort); entity.Sort = sort + 1; } await _dictRepository.InsertAsync(entity); return entity.Id; } /// /// 修改 /// /// /// public async Task UpdateAsync(DictUpdateInput input) { var entity = await _dictRepository.GetAsync(input.Id); if (!(entity?.Id > 0)) { throw ResultOutput.Exception("字典不存在"); } if (await _dictRepository.Select.AnyAsync(a => a.Id != input.Id && a.DictTypeId == input.DictTypeId && a.Name == input.Name)) { throw ResultOutput.Exception($"字典已存在"); } if (input.Code.NotNull() && await _dictRepository.Select.AnyAsync(a => a.Id != input.Id && a.DictTypeId == input.DictTypeId && a.Code == input.Code)) { throw ResultOutput.Exception($"字典编码已存在"); } if (input.Value.NotNull() && await _dictRepository.Select.AnyAsync(a => a.Id != input.Id && a.DictTypeId == input.DictTypeId && a.Value == input.Value)) { throw ResultOutput.Exception($"字典值已存在"); } Mapper.Map(input, entity); await _dictRepository.UpdateAsync(entity); } /// /// 彻底删除 /// /// /// public async Task DeleteAsync(long id) { await _dictRepository.DeleteAsync(m => m.Id == id); } /// /// 批量彻底删除 /// /// /// public async Task BatchDeleteAsync(long[] ids) { await _dictRepository.DeleteAsync(a => ids.Contains(a.Id)); } /// /// 删除 /// /// /// public async Task SoftDeleteAsync(long id) { await _dictRepository.SoftDeleteAsync(id); } /// /// 批量删除 /// /// /// public async Task BatchSoftDeleteAsync(long[] ids) { await _dictRepository.SoftDeleteAsync(ids); } }