DigitalFactory/Admin.NET/Admin.NET.Application/Service/CodeElement/CodeElementService.cs

132 lines
4.3 KiB
C#
Raw Normal View History

2024-05-24 10:22:01 +00:00
using Admin.NET.Core.Service;
using Admin.NET.Application.Const;
using Admin.NET.Application.Entity;
using Microsoft.AspNetCore.Http;
namespace Admin.NET.Application;
/// <summary>
/// 码元素服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class CodeElementService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<CodeElement> _rep;
public CodeElementService(SqlSugarRepository<CodeElement> rep)
{
_rep = rep;
}
/// <summary>
/// 分页查询码元素
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Page")]
public async Task<SqlSugarPagedList<CodeElementOutput>> Page(CodeElementInput input)
{
var query = _rep.AsQueryable()
.WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
u.CodeName.Contains(input.SearchKey.Trim())
|| u.ExportFormatExample.Contains(input.SearchKey.Trim())
)
.WhereIF(!string.IsNullOrWhiteSpace(input.CodeName), u => u.CodeName.Contains(input.CodeName.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.ExportFormatExample), u => u.ExportFormatExample.Contains(input.ExportFormatExample.Trim()))
.Select<CodeElementOutput>();
return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
}
/// <summary>
/// 增加码元素
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Add")]
public async Task<long> Add(AddCodeElementInput input)
{
var entity = input.Adapt<CodeElement>();
await _rep.InsertAsync(entity);
return entity.Id;
}
/// <summary>
/// 删除码元素
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Delete")]
public async Task Delete(DeleteCodeElementInput input)
{
var entity = await _rep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
//var entity = await _rep.GetFirstAsync(u => u.CodeLength == input.CodeLength) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
//await _rep.FakeDeleteAsync(entity); //假删除
await _rep.DeleteAsync(entity); //真删除
}
/// <summary>
/// 更新码元素
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Update")]
public async Task Update(UpdateCodeElementInput input)
{
var entity = input.Adapt<CodeElement>();
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
/// <summary>
/// 获取码元素
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "Detail")]
public async Task<CodeElement> Detail([FromQuery] QueryByIdCodeElementInput input)
{
return await _rep.GetFirstAsync(u => u.Id == input.Id);
//return await _rep.GetFirstAsync(u => u.CodeLength == input.CodeLength);
}
/// <summary>
/// 获取码元素
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "ElementByName")]
2024-06-24 10:21:52 +00:00
public async Task<CodeElement> GetElementByName([FromQuery] string input,long? unitGroupId)
2024-05-24 10:22:01 +00:00
{
2024-06-24 10:21:52 +00:00
return await _rep.GetFirstAsync(u => u.UnitGroupId == unitGroupId && u.CodeName == input);
2024-05-24 10:22:01 +00:00
}
/// <summary>
/// 获取码元素列表
/// </summary>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "List")]
public async Task<List<CodeElementOutput>> List()
{
return await _rep.AsQueryable().Where(a => !a.IsDelete).Select<CodeElementOutput>().ToListAsync();
}
2024-06-24 10:21:52 +00:00
/// <summary>
/// 获取码包配置列表
/// </summary>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "ListByGroupId")]
public async Task<List<CodeElementOutput>> ListByGroupId(long? unitGroupId)
{
var list = await _rep.AsQueryable().Where(a => a.UnitGroupId == unitGroupId && !a.IsDelete).Select<CodeElementOutput>().ToListAsync();
return list;
}
2024-05-24 10:22:01 +00:00
}