DigitalFactory/Admin.NET/Admin.NET.Application/Service/Materials/MaterialsService.cs

160 lines
5.3 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 MaterialsService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<Materials> _rep;
private readonly SysUnitService _repUnit;
private readonly SysUnitGroupService _repUnitGroup;
private readonly PackageInfoService _repPackage;
public MaterialsService(SqlSugarRepository<Materials> rep,
PackageInfoService repPackage,
SysUnitService repUnit,
SysUnitGroupService repUnitGroup)
{
_rep = rep;
_repPackage = repPackage;
_repUnit = repUnit;
_repUnitGroup = repUnitGroup;
}
/// <summary>
/// 分页查询物料
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Page")]
public async Task<SqlSugarPagedList<MaterialsOutput>> Page(MaterialsInput input)
{
var query = _rep.AsQueryable().Where(a => !a.IsDelete)
.WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
u.Name.Contains(input.SearchKey.Trim())
|| u.CodeNum.Contains(input.SearchKey.Trim())
|| u.AliasName.Contains(input.SearchKey.Trim())
|| u.SimpleNumber.Contains(input.SearchKey.Trim())
|| u.Specifications.Contains(input.SearchKey.Trim())
)
.WhereIF(!string.IsNullOrWhiteSpace(input.Name), u => u.Name.Contains(input.Name.Trim()))
.WhereIF(input.Classify>0, u => u.Classify == input.Classify)
.WhereIF(!string.IsNullOrWhiteSpace(input.CodeNum), u => u.CodeNum.Contains(input.CodeNum.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.AliasName), u => u.AliasName.Contains(input.AliasName.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.SimpleNumber), u => u.SimpleNumber.Contains(input.SimpleNumber.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.Specifications), u => u.Specifications.Contains(input.Specifications.Trim()))
.Select<MaterialsOutput>();
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(AddMaterialsInput input)
{
var entity = input.Adapt<Materials>();
await _rep.InsertAsync(entity);
return entity.Id;
}
/// <summary>
/// 删除物料
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Delete")]
public async Task Delete(DeleteMaterialsInput 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(UpdateMaterialsInput input)
{
var entity = input.Adapt<Materials>();
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
/// <summary>
/// 获取物料
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "Detail")]
public async Task<Materials> Detail([FromQuery] QueryByIdMaterialsInput input)
{
//return await _rep.GetFirstAsync(u => u.Id == input.Id);
var model = await _rep.GetFirstAsync(u => u.Id == input.Id);
if (model != null)
{
model.PackageInfos = await GetPackageList(input.Id);
}
return model;
}
/// <summary>
/// 获取物料列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "List")]
public async Task<List<MaterialsOutput>> List()
{
var list = await _rep.AsQueryable().Where(a => !a.IsDelete).Select<MaterialsOutput>().ToListAsync();
list.Reverse();
return list;
}
/// <summary>
/// 获取物料
/// </summary>
/// <param name="materialsId"></param>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "GetById")]
public async Task<Materials> GetById(long? materialsId)
{
var model = await _rep.GetFirstAsync(u => u.Id == materialsId);
if (model!=null)
{
model.PackageInfos = await GetPackageList(materialsId);
}
return model;
}
/// <summary>
/// 获取包装关系列表
/// </summary>
/// <param name="materialsId"></param>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "PackageList")]
public async Task<List<PackageInfoOutput>> GetPackageList(long? materialsId)
{
return await _repPackage.List(materialsId);
}
}