AdoS0LocationShelvesController.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using Admin.NET.Plugin.AiDOP.Dto.S0.Warehouse;
  2. using Admin.NET.Plugin.AiDOP.Entity.S0.Warehouse;
  3. using Admin.NET.Plugin.AiDOP.Infrastructure;
  4. using Microsoft.Extensions.Logging;
  5. namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Warehouse;
  6. /// <summary>
  7. /// S0 货架主数据(LocationShelfMaster 语义)
  8. /// </summary>
  9. [ApiController]
  10. [Route("api/s0/warehouse/location-shelves")]
  11. [AllowAnonymous]
  12. [NonUnify]
  13. public class AdoS0LocationShelvesController : ControllerBase
  14. {
  15. private readonly SqlSugarRepository<AdoS0LocationShelfMaster> _rep;
  16. private readonly SqlSugarRepository<AdoS0LocationMaster> _locRep;
  17. private readonly AdoS0ReferenceChecker _refChecker;
  18. private readonly ILogger<AdoS0LocationShelvesController> _logger;
  19. public AdoS0LocationShelvesController(
  20. SqlSugarRepository<AdoS0LocationShelfMaster> rep,
  21. SqlSugarRepository<AdoS0LocationMaster> locRep,
  22. AdoS0ReferenceChecker refChecker,
  23. ILogger<AdoS0LocationShelvesController> logger)
  24. {
  25. _rep = rep;
  26. _locRep = locRep;
  27. _refChecker = refChecker;
  28. _logger = logger;
  29. }
  30. [HttpGet]
  31. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0LocationShelfQueryDto q)
  32. {
  33. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  34. var query = _rep.AsQueryable()
  35. .WhereIF(q.CompanyRefId.HasValue, x => x.CompanyRefId == q.CompanyRefId!.Value)
  36. .WhereIF(q.FactoryRefId.HasValue, x => x.FactoryRefId == q.FactoryRefId!.Value)
  37. .WhereIF(!string.IsNullOrWhiteSpace(q.DomainCode), x => x.DomainCode == q.DomainCode)
  38. .WhereIF(!string.IsNullOrWhiteSpace(q.Location), x => x.Location == q.Location)
  39. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword),
  40. x => x.InvShelf.Contains(q.Keyword!) || x.Location.Contains(q.Keyword!));
  41. var total = await query.CountAsync();
  42. var list = await query
  43. .OrderBy(x => x.Location)
  44. .OrderBy(x => x.InvShelf)
  45. .Skip((q.Page - 1) * q.PageSize)
  46. .Take(q.PageSize)
  47. .ToListAsync();
  48. await ApplyLocationDescrAsync(list);
  49. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  50. }
  51. [HttpGet("{id:long}")]
  52. public async Task<IActionResult> GetAsync(long id)
  53. {
  54. var item = await _rep.GetByIdAsync(id);
  55. if (item == null) return NotFound();
  56. await ApplyLocationDescrAsync(new List<AdoS0LocationShelfMaster> { item });
  57. return Ok(item);
  58. }
  59. [HttpPost]
  60. public async Task<IActionResult> CreateAsync([FromBody] AdoS0LocationShelfUpsertDto dto)
  61. {
  62. if (await _rep.IsAnyAsync(x => x.FactoryRefId == dto.FactoryRefId && x.Location == dto.Location && x.InvShelf == dto.InvShelf))
  63. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.DuplicateCode, "货架编码已存在");
  64. // B2:Create 一律严格作用域校验,禁止降级(参 AdoS0MfgRoutingOpDetailsController)。
  65. var locResult = await _refChecker.LocationExistsInScopeAsync(dto.Location, dto.CompanyRefId, dto.FactoryRefId);
  66. switch (locResult)
  67. {
  68. case MaterialScopeCheck.NotFound:
  69. return AdoS0ApiErrors.InvalidReference(AdoS0ErrorCodes.ReferenceNotFound,
  70. $"库位编码 '{dto.Location}' 不存在于库位主数据");
  71. case MaterialScopeCheck.ScopeMiss:
  72. return AdoS0ApiErrors.InvalidReference(AdoS0ErrorCodes.InvalidReferenceScope,
  73. $"库位编码 '{dto.Location}' 不属于当前公司/工厂 (CompanyRefId={dto.CompanyRefId}, FactoryRefId={dto.FactoryRefId})");
  74. }
  75. var entity = new AdoS0LocationShelfMaster
  76. {
  77. CompanyRefId = dto.CompanyRefId,
  78. FactoryRefId = dto.FactoryRefId,
  79. DomainCode = dto.DomainCode ?? string.Empty,
  80. Location = dto.Location,
  81. InvShelf = dto.InvShelf,
  82. Descr = dto.Descr,
  83. Area = dto.Area,
  84. CreateUser = dto.CreateUser,
  85. CreateTime = DateTime.Now
  86. };
  87. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  88. return Ok(entity);
  89. }
  90. [HttpPut("{id:long}")]
  91. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0LocationShelfUpsertDto dto)
  92. {
  93. var entity = await _rep.GetByIdAsync(id);
  94. if (entity == null) return NotFound();
  95. if (await _rep.IsAnyAsync(x => x.Id != id && x.FactoryRefId == dto.FactoryRefId && x.Location == dto.Location && x.InvShelf == dto.InvShelf))
  96. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.DuplicateCode, "货架编码已存在");
  97. // B2 + 历史兼容降级(D-03 严格条件,参 AdoS0MfgRoutingOpDetailsController)
  98. var locResult = await _refChecker.LocationExistsInScopeAsync(dto.Location, dto.CompanyRefId, dto.FactoryRefId);
  99. if (locResult == MaterialScopeCheck.NotFound)
  100. return AdoS0ApiErrors.InvalidReference(AdoS0ErrorCodes.ReferenceNotFound,
  101. $"库位编码 '{dto.Location}' 不存在于库位主数据");
  102. if (locResult == MaterialScopeCheck.ScopeMiss)
  103. {
  104. // 降级条件(全部满足才允许):
  105. // 1. 是 Update(已满足)
  106. // 2. DB 原记录 scope 为 0/0
  107. // 3. 本次未修改 Location 引用值
  108. var originScopeEmpty = entity.CompanyRefId == 0 && entity.FactoryRefId == 0;
  109. var locationUnchanged = string.Equals(entity.Location, dto.Location?.Trim(), StringComparison.Ordinal);
  110. if (originScopeEmpty && locationUnchanged)
  111. {
  112. _logger.LogWarning(
  113. "[B2 Downgrade] Table={TableName} PrimaryKey={PrimaryKey} ReferenceField={ReferenceField} " +
  114. "OldValue={OldValue} NewValue={NewValue} CompanyRefId={CompanyRefId} FactoryRefId={FactoryRefId} " +
  115. "DowngradeReason={DowngradeReason}",
  116. "LocationShelfMaster", entity.Id, "Location",
  117. entity.Location, dto.Location,
  118. dto.CompanyRefId, dto.FactoryRefId,
  119. "LegacyRecord_OriginScopeZero_LocationUnchanged");
  120. // 放行,继续执行写入
  121. }
  122. else
  123. {
  124. return AdoS0ApiErrors.InvalidReference(AdoS0ErrorCodes.InvalidReferenceScope,
  125. $"库位编码 '{dto.Location}' 不属于当前公司/工厂 (CompanyRefId={dto.CompanyRefId}, FactoryRefId={dto.FactoryRefId})");
  126. }
  127. }
  128. entity.CompanyRefId = dto.CompanyRefId;
  129. entity.FactoryRefId = dto.FactoryRefId;
  130. entity.DomainCode = dto.DomainCode ?? string.Empty;
  131. entity.Location = dto.Location;
  132. entity.InvShelf = dto.InvShelf;
  133. entity.Descr = dto.Descr;
  134. entity.Area = dto.Area;
  135. entity.UpdateUser = dto.UpdateUser;
  136. entity.UpdateTime = DateTime.Now;
  137. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  138. return Ok(entity);
  139. }
  140. [HttpDelete("{id:long}")]
  141. public async Task<IActionResult> DeleteAsync(long id)
  142. {
  143. var item = await _rep.GetByIdAsync(id);
  144. if (item == null) return NotFound();
  145. await _rep.DeleteAsync(item);
  146. return Ok(new { message = "删除成功" });
  147. }
  148. private async Task ApplyLocationDescrAsync(List<AdoS0LocationShelfMaster> shelves)
  149. {
  150. if (shelves.Count == 0) return;
  151. var domainCodes = shelves.Select(s => s.DomainCode).Distinct().ToList();
  152. var locations = await _locRep.AsQueryable()
  153. .Where(l => domainCodes.Contains(l.DomainCode))
  154. .ToListAsync();
  155. foreach (var shelf in shelves)
  156. {
  157. var loc = locations.Find(l =>
  158. string.Equals(l.DomainCode, shelf.DomainCode, StringComparison.OrdinalIgnoreCase)
  159. && string.Equals(l.Location, shelf.Location, StringComparison.OrdinalIgnoreCase));
  160. shelf.LocationDescr = loc?.Descr;
  161. shelf.KwhjName = $"{shelf.Location}:{shelf.InvShelf}";
  162. }
  163. }
  164. }