AdoS0MfgPreprocessElementParamsController.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. /// <summary>
  7. /// 前处理要素参数行(独立列表页,与聚合保存并存)。
  8. /// </summary>
  9. [ApiController]
  10. [Route("api/s0/manufacturing/preprocess-element-params")]
  11. [AllowAnonymous]
  12. [NonUnify]
  13. public class AdoS0MfgPreprocessElementParamsController : ControllerBase
  14. {
  15. private readonly SqlSugarRepository<AdoS0MfgPreprocessElementParam> _rep;
  16. private readonly SqlSugarRepository<AdoS0MfgElementParam> _elementParamRep;
  17. public AdoS0MfgPreprocessElementParamsController(
  18. SqlSugarRepository<AdoS0MfgPreprocessElementParam> rep,
  19. SqlSugarRepository<AdoS0MfgElementParam> elementParamRep)
  20. {
  21. _rep = rep;
  22. _elementParamRep = elementParamRep;
  23. }
  24. [HttpGet]
  25. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0MfgPreprocessElementParamQueryDto q)
  26. {
  27. var page = q.EffectivePage;
  28. var pageSize = q.PageSize;
  29. (page, pageSize) = PagingGuard.Normalize(page, pageSize);
  30. var query = _rep.AsQueryable()
  31. .WhereIF(q.CompanyRefId.HasValue, x => x.CompanyRefId == q.CompanyRefId!.Value)
  32. .WhereIF(q.FactoryRefId.HasValue, x => x.FactoryRefId == q.FactoryRefId!.Value)
  33. .WhereIF(q.PreprocessElementId.HasValue, x => x.PreprocessElementId == q.PreprocessElementId!.Value)
  34. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword),
  35. x => (x.Remark != null && x.Remark.Contains(q.Keyword!)))
  36. .WhereIF(q.IsEnabled.HasValue, x => x.IsEnabled == q.IsEnabled!.Value);
  37. var total = await query.CountAsync();
  38. var list = await query.OrderByDescending(x => x.CreatedAt).Skip((page - 1) * pageSize).Take(pageSize).ToListAsync();
  39. return Ok(new { total, page, pageSize, list });
  40. }
  41. [HttpGet("{id:long}")]
  42. public async Task<IActionResult> GetAsync(long id)
  43. {
  44. var item = await _rep.GetByIdAsync(id);
  45. return item == null ? NotFound() : Ok(item);
  46. }
  47. [HttpPost]
  48. public async Task<IActionResult> CreateAsync([FromBody] AdoS0MfgPreprocessElementParamUpsertDto dto)
  49. {
  50. var ok = await _elementParamRep.IsAnyAsync(e =>
  51. e.Id == dto.ElementParamId && e.CompanyRefId == dto.CompanyRefId && e.FactoryRefId == dto.FactoryRefId);
  52. if (!ok) return BadRequest(new { message = "生产要素参数引用无效" });
  53. var entity = new AdoS0MfgPreprocessElementParam
  54. {
  55. CompanyRefId = dto.CompanyRefId,
  56. FactoryRefId = dto.FactoryRefId,
  57. PreprocessElementId = dto.PreprocessElementId,
  58. ElementParamId = dto.ElementParamId,
  59. ParamValue = dto.ParamValue,
  60. SortNo = dto.SortNo,
  61. Remark = dto.Remark,
  62. IsEnabled = dto.IsEnabled,
  63. CreatedAt = 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] AdoS0MfgPreprocessElementParamUpsertDto dto)
  70. {
  71. var entity = await _rep.GetByIdAsync(id);
  72. if (entity == null) return NotFound();
  73. var ok = await _elementParamRep.IsAnyAsync(e =>
  74. e.Id == dto.ElementParamId && e.CompanyRefId == dto.CompanyRefId && e.FactoryRefId == dto.FactoryRefId);
  75. if (!ok) return BadRequest(new { message = "生产要素参数引用无效" });
  76. entity.CompanyRefId = dto.CompanyRefId;
  77. entity.FactoryRefId = dto.FactoryRefId;
  78. entity.PreprocessElementId = dto.PreprocessElementId;
  79. entity.ElementParamId = dto.ElementParamId;
  80. entity.ParamValue = dto.ParamValue;
  81. entity.SortNo = dto.SortNo;
  82. entity.Remark = dto.Remark;
  83. entity.IsEnabled = dto.IsEnabled;
  84. entity.UpdatedAt = DateTime.Now;
  85. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  86. return Ok(entity);
  87. }
  88. [HttpPatch("{id:long}/toggle-enabled")]
  89. public async Task<IActionResult> ToggleEnabledAsync(long id, [FromBody] AdoS0ToggleEnabledDto dto)
  90. {
  91. var entity = await _rep.GetByIdAsync(id);
  92. if (entity == null) return NotFound();
  93. entity.IsEnabled = dto.IsEnabled;
  94. entity.UpdatedAt = DateTime.Now;
  95. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  96. return Ok(entity);
  97. }
  98. [HttpDelete("{id:long}")]
  99. public async Task<IActionResult> DeleteAsync(long id)
  100. {
  101. var item = await _rep.GetByIdAsync(id);
  102. if (item == null) return NotFound();
  103. await _rep.DeleteAsync(item);
  104. return Ok(new { message = "删除成功" });
  105. }
  106. }