You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
177 lines
6.1 KiB
177 lines
6.1 KiB
namespace Admin.NET.Core.Service;
|
|
|
|
/// <summary>
|
|
/// 系统字典值服务
|
|
/// </summary>
|
|
[ApiDescriptionSettings(Order = 420)]
|
|
[AllowAnonymous]
|
|
public class SysDictDataService : IDynamicApiController, ITransient
|
|
{
|
|
private readonly SqlSugarRepository<SysDictData> _sysDictDataRep;
|
|
|
|
public SysDictDataService(SqlSugarRepository<SysDictData> sysDictDataRep)
|
|
{
|
|
_sysDictDataRep = sysDictDataRep;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取字典值分页列表
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
[DisplayName("获取字典值分页列表")]
|
|
public async Task<SqlSugarPagedList<SysDictData>> 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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取字典值列表
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[DisplayName("获取字典值列表")]
|
|
public async Task<List<SysDictData>> GetList([FromQuery] GetDataDictDataInput input)
|
|
{
|
|
return await GetDictDataListByDictTypeId(input.DictTypeId);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 增加字典值
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
[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<SysDictData>());
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新字典值
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
[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<SysDictData>());
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除字典值
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
[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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取字典值详情
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
[DisplayName("获取字典值详情")]
|
|
public async Task<SysDictData> GetDetail([FromQuery] DictDataInput input)
|
|
{
|
|
return await _sysDictDataRep.GetFirstAsync(u => u.Id == input.Id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 修改字典值状态
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
[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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据字典类型Id获取字典值集合
|
|
/// </summary>
|
|
/// <param name="dictTypeId"></param>
|
|
/// <returns></returns>
|
|
[ApiDescriptionSettings(false)]
|
|
public async Task<List<SysDictData>> GetDictDataListByDictTypeId(long dictTypeId)
|
|
{
|
|
return await _sysDictDataRep.AsQueryable()
|
|
.Where(u => u.DictTypeId == dictTypeId)
|
|
.OrderBy(u => u.OrderNo).ToListAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据字典类型编码获取字典值集合
|
|
/// </summary>
|
|
/// <param name="code"></param>
|
|
/// <returns></returns>
|
|
[DisplayName("根据字典类型编码获取字典值集合")]
|
|
public async Task<List<SysDictData>> GetDataList(string code)
|
|
{
|
|
return await _sysDictDataRep.Context.Queryable<SysDictType>()
|
|
.LeftJoin<SysDictData>((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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据查询条件获取字典值集合
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
[DisplayName("根据查询条件获取字典值集合")]
|
|
public async Task<List<SysDictData>> GetDataList([FromQuery] QueryDictDataInput input)
|
|
{
|
|
return await _sysDictDataRep.Context.Queryable<SysDictType>()
|
|
.LeftJoin<SysDictData>((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();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据字典类型Id删除字典值
|
|
/// </summary>
|
|
/// <param name="dictTypeId"></param>
|
|
/// <returns></returns>
|
|
[ApiDescriptionSettings(false)]
|
|
public async Task DeleteDictData(long dictTypeId)
|
|
{
|
|
await _sysDictDataRep.DeleteAsync(u => u.DictTypeId == dictTypeId);
|
|
}
|
|
}
|