AdoS0MfgLinePostsController.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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/line-posts")]
  8. [AllowAnonymous]
  9. [NonUnify]
  10. public class AdoS0MfgLinePostsController : ControllerBase
  11. {
  12. private readonly SqlSugarRepository<AdoS0LineSkillMaster> _postRep;
  13. private readonly SqlSugarRepository<AdoS0LineSkillDetail> _skillRep;
  14. private readonly SqlSugarRepository<AdoS0MfgPersonSkill> _personSkillRep;
  15. private readonly SqlSugarRepository<AdoS0LineMaster> _lineMasterRep;
  16. public AdoS0MfgLinePostsController(
  17. SqlSugarRepository<AdoS0LineSkillMaster> postRep,
  18. SqlSugarRepository<AdoS0LineSkillDetail> skillRep,
  19. SqlSugarRepository<AdoS0MfgPersonSkill> personSkillRep,
  20. SqlSugarRepository<AdoS0LineMaster> lineMasterRep)
  21. {
  22. _postRep = postRep;
  23. _skillRep = skillRep;
  24. _personSkillRep = personSkillRep;
  25. _lineMasterRep = lineMasterRep;
  26. }
  27. [HttpGet]
  28. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0LineSkillMasterQueryDto q)
  29. {
  30. var page = q.Page;
  31. var pageSize = q.PageSize;
  32. (page, pageSize) = PagingGuard.Normalize(page, pageSize);
  33. var domain = q.Domain?.Trim();
  34. var query = _postRep.AsQueryable()
  35. .WhereIF(!string.IsNullOrWhiteSpace(domain), x => x.Domain == domain)
  36. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword),
  37. x => x.ProdLine.Contains(q.Keyword!) || x.JOBNo.Contains(q.Keyword!) || (x.JOBDescr != null && x.JOBDescr.Contains(q.Keyword!)));
  38. var total = await query.CountAsync();
  39. var list = await query
  40. .OrderByDescending(x => x.UpdateTime ?? x.CreateTime)
  41. .Skip((page - 1) * pageSize)
  42. .Take(pageSize)
  43. .ToListAsync();
  44. // 补展示字段:按 (Domain, ProdLine) 元组反查 LineMaster.Describe,避免跨域错配
  45. var domains = list.Select(x => x.Domain).Distinct().ToList();
  46. var lineCodes = list.Select(x => x.ProdLine).Distinct().ToList();
  47. var lineMap = await _lineMasterRep.AsQueryable()
  48. .Where(lm => domains.Contains(lm.Domain) && lineCodes.Contains(lm.Line))
  49. .Select(lm => new { lm.Domain, lm.Line, lm.Describe })
  50. .ToListAsync();
  51. var describeByKey = lineMap
  52. .GroupBy(x => (x.Domain, x.Line))
  53. .ToDictionary(g => g.Key, g => g.First().Describe ?? string.Empty);
  54. var shaped = list.Select(x => new
  55. {
  56. id = x.Id,
  57. domain = x.Domain,
  58. prodLine = x.ProdLine,
  59. prodLineDisplay = $"{x.ProdLine} {(describeByKey.TryGetValue((x.Domain, x.ProdLine), out var d) ? d : string.Empty)}".Trim(),
  60. jobNo = x.JOBNo,
  61. jobDescr = x.JOBDescr,
  62. site = x.Site,
  63. orderby = x.Orderby,
  64. wType = x.WType,
  65. runCrew = x.RunCrew,
  66. createUser = x.CreateUser,
  67. createTime = x.CreateTime,
  68. updateUser = x.UpdateUser,
  69. updateTime = x.UpdateTime
  70. }).ToList();
  71. return Ok(new { total, page, pageSize, list = shaped });
  72. }
  73. [HttpGet("{id:long}")]
  74. public async Task<IActionResult> GetDetailAsync(long id)
  75. {
  76. var header = await _postRep.GetByIdAsync(id);
  77. if (header == null) return NotFound();
  78. var skills = await _skillRep.AsQueryable()
  79. .Where(x => x.LineSkillMasterId == id)
  80. .OrderBy(x => x.Id)
  81. .ToListAsync();
  82. return Ok(new { header, linePostSkills = skills });
  83. }
  84. [HttpPost]
  85. public async Task<IActionResult> CreateAsync([FromBody] AdoS0LineSkillMasterUpsertDto dto)
  86. {
  87. var fkErr = await ValidatePersonSkillsAsync(dto);
  88. if (fkErr != null) return BadRequest(new { message = fkErr });
  89. var db = _postRep.Context;
  90. await db.Ado.BeginTranAsync();
  91. try
  92. {
  93. var now = DateTime.Now;
  94. var post = new AdoS0LineSkillMaster
  95. {
  96. Domain = dto.Domain.Trim(),
  97. ProdLine = dto.ProdLine.Trim(),
  98. JOBNo = dto.JOBNo.Trim(),
  99. JOBDescr = dto.JOBDescr,
  100. Site = dto.Site,
  101. Orderby = dto.Orderby,
  102. WType = dto.WType,
  103. RunCrew = dto.RunCrew,
  104. CreateUser = dto.CreateUser ?? dto.UpdateUser,
  105. CreateTime = dto.CreateTime ?? dto.UpdateTime ?? now,
  106. UpdateUser = dto.UpdateUser,
  107. UpdateTime = dto.UpdateTime
  108. };
  109. post = await _postRep.AsInsertable(post).ExecuteReturnEntityAsync();
  110. foreach (var s in dto.Skills)
  111. {
  112. var row = MapSkill(post.Id, s);
  113. await _skillRep.AsInsertable(row).ExecuteCommandAsync();
  114. }
  115. await db.Ado.CommitTranAsync();
  116. return await GetDetailAsync(post.Id);
  117. }
  118. catch
  119. {
  120. await db.Ado.RollbackTranAsync();
  121. throw;
  122. }
  123. }
  124. [HttpPut("{id:long}")]
  125. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0LineSkillMasterUpsertDto dto)
  126. {
  127. var header = await _postRep.GetByIdAsync(id);
  128. if (header == null) return NotFound();
  129. var fkErr = await ValidatePersonSkillsAsync(dto);
  130. if (fkErr != null) return BadRequest(new { message = fkErr });
  131. var db = _postRep.Context;
  132. await db.Ado.BeginTranAsync();
  133. try
  134. {
  135. header.Domain = dto.Domain.Trim();
  136. header.ProdLine = dto.ProdLine.Trim();
  137. header.JOBNo = dto.JOBNo.Trim();
  138. header.JOBDescr = dto.JOBDescr;
  139. header.Site = dto.Site;
  140. header.Orderby = dto.Orderby;
  141. header.WType = dto.WType;
  142. header.RunCrew = dto.RunCrew;
  143. header.UpdateUser = dto.UpdateUser;
  144. header.UpdateTime = dto.UpdateTime ?? DateTime.Now;
  145. await _postRep.AsUpdateable(header).ExecuteCommandAsync();
  146. await _skillRep.AsDeleteable().Where(x => x.LineSkillMasterId == id).ExecuteCommandAsync();
  147. foreach (var s in dto.Skills)
  148. {
  149. var row = MapSkill(id, s);
  150. await _skillRep.AsInsertable(row).ExecuteCommandAsync();
  151. }
  152. await db.Ado.CommitTranAsync();
  153. return await GetDetailAsync(id);
  154. }
  155. catch
  156. {
  157. await db.Ado.RollbackTranAsync();
  158. throw;
  159. }
  160. }
  161. [HttpDelete("{id:long}")]
  162. public async Task<IActionResult> DeleteAsync(long id)
  163. {
  164. var header = await _postRep.GetByIdAsync(id);
  165. if (header == null) return NotFound();
  166. var db = _postRep.Context;
  167. await db.Ado.BeginTranAsync();
  168. try
  169. {
  170. await _skillRep.AsDeleteable().Where(x => x.LineSkillMasterId == id).ExecuteCommandAsync();
  171. await _postRep.DeleteAsync(header);
  172. await db.Ado.CommitTranAsync();
  173. return Ok(new { message = "删除成功" });
  174. }
  175. catch
  176. {
  177. await db.Ado.RollbackTranAsync();
  178. throw;
  179. }
  180. }
  181. private async Task<string?> ValidatePersonSkillsAsync(AdoS0LineSkillMasterUpsertDto dto)
  182. {
  183. if (dto.Skills == null || dto.Skills.Count == 0)
  184. return null;
  185. var ids = dto.Skills.Select(s => s.PersonSkillId).Distinct().ToList();
  186. var cnt = await _personSkillRep.AsQueryable()
  187. .Where(p => ids.Contains(p.Id))
  188. .CountAsync();
  189. return cnt == ids.Count ? null : "存在无效的人员技能主数据引用";
  190. }
  191. private static AdoS0LineSkillDetail MapSkill(long lineSkillMasterId, AdoS0LineSkillDetailUpsertDto s)
  192. {
  193. return new AdoS0LineSkillDetail
  194. {
  195. LineSkillMasterId = lineSkillMasterId,
  196. PersonSkillId = s.PersonSkillId,
  197. RequiredLevel = s.RequiredLevel,
  198. Remark = s.Remark,
  199. EffectiveDate = s.EffectiveDate,
  200. IsEnabled = s.IsEnabled,
  201. CreateTime = DateTime.Now
  202. };
  203. }
  204. }