AdoS0QualityFqcOqcControllers.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. using Admin.NET.Plugin.AiDOP.Dto.S0.Quality;
  2. using Admin.NET.Plugin.AiDOP.Entity.S0.Quality;
  3. using Admin.NET.Plugin.AiDOP.Infrastructure;
  4. using static Admin.NET.Plugin.AiDOP.Controllers.S0.Quality.AdoS0QmsControllerHelpers;
  5. namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Quality;
  6. [ApiController]
  7. [Route("api/s0/quality/fqc-inspection-specs")]
  8. [AllowAnonymous]
  9. [NonUnify]
  10. public class AdoS0QmsFqcInspectionSpecsController : ControllerBase
  11. {
  12. private readonly SqlSugarRepository<AdoS0QmsFqcInspectionSpec> _rep;
  13. private readonly SqlSugarRepository<AdoS0QmsFqcInspectionSpecEntry> _entryRep;
  14. public AdoS0QmsFqcInspectionSpecsController(
  15. SqlSugarRepository<AdoS0QmsFqcInspectionSpec> rep,
  16. SqlSugarRepository<AdoS0QmsFqcInspectionSpecEntry> entryRep)
  17. {
  18. _rep = rep;
  19. _entryRep = entryRep;
  20. }
  21. [HttpGet]
  22. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0QualityPagedQueryDto q)
  23. {
  24. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  25. var query = _rep.AsQueryable()
  26. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword), x =>
  27. (x.FileNumber != null && x.FileNumber.Contains(q.Keyword!))
  28. || (x.ApplicableModel != null && x.ApplicableModel.Contains(q.Keyword!))
  29. || (x.MaterialCode != null && x.MaterialCode.Contains(q.Keyword!)));
  30. var total = await query.CountAsync();
  31. var list = await query.OrderByDescending(x => x.Id).Skip((q.Page - 1) * q.PageSize).Take(q.PageSize).ToListAsync();
  32. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  33. }
  34. [HttpGet("{id:long}")]
  35. public async Task<IActionResult> GetDetailAsync(long id)
  36. {
  37. var master = await _rep.GetByIdAsync(id);
  38. if (master == null) return NotFound();
  39. var items = await _entryRep.AsQueryable().Where(x => x.MasterId == id).OrderBy(x => x.Id).ToListAsync();
  40. return Ok(new { master, items });
  41. }
  42. [HttpPost]
  43. public async Task<IActionResult> CreateAsync([FromBody] AdoS0QmsFqcInspectionSpecUpsertDto dto)
  44. {
  45. var db = _rep.Context;
  46. await db.Ado.BeginTranAsync();
  47. try
  48. {
  49. var master = new AdoS0QmsFqcInspectionSpec();
  50. ApplyFqcSpec(master, dto);
  51. await _rep.AsInsertable(master).ExecuteReturnEntityAsync();
  52. await SyncFqcEntriesAsync(master.Id, dto.Items);
  53. await db.Ado.CommitTranAsync();
  54. return await GetDetailAsync(master.Id);
  55. }
  56. catch
  57. {
  58. await db.Ado.RollbackTranAsync();
  59. throw;
  60. }
  61. }
  62. [HttpPut("{id:long}")]
  63. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0QmsFqcInspectionSpecUpsertDto dto)
  64. {
  65. var master = await _rep.GetByIdAsync(id);
  66. if (master == null) return NotFound();
  67. var db = _rep.Context;
  68. await db.Ado.BeginTranAsync();
  69. try
  70. {
  71. ApplyFqcSpec(master, dto);
  72. await _rep.AsUpdateable(master).ExecuteCommandAsync();
  73. await _entryRep.AsDeleteable().Where(x => x.MasterId == id).ExecuteCommandAsync();
  74. await SyncFqcEntriesAsync(id, dto.Items);
  75. await db.Ado.CommitTranAsync();
  76. return await GetDetailAsync(id);
  77. }
  78. catch
  79. {
  80. await db.Ado.RollbackTranAsync();
  81. throw;
  82. }
  83. }
  84. [HttpDelete("{id:long}")]
  85. public async Task<IActionResult> DeleteAsync(long id)
  86. {
  87. var master = await _rep.GetByIdAsync(id);
  88. if (master == null) return NotFound();
  89. var db = _rep.Context;
  90. await db.Ado.BeginTranAsync();
  91. try
  92. {
  93. await _entryRep.AsDeleteable().Where(x => x.MasterId == id).ExecuteCommandAsync();
  94. await _rep.DeleteAsync(master);
  95. await db.Ado.CommitTranAsync();
  96. return Ok(new { message = "删除成功" });
  97. }
  98. catch
  99. {
  100. await db.Ado.RollbackTranAsync();
  101. throw;
  102. }
  103. }
  104. private static void ApplyFqcSpec(AdoS0QmsFqcInspectionSpec entity, AdoS0QmsFqcInspectionSpecUpsertDto dto)
  105. {
  106. entity.ApplicableModel = NullIfWhiteSpace(dto.ApplicableModel);
  107. entity.FileNumber = dto.FileNumber.Trim();
  108. entity.VersionNo = NullIfWhiteSpace(dto.VersionNo);
  109. entity.EffectiveDate = NullIfWhiteSpace(dto.EffectiveDate);
  110. entity.Attachment = NullIfWhiteSpace(dto.Attachment);
  111. entity.MaterialCode = NullIfWhiteSpace(dto.MaterialCode);
  112. entity.Attachment2 = NullIfWhiteSpace(dto.Attachment2);
  113. entity.Version = dto.Version;
  114. }
  115. private async Task SyncFqcEntriesAsync(long masterId, IEnumerable<AdoS0QmsFqcInspectionSpecEntryDto> items)
  116. {
  117. foreach (var item in items)
  118. {
  119. var entity = new AdoS0QmsFqcInspectionSpecEntry
  120. {
  121. MasterId = masterId,
  122. OperationCode = NullIfWhiteSpace(item.OperationCode),
  123. OperationName = NullIfWhiteSpace(item.OperationName),
  124. InspectionItem = NullIfWhiteSpace(item.InspectionItem),
  125. InspectionMethod = NullIfWhiteSpace(item.InspectionMethod),
  126. InspectionSpec = NullIfWhiteSpace(item.InspectionSpec),
  127. ImageCategory = NullIfWhiteSpace(item.ImageCategory),
  128. InspectionFrequency = NullIfWhiteSpace(item.InspectionFrequency),
  129. TechnicalStandard = NullIfWhiteSpace(item.TechnicalStandard),
  130. PeelingForce = item.PeelingForce,
  131. UpperLimit = NullIfWhiteSpace(item.UpperLimit),
  132. LowerLimit = NullIfWhiteSpace(item.LowerLimit)
  133. };
  134. await _entryRep.AsInsertable(entity).ExecuteCommandAsync();
  135. }
  136. }
  137. }
  138. [ApiController]
  139. [Route("api/s0/quality/oqc-inspection-specs")]
  140. [AllowAnonymous]
  141. [NonUnify]
  142. public class AdoS0QmsOqcInspectionSpecsController : ControllerBase
  143. {
  144. private readonly SqlSugarRepository<AdoS0QmsOqcInspectionSpec> _rep;
  145. private readonly SqlSugarRepository<AdoS0QmsOqcInspectionSpecEntry> _entryRep;
  146. public AdoS0QmsOqcInspectionSpecsController(
  147. SqlSugarRepository<AdoS0QmsOqcInspectionSpec> rep,
  148. SqlSugarRepository<AdoS0QmsOqcInspectionSpecEntry> entryRep)
  149. {
  150. _rep = rep;
  151. _entryRep = entryRep;
  152. }
  153. [HttpGet]
  154. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0QualityPagedQueryDto q)
  155. {
  156. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  157. var query = _rep.AsQueryable()
  158. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword), x =>
  159. (x.FileNumber != null && x.FileNumber.Contains(q.Keyword!))
  160. || (x.ApplicableModel != null && x.ApplicableModel.Contains(q.Keyword!))
  161. || (x.MaterialCode != null && x.MaterialCode.Contains(q.Keyword!)));
  162. var total = await query.CountAsync();
  163. var list = await query.OrderByDescending(x => x.Id).Skip((q.Page - 1) * q.PageSize).Take(q.PageSize).ToListAsync();
  164. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  165. }
  166. [HttpGet("{id:long}")]
  167. public async Task<IActionResult> GetDetailAsync(long id)
  168. {
  169. var master = await _rep.GetByIdAsync(id);
  170. if (master == null) return NotFound();
  171. var items = await _entryRep.AsQueryable().Where(x => x.MasterId == id).OrderBy(x => x.Id).ToListAsync();
  172. return Ok(new { master, items });
  173. }
  174. [HttpPost]
  175. public async Task<IActionResult> CreateAsync([FromBody] AdoS0QmsOqcInspectionSpecUpsertDto dto)
  176. {
  177. var db = _rep.Context;
  178. await db.Ado.BeginTranAsync();
  179. try
  180. {
  181. var master = new AdoS0QmsOqcInspectionSpec();
  182. ApplyOqcSpec(master, dto);
  183. await _rep.AsInsertable(master).ExecuteReturnEntityAsync();
  184. await SyncOqcEntriesAsync(master.Id, dto.Items);
  185. await db.Ado.CommitTranAsync();
  186. return await GetDetailAsync(master.Id);
  187. }
  188. catch
  189. {
  190. await db.Ado.RollbackTranAsync();
  191. throw;
  192. }
  193. }
  194. [HttpPut("{id:long}")]
  195. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0QmsOqcInspectionSpecUpsertDto dto)
  196. {
  197. var master = await _rep.GetByIdAsync(id);
  198. if (master == null) return NotFound();
  199. var db = _rep.Context;
  200. await db.Ado.BeginTranAsync();
  201. try
  202. {
  203. ApplyOqcSpec(master, dto);
  204. await _rep.AsUpdateable(master).ExecuteCommandAsync();
  205. await _entryRep.AsDeleteable().Where(x => x.MasterId == id).ExecuteCommandAsync();
  206. await SyncOqcEntriesAsync(id, dto.Items);
  207. await db.Ado.CommitTranAsync();
  208. return await GetDetailAsync(id);
  209. }
  210. catch
  211. {
  212. await db.Ado.RollbackTranAsync();
  213. throw;
  214. }
  215. }
  216. [HttpDelete("{id:long}")]
  217. public async Task<IActionResult> DeleteAsync(long id)
  218. {
  219. var master = await _rep.GetByIdAsync(id);
  220. if (master == null) return NotFound();
  221. var db = _rep.Context;
  222. await db.Ado.BeginTranAsync();
  223. try
  224. {
  225. await _entryRep.AsDeleteable().Where(x => x.MasterId == id).ExecuteCommandAsync();
  226. await _rep.DeleteAsync(master);
  227. await db.Ado.CommitTranAsync();
  228. return Ok(new { message = "删除成功" });
  229. }
  230. catch
  231. {
  232. await db.Ado.RollbackTranAsync();
  233. throw;
  234. }
  235. }
  236. private static void ApplyOqcSpec(AdoS0QmsOqcInspectionSpec entity, AdoS0QmsOqcInspectionSpecUpsertDto dto)
  237. {
  238. entity.ApplicableModel = NullIfWhiteSpace(dto.ApplicableModel);
  239. entity.FileNumber = dto.FileNumber.Trim();
  240. entity.VersionNo = NullIfWhiteSpace(dto.VersionNo);
  241. entity.EffectiveDate = NullIfWhiteSpace(dto.EffectiveDate);
  242. entity.Attachment = NullIfWhiteSpace(dto.Attachment);
  243. entity.MaterialCode = NullIfWhiteSpace(dto.MaterialCode);
  244. entity.Attachment2 = NullIfWhiteSpace(dto.Attachment2);
  245. entity.Version = dto.Version;
  246. }
  247. private async Task SyncOqcEntriesAsync(long masterId, IEnumerable<AdoS0QmsOqcInspectionSpecEntryDto> items)
  248. {
  249. foreach (var item in items)
  250. {
  251. var entity = new AdoS0QmsOqcInspectionSpecEntry
  252. {
  253. MasterId = masterId,
  254. OperationCode = NullIfWhiteSpace(item.OperationCode),
  255. OperationName = NullIfWhiteSpace(item.OperationName),
  256. InspectionItem = NullIfWhiteSpace(item.InspectionItem),
  257. InspectionMethod = NullIfWhiteSpace(item.InspectionMethod),
  258. InspectionSpec = NullIfWhiteSpace(item.InspectionSpec),
  259. ImageCategory = NullIfWhiteSpace(item.ImageCategory),
  260. InspectionFrequency = NullIfWhiteSpace(item.InspectionFrequency),
  261. TechnicalStandard = NullIfWhiteSpace(item.TechnicalStandard),
  262. PeelingForce = item.PeelingForce,
  263. UpperLimit = NullIfWhiteSpace(item.UpperLimit),
  264. LowerLimit = NullIfWhiteSpace(item.LowerLimit)
  265. };
  266. await _entryRep.AsInsertable(entity).ExecuteCommandAsync();
  267. }
  268. }
  269. }