AdoS0MfgMaterialSubstitutionsController.cs 3.8 KB

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