DigitalFactory/Admin.NET/Admin.NET.Application/Service/PackageInfo/PackageInfoService.cs

152 lines
6.1 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 PackageInfoService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository<PackageInfo> _rep;
private readonly SysUnitService _unitService;
public PackageInfoService(SqlSugarRepository<PackageInfo> rep, SysUnitService unitService)
{
_rep = rep;
_unitService = unitService;
}
/// <summary>
/// 分页查询包装关系
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Page")]
public async Task<SqlSugarPagedList<PackageInfoOutput>> Page(PackageInfoInput input)
{
var query = _rep.AsQueryable().Where(a => !a.IsDelete)
.WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
u.CodeTypeNum.Contains(input.SearchKey.Trim())
|| u.CodeType.Contains(input.SearchKey.Trim())
|| u.PackageName.Contains(input.SearchKey.Trim())
|| u.ProductCount.Contains(input.SearchKey.Trim())
)
.WhereIF(input.MaterialsId>0, u => u.MaterialsId == input.MaterialsId)
.WhereIF(!string.IsNullOrWhiteSpace(input.CodeTypeNum), u => u.CodeTypeNum.Contains(input.CodeTypeNum.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.CodeType), u => u.CodeType.Contains(input.CodeType.Trim()))
.WhereIF(!string.IsNullOrWhiteSpace(input.PackageName), u => u.PackageName.Contains(input.PackageName.Trim()))
.WhereIF(input.UnitId>0, u => u.UnitId == input.UnitId)
.WhereIF(input.UnitGroupId>0, u => u.UnitGroupId == input.UnitGroupId)
.WhereIF(!string.IsNullOrWhiteSpace(input.ProductCount), u => u.ProductCount.Contains(input.ProductCount.Trim()))
.Select<PackageInfoOutput>();
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(AddPackageInfoInput input)
{
var entity = input.Adapt<PackageInfo>();
await _rep.InsertAsync(entity);
return entity.Id;
}
/// <summary>
/// 增加包装关系
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "AddEntity")]
public async Task<PackageInfo> AddEntity(AddPackageInfoInput input)
{
var entity = input.Adapt<PackageInfo>();
await _rep.InsertAsync(entity);
return entity;
}
/// <summary>
/// 删除包装关系
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
[ApiDescriptionSettings(Name = "Delete")]
public async Task Delete(DeletePackageInfoInput 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(UpdatePackageInfoInput input)
{
var entity = input.Adapt<PackageInfo>();
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
/// <summary>
/// 获取包装关系
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "Detail")]
public async Task<PackageInfo> Detail([FromQuery] QueryByIdPackageInfoInput input)
{
return await _rep.GetFirstAsync(u => u.Id == input.Id);
}
/// <summary>
/// 获取包装关系列表
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpGet]
[ApiDescriptionSettings(Name = "List")]
public async Task<List<PackageInfoOutput>> List(long? materialsId)
{
var list = await _rep.AsQueryable().Where(u =>!u.IsDelete && u.MaterialsId == materialsId)?.Select<PackageInfoOutput>().ToListAsync();
if (list != null && list.Count > 5)
{
var units = await _unitService.List();
foreach (var item in list)
{
item.Unit = units.Find(a => a.Id == item.UnitId)?.Name;
}
return list;
}
else
{
var result = new List<PackageInfo>();
var newList = new List<AddPackageInfoInput>();
newList.Add(new AddPackageInfoInput() { IsEnable = true, CodeTypeNum = "BT30", CodeType = "垛", PackageName = "垛", MaterialsId = materialsId });
newList.Add(new AddPackageInfoInput() { IsEnable = true, CodeTypeNum = "BT25", CodeType = "层", PackageName = "层", MaterialsId = materialsId });
newList.Add(new AddPackageInfoInput() { IsEnable = true, CodeTypeNum = "BT20", CodeType = "箱", PackageName = "箱", MaterialsId = materialsId });
newList.Add(new AddPackageInfoInput() { IsEnable = true, CodeTypeNum = "BT15", CodeType = "套", PackageName = "套", MaterialsId = materialsId });
newList.Add(new AddPackageInfoInput() { IsEnable = true, CodeTypeNum = "BT10", CodeType = "袋", PackageName = "袋", MaterialsId = materialsId });
newList.Add(new AddPackageInfoInput() { IsEnable = true, CodeTypeNum = "BT00", CodeType = "个", PackageName = "个", MaterialsId = materialsId });
foreach (var item in newList)
{
await Add(item);
}
return await _rep.AsQueryable().Where(u => u.MaterialsId == materialsId)?.Select<PackageInfoOutput>().ToListAsync();
}
}
}