using Admin.NET.Core.Service;
using Admin.NET.Application.Entity;
using Microsoft.AspNetCore.Http;
namespace Admin.NET.Application;
///
/// 容器关系基础表服务
///
[ApiDescriptionSettings(ApplicationConst.WmsBaseGroupName, Order = 100)]
public class WmsContainerPackagingService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _rep;
public WmsContainerPackagingService(SqlSugarRepository rep)
{
_rep = rep;
}
///
/// 分页查询容器关系基础表
///
///
///
[HttpPost]
[ApiDescriptionSettings(Name = "Page")]
public async Task> Page(WmsContainerPackagingInput input)
{
var query = _rep.AsQueryable()
.WhereIF(input.ContainerTypeId>0, u => u.ContainerTypeId == input.ContainerTypeId)
.WhereIF(input.MaterialTypeId>0, u => u.MaterialTypeId == input.MaterialTypeId)
.Select();
return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
}
///
/// 增加容器关系基础表
///
///
///
[HttpPost]
[ApiDescriptionSettings(Name = "Add")]
public async Task Add(AddWmsContainerPackagingInput input)
{
var entity = input.Adapt();
//验重
await CheckExisit(entity);
await _rep.InsertAsync(entity);
return entity.Id;
}
///
/// 删除容器关系基础表
///
///
///
[HttpPost]
[ApiDescriptionSettings(Name = "Delete")]
public async Task Delete(DeleteWmsContainerPackagingInput 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(UpdateWmsContainerPackagingInput input)
{
var entity = input.Adapt();
//验重
await CheckExisit(entity, true);
await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
}
///
/// 获取容器关系基础表
///
///
///
[HttpGet]
[ApiDescriptionSettings(Name = "Detail")]
public async Task Detail([FromQuery] QueryByIdWmsContainerPackagingInput input)
{
return await _rep.GetFirstAsync(u => u.Id == input.Id);
}
///
/// 获取容器关系基础表列表
///
///
///
[HttpGet]
[ApiDescriptionSettings(Name = "List")]
public async Task> List([FromQuery] WmsContainerPackagingInput input)
{
return await _rep.AsQueryable().Select().ToListAsync();
}
#region 私有方法
///
/// 根据联合主键验证数据是否已存在-数据库
///
///
///
///
private async Task CheckExisit(WmsBaseContainerPackaging input, bool isEdit = false)
{
//输出数据已存在错误
ErrorCodeItemMetadataAttribute metadata = ErrorCodeEnum.D1006.GetErrorCodeItemMetadata();
WmsBaseContainerPackaging _existItem = null
;
if (!isEdit)//新增
{
_existItem = await _rep.GetFirstAsync(u => u.MaterialId.Equals(input.MaterialId) && u.ContainerTypeId.Equals(input.ContainerTypeId));
if (_existItem != null) throw Oops.Oh($"物料编号[{input.MaterialCode}]容器类型[{input.ContainerTypeName}]已关联{metadata.ErrorMessage}");
}
else//编辑
{
//当前编辑数据以外是否存在重复
_existItem = await _rep.GetFirstAsync(u => u.Id != input.Id && u.MaterialId.Equals(input.MaterialId) && u.ContainerTypeId.Equals(input.ContainerTypeId));
if (_existItem != null) throw Oops.Oh($"物料编号[{input.MaterialCode}]容器类型[{input.ContainerTypeName}]已关联{metadata.ErrorMessage}");
}
}
#endregion
}