AdoS0LocationsController.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. public AdoS0LocationsController(SqlSugarRepository<AdoS0LocationMaster> rep)
  16. {
  17. _rep = rep;
  18. }
  19. [HttpGet]
  20. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0LocationQueryDto q)
  21. {
  22. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  23. var query = _rep.AsQueryable()
  24. .WhereIF(q.CompanyRefId.HasValue, x => x.CompanyRefId == q.CompanyRefId!.Value)
  25. .WhereIF(q.FactoryRefId.HasValue, x => x.FactoryRefId == q.FactoryRefId!.Value)
  26. .WhereIF(!string.IsNullOrWhiteSpace(q.DomainCode), x => x.DomainCode == q.DomainCode)
  27. .WhereIF(!string.IsNullOrWhiteSpace(q.Typed), x => x.Typed == q.Typed)
  28. .WhereIF(q.IsActive.HasValue, x => x.IsActive == q.IsActive!.Value)
  29. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword),
  30. x => x.Location.Contains(q.Keyword!) || (x.Descr != null && x.Descr.Contains(q.Keyword!)));
  31. var total = await query.CountAsync();
  32. var list = await query
  33. .OrderBy(x => x.Typed == "Supp" ? 2 : 0)
  34. .OrderBy(x => x.Location)
  35. .Skip((q.Page - 1) * q.PageSize)
  36. .Take(q.PageSize)
  37. .ToListAsync();
  38. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  39. }
  40. [HttpGet("{id:long}")]
  41. public async Task<IActionResult> GetAsync(long id)
  42. {
  43. var item = await _rep.GetByIdAsync(id);
  44. return item == null ? NotFound() : Ok(item);
  45. }
  46. [HttpPost]
  47. public async Task<IActionResult> CreateAsync([FromBody] AdoS0LocationUpsertDto dto)
  48. {
  49. if (await _rep.IsAnyAsync(x => x.FactoryRefId == dto.FactoryRefId && x.Location == dto.Location))
  50. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.DuplicateCode, "库位编码已存在");
  51. var entity = new AdoS0LocationMaster
  52. {
  53. CompanyRefId = dto.CompanyRefId,
  54. FactoryRefId = dto.FactoryRefId,
  55. DomainCode = dto.DomainCode ?? string.Empty,
  56. Location = dto.Location,
  57. Descr = dto.Descr,
  58. Storer = dto.Storer,
  59. Typed = dto.Typed,
  60. PhysicalAddress = dto.PhysicalAddress,
  61. IsActive = dto.IsActive,
  62. CreateUser = dto.CreateUser,
  63. CreateTime = DateTime.Now
  64. };
  65. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  66. return Ok(entity);
  67. }
  68. [HttpPut("{id:long}")]
  69. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0LocationUpsertDto dto)
  70. {
  71. var entity = await _rep.GetByIdAsync(id);
  72. if (entity == null) return NotFound();
  73. if (await _rep.IsAnyAsync(x => x.Id != id && x.FactoryRefId == dto.FactoryRefId && x.Location == dto.Location))
  74. return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.DuplicateCode, "库位编码已存在");
  75. entity.CompanyRefId = dto.CompanyRefId;
  76. entity.FactoryRefId = dto.FactoryRefId;
  77. entity.DomainCode = dto.DomainCode ?? string.Empty;
  78. entity.Location = dto.Location;
  79. entity.Descr = dto.Descr;
  80. entity.Storer = dto.Storer;
  81. entity.Typed = dto.Typed;
  82. entity.PhysicalAddress = dto.PhysicalAddress;
  83. entity.IsActive = dto.IsActive;
  84. entity.UpdateUser = dto.UpdateUser;
  85. entity.UpdateTime = DateTime.Now;
  86. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  87. return Ok(entity);
  88. }
  89. [HttpDelete("{id:long}")]
  90. public async Task<IActionResult> DeleteAsync(long id)
  91. {
  92. var item = await _rep.GetByIdAsync(id);
  93. if (item == null) return NotFound();
  94. await _rep.DeleteAsync(item);
  95. return Ok(new { message = "删除成功" });
  96. }
  97. }