AdoS0LocationsController.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. [HttpPost]
  49. public async Task<IActionResult> CreateAsync([FromBody] AdoS0LocationUpsertDto dto)
  50. {
  51. if (await _rep.IsAnyAsync(x => x.FactoryRefId == dto.FactoryRefId && x.Location == dto.Location))
  52. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.DuplicateCode, "库位编码已存在");
  53. var entity = new AdoS0LocationMaster
  54. {
  55. CompanyRefId = dto.CompanyRefId,
  56. FactoryRefId = dto.FactoryRefId,
  57. DomainCode = dto.DomainCode ?? string.Empty,
  58. Location = dto.Location,
  59. Descr = dto.Descr,
  60. Storer = dto.Storer,
  61. Typed = dto.Typed,
  62. PhysicalAddress = dto.PhysicalAddress,
  63. IsActive = dto.IsActive,
  64. CreateUser = dto.CreateUser,
  65. CreateTime = DateTime.Now
  66. };
  67. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  68. return Ok(entity);
  69. }
  70. [HttpPut("{id:long}")]
  71. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0LocationUpsertDto dto)
  72. {
  73. var entity = await _rep.GetByIdAsync(id);
  74. if (entity == null) return NotFound();
  75. if (await _rep.IsAnyAsync(x => x.Id != id && x.FactoryRefId == dto.FactoryRefId && x.Location == dto.Location))
  76. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.DuplicateCode, "库位编码已存在");
  77. entity.CompanyRefId = dto.CompanyRefId;
  78. entity.FactoryRefId = dto.FactoryRefId;
  79. entity.DomainCode = dto.DomainCode ?? string.Empty;
  80. entity.Location = dto.Location;
  81. entity.Descr = dto.Descr;
  82. entity.Storer = dto.Storer;
  83. entity.Typed = dto.Typed;
  84. entity.PhysicalAddress = dto.PhysicalAddress;
  85. entity.IsActive = dto.IsActive;
  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. var refInfo = await _refChecker.LocationReferencesAsync(item.Location);
  97. if (refInfo is { } r)
  98. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.DeleteBlocked,
  99. $"存在 {r.Count} 条 {r.Table} 引用该库位,无法删除");
  100. await _rep.DeleteAsync(item);
  101. return Ok(new { message = "删除成功" });
  102. }
  103. }