AdoS0MfgMaterialProcessElementsController.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. // 去 domain 化(S0-DOMAIN-FILTER-REMOVAL-READONLY-1):不再按 Domain 过滤,租户隔离由 ITenantIdFilter 自动生效;保留 CodeType 业务条件。
  25. var query = _rep.AsQueryable()
  26. .Where(x => 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. // 去 domain 化:物料名补显不再按 Domain 等值;ItemMaster 带 ITenantIdFilter,租户内 ItemNum 唯一,确定性以 IsActive/RecID 稳定排序兜底。
  44. var items = await _rep.Context.Queryable<AdoS0ItemMaster>()
  45. .Where(m => itemNums.Contains(m.ItemNum))
  46. .OrderByDescending(m => m.IsActive)
  47. .OrderBy(m => m.Id)
  48. .Select(m => new { m.ItemNum, m.Descr })
  49. .ToListAsync();
  50. var itemDescrMap = items
  51. .GroupBy(x => x.ItemNum)
  52. .ToDictionary(g => g.Key, g => g.First().Descr);
  53. var opKeys = list
  54. .Where(x => !string.IsNullOrWhiteSpace(x.Op))
  55. .Select(x => new { x.ItemNum, Op = x.Op! })
  56. .Distinct()
  57. .ToList();
  58. var opDescrMap = new Dictionary<string, string>();
  59. if (opKeys.Count > 0)
  60. {
  61. var r = await _rep.Context.Queryable<AdoS0MfgRoutingOpDetail>()
  62. .Where(x => x.MaterialCode != null && itemNums.Contains(x.MaterialCode))
  63. .ToListAsync();
  64. foreach (var k in opKeys)
  65. {
  66. var match = r.FirstOrDefault(x => x.MaterialCode == k.ItemNum && x.OperationCode == k.Op);
  67. if (match != null) opDescrMap[$"{k.ItemNum}::{k.Op}"] = match.OperationDescription;
  68. }
  69. }
  70. var shaped = list.Select(x => new
  71. {
  72. id = x.Id,
  73. itemNum = x.ItemNum,
  74. itemDescr = itemDescrMap.TryGetValue(x.ItemNum, out var idesc) ? idesc : "",
  75. op = x.Op,
  76. opDescr = x.Op != null && opDescrMap.TryGetValue($"{x.ItemNum}::{x.Op}", out var odesc) ? odesc : "",
  77. line = x.Line,
  78. typed = x.Typed,
  79. typedDescr = "",
  80. itemCode = x.ItemCode,
  81. descr = x.Descr,
  82. isMain = x.IsMain,
  83. codeType = x.CodeType,
  84. domain = x.Domain,
  85. createUser = x.CreateUser,
  86. createTime = x.CreateTime,
  87. updateUser = x.UpdateUser,
  88. updateTime = x.UpdateTime,
  89. }).ToList();
  90. return Ok(new { total, page, pageSize, list = shaped });
  91. }
  92. [HttpGet("{id:long}")]
  93. public async Task<IActionResult> GetAsync(long id)
  94. {
  95. var item = await _rep.GetByIdAsync(id);
  96. return item == null ? NotFound() : Ok(item);
  97. }
  98. [HttpPost]
  99. public async Task<IActionResult> CreateAsync([FromBody] AdoS0ItemOpConditionUpsertDto dto)
  100. {
  101. var now = DateTime.Now;
  102. var entity = new AdoS0ItemOpCondition
  103. {
  104. Domain = dto.Domain.Trim(),
  105. CodeType = "Prod",
  106. ItemNum = dto.ItemNum.Trim(),
  107. Op = dto.Op,
  108. Line = dto.Line,
  109. Typed = dto.Typed,
  110. ItemCode = dto.ItemCode,
  111. Descr = dto.Descr,
  112. IsMain = dto.IsMain,
  113. CreateUser = dto.CreateUser ?? dto.UpdateUser,
  114. CreateTime = dto.CreateTime ?? dto.UpdateTime ?? now,
  115. UpdateUser = dto.UpdateUser,
  116. UpdateTime = dto.UpdateTime
  117. };
  118. await _rep.AsInsertable(entity).ExecuteReturnEntityAsync();
  119. return Ok(entity);
  120. }
  121. [HttpPut("{id:long}")]
  122. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0ItemOpConditionUpsertDto dto)
  123. {
  124. var entity = await _rep.GetByIdAsync(id);
  125. if (entity == null) return NotFound();
  126. entity.Domain = dto.Domain.Trim();
  127. entity.CodeType = "Prod";
  128. entity.ItemNum = dto.ItemNum.Trim();
  129. entity.Op = dto.Op;
  130. entity.Line = dto.Line;
  131. entity.Typed = dto.Typed;
  132. entity.ItemCode = dto.ItemCode;
  133. entity.Descr = dto.Descr;
  134. entity.IsMain = dto.IsMain;
  135. entity.UpdateUser = dto.UpdateUser;
  136. entity.UpdateTime = dto.UpdateTime ?? DateTime.Now;
  137. await _rep.AsUpdateable(entity).ExecuteCommandAsync();
  138. return Ok(entity);
  139. }
  140. [HttpDelete("{id:long}")]
  141. public async Task<IActionResult> DeleteAsync(long id)
  142. {
  143. var item = await _rep.GetByIdAsync(id);
  144. if (item == null) return NotFound();
  145. await _rep.DeleteAsync(item);
  146. return Ok(new { message = "删除成功" });
  147. }
  148. }