DigitalFactory/Admin.NET/Admin.NET.Application/Service/MaterialList/MaterialListService.cs

121 lines
3.9 KiB
C#

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 MaterialListService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<MaterialList> _rep;
public MaterialListService(SqlSugarRepository<MaterialList> rep)
{
_rep = rep;
}
/// <summary>
/// 分页查询物料列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Page")]
public async Task<SqlSugarPagedList<MaterialListOutput>> Page(MaterialListInput input)
{
var query = _rep.AsQueryable()
.WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
u.Unit.Contains(input.SearchKey.Trim())
|| u.Remarks.Contains(input.SearchKey.Trim())
)
.WhereIF(input.MaterialsId>0, u => u.MaterialsId == input.MaterialsId)
.WhereIF(!string.IsNullOrWhiteSpace(input.Unit), u => u.Unit.Contains(input.Unit.Trim()))
.WhereIF(input.Count>0, u => u.Count == input.Count)
.WhereIF(input.SourceId>0, u => u.SourceId == input.SourceId)
.WhereIF(!string.IsNullOrWhiteSpace(input.Remarks), u => u.Remarks.Contains(input.Remarks.Trim()))
.Select<MaterialListOutput>();
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(AddMaterialListInput input)
{
var entity = input.Adapt<MaterialList>();
await _rep.InsertAsync(entity);
return entity.Id;
}
/// <summary>
/// 删除物料列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Delete")]
public async Task Delete(DeleteMaterialListInput input)
{
var entity = await _rep.GetFirstAsync(u => u.Id == input.Id) ?? 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(UpdateMaterialListInput input)
{
var entity = input.Adapt<MaterialList>();
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
/// <summary>
/// 获取物料列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "Detail")]
public async Task<MaterialList> Detail([FromQuery] QueryByIdMaterialListInput input)
{
return await _rep.GetFirstAsync(u => u.Id == input.Id);
}
/// <summary>
/// 获取物料列表列表
/// </summary>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "List")]
public async Task<List<MaterialListOutput>> List()
{
return await _rep.AsQueryable().Select<MaterialListOutput>().ToListAsync();
}
/// <summary>
/// 获取物料列表列表
/// </summary>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "ListBySourceId")]
public async Task<List<MaterialListOutput>> ListBySourceId(long? sourceId)
{
return await _rep.AsQueryable().WhereIF(sourceId > 0, u => u.SourceId == sourceId).Select<MaterialListOutput>().ToListAsync();
}
}