namespace Admin.NET.Core.Service; /// /// 系统字典值服务 /// [ApiDescriptionSettings(Order = 420)] [AllowAnonymous] public class SysDictDataService : IDynamicApiController, ITransient { private readonly SqlSugarRepository _sysDictDataRep; public SysDictDataService(SqlSugarRepository sysDictDataRep) { _sysDictDataRep = sysDictDataRep; } /// /// 获取字典值分页列表 /// /// /// [DisplayName("获取字典值分页列表")] public async Task> Page(PageDictDataInput input) { var code = !string.IsNullOrEmpty(input.Code?.Trim()); var value = !string.IsNullOrEmpty(input.Value?.Trim()); return await _sysDictDataRep.AsQueryable() .Where(u => u.DictTypeId == input.DictTypeId) .WhereIF(code, u => u.Code.Contains(input.Code)) .WhereIF(value, u => u.Value.Contains(input.Value)) .OrderBy(u => u.OrderNo) .ToPagedListAsync(input.Page, input.PageSize); } /// /// 获取字典值列表 /// /// [DisplayName("获取字典值列表")] public async Task> GetList([FromQuery] GetDataDictDataInput input) { return await GetDictDataListByDictTypeId(input.DictTypeId); } /// /// 增加字典值 /// /// /// [ApiDescriptionSettings(Name = "Add"), HttpPost] [DisplayName("增加字典值")] public async Task AddDictData(AddDictDataInput input) { var isExist = await _sysDictDataRep.IsAnyAsync(u => u.Code == input.Code && u.DictTypeId == input.DictTypeId); if (isExist) throw Oops.Oh(ErrorCodeEnum.D3003); await _sysDictDataRep.InsertAsync(input.Adapt()); } /// /// 更新字典值 /// /// /// [ApiDescriptionSettings(Name = "Update"), HttpPost] [DisplayName("更新字典值")] public async Task UpdateDictData(UpdateDictDataInput input) { var isExist = await _sysDictDataRep.IsAnyAsync(u => u.Id == input.Id); if (!isExist) throw Oops.Oh(ErrorCodeEnum.D3004); isExist = await _sysDictDataRep.IsAnyAsync(u => u.Code == input.Code && u.DictTypeId == input.DictTypeId && u.Id != input.Id); if (isExist) throw Oops.Oh(ErrorCodeEnum.D3003); await _sysDictDataRep.UpdateAsync(input.Adapt()); } /// /// 删除字典值 /// /// /// [ApiDescriptionSettings(Name = "Delete"), HttpPost] [DisplayName("删除字典值")] public async Task DeleteDictData(DeleteDictDataInput input) { var dictData = await _sysDictDataRep.GetFirstAsync(u => u.Id == input.Id); if (dictData == null) throw Oops.Oh(ErrorCodeEnum.D3004); await _sysDictDataRep.DeleteAsync(dictData); } /// /// 获取字典值详情 /// /// /// [DisplayName("获取字典值详情")] public async Task GetDetail([FromQuery] DictDataInput input) { return await _sysDictDataRep.GetFirstAsync(u => u.Id == input.Id); } /// /// 修改字典值状态 /// /// /// [DisplayName("修改字典值状态")] public async Task SetStatus(DictDataInput input) { var dictData = await _sysDictDataRep.GetFirstAsync(u => u.Id == input.Id); if (dictData == null) throw Oops.Oh(ErrorCodeEnum.D3004); if (!Enum.IsDefined(typeof(StatusEnum), input.Status)) throw Oops.Oh(ErrorCodeEnum.D3005); dictData.Status = (StatusEnum)input.Status; await _sysDictDataRep.UpdateAsync(dictData); } /// /// 根据字典类型Id获取字典值集合 /// /// /// [ApiDescriptionSettings(false)] public async Task> GetDictDataListByDictTypeId(long dictTypeId) { return await _sysDictDataRep.AsQueryable() .Where(u => u.DictTypeId == dictTypeId) .OrderBy(u => u.OrderNo).ToListAsync(); } /// /// 根据字典类型编码获取字典值集合 /// /// /// [DisplayName("根据字典类型编码获取字典值集合")] public async Task> GetDataList(string code) { return await _sysDictDataRep.Context.Queryable() .LeftJoin((a, b) => a.Id == b.DictTypeId) .Where((a, b) => a.Code == code && a.Status == StatusEnum.Enable && b.Status == StatusEnum.Enable) .Select((a, b) => b).ToListAsync(); } /// /// 根据查询条件获取字典值集合 /// /// /// [DisplayName("根据查询条件获取字典值集合")] public async Task> GetDataList([FromQuery] QueryDictDataInput input) { return await _sysDictDataRep.Context.Queryable() .LeftJoin((a, b) => a.Id == b.DictTypeId) .Where((a, b) => a.Code == input.Code) .WhereIF(input.Status.HasValue, (a, b) => b.Status == (StatusEnum)input.Status.Value) .Select((a, b) => b).ToListAsync(); } /// /// 根据字典类型Id删除字典值 /// /// /// [ApiDescriptionSettings(false)] public async Task DeleteDictData(long dictTypeId) { await _sysDictDataRep.DeleteAsync(u => u.DictTypeId == dictTypeId); } }