AdoS0MfgSopDocumentsController.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.Entity.S0.Sales;
  5. using Admin.NET.Plugin.AiDOP.Infrastructure;
  6. namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Manufacturing;
  7. [ApiController]
  8. [Route("api/s0/manufacturing/sop-documents")]
  9. [AllowAnonymous]
  10. [NonUnify]
  11. public class AdoS0MfgSopDocumentsController : ControllerBase
  12. {
  13. private readonly SqlSugarRepository<AdoS0MfgSopDocument> _rep;
  14. public AdoS0MfgSopDocumentsController(SqlSugarRepository<AdoS0MfgSopDocument> rep)
  15. {
  16. _rep = rep;
  17. }
  18. [HttpGet]
  19. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0MfgSopDocumentQueryDto q)
  20. {
  21. var page = q.EffectivePage;
  22. var pageSize = q.PageSize;
  23. (page, pageSize) = PagingGuard.Normalize(page, pageSize);
  24. List<long>? materialFilterIds = null;
  25. if (!string.IsNullOrWhiteSpace(q.MaterialCode))
  26. {
  27. var mq = _rep.Context.Queryable<AdoS0Material>()
  28. .WhereIF(q.CompanyRefId.HasValue, m => m.CompanyRefId == q.CompanyRefId!.Value)
  29. .WhereIF(q.FactoryRefId.HasValue, m => m.FactoryRefId == q.FactoryRefId!.Value)
  30. .Where(m => m.Code.Contains(q.MaterialCode!))
  31. .Select(m => m.Id);
  32. materialFilterIds = await mq.ToListAsync();
  33. if (materialFilterIds.Count == 0)
  34. return Ok(new { total = 0, page, pageSize, list = Array.Empty<AdoS0MfgSopDocument>() });
  35. }
  36. var query = _rep.AsQueryable()
  37. .WhereIF(q.CompanyRefId.HasValue, x => x.CompanyRefId == q.CompanyRefId!.Value)
  38. .WhereIF(q.FactoryRefId.HasValue, x => x.FactoryRefId == q.FactoryRefId!.Value)
  39. .WhereIF(q.ProductionLineId.HasValue, x => x.ProductionLineId == q.ProductionLineId!.Value)
  40. .WhereIF(materialFilterIds != null, x => x.MaterialId != null && materialFilterIds!.Contains(x.MaterialId.Value))
  41. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword),
  42. x => x.Code.Contains(q.Keyword!) || x.Name.Contains(q.Keyword!))
  43. .WhereIF(q.IsEnabled.HasValue, x => x.IsEnabled == q.IsEnabled!.Value);
  44. var total = await query.CountAsync();
  45. var list = await query.OrderByDescending(x => x.CreatedAt).Skip((page - 1) * pageSize).Take(pageSize).ToListAsync();
  46. return Ok(new { total, page, pageSize, list });
  47. }
  48. [HttpGet("{id:long}")]
  49. public async Task<IActionResult> GetAsync(long id)
  50. {
  51. var item = await _rep.GetByIdAsync(id);
  52. return item == null ? NotFound() : Ok(item);
  53. }
  54. [HttpPost]
  55. public async Task<IActionResult> CreateAsync([FromBody] AdoS0MfgSopDocumentUpsertDto dto)
  56. {
  57. var entity = new AdoS0MfgSopDocument
  58. {
  59. CompanyRefId = dto.CompanyRefId,
  60. FactoryRefId = dto.FactoryRefId,
  61. Code = dto.Code,
  62. Name = dto.Name,
  63. ProductionLineId = dto.ProductionLineId,
  64. SopFileTypeId = dto.SopFileTypeId,
  65. MaterialId = dto.MaterialId,
  66. OperationId = dto.OperationId,
  67. FileVersion = dto.FileVersion,
  68. FileId = dto.FileId,
  69. DocStatus = dto.DocStatus,
  70. Remark = dto.Remark,
  71. IsEnabled = dto.IsEnabled,
  72. CreatedAt = DateTime.Now
  73. };
  74. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  75. return Ok(entity);
  76. }
  77. [HttpPut("{id:long}")]
  78. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0MfgSopDocumentUpsertDto dto)
  79. {
  80. var entity = await _rep.GetByIdAsync(id);
  81. if (entity == null) return NotFound();
  82. entity.CompanyRefId = dto.CompanyRefId;
  83. entity.FactoryRefId = dto.FactoryRefId;
  84. entity.Code = dto.Code;
  85. entity.Name = dto.Name;
  86. entity.ProductionLineId = dto.ProductionLineId;
  87. entity.SopFileTypeId = dto.SopFileTypeId;
  88. entity.MaterialId = dto.MaterialId;
  89. entity.OperationId = dto.OperationId;
  90. entity.FileVersion = dto.FileVersion;
  91. entity.FileId = dto.FileId;
  92. entity.DocStatus = dto.DocStatus;
  93. entity.Remark = dto.Remark;
  94. entity.IsEnabled = dto.IsEnabled;
  95. entity.UpdatedAt = DateTime.Now;
  96. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  97. return Ok(entity);
  98. }
  99. [HttpPatch("{id:long}/toggle-enabled")]
  100. public async Task<IActionResult> ToggleEnabledAsync(long id, [FromBody] AdoS0ToggleEnabledDto dto)
  101. {
  102. var entity = await _rep.GetByIdAsync(id);
  103. if (entity == null) return NotFound();
  104. entity.IsEnabled = dto.IsEnabled;
  105. entity.UpdatedAt = DateTime.Now;
  106. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  107. return Ok(entity);
  108. }
  109. [HttpDelete("{id:long}")]
  110. public async Task<IActionResult> DeleteAsync(long id)
  111. {
  112. var item = await _rep.GetByIdAsync(id);
  113. if (item == null) return NotFound();
  114. await _rep.DeleteAsync(item);
  115. return Ok(new { message = "删除成功" });
  116. }
  117. }