AdoS0LocationsController.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 库位主数据(LocationMaster 语义)
  7. /// </summary>
  8. [ApiController]
  9. [Route("api/s0/warehouse/locations")]
  10. [AllowAnonymous]
  11. [NonUnify]
  12. public class AdoS0LocationsController : ControllerBase
  13. {
  14. private readonly SqlSugarRepository<AdoS0LocationMaster> _rep;
  15. private readonly AdoS0ReferenceChecker _refChecker;
  16. public AdoS0LocationsController(SqlSugarRepository<AdoS0LocationMaster> rep, AdoS0ReferenceChecker refChecker)
  17. {
  18. _rep = rep;
  19. _refChecker = refChecker;
  20. }
  21. [HttpGet]
  22. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0LocationQueryDto q)
  23. {
  24. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  25. var query = _rep.AsQueryable()
  26. .WhereIF(q.CompanyRefId.HasValue, x => x.CompanyRefId == q.CompanyRefId!.Value)
  27. .WhereIF(q.FactoryRefId.HasValue, x => x.FactoryRefId == q.FactoryRefId!.Value)
  28. .WhereIF(!string.IsNullOrWhiteSpace(q.DomainCode), x => x.DomainCode == q.DomainCode)
  29. .WhereIF(!string.IsNullOrWhiteSpace(q.Typed), x => x.Typed == q.Typed)
  30. .WhereIF(q.IsActive.HasValue, x => x.IsActive == q.IsActive!.Value)
  31. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword),
  32. x => x.Location.Contains(q.Keyword!) || (x.Descr != null && x.Descr.Contains(q.Keyword!)));
  33. var total = await query.CountAsync();
  34. var list = await query
  35. .OrderBy(x => x.Typed == "Supp" ? 2 : 0)
  36. .OrderBy(x => x.Location)
  37. .Skip((q.Page - 1) * q.PageSize)
  38. .Take(q.PageSize)
  39. .ToListAsync();
  40. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  41. }
  42. [HttpGet("{id:long}")]
  43. public async Task<IActionResult> GetAsync(long id)
  44. {
  45. var item = await _rep.GetByIdAsync(id);
  46. return item == null ? NotFound() : Ok(item);
  47. }
  48. [HttpGet("options")]
  49. public async Task<IActionResult> GetOptionsAsync(
  50. [FromQuery] long? companyRefId,
  51. [FromQuery] long? factoryRefId,
  52. [FromQuery] string? domainCode,
  53. [FromQuery] string? keyword,
  54. [FromQuery] bool? isActive,
  55. [FromQuery] int? limit)
  56. {
  57. var enabledFilter = isActive ?? true;
  58. var take = Math.Clamp(limit ?? 200, 1, 500);
  59. var list = await _rep.AsQueryable()
  60. .WhereIF(companyRefId.HasValue, x => x.CompanyRefId == companyRefId!.Value)
  61. .WhereIF(factoryRefId.HasValue, x => x.FactoryRefId == factoryRefId!.Value)
  62. .WhereIF(!string.IsNullOrWhiteSpace(domainCode), x => x.DomainCode == domainCode)
  63. .WhereIF(!string.IsNullOrWhiteSpace(keyword),
  64. x => x.Location.Contains(keyword!) || (x.Descr != null && x.Descr.Contains(keyword!)))
  65. .Where(x => x.IsActive == enabledFilter)
  66. .OrderBy(x => x.Location)
  67. .Take(take)
  68. .Select(x => new S0LocationOptionRow
  69. {
  70. Value = x.Location,
  71. Label = x.Descr == null || x.Descr == "" ? x.Location : x.Location + " / " + x.Descr,
  72. Code = x.Location,
  73. Name = x.Descr,
  74. DomainCode = x.DomainCode,
  75. IsActive = x.IsActive
  76. })
  77. .ToListAsync();
  78. return Ok(list);
  79. }
  80. [HttpPost]
  81. public async Task<IActionResult> CreateAsync([FromBody] AdoS0LocationUpsertDto dto)
  82. {
  83. if (await _rep.IsAnyAsync(x => x.FactoryRefId == dto.FactoryRefId && x.Location == dto.Location))
  84. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.DuplicateCode, "库位编码已存在");
  85. var entity = new AdoS0LocationMaster
  86. {
  87. CompanyRefId = dto.CompanyRefId,
  88. FactoryRefId = dto.FactoryRefId,
  89. DomainCode = dto.DomainCode ?? string.Empty,
  90. Location = dto.Location,
  91. Descr = dto.Descr,
  92. Storer = dto.Storer,
  93. Typed = dto.Typed,
  94. PhysicalAddress = dto.PhysicalAddress,
  95. IsActive = dto.IsActive,
  96. CreateUser = dto.CreateUser,
  97. CreateTime = DateTime.Now
  98. };
  99. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  100. return Ok(entity);
  101. }
  102. [HttpPut("{id:long}")]
  103. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0LocationUpsertDto dto)
  104. {
  105. var entity = await _rep.GetByIdAsync(id);
  106. if (entity == null) return NotFound();
  107. if (await _rep.IsAnyAsync(x => x.Id != id && x.FactoryRefId == dto.FactoryRefId && x.Location == dto.Location))
  108. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.DuplicateCode, "库位编码已存在");
  109. entity.CompanyRefId = dto.CompanyRefId;
  110. entity.FactoryRefId = dto.FactoryRefId;
  111. entity.DomainCode = dto.DomainCode ?? string.Empty;
  112. entity.Location = dto.Location;
  113. entity.Descr = dto.Descr;
  114. entity.Storer = dto.Storer;
  115. entity.Typed = dto.Typed;
  116. entity.PhysicalAddress = dto.PhysicalAddress;
  117. entity.IsActive = dto.IsActive;
  118. entity.UpdateUser = dto.UpdateUser;
  119. entity.UpdateTime = DateTime.Now;
  120. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  121. return Ok(entity);
  122. }
  123. [HttpDelete("{id:long}")]
  124. public async Task<IActionResult> DeleteAsync(long id)
  125. {
  126. var item = await _rep.GetByIdAsync(id);
  127. if (item == null) return NotFound();
  128. var refInfo = await _refChecker.LocationReferencesAsync(item.Location);
  129. if (refInfo is { } r)
  130. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.DeleteBlocked,
  131. $"存在 {r.Count} 条 {r.Table} 引用该库位,无法删除");
  132. await _rep.DeleteAsync(item);
  133. return Ok(new { message = "删除成功" });
  134. }
  135. }