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 WmsControlRuleDetailService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _rep;
private readonly SqlSugarRepository _repRuleDetailRep;
private readonly SqlSugarRepository _repSNRep;
private readonly SqlSugarRepository _wmsMaterialRep;
public WmsControlRuleDetailService(SqlSugarRepository rep, SqlSugarRepository repRuleDetailRep, SqlSugarRepository repSNRep, SqlSugarRepository wmsMaterialRep)
{
_rep = rep;
_repRuleDetailRep = repRuleDetailRep;
_repSNRep = repSNRep;
_wmsMaterialRep = wmsMaterialRep;
}
///
/// 分页查询控制属性规则明细
///
///
///
[HttpPost]
[ApiDescriptionSettings(Name = "Page")]
public async Task> Page(WmsControlRuleDetailInput input)
{
var query = _rep.AsQueryable()
.WhereIF(!string.IsNullOrWhiteSpace(input.SearchKey), u =>
u.MaterialCode.Contains(input.SearchKey.Trim())
)
.WhereIF(input.IsDisabled != null, u => u.IsDisabled == input.IsDisabled)
.WhereIF(!string.IsNullOrWhiteSpace(input.MaterialCode), u => u.MaterialCode.Contains(input.MaterialCode.Trim()))
.Select();
return await query.OrderBuilder(input).ToPagedListAsync(input.Page, input.PageSize);
}
///
/// 增加控制属性规则明细
///
///
///
[HttpPost]
[ApiDescriptionSettings(Name = "Add")]
public async Task Add(AddWmsControlRuleDetailInput input)
{
var entity = input.Adapt();
//验重
await CheckExisit(entity);
await _rep.InsertAsync(entity);
return entity.Id;
}
///
/// 删除控制属性规则明细
///
///
///
[HttpPost]
[ApiDescriptionSettings(Name = "Delete")]
public async Task Delete(DeleteWmsControlRuleDetailInput input)
{
var entity = await _rep.GetFirstAsync(u => u.Id == input.Id) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
//物料
var wmsMaterialentity = await _wmsMaterialRep.GetFirstAsync(u => u.ControlRuleId == entity.Id && u.IsDelete == false);
if (wmsMaterialentity != null)
{
throw Oops.Oh($"存在绑定关系,不可删除");
}
//await _rep.FakeDeleteAsync(entity); //假删除
await _rep.DeleteAsync(entity); //真删除
}
///
/// 更新控制属性规则明细
///
///
///
[HttpPost]
[ApiDescriptionSettings(Name = "Update")]
public async Task Update(UpdateWmsControlRuleDetailInput 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] QueryByIdWmsControlRuleDetailInput input)
{
return await _rep.GetFirstAsync(u => u.Id == input.Id);
}
///
/// 获取控制属性规则明细列表
///
///
///
[HttpGet]
[ApiDescriptionSettings(Name = "List")]
public async Task> List([FromQuery] WmsControlRuleDetailInput input)
{
return await _rep.AsQueryable().Select().ToListAsync();
}
#region 私有方法
///
/// 根据联合主键验证数据是否已存在-数据库
///
///
///
///
private async Task CheckExisit(WmsBaseControlRule input, bool isEdit = false)
{
//输出数据已存在错误
ErrorCodeItemMetadataAttribute metadata = ErrorCodeEnum.D1006.GetErrorCodeItemMetadata();
WmsBaseControlRule _existItem = null;
if (!isEdit)//新增
{
_existItem = await _rep.GetFirstAsync(u => u.MaterialCode.Equals(input.MaterialCode));
if (_existItem != null) throw Oops.Oh($"物料[{input.MaterialCode}]控制属性已存在");
}
else//编辑
{
//当前编辑数据以外是否存在重复
_existItem = await _rep.GetFirstAsync(u => u.Id != input.Id && u.MaterialCode.Equals(input.MaterialCode));
if (_existItem != null) throw Oops.Oh($"物料[{input.MaterialCode}]控制属性已存在");
}
}
#endregion
}