AdoS0MfgLinePostsController.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. .Where(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. // 补展示字段:ProdLine + Describe
  45. var lineCodes = list.Select(x => x.ProdLine).Distinct().ToList();
  46. var lineMap = await _lineMasterRep.AsQueryable()
  47. .Where(lm => lm.Domain == domain && lineCodes.Contains(lm.Line))
  48. .ToListAsync();
  49. var describeByLine = lineMap.ToDictionary(x => x.Line, x => x.Describe ?? string.Empty);
  50. var shaped = list.Select(x => new
  51. {
  52. id = x.Id,
  53. domain = x.Domain,
  54. prodLine = x.ProdLine,
  55. prodLineDisplay = $"{x.ProdLine} { (describeByLine.TryGetValue(x.ProdLine, out var d) ? d : string.Empty) }".Trim(),
  56. jobNo = x.JOBNo,
  57. jobDescr = x.JOBDescr,
  58. createUser = x.CreateUser,
  59. createTime = x.CreateTime,
  60. updateUser = x.UpdateUser,
  61. updateTime = x.UpdateTime
  62. }).ToList();
  63. return Ok(new { total, page, pageSize, list = shaped });
  64. }
  65. [HttpGet("{id:long}")]
  66. public async Task<IActionResult> GetDetailAsync(long id)
  67. {
  68. var header = await _postRep.GetByIdAsync(id);
  69. if (header == null) return NotFound();
  70. var skills = await _skillRep.AsQueryable()
  71. .Where(x => x.LineSkillMasterId == id)
  72. .OrderBy(x => x.Id)
  73. .ToListAsync();
  74. return Ok(new { header, linePostSkills = skills });
  75. }
  76. [HttpPost]
  77. public async Task<IActionResult> CreateAsync([FromBody] AdoS0LineSkillMasterUpsertDto dto)
  78. {
  79. var fkErr = await ValidatePersonSkillsAsync(dto);
  80. if (fkErr != null) return BadRequest(new { message = fkErr });
  81. var db = _postRep.Context;
  82. await db.Ado.BeginTranAsync();
  83. try
  84. {
  85. var now = DateTime.Now;
  86. var post = new AdoS0LineSkillMaster
  87. {
  88. Domain = dto.Domain.Trim(),
  89. ProdLine = dto.ProdLine.Trim(),
  90. JOBNo = dto.JOBNo.Trim(),
  91. JOBDescr = dto.JOBDescr,
  92. CreateUser = dto.CreateUser ?? dto.UpdateUser,
  93. CreateTime = dto.CreateTime ?? dto.UpdateTime ?? now,
  94. UpdateUser = dto.UpdateUser,
  95. UpdateTime = dto.UpdateTime
  96. };
  97. post = await _postRep.AsInsertable(post).ExecuteReturnEntityAsync();
  98. foreach (var s in dto.Skills)
  99. {
  100. var row = MapSkill(post.Id, s);
  101. await _skillRep.AsInsertable(row).ExecuteCommandAsync();
  102. }
  103. await db.Ado.CommitTranAsync();
  104. return await GetDetailAsync(post.Id);
  105. }
  106. catch
  107. {
  108. await db.Ado.RollbackTranAsync();
  109. throw;
  110. }
  111. }
  112. [HttpPut("{id:long}")]
  113. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0LineSkillMasterUpsertDto dto)
  114. {
  115. var header = await _postRep.GetByIdAsync(id);
  116. if (header == null) return NotFound();
  117. var fkErr = await ValidatePersonSkillsAsync(dto);
  118. if (fkErr != null) return BadRequest(new { message = fkErr });
  119. var db = _postRep.Context;
  120. await db.Ado.BeginTranAsync();
  121. try
  122. {
  123. header.Domain = dto.Domain.Trim();
  124. header.ProdLine = dto.ProdLine.Trim();
  125. header.JOBNo = dto.JOBNo.Trim();
  126. header.JOBDescr = dto.JOBDescr;
  127. header.UpdateUser = dto.UpdateUser;
  128. header.UpdateTime = dto.UpdateTime ?? DateTime.Now;
  129. await _postRep.AsUpdateable(header).ExecuteCommandAsync();
  130. await _skillRep.AsDeleteable().Where(x => x.LineSkillMasterId == id).ExecuteCommandAsync();
  131. foreach (var s in dto.Skills)
  132. {
  133. var row = MapSkill(id, s);
  134. await _skillRep.AsInsertable(row).ExecuteCommandAsync();
  135. }
  136. await db.Ado.CommitTranAsync();
  137. return await GetDetailAsync(id);
  138. }
  139. catch
  140. {
  141. await db.Ado.RollbackTranAsync();
  142. throw;
  143. }
  144. }
  145. [HttpDelete("{id:long}")]
  146. public async Task<IActionResult> DeleteAsync(long id)
  147. {
  148. var header = await _postRep.GetByIdAsync(id);
  149. if (header == null) return NotFound();
  150. var db = _postRep.Context;
  151. await db.Ado.BeginTranAsync();
  152. try
  153. {
  154. await _skillRep.AsDeleteable().Where(x => x.LineSkillMasterId == id).ExecuteCommandAsync();
  155. await _postRep.DeleteAsync(header);
  156. await db.Ado.CommitTranAsync();
  157. return Ok(new { message = "删除成功" });
  158. }
  159. catch
  160. {
  161. await db.Ado.RollbackTranAsync();
  162. throw;
  163. }
  164. }
  165. private async Task<string?> ValidatePersonSkillsAsync(AdoS0LineSkillMasterUpsertDto dto)
  166. {
  167. if (dto.Skills == null || dto.Skills.Count == 0)
  168. return null;
  169. var ids = dto.Skills.Select(s => s.PersonSkillId).Distinct().ToList();
  170. var cnt = await _personSkillRep.AsQueryable()
  171. .Where(p => ids.Contains(p.Id))
  172. .CountAsync();
  173. return cnt == ids.Count ? null : "存在无效的人员技能主数据引用";
  174. }
  175. private static AdoS0LineSkillDetail MapSkill(long lineSkillMasterId, AdoS0LineSkillDetailUpsertDto s)
  176. {
  177. return new AdoS0LineSkillDetail
  178. {
  179. LineSkillMasterId = lineSkillMasterId,
  180. PersonSkillId = s.PersonSkillId,
  181. RequiredLevel = s.RequiredLevel,
  182. Remark = s.Remark,
  183. EffectiveDate = s.EffectiveDate,
  184. IsEnabled = s.IsEnabled,
  185. CreateTime = DateTime.Now
  186. };
  187. }
  188. }