using Admin.NET.Core.Service;
using Admin.NET.Application.Const;
using Admin.NET.Application.Entity;
using Microsoft.AspNetCore.Http;
namespace Admin.NET.Application;
///
/// 包装关系服务
///
[ApiDescriptionSettings(ApplicationConst.GroupName, Order = 100)]
public class PackageInfoService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _rep;
private readonly SysUnitService _unitService;
public PackageInfoService(SqlSugarRepository rep, SysUnitService unitService)
{
_rep = rep;
_unitService = unitService;
}
///
/// 分页查询包装关系
///
///
///
[HttpPost]
[ApiDescriptionSettings(Name = "Page")]
public async Task> 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();
return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
}
///
/// 增加包装关系
///
///
///
[HttpPost]
[ApiDescriptionSettings(Name = "Add")]
public async Task Add(AddPackageInfoInput input)
{
var entity = input.Adapt();
await _rep.InsertAsync(entity);
return entity.Id;
}
///
/// 增加包装关系
///
///
///
[HttpPost]
[ApiDescriptionSettings(Name = "AddEntity")]
public async Task AddEntity(AddPackageInfoInput input)
{
var entity = input.Adapt();
await _rep.InsertAsync(entity);
return entity;
}
///
/// 删除包装关系
///
///
///
[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); //真删除
}
///
/// 更新包装关系
///
///
///
[HttpPost]
[ApiDescriptionSettings(Name = "Update")]
public async Task Update(UpdatePackageInfoInput input)
{
var entity = input.Adapt();
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
///
/// 获取包装关系
///
///
///
[HttpGet]
[ApiDescriptionSettings(Name = "Detail")]
public async Task Detail([FromQuery] QueryByIdPackageInfoInput input)
{
return await _rep.GetFirstAsync(u => u.Id == input.Id);
}
///
/// 获取包装关系列表
///
///
///
[HttpGet]
[ApiDescriptionSettings(Name = "List")]
public async Task> List(long? materialsId)
{
var list = await _rep.AsQueryable().Where(u =>!u.IsDelete && u.MaterialsId == materialsId)?.Select().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();
var newList = new List();
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().ToListAsync();
}
}
}