AdoS0LocationShelvesController.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. public AdoS0LocationShelvesController(
  17. SqlSugarRepository<AdoS0LocationShelfMaster> rep,
  18. SqlSugarRepository<AdoS0LocationMaster> locRep)
  19. {
  20. _rep = rep;
  21. _locRep = locRep;
  22. }
  23. [HttpGet]
  24. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0LocationShelfQueryDto q)
  25. {
  26. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  27. var query = _rep.AsQueryable()
  28. .WhereIF(q.CompanyRefId.HasValue, x => x.CompanyRefId == q.CompanyRefId!.Value)
  29. .WhereIF(q.FactoryRefId.HasValue, x => x.FactoryRefId == q.FactoryRefId!.Value)
  30. .WhereIF(!string.IsNullOrWhiteSpace(q.DomainCode), x => x.DomainCode == q.DomainCode)
  31. .WhereIF(!string.IsNullOrWhiteSpace(q.Location), x => x.Location == q.Location)
  32. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword),
  33. x => x.InvShelf.Contains(q.Keyword!) || x.Location.Contains(q.Keyword!));
  34. var total = await query.CountAsync();
  35. var list = await query
  36. .OrderBy(x => x.Location)
  37. .OrderBy(x => x.InvShelf)
  38. .Skip((q.Page - 1) * q.PageSize)
  39. .Take(q.PageSize)
  40. .ToListAsync();
  41. await ApplyLocationDescrAsync(list);
  42. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  43. }
  44. [HttpGet("{id:long}")]
  45. public async Task<IActionResult> GetAsync(long id)
  46. {
  47. var item = await _rep.GetByIdAsync(id);
  48. if (item == null) return NotFound();
  49. await ApplyLocationDescrAsync(new List<AdoS0LocationShelfMaster> { item });
  50. return Ok(item);
  51. }
  52. [HttpPost]
  53. public async Task<IActionResult> CreateAsync([FromBody] AdoS0LocationShelfUpsertDto dto)
  54. {
  55. if (await _rep.IsAnyAsync(x => x.FactoryRefId == dto.FactoryRefId && x.Location == dto.Location && x.InvShelf == dto.InvShelf))
  56. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.DuplicateCode, "货架编码已存在");
  57. var entity = new AdoS0LocationShelfMaster
  58. {
  59. CompanyRefId = dto.CompanyRefId,
  60. FactoryRefId = dto.FactoryRefId,
  61. DomainCode = dto.DomainCode ?? string.Empty,
  62. Location = dto.Location,
  63. InvShelf = dto.InvShelf,
  64. Descr = dto.Descr,
  65. Area = dto.Area,
  66. CreateUser = dto.CreateUser,
  67. CreateTime = DateTime.Now
  68. };
  69. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  70. return Ok(entity);
  71. }
  72. [HttpPut("{id:long}")]
  73. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0LocationShelfUpsertDto dto)
  74. {
  75. var entity = await _rep.GetByIdAsync(id);
  76. if (entity == null) return NotFound();
  77. if (await _rep.IsAnyAsync(x => x.Id != id && x.FactoryRefId == dto.FactoryRefId && x.Location == dto.Location && x.InvShelf == dto.InvShelf))
  78. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.DuplicateCode, "货架编码已存在");
  79. entity.CompanyRefId = dto.CompanyRefId;
  80. entity.FactoryRefId = dto.FactoryRefId;
  81. entity.DomainCode = dto.DomainCode ?? string.Empty;
  82. entity.Location = dto.Location;
  83. entity.InvShelf = dto.InvShelf;
  84. entity.Descr = dto.Descr;
  85. entity.Area = dto.Area;
  86. entity.UpdateUser = dto.UpdateUser;
  87. entity.UpdateTime = DateTime.Now;
  88. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  89. return Ok(entity);
  90. }
  91. [HttpDelete("{id:long}")]
  92. public async Task<IActionResult> DeleteAsync(long id)
  93. {
  94. var item = await _rep.GetByIdAsync(id);
  95. if (item == null) return NotFound();
  96. await _rep.DeleteAsync(item);
  97. return Ok(new { message = "删除成功" });
  98. }
  99. private async Task ApplyLocationDescrAsync(List<AdoS0LocationShelfMaster> shelves)
  100. {
  101. if (shelves.Count == 0) return;
  102. var domainCodes = shelves.Select(s => s.DomainCode).Distinct().ToList();
  103. var locations = await _locRep.AsQueryable()
  104. .Where(l => domainCodes.Contains(l.DomainCode))
  105. .ToListAsync();
  106. foreach (var shelf in shelves)
  107. {
  108. var loc = locations.Find(l =>
  109. string.Equals(l.DomainCode, shelf.DomainCode, StringComparison.OrdinalIgnoreCase)
  110. && string.Equals(l.Location, shelf.Location, StringComparison.OrdinalIgnoreCase));
  111. shelf.LocationDescr = loc?.Descr;
  112. shelf.KwhjName = $"{shelf.Location}:{shelf.InvShelf}";
  113. }
  114. }
  115. }