AdoS0LocationShelvesController.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. /// </summary>
  8. [ApiController]
  9. [Route("api/s0/warehouse/location-shelves")]
  10. [AllowAnonymous]
  11. [NonUnify]
  12. public class AdoS0LocationShelvesController : ControllerBase
  13. {
  14. private readonly SqlSugarRepository<AdoS0LocationShelfMaster> _rep;
  15. private readonly SqlSugarRepository<AdoS0LocationMaster> _locRep;
  16. private readonly AdoS0ReferenceChecker _refChecker;
  17. public AdoS0LocationShelvesController(
  18. SqlSugarRepository<AdoS0LocationShelfMaster> rep,
  19. SqlSugarRepository<AdoS0LocationMaster> locRep,
  20. AdoS0ReferenceChecker refChecker)
  21. {
  22. _rep = rep;
  23. _locRep = locRep;
  24. _refChecker = refChecker;
  25. }
  26. [HttpGet]
  27. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0LocationShelfQueryDto q)
  28. {
  29. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  30. var query = _rep.AsQueryable()
  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);
  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. var item = await _rep.GetByIdAsync(id);
  51. if (item == null) return NotFound();
  52. await ApplyLocationDescrAsync(new List<AdoS0LocationShelfMaster> { item });
  53. return Ok(item);
  54. }
  55. [HttpPost]
  56. public async Task<IActionResult> CreateAsync([FromBody] AdoS0LocationShelfUpsertDto dto)
  57. {
  58. if (await _rep.IsAnyAsync(x => x.FactoryRefId == dto.FactoryRefId && x.Location == dto.Location && x.InvShelf == dto.InvShelf))
  59. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.DuplicateCode, "货架编码已存在");
  60. if (!await _refChecker.LocationExistsAsync(dto.Location))
  61. return AdoS0ApiErrors.InvalidReference(AdoS0ErrorCodes.ReferenceNotFound,
  62. $"库位编码 '{dto.Location}' 不存在于库位主数据");
  63. var entity = new AdoS0LocationShelfMaster
  64. {
  65. CompanyRefId = dto.CompanyRefId,
  66. FactoryRefId = dto.FactoryRefId,
  67. DomainCode = dto.DomainCode ?? string.Empty,
  68. Location = dto.Location,
  69. InvShelf = dto.InvShelf,
  70. Descr = dto.Descr,
  71. Area = dto.Area,
  72. CreateUser = dto.CreateUser,
  73. CreateTime = DateTime.Now
  74. };
  75. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  76. return Ok(entity);
  77. }
  78. [HttpPut("{id:long}")]
  79. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0LocationShelfUpsertDto dto)
  80. {
  81. var entity = await _rep.GetByIdAsync(id);
  82. if (entity == null) return NotFound();
  83. if (await _rep.IsAnyAsync(x => x.Id != id && x.FactoryRefId == dto.FactoryRefId && x.Location == dto.Location && x.InvShelf == dto.InvShelf))
  84. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.DuplicateCode, "货架编码已存在");
  85. if (!await _refChecker.LocationExistsAsync(dto.Location))
  86. return AdoS0ApiErrors.InvalidReference(AdoS0ErrorCodes.ReferenceNotFound,
  87. $"库位编码 '{dto.Location}' 不存在于库位主数据");
  88. entity.CompanyRefId = dto.CompanyRefId;
  89. entity.FactoryRefId = dto.FactoryRefId;
  90. entity.DomainCode = dto.DomainCode ?? string.Empty;
  91. entity.Location = dto.Location;
  92. entity.InvShelf = dto.InvShelf;
  93. entity.Descr = dto.Descr;
  94. entity.Area = dto.Area;
  95. entity.UpdateUser = dto.UpdateUser;
  96. entity.UpdateTime = DateTime.Now;
  97. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  98. return Ok(entity);
  99. }
  100. [HttpDelete("{id:long}")]
  101. public async Task<IActionResult> DeleteAsync(long id)
  102. {
  103. var item = await _rep.GetByIdAsync(id);
  104. if (item == null) return NotFound();
  105. await _rep.DeleteAsync(item);
  106. return Ok(new { message = "删除成功" });
  107. }
  108. private async Task ApplyLocationDescrAsync(List<AdoS0LocationShelfMaster> shelves)
  109. {
  110. if (shelves.Count == 0) return;
  111. var domainCodes = shelves.Select(s => s.DomainCode).Distinct().ToList();
  112. var locations = await _locRep.AsQueryable()
  113. .Where(l => domainCodes.Contains(l.DomainCode))
  114. .ToListAsync();
  115. foreach (var shelf in shelves)
  116. {
  117. var loc = locations.Find(l =>
  118. string.Equals(l.DomainCode, shelf.DomainCode, StringComparison.OrdinalIgnoreCase)
  119. && string.Equals(l.Location, shelf.Location, StringComparison.OrdinalIgnoreCase));
  120. shelf.LocationDescr = loc?.Descr;
  121. shelf.KwhjName = $"{shelf.Location}:{shelf.InvShelf}";
  122. }
  123. }
  124. }