|
|
@@ -156,11 +156,16 @@ public class AdoS0ProductStructuresController : ControllerBase
|
|
|
var fkErr = await ValidateMaterialRefsAsync(dto.CompanyRefId, dto.FactoryRefId, dto.ParentMaterialId, dto.ComponentMaterialId);
|
|
|
if (fkErr != null) return BadRequest(new { message = fkErr });
|
|
|
|
|
|
+ var (parentItem, componentItem, itemErr) = await ResolveItemNumsAsync(dto.ParentMaterialId, dto.ComponentMaterialId);
|
|
|
+ if (itemErr != null) return BadRequest(new { message = itemErr });
|
|
|
+
|
|
|
var db = _masterRep.Context;
|
|
|
await db.Ado.BeginTranAsync();
|
|
|
try
|
|
|
{
|
|
|
var entity = MapMaster(dto);
|
|
|
+ entity.ParentItem = parentItem!;
|
|
|
+ entity.ComponentItem = componentItem!;
|
|
|
entity.CreatedAt = DateTime.Now;
|
|
|
entity = await _masterRep.AsInsertable(entity).ExecuteReturnEntityAsync();
|
|
|
await SyncOpsAsync(entity.Id, dto, entity.ParentMaterialId, entity.ComponentMaterialId);
|
|
|
@@ -186,11 +191,16 @@ public class AdoS0ProductStructuresController : ControllerBase
|
|
|
var fkErr = await ValidateMaterialRefsAsync(dto.CompanyRefId, dto.FactoryRefId, dto.ParentMaterialId, dto.ComponentMaterialId);
|
|
|
if (fkErr != null) return BadRequest(new { message = fkErr });
|
|
|
|
|
|
+ var (parentItem, componentItem, itemErr) = await ResolveItemNumsAsync(dto.ParentMaterialId, dto.ComponentMaterialId);
|
|
|
+ if (itemErr != null) return BadRequest(new { message = itemErr });
|
|
|
+
|
|
|
var db = _masterRep.Context;
|
|
|
await db.Ado.BeginTranAsync();
|
|
|
try
|
|
|
{
|
|
|
ApplyMaster(existing, dto);
|
|
|
+ existing.ParentItem = parentItem!;
|
|
|
+ existing.ComponentItem = componentItem!;
|
|
|
existing.UpdatedAt = DateTime.Now;
|
|
|
await _masterRep.AsUpdateable(existing).ExecuteCommandAsync();
|
|
|
|
|
|
@@ -287,6 +297,28 @@ public class AdoS0ProductStructuresController : ControllerBase
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 解析父/子物料的真实 ItemNum 文本,落 ProductStructureMaster.ParentItem/ComponentItem(NOT NULL)。
|
|
|
+ /// 物料不存在或编码为空时返回业务错误(400),避免插 NULL 导致裸 500。
|
|
|
+ /// </summary>
|
|
|
+ private async Task<(string? ParentItem, string? ComponentItem, string? Error)> ResolveItemNumsAsync(long parentId, long componentId)
|
|
|
+ {
|
|
|
+ var mats = await _materialRep.AsQueryable()
|
|
|
+ .Where(m => m.Id == parentId || m.Id == componentId)
|
|
|
+ .Select(m => new { m.Id, m.ItemNum })
|
|
|
+ .ToListAsync();
|
|
|
+
|
|
|
+ var parentItem = mats.FirstOrDefault(x => x.Id == parentId)?.ItemNum;
|
|
|
+ if (string.IsNullOrWhiteSpace(parentItem))
|
|
|
+ return (null, null, "父项物料不存在或编码为空");
|
|
|
+
|
|
|
+ var componentItem = mats.FirstOrDefault(x => x.Id == componentId)?.ItemNum;
|
|
|
+ if (string.IsNullOrWhiteSpace(componentItem))
|
|
|
+ return (null, null, "子项物料不存在或编码为空");
|
|
|
+
|
|
|
+ return (parentItem, componentItem, null);
|
|
|
+ }
|
|
|
+
|
|
|
private async Task<string?> ValidateMaterialRefsAsync(long companyRefId, long factoryRefId, long parentId, long componentId)
|
|
|
{
|
|
|
var count = await _materialRep.AsQueryable()
|