AdoS0LocationShelvesController.cs 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Warehouse;
  5. /// <summary>
  6. /// S0 货架主数据(LocationShelfMaster 语义)。独立货架页只读查询。
  7. /// 货架的新增/修改/删除统一走「库位维护」主从保存(AdoS0LocationsController),本控制器不再暴露写入口。
  8. /// 多租户隔离:所有读取显式限定当前请求租户;不依赖全局 AOP。
  9. /// </summary>
  10. [ApiController]
  11. [Route("api/s0/warehouse/location-shelves")]
  12. [NonUnify]
  13. public class AdoS0LocationShelvesController : ControllerBase
  14. {
  15. private readonly SqlSugarRepository<AdoS0LocationShelfMaster> _rep;
  16. private readonly SqlSugarRepository<AdoS0LocationMaster> _locRep;
  17. public AdoS0LocationShelvesController(
  18. SqlSugarRepository<AdoS0LocationShelfMaster> rep,
  19. SqlSugarRepository<AdoS0LocationMaster> locRep)
  20. {
  21. _rep = rep;
  22. _locRep = locRep;
  23. }
  24. [HttpGet]
  25. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0LocationShelfQueryDto q)
  26. {
  27. if (!AdoS0TenantScope.TryResolveRequired(out var tenantId, out var tenantError)) return tenantError!;
  28. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  29. var query = _rep.AsQueryable()
  30. .Where(x => x.TenantId == tenantId)
  31. .WhereIF(q.CompanyRefId.HasValue, x => x.CompanyRefId == q.CompanyRefId!.Value)
  32. .WhereIF(q.FactoryRefId.HasValue, x => x.FactoryRefId == q.FactoryRefId!.Value)
  33. .WhereIF(!string.IsNullOrWhiteSpace(q.DomainCode), x => x.DomainCode == q.DomainCode)
  34. .WhereIF(!string.IsNullOrWhiteSpace(q.Location), x => x.Location == q.Location)
  35. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword),
  36. x => x.InvShelf.Contains(q.Keyword!) || x.Location.Contains(q.Keyword!));
  37. var total = await query.CountAsync();
  38. var list = await query
  39. .OrderBy(x => x.Location)
  40. .OrderBy(x => x.InvShelf)
  41. .Skip((q.Page - 1) * q.PageSize)
  42. .Take(q.PageSize)
  43. .ToListAsync();
  44. await ApplyLocationDescrAsync(list, tenantId);
  45. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  46. }
  47. [HttpGet("{id:long}")]
  48. public async Task<IActionResult> GetAsync(long id)
  49. {
  50. if (!AdoS0TenantScope.TryResolveRequired(out var tenantId, out var tenantError)) return tenantError!;
  51. var item = await _rep.AsQueryable().Where(x => x.Id == id && x.TenantId == tenantId).FirstAsync();
  52. if (item == null) return NotFound();
  53. await ApplyLocationDescrAsync(new List<AdoS0LocationShelfMaster> { item }, tenantId);
  54. return Ok(item);
  55. }
  56. // 写入口(POST/PUT/DELETE)已下线:S0 货架的新增/修改/删除统一走「库位维护」主从保存
  57. // (AdoS0LocationsController 的 locations 端点,随库位主表 FULL Replace)。本控制器仅保留 GET 查询。
  58. /// <summary>
  59. /// 补显库位说明 + 「库位:货架」拼接。关联 LocationMaster 时限定当前租户,避免同编码库位跨租户补错描述。
  60. /// </summary>
  61. private async Task ApplyLocationDescrAsync(List<AdoS0LocationShelfMaster> shelves, long tenantId)
  62. {
  63. if (shelves.Count == 0) return;
  64. var domainCodes = shelves.Select(s => s.DomainCode).Distinct().ToList();
  65. var locations = await _locRep.AsQueryable()
  66. .Where(l => l.TenantId == tenantId && domainCodes.Contains(l.DomainCode))
  67. .ToListAsync();
  68. foreach (var shelf in shelves)
  69. {
  70. var loc = locations.Find(l =>
  71. string.Equals(l.DomainCode, shelf.DomainCode, StringComparison.OrdinalIgnoreCase)
  72. && string.Equals(l.Location, shelf.Location, StringComparison.OrdinalIgnoreCase));
  73. shelf.LocationDescr = loc?.Descr;
  74. shelf.KwhjName = $"{shelf.Location}:{shelf.InvShelf}";
  75. }
  76. }
  77. }