// 大名科技(天津)有限公司版权所有 电话:18020030720 QQ:515096995
//
// 此源代码遵循位于源代码树根目录中的 LICENSE 文件的许可证
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)
{
return await _sysDictDataRep.AsQueryable()
.Where(u => u.DictTypeId == input.DictTypeId)
.WhereIF(!string.IsNullOrEmpty(input.Code?.Trim()), u => u.Code.Contains(input.Code))
.WhereIF(!string.IsNullOrEmpty(input.Value?.Trim()), u => u.Value.Contains(input.Value))
.OrderBy(u => new { u.OrderNo, u.Code })
.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 = input.Status;
await _sysDictDataRep.UpdateAsync(dictData);
}
///
/// 根据字典类型Id获取字典值集合
///
///
///
[NonAction]
public async Task> GetDictDataListByDictTypeId(long dictTypeId)
{
return await _sysDictDataRep.AsQueryable()
.Where(u => u.DictTypeId == dictTypeId)
.OrderBy(u => new { u.OrderNo, u.Code })
.ToListAsync();
}
///
/// 根据字典类型编码获取字典值集合
///
///
///
[DisplayName("根据字典类型编码获取字典值集合")]
public async Task> GetDataList(string code)
{
return await _sysDictDataRep.Context.Queryable()
.LeftJoin((u, a) => u.Id == a.DictTypeId)
.Where((u, a) => u.Code == code && u.Status == StatusEnum.Enable && a.Status == StatusEnum.Enable)
.OrderBy((u, a) => new { a.OrderNo, a.Code })
.Select((u, a) => a).ToListAsync();
}
///
/// 根据查询条件获取字典值集合
///
///
///
[DisplayName("根据查询条件获取字典值集合")]
public async Task> GetDataList([FromQuery] QueryDictDataInput input)
{
return await _sysDictDataRep.Context.Queryable()
.LeftJoin((u, a) => u.Id == a.DictTypeId)
.Where((u, a) => u.Code == input.Code)
.WhereIF(input.Status.HasValue, (u, a) => a.Status == (StatusEnum)input.Status.Value)
.OrderBy((u, a) => new { a.OrderNo, a.Code })
.Select((u, a) => a).ToListAsync();
}
///
/// 根据字典类型Id删除字典值
///
///
///
[NonAction]
public async Task DeleteDictData(long dictTypeId)
{
await _sysDictDataRep.DeleteAsync(u => u.DictTypeId == dictTypeId);
}
}