AdoS0MfgMaterialProcessElementsController.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/material-process-elements")]
  9. [AllowAnonymous]
  10. [NonUnify]
  11. public class AdoS0MfgMaterialProcessElementsController : ControllerBase
  12. {
  13. private readonly SqlSugarRepository<AdoS0ItemOpCondition> _rep;
  14. public AdoS0MfgMaterialProcessElementsController(SqlSugarRepository<AdoS0ItemOpCondition> rep)
  15. {
  16. _rep = rep;
  17. }
  18. [HttpGet]
  19. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0ItemOpConditionQueryDto q)
  20. {
  21. var page = q.Page;
  22. var pageSize = q.PageSize;
  23. (page, pageSize) = PagingGuard.Normalize(page, pageSize);
  24. var domain = q.Domain.Trim();
  25. var query = _rep.AsQueryable()
  26. .Where(x => x.Domain == domain && x.CodeType == "Prod")
  27. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword),
  28. x =>
  29. x.ItemNum.Contains(q.Keyword!)
  30. || (x.ItemCode != null && x.ItemCode.Contains(q.Keyword!))
  31. || (x.Descr != null && x.Descr.Contains(q.Keyword!))
  32. || (x.Op != null && x.Op.Contains(q.Keyword!))
  33. || (x.Typed != null && x.Typed.Contains(q.Keyword!)))
  34. .WhereIF(!string.IsNullOrWhiteSpace(q.ItemNum), x => x.ItemNum.Contains(q.ItemNum!))
  35. .WhereIF(!string.IsNullOrWhiteSpace(q.Op), x => x.Op != null && x.Op.Contains(q.Op!))
  36. .WhereIF(!string.IsNullOrWhiteSpace(q.Line), x => x.Line != null && x.Line.Contains(q.Line!))
  37. .WhereIF(!string.IsNullOrWhiteSpace(q.Typed), x => x.Typed != null && x.Typed.Contains(q.Typed!))
  38. .WhereIF(!string.IsNullOrWhiteSpace(q.ItemCode), x => x.ItemCode != null && x.ItemCode.Contains(q.ItemCode!));
  39. var total = await query.CountAsync();
  40. var list = await query.OrderByDescending(x => x.UpdateTime ?? x.CreateTime).Skip((page - 1) * pageSize).Take(pageSize).ToListAsync();
  41. // 展示字段:ItemDescr / OpDescr / TypedDescr(当前阶段仅能稳定补 ItemDescr 与 OpDescr;TypedDescr 先留空)
  42. var itemNums = list.Select(x => x.ItemNum).Distinct().ToList();
  43. var items = await _rep.Context.Queryable<AdoS0ItemMaster>()
  44. .Where(m => m.DomainCode == domain && itemNums.Contains(m.ItemNum))
  45. .Select(m => new { m.ItemNum, m.Descr })
  46. .ToListAsync();
  47. var itemDescrMap = items.ToDictionary(x => x.ItemNum, x => x.Descr);
  48. var opKeys = list
  49. .Where(x => !string.IsNullOrWhiteSpace(x.Op))
  50. .Select(x => new { x.ItemNum, Op = x.Op! })
  51. .Distinct()
  52. .ToList();
  53. var opDescrMap = new Dictionary<string, string>();
  54. if (opKeys.Count > 0)
  55. {
  56. var r = await _rep.Context.Queryable<AdoS0MfgRoutingOpDetail>()
  57. .Where(x => x.MaterialCode != null && itemNums.Contains(x.MaterialCode))
  58. .ToListAsync();
  59. foreach (var k in opKeys)
  60. {
  61. var match = r.FirstOrDefault(x => x.MaterialCode == k.ItemNum && x.OperationCode == k.Op);
  62. if (match != null) opDescrMap[$"{k.ItemNum}::{k.Op}"] = match.OperationDescription;
  63. }
  64. }
  65. var shaped = list.Select(x => new
  66. {
  67. id = x.Id,
  68. itemNum = x.ItemNum,
  69. itemDescr = itemDescrMap.TryGetValue(x.ItemNum, out var idesc) ? idesc : "",
  70. op = x.Op,
  71. opDescr = x.Op != null && opDescrMap.TryGetValue($"{x.ItemNum}::{x.Op}", out var odesc) ? odesc : "",
  72. line = x.Line,
  73. typed = x.Typed,
  74. typedDescr = "",
  75. itemCode = x.ItemCode,
  76. descr = x.Descr,
  77. isMain = x.IsMain,
  78. codeType = x.CodeType,
  79. domain = x.Domain,
  80. createUser = x.CreateUser,
  81. createTime = x.CreateTime,
  82. updateUser = x.UpdateUser,
  83. updateTime = x.UpdateTime,
  84. }).ToList();
  85. return Ok(new { total, page, pageSize, list = shaped });
  86. }
  87. [HttpGet("{id:long}")]
  88. public async Task<IActionResult> GetAsync(long id)
  89. {
  90. var item = await _rep.GetByIdAsync(id);
  91. return item == null ? NotFound() : Ok(item);
  92. }
  93. [HttpPost]
  94. public async Task<IActionResult> CreateAsync([FromBody] AdoS0ItemOpConditionUpsertDto dto)
  95. {
  96. var now = DateTime.Now;
  97. var entity = new AdoS0ItemOpCondition
  98. {
  99. Domain = dto.Domain.Trim(),
  100. CodeType = "Prod",
  101. ItemNum = dto.ItemNum.Trim(),
  102. Op = dto.Op,
  103. Line = dto.Line,
  104. Typed = dto.Typed,
  105. ItemCode = dto.ItemCode,
  106. Descr = dto.Descr,
  107. IsMain = dto.IsMain,
  108. CreateUser = dto.CreateUser ?? dto.UpdateUser,
  109. CreateTime = dto.CreateTime ?? dto.UpdateTime ?? now,
  110. UpdateUser = dto.UpdateUser,
  111. UpdateTime = dto.UpdateTime
  112. };
  113. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  114. return Ok(entity);
  115. }
  116. [HttpPut("{id:long}")]
  117. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0ItemOpConditionUpsertDto dto)
  118. {
  119. var entity = await _rep.GetByIdAsync(id);
  120. if (entity == null) return NotFound();
  121. entity.Domain = dto.Domain.Trim();
  122. entity.CodeType = "Prod";
  123. entity.ItemNum = dto.ItemNum.Trim();
  124. entity.Op = dto.Op;
  125. entity.Line = dto.Line;
  126. entity.Typed = dto.Typed;
  127. entity.ItemCode = dto.ItemCode;
  128. entity.Descr = dto.Descr;
  129. entity.IsMain = dto.IsMain;
  130. entity.UpdateUser = dto.UpdateUser;
  131. entity.UpdateTime = dto.UpdateTime ?? DateTime.Now;
  132. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  133. return Ok(entity);
  134. }
  135. [HttpDelete("{id:long}")]
  136. public async Task<IActionResult> DeleteAsync(long id)
  137. {
  138. var item = await _rep.GetByIdAsync(id);
  139. if (item == null) return NotFound();
  140. await _rep.DeleteAsync(item);
  141. return Ok(new { message = "删除成功" });
  142. }
  143. }