AdoS0MfgProductionLinesController.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using Admin.NET.Plugin.AiDOP.Dto.S0.Manufacturing;
  2. using Admin.NET.Plugin.AiDOP.Dto.S0.Sales;
  3. using Admin.NET.Plugin.AiDOP.Entity.S0.Manufacturing;
  4. using Admin.NET.Plugin.AiDOP.Infrastructure;
  5. namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Manufacturing;
  6. [ApiController]
  7. [Route("api/s0/manufacturing/production-lines")]
  8. [AllowAnonymous]
  9. [NonUnify]
  10. public class AdoS0MfgProductionLinesController : ControllerBase
  11. {
  12. private readonly SqlSugarRepository<AdoS0MfgProductionLine> _rep;
  13. public AdoS0MfgProductionLinesController(SqlSugarRepository<AdoS0MfgProductionLine> rep)
  14. {
  15. _rep = rep;
  16. }
  17. [HttpGet]
  18. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0MfgProductionLineQueryDto q)
  19. {
  20. var page = q.EffectivePage;
  21. var pageSize = q.PageSize;
  22. (page, pageSize) = PagingGuard.Normalize(page, 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.Keyword),
  27. x => x.Code.Contains(q.Keyword!) || x.Name.Contains(q.Keyword!))
  28. .WhereIF(!string.IsNullOrWhiteSpace(q.Line),
  29. x => x.Code.Contains(q.Line!) || x.Name.Contains(q.Line!))
  30. .WhereIF(q.LocationId.HasValue, x => x.LocationId == q.LocationId!.Value)
  31. .WhereIF(q.IsEnabled.HasValue, x => x.IsEnabled == q.IsEnabled!.Value);
  32. var total = await query.CountAsync();
  33. var list = await query
  34. .OrderByDescending(x => x.CreatedAt)
  35. .Skip((page - 1) * pageSize)
  36. .Take(pageSize)
  37. .ToListAsync();
  38. return Ok(new { total, page, 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] AdoS0MfgProductionLineUpsertDto dto)
  48. {
  49. var entity = new AdoS0MfgProductionLine
  50. {
  51. CompanyRefId = dto.CompanyRefId,
  52. FactoryRefId = dto.FactoryRefId,
  53. Code = dto.Code,
  54. Name = dto.Name,
  55. LineType = dto.LineType,
  56. LineCategory = dto.LineCategory,
  57. LocationId = dto.LocationId,
  58. WorkshopId = dto.WorkshopId,
  59. Capacity = dto.Capacity,
  60. Remark = dto.Remark,
  61. IsEnabled = dto.IsEnabled,
  62. CreatedAt = DateTime.Now
  63. };
  64. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  65. return Ok(entity);
  66. }
  67. [HttpPut("{id:long}")]
  68. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0MfgProductionLineUpsertDto dto)
  69. {
  70. var entity = await _rep.GetByIdAsync(id);
  71. if (entity == null) return NotFound();
  72. entity.CompanyRefId = dto.CompanyRefId;
  73. entity.FactoryRefId = dto.FactoryRefId;
  74. entity.Code = dto.Code;
  75. entity.Name = dto.Name;
  76. entity.LineType = dto.LineType;
  77. entity.LineCategory = dto.LineCategory;
  78. entity.LocationId = dto.LocationId;
  79. entity.WorkshopId = dto.WorkshopId;
  80. entity.Capacity = dto.Capacity;
  81. entity.Remark = dto.Remark;
  82. entity.IsEnabled = dto.IsEnabled;
  83. entity.UpdatedAt = DateTime.Now;
  84. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  85. return Ok(entity);
  86. }
  87. [HttpPatch("{id:long}/toggle-enabled")]
  88. public async Task<IActionResult> ToggleEnabledAsync(long id, [FromBody] AdoS0ToggleEnabledDto dto)
  89. {
  90. var entity = await _rep.GetByIdAsync(id);
  91. if (entity == null) return NotFound();
  92. entity.IsEnabled = dto.IsEnabled;
  93. entity.UpdatedAt = DateTime.Now;
  94. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  95. return Ok(entity);
  96. }
  97. [HttpDelete("{id:long}")]
  98. public async Task<IActionResult> DeleteAsync(long id)
  99. {
  100. var item = await _rep.GetByIdAsync(id);
  101. if (item == null) return NotFound();
  102. await _rep.DeleteAsync(item);
  103. return Ok(new { message = "删除成功" });
  104. }
  105. }