AdoS0MfgStandardOperationsController.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/standard-operations")]
  8. [AllowAnonymous]
  9. [NonUnify]
  10. public class AdoS0MfgStandardOperationsController : ControllerBase
  11. {
  12. private readonly SqlSugarRepository<AdoS0MfgStandardOperation> _rep;
  13. public AdoS0MfgStandardOperationsController(SqlSugarRepository<AdoS0MfgStandardOperation> rep)
  14. {
  15. _rep = rep;
  16. }
  17. [HttpGet]
  18. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0MfgStandardOperationQueryDto 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.Name), x => x.Name.Contains(q.Name!))
  29. .WhereIF(q.IsEnabled.HasValue, x => x.IsEnabled == q.IsEnabled!.Value);
  30. var total = await query.CountAsync();
  31. var list = await query
  32. .OrderByDescending(x => x.CreatedAt)
  33. .Skip((page - 1) * pageSize)
  34. .Take(pageSize)
  35. .ToListAsync();
  36. return Ok(new { total, page, pageSize, list });
  37. }
  38. [HttpGet("{id:long}")]
  39. public async Task<IActionResult> GetAsync(long id)
  40. {
  41. var item = await _rep.GetByIdAsync(id);
  42. return item == null ? NotFound() : Ok(item);
  43. }
  44. [HttpPost]
  45. public async Task<IActionResult> CreateAsync([FromBody] AdoS0MfgStandardOperationUpsertDto dto)
  46. {
  47. var entity = new AdoS0MfgStandardOperation
  48. {
  49. CompanyRefId = dto.CompanyRefId,
  50. FactoryRefId = dto.FactoryRefId,
  51. Code = dto.Code,
  52. Name = dto.Name,
  53. IsKeyOperation = dto.IsKeyOperation,
  54. OperationType = dto.OperationType,
  55. StdTime = dto.StdTime,
  56. Remark = dto.Remark,
  57. IsEnabled = dto.IsEnabled,
  58. CreatedAt = DateTime.Now
  59. };
  60. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  61. return Ok(entity);
  62. }
  63. [HttpPut("{id:long}")]
  64. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0MfgStandardOperationUpsertDto dto)
  65. {
  66. var entity = await _rep.GetByIdAsync(id);
  67. if (entity == null) return NotFound();
  68. entity.CompanyRefId = dto.CompanyRefId;
  69. entity.FactoryRefId = dto.FactoryRefId;
  70. entity.Code = dto.Code;
  71. entity.Name = dto.Name;
  72. entity.IsKeyOperation = dto.IsKeyOperation;
  73. entity.OperationType = dto.OperationType;
  74. entity.StdTime = dto.StdTime;
  75. entity.Remark = dto.Remark;
  76. entity.IsEnabled = dto.IsEnabled;
  77. entity.UpdatedAt = DateTime.Now;
  78. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  79. return Ok(entity);
  80. }
  81. [HttpPatch("{id:long}/toggle-enabled")]
  82. public async Task<IActionResult> ToggleEnabledAsync(long id, [FromBody] AdoS0ToggleEnabledDto dto)
  83. {
  84. var entity = await _rep.GetByIdAsync(id);
  85. if (entity == null) return NotFound();
  86. entity.IsEnabled = dto.IsEnabled;
  87. entity.UpdatedAt = 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. }