AdoS0QualityAggregateControllers.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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/inspection-bases")]
  8. [AllowAnonymous]
  9. [NonUnify]
  10. public class AdoS0QmsInspectionBasesController : ControllerBase
  11. {
  12. private readonly SqlSugarRepository<AdoS0QmsInspectionBasis> _rep;
  13. private readonly SqlSugarRepository<AdoS0QmsInspectionBasisEntry> _entryRep;
  14. public AdoS0QmsInspectionBasesController(
  15. SqlSugarRepository<AdoS0QmsInspectionBasis> rep,
  16. SqlSugarRepository<AdoS0QmsInspectionBasisEntry> 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.Number != null && x.Number.Contains(q.Keyword!)) ||
  28. (x.Name != null && x.Name.Contains(q.Keyword!)));
  29. var total = await query.CountAsync();
  30. var list = await query.OrderBy(x => x.Number).Skip((q.Page - 1) * q.PageSize).Take(q.PageSize).ToListAsync();
  31. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  32. }
  33. [HttpGet("{id:long}")]
  34. public async Task<IActionResult> GetDetailAsync(long id)
  35. {
  36. var master = await _rep.GetByIdAsync(id);
  37. if (master == null) return NotFound();
  38. var items = await _entryRep.AsQueryable().Where(x => x.MasterId == id).OrderBy(x => x.Seq).OrderBy(x => x.Id).ToListAsync();
  39. return Ok(new { master, items });
  40. }
  41. [HttpGet("options")]
  42. public async Task<IActionResult> GetOptionsAsync() => Ok(await _rep.AsQueryable()
  43. .OrderBy(x => x.Number)
  44. .Select(x => new AdoS0QualitySimpleOptionDto { Value = x.Id, Label = (x.Number ?? "") + " / " + (x.Name ?? "") })
  45. .ToListAsync());
  46. [HttpPost]
  47. public async Task<IActionResult> CreateAsync([FromBody] AdoS0QmsInspectionBasisUpsertDto dto)
  48. {
  49. var db = _rep.Context;
  50. await db.Ado.BeginTranAsync();
  51. try
  52. {
  53. var master = new AdoS0QmsInspectionBasis();
  54. ApplyInspectionBasis(master, dto, true);
  55. await _rep.AsInsertable(master).ExecuteReturnEntityAsync();
  56. await SyncInspectionBasisEntriesAsync(master.Id, dto.Items);
  57. await db.Ado.CommitTranAsync();
  58. return await GetDetailAsync(master.Id);
  59. }
  60. catch
  61. {
  62. await db.Ado.RollbackTranAsync();
  63. throw;
  64. }
  65. }
  66. [HttpPut("{id:long}")]
  67. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0QmsInspectionBasisUpsertDto dto)
  68. {
  69. var master = await _rep.GetByIdAsync(id);
  70. if (master == null) return NotFound();
  71. var db = _rep.Context;
  72. await db.Ado.BeginTranAsync();
  73. try
  74. {
  75. ApplyInspectionBasis(master, dto, false);
  76. await _rep.AsUpdateable(master).ExecuteCommandAsync();
  77. await _entryRep.AsDeleteable().Where(x => x.MasterId == id).ExecuteCommandAsync();
  78. await SyncInspectionBasisEntriesAsync(id, dto.Items);
  79. await db.Ado.CommitTranAsync();
  80. return await GetDetailAsync(id);
  81. }
  82. catch
  83. {
  84. await db.Ado.RollbackTranAsync();
  85. throw;
  86. }
  87. }
  88. [HttpDelete("{id:long}")]
  89. public async Task<IActionResult> DeleteAsync(long id)
  90. {
  91. var master = await _rep.GetByIdAsync(id);
  92. if (master == null) return NotFound();
  93. var db = _rep.Context;
  94. await db.Ado.BeginTranAsync();
  95. try
  96. {
  97. await _entryRep.AsDeleteable().Where(x => x.MasterId == id).ExecuteCommandAsync();
  98. await _rep.DeleteAsync(master);
  99. await db.Ado.CommitTranAsync();
  100. return Ok(new { message = "删除成功" });
  101. }
  102. catch
  103. {
  104. await db.Ado.RollbackTranAsync();
  105. throw;
  106. }
  107. }
  108. private static void ApplyInspectionBasis(AdoS0QmsInspectionBasis entity, AdoS0QmsInspectionBasisUpsertDto dto, bool isCreate)
  109. {
  110. entity.Number = dto.Number.Trim();
  111. entity.Name = dto.Name.Trim();
  112. entity.ControlStrategy = NullIfWhiteSpace(dto.ControlStrategy);
  113. entity.CreateOrgId = dto.CreateOrgId;
  114. entity.UseOrgId = dto.UseOrgId;
  115. entity.Comment = NullIfWhiteSpace(dto.Comment);
  116. entity.Status = NullIfWhiteSpace(dto.Status);
  117. entity.EnableStatus = NullIfWhiteSpace(dto.EnableStatus);
  118. if (isCreate) entity.CreateTime = DateTime.Now;
  119. entity.ModifyTime = DateTime.Now;
  120. }
  121. private async Task SyncInspectionBasisEntriesAsync(long masterId, IEnumerable<AdoS0QmsInspectionBasisEntryDto> items)
  122. {
  123. var seq = 1L;
  124. foreach (var item in items)
  125. {
  126. var entity = new AdoS0QmsInspectionBasisEntry
  127. {
  128. MasterId = masterId,
  129. Seq = item.Seq ?? seq++,
  130. DocumentNumber = NullIfWhiteSpace(item.DocumentNumber),
  131. DocumentName = NullIfWhiteSpace(item.DocumentName),
  132. Attachment = NullIfWhiteSpace(item.Attachment)
  133. };
  134. await _entryRep.AsInsertable(entity).ExecuteCommandAsync();
  135. }
  136. }
  137. }
  138. [ApiController]
  139. [Route("api/s0/quality/inspection-standards")]
  140. [AllowAnonymous]
  141. [NonUnify]
  142. public class AdoS0QmsInspectionStandardsController : ControllerBase
  143. {
  144. private readonly SqlSugarRepository<AdoS0QmsInspectionStandard> _rep;
  145. private readonly SqlSugarRepository<AdoS0QmsInspectionStandardEntry> _entryRep;
  146. public AdoS0QmsInspectionStandardsController(
  147. SqlSugarRepository<AdoS0QmsInspectionStandard> rep,
  148. SqlSugarRepository<AdoS0QmsInspectionStandardEntry> 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.Number != null && x.Number.Contains(q.Keyword!)) ||
  160. (x.Name != null && x.Name.Contains(q.Keyword!)));
  161. var total = await query.CountAsync();
  162. var list = await query.OrderBy(x => x.Number).Skip((q.Page - 1) * q.PageSize).Take(q.PageSize).ToListAsync();
  163. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  164. }
  165. [HttpGet("{id:long}")]
  166. public async Task<IActionResult> GetDetailAsync(long id)
  167. {
  168. var master = await _rep.GetByIdAsync(id);
  169. if (master == null) return NotFound();
  170. var items = await _entryRep.AsQueryable().Where(x => x.MasterId == id).OrderBy(x => x.Seq).OrderBy(x => x.Id).ToListAsync();
  171. return Ok(new { master, items });
  172. }
  173. [HttpGet("options")]
  174. public async Task<IActionResult> GetOptionsAsync() => Ok(await _rep.AsQueryable()
  175. .OrderBy(x => x.Number)
  176. .Select(x => new AdoS0QualitySimpleOptionDto { Value = x.Id, Label = (x.Number ?? "") + " / " + (x.Name ?? "") })
  177. .ToListAsync());
  178. [HttpPost]
  179. public async Task<IActionResult> CreateAsync([FromBody] AdoS0QmsInspectionStandardUpsertDto dto)
  180. {
  181. var db = _rep.Context;
  182. await db.Ado.BeginTranAsync();
  183. try
  184. {
  185. var master = new AdoS0QmsInspectionStandard();
  186. ApplyInspectionStandard(master, dto);
  187. await _rep.AsInsertable(master).ExecuteReturnEntityAsync();
  188. await SyncInspectionStandardEntriesAsync(master.Id, dto.Items);
  189. await db.Ado.CommitTranAsync();
  190. return await GetDetailAsync(master.Id);
  191. }
  192. catch
  193. {
  194. await db.Ado.RollbackTranAsync();
  195. throw;
  196. }
  197. }
  198. [HttpPut("{id:long}")]
  199. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0QmsInspectionStandardUpsertDto dto)
  200. {
  201. var master = await _rep.GetByIdAsync(id);
  202. if (master == null) return NotFound();
  203. var db = _rep.Context;
  204. await db.Ado.BeginTranAsync();
  205. try
  206. {
  207. ApplyInspectionStandard(master, dto);
  208. await _rep.AsUpdateable(master).ExecuteCommandAsync();
  209. await _entryRep.AsDeleteable().Where(x => x.MasterId == id).ExecuteCommandAsync();
  210. await SyncInspectionStandardEntriesAsync(id, dto.Items);
  211. await db.Ado.CommitTranAsync();
  212. return await GetDetailAsync(id);
  213. }
  214. catch
  215. {
  216. await db.Ado.RollbackTranAsync();
  217. throw;
  218. }
  219. }
  220. [HttpDelete("{id:long}")]
  221. public async Task<IActionResult> DeleteAsync(long id)
  222. {
  223. var master = await _rep.GetByIdAsync(id);
  224. if (master == null) return NotFound();
  225. var db = _rep.Context;
  226. await db.Ado.BeginTranAsync();
  227. try
  228. {
  229. await _entryRep.AsDeleteable().Where(x => x.MasterId == id).ExecuteCommandAsync();
  230. await _rep.DeleteAsync(master);
  231. await db.Ado.CommitTranAsync();
  232. return Ok(new { message = "删除成功" });
  233. }
  234. catch
  235. {
  236. await db.Ado.RollbackTranAsync();
  237. throw;
  238. }
  239. }
  240. private static void ApplyInspectionStandard(AdoS0QmsInspectionStandard entity, AdoS0QmsInspectionStandardUpsertDto dto)
  241. {
  242. entity.Number = dto.Number.Trim();
  243. entity.Name = dto.Name.Trim();
  244. entity.Comment = NullIfWhiteSpace(dto.Comment);
  245. entity.ControlStrategy = NullIfWhiteSpace(dto.ControlStrategy);
  246. entity.CreateOrgId = dto.CreateOrgId;
  247. entity.UseOrgId = dto.UseOrgId;
  248. entity.Status = NullIfWhiteSpace(dto.Status);
  249. entity.EnableStatus = NullIfWhiteSpace(dto.EnableStatus);
  250. }
  251. private async Task SyncInspectionStandardEntriesAsync(long masterId, IEnumerable<AdoS0QmsInspectionStandardEntryDto> items)
  252. {
  253. var seq = 1L;
  254. foreach (var item in items)
  255. {
  256. var entity = new AdoS0QmsInspectionStandardEntry
  257. {
  258. MasterId = masterId,
  259. Seq = item.Seq ?? seq++,
  260. CheckItems = NullIfWhiteSpace(item.CheckItems),
  261. CheckContent = NullIfWhiteSpace(item.CheckContent),
  262. NormType = NullIfWhiteSpace(item.NormType),
  263. SpecValue = NullIfWhiteSpace(item.SpecValue),
  264. TopValue = item.TopValue,
  265. DownValue = item.DownValue,
  266. CheckBasisId = item.CheckBasisId,
  267. CheckMethodId = item.CheckMethodId,
  268. CheckFrequencyId = item.CheckFrequencyId,
  269. CheckInstructId = item.CheckInstructId,
  270. Unit = NullIfWhiteSpace(item.Unit),
  271. KeyQuality = item.KeyQuality
  272. };
  273. await _entryRep.AsInsertable(entity).ExecuteCommandAsync();
  274. }
  275. }
  276. }
  277. [ApiController]
  278. [Route("api/s0/quality/inspection-plans")]
  279. [AllowAnonymous]
  280. [NonUnify]
  281. public class AdoS0QmsInspectionPlansController : ControllerBase
  282. {
  283. private readonly SqlSugarRepository<AdoS0QmsInspectionPlan> _rep;
  284. private readonly SqlSugarRepository<AdoS0QmsInspectionPlanEntry> _entryRep;
  285. public AdoS0QmsInspectionPlansController(
  286. SqlSugarRepository<AdoS0QmsInspectionPlan> rep,
  287. SqlSugarRepository<AdoS0QmsInspectionPlanEntry> entryRep)
  288. {
  289. _rep = rep;
  290. _entryRep = entryRep;
  291. }
  292. [HttpGet]
  293. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0QualityPagedQueryDto q)
  294. {
  295. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  296. var query = _rep.AsQueryable()
  297. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword), x =>
  298. (x.Number != null && x.Number.Contains(q.Keyword!)) ||
  299. (x.Name != null && x.Name.Contains(q.Keyword!)));
  300. var total = await query.CountAsync();
  301. var list = await query.OrderBy(x => x.Number).Skip((q.Page - 1) * q.PageSize).Take(q.PageSize).ToListAsync();
  302. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  303. }
  304. [HttpGet("{id:long}")]
  305. public async Task<IActionResult> GetDetailAsync(long id)
  306. {
  307. var master = await _rep.GetByIdAsync(id);
  308. if (master == null) return NotFound();
  309. var items = await _entryRep.AsQueryable().Where(x => x.MasterId == id).OrderBy(x => x.Seq).OrderBy(x => x.Id).ToListAsync();
  310. return Ok(new { master, items });
  311. }
  312. [HttpPost]
  313. public async Task<IActionResult> CreateAsync([FromBody] AdoS0QmsInspectionPlanUpsertDto dto)
  314. {
  315. var db = _rep.Context;
  316. await db.Ado.BeginTranAsync();
  317. try
  318. {
  319. var master = new AdoS0QmsInspectionPlan();
  320. ApplyInspectionPlan(master, dto, true);
  321. await _rep.AsInsertable(master).ExecuteReturnEntityAsync();
  322. await SyncInspectionPlanEntriesAsync(master.Id, dto.Items);
  323. await db.Ado.CommitTranAsync();
  324. return await GetDetailAsync(master.Id);
  325. }
  326. catch
  327. {
  328. await db.Ado.RollbackTranAsync();
  329. throw;
  330. }
  331. }
  332. [HttpPut("{id:long}")]
  333. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0QmsInspectionPlanUpsertDto dto)
  334. {
  335. var master = await _rep.GetByIdAsync(id);
  336. if (master == null) return NotFound();
  337. var db = _rep.Context;
  338. await db.Ado.BeginTranAsync();
  339. try
  340. {
  341. ApplyInspectionPlan(master, dto, false);
  342. await _rep.AsUpdateable(master).ExecuteCommandAsync();
  343. await _entryRep.AsDeleteable().Where(x => x.MasterId == id).ExecuteCommandAsync();
  344. await SyncInspectionPlanEntriesAsync(id, dto.Items);
  345. await db.Ado.CommitTranAsync();
  346. return await GetDetailAsync(id);
  347. }
  348. catch
  349. {
  350. await db.Ado.RollbackTranAsync();
  351. throw;
  352. }
  353. }
  354. [HttpDelete("{id:long}")]
  355. public async Task<IActionResult> DeleteAsync(long id)
  356. {
  357. var master = await _rep.GetByIdAsync(id);
  358. if (master == null) return NotFound();
  359. var db = _rep.Context;
  360. await db.Ado.BeginTranAsync();
  361. try
  362. {
  363. await _entryRep.AsDeleteable().Where(x => x.MasterId == id).ExecuteCommandAsync();
  364. await _rep.DeleteAsync(master);
  365. await db.Ado.CommitTranAsync();
  366. return Ok(new { message = "删除成功" });
  367. }
  368. catch
  369. {
  370. await db.Ado.RollbackTranAsync();
  371. throw;
  372. }
  373. }
  374. private static void ApplyInspectionPlan(AdoS0QmsInspectionPlan entity, AdoS0QmsInspectionPlanUpsertDto dto, bool isCreate)
  375. {
  376. entity.Number = dto.Number.Trim();
  377. entity.Name = dto.Name.Trim();
  378. entity.BizTypeId = NullIfWhiteSpace(dto.BizTypeId);
  379. entity.Comment = NullIfWhiteSpace(dto.Comment);
  380. entity.ControlStrategy = NullIfWhiteSpace(dto.ControlStrategy);
  381. entity.CreateOrgId = dto.CreateOrgId;
  382. entity.UseOrgId = dto.UseOrgId;
  383. entity.Status = NullIfWhiteSpace(dto.Status);
  384. entity.EnableStatus = NullIfWhiteSpace(dto.EnableStatus);
  385. if (isCreate) entity.CreateTime = DateTime.Now;
  386. entity.ModifyTime = DateTime.Now;
  387. }
  388. private async Task SyncInspectionPlanEntriesAsync(long masterId, IEnumerable<AdoS0QmsInspectionPlanEntryDto> items)
  389. {
  390. var seq = 1;
  391. foreach (var item in items)
  392. {
  393. var entity = new AdoS0QmsInspectionPlanEntry
  394. {
  395. MasterId = masterId,
  396. Seq = item.Seq ?? seq++,
  397. SetupType = NullIfWhiteSpace(item.SetupType),
  398. MaterialCode = NullIfWhiteSpace(item.MaterialCode),
  399. MaterialName = NullIfWhiteSpace(item.MaterialName),
  400. MaterialTypeId = item.MaterialTypeId,
  401. SupplierId = NullIfWhiteSpace(item.SupplierId),
  402. SamplingSchemeId = item.SamplingSchemeId,
  403. InspectionStandardId = item.InspectionStandardId,
  404. InspectOrgId = item.InspectOrgId,
  405. InspectUserId = item.InspectUserId,
  406. QRouteId = item.QRouteId,
  407. OperationNo = NullIfWhiteSpace(item.OperationNo),
  408. OperationId = item.OperationId,
  409. InspectionFrequencyId = item.InspectionFrequencyId,
  410. ProcessSeq = NullIfWhiteSpace(item.ProcessSeq),
  411. InspectionType = item.InspectionType
  412. };
  413. await _entryRep.AsInsertable(entity).ExecuteCommandAsync();
  414. }
  415. }
  416. }
  417. [ApiController]
  418. [Route("api/s0/quality/raw-inspection-specs")]
  419. [AllowAnonymous]
  420. [NonUnify]
  421. public class AdoS0QmsRawInspectionSpecsController : ControllerBase
  422. {
  423. private readonly SqlSugarRepository<AdoS0QmsRawInspectionSpec> _rep;
  424. private readonly SqlSugarRepository<AdoS0QmsRawInspectionSpecEntry> _entryRep;
  425. public AdoS0QmsRawInspectionSpecsController(
  426. SqlSugarRepository<AdoS0QmsRawInspectionSpec> rep,
  427. SqlSugarRepository<AdoS0QmsRawInspectionSpecEntry> entryRep)
  428. {
  429. _rep = rep;
  430. _entryRep = entryRep;
  431. }
  432. [HttpGet]
  433. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0QualityPagedQueryDto q)
  434. {
  435. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  436. var query = _rep.AsQueryable()
  437. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword), x =>
  438. (x.FileNumber != null && x.FileNumber.Contains(q.Keyword!)) ||
  439. (x.RawMaterialName != null && x.RawMaterialName.Contains(q.Keyword!)) ||
  440. (x.MaterialCode != null && x.MaterialCode.Contains(q.Keyword!)));
  441. var total = await query.CountAsync();
  442. var list = await query.OrderByDescending(x => x.Id).Skip((q.Page - 1) * q.PageSize).Take(q.PageSize).ToListAsync();
  443. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  444. }
  445. [HttpGet("{id:long}")]
  446. public async Task<IActionResult> GetDetailAsync(long id)
  447. {
  448. var master = await _rep.GetByIdAsync(id);
  449. if (master == null) return NotFound();
  450. var items = await _entryRep.AsQueryable().Where(x => x.MasterId == id).OrderBy(x => x.Seq).OrderBy(x => x.Id).ToListAsync();
  451. return Ok(new { master, items });
  452. }
  453. [HttpPost]
  454. public async Task<IActionResult> CreateAsync([FromBody] AdoS0QmsRawInspectionSpecUpsertDto dto)
  455. {
  456. var db = _rep.Context;
  457. await db.Ado.BeginTranAsync();
  458. try
  459. {
  460. var master = new AdoS0QmsRawInspectionSpec();
  461. ApplyRawInspectionSpec(master, dto);
  462. await _rep.AsInsertable(master).ExecuteReturnEntityAsync();
  463. await SyncRawInspectionSpecEntriesAsync(master.Id, dto.Items);
  464. await db.Ado.CommitTranAsync();
  465. return await GetDetailAsync(master.Id);
  466. }
  467. catch
  468. {
  469. await db.Ado.RollbackTranAsync();
  470. throw;
  471. }
  472. }
  473. [HttpPut("{id:long}")]
  474. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0QmsRawInspectionSpecUpsertDto dto)
  475. {
  476. var master = await _rep.GetByIdAsync(id);
  477. if (master == null) return NotFound();
  478. var db = _rep.Context;
  479. await db.Ado.BeginTranAsync();
  480. try
  481. {
  482. ApplyRawInspectionSpec(master, dto);
  483. await _rep.AsUpdateable(master).ExecuteCommandAsync();
  484. await _entryRep.AsDeleteable().Where(x => x.MasterId == id).ExecuteCommandAsync();
  485. await SyncRawInspectionSpecEntriesAsync(id, dto.Items);
  486. await db.Ado.CommitTranAsync();
  487. return await GetDetailAsync(id);
  488. }
  489. catch
  490. {
  491. await db.Ado.RollbackTranAsync();
  492. throw;
  493. }
  494. }
  495. [HttpDelete("{id:long}")]
  496. public async Task<IActionResult> DeleteAsync(long id)
  497. {
  498. var master = await _rep.GetByIdAsync(id);
  499. if (master == null) return NotFound();
  500. var db = _rep.Context;
  501. await db.Ado.BeginTranAsync();
  502. try
  503. {
  504. await _entryRep.AsDeleteable().Where(x => x.MasterId == id).ExecuteCommandAsync();
  505. await _rep.DeleteAsync(master);
  506. await db.Ado.CommitTranAsync();
  507. return Ok(new { message = "删除成功" });
  508. }
  509. catch
  510. {
  511. await db.Ado.RollbackTranAsync();
  512. throw;
  513. }
  514. }
  515. private static void ApplyRawInspectionSpec(AdoS0QmsRawInspectionSpec entity, AdoS0QmsRawInspectionSpecUpsertDto dto)
  516. {
  517. entity.FileNumber = dto.FileNumber.Trim();
  518. entity.VersionNo = NullIfWhiteSpace(dto.VersionNo);
  519. entity.DrawingNo = NullIfWhiteSpace(dto.DrawingNo);
  520. entity.RawMaterialName = NullIfWhiteSpace(dto.RawMaterialName);
  521. entity.MaterialCode = NullIfWhiteSpace(dto.MaterialCode);
  522. entity.EffectiveDate = NullIfWhiteSpace(dto.EffectiveDate);
  523. entity.DrawingVersion = NullIfWhiteSpace(dto.DrawingVersion);
  524. entity.MaterialGrade = NullIfWhiteSpace(dto.MaterialGrade);
  525. entity.CavityOrMold = NullIfWhiteSpace(dto.CavityOrMold);
  526. entity.Attachment = NullIfWhiteSpace(dto.Attachment);
  527. entity.FileName = NullIfWhiteSpace(dto.FileName);
  528. entity.Title = NullIfWhiteSpace(dto.Title);
  529. }
  530. private async Task SyncRawInspectionSpecEntriesAsync(long masterId, IEnumerable<AdoS0QmsRawInspectionSpecEntryDto> items)
  531. {
  532. var seq = 1;
  533. foreach (var item in items)
  534. {
  535. var entity = new AdoS0QmsRawInspectionSpecEntry
  536. {
  537. MasterId = masterId,
  538. Seq = item.Seq ?? seq++,
  539. InspectionItem = NullIfWhiteSpace(item.InspectionItem),
  540. InspectionStandard = NullIfWhiteSpace(item.InspectionStandard),
  541. InspectionMethod = NullIfWhiteSpace(item.InspectionMethod),
  542. ImageCategory = NullIfWhiteSpace(item.ImageCategory),
  543. SamplingScheme = NullIfWhiteSpace(item.SamplingScheme),
  544. Remark = NullIfWhiteSpace(item.Remark),
  545. Attachment = NullIfWhiteSpace(item.Attachment),
  546. UpperLimit = NullIfWhiteSpace(item.UpperLimit),
  547. LowerLimit = NullIfWhiteSpace(item.LowerLimit)
  548. };
  549. await _entryRep.AsInsertable(entity).ExecuteCommandAsync();
  550. }
  551. }
  552. }
  553. [ApiController]
  554. [Route("api/s0/quality/process-inspection-specs")]
  555. [AllowAnonymous]
  556. [NonUnify]
  557. public class AdoS0QmsProcessInspectionSpecsController : ControllerBase
  558. {
  559. private readonly SqlSugarRepository<AdoS0QmsProcessInspectionSpec> _rep;
  560. private readonly SqlSugarRepository<AdoS0QmsProcessInspectionSpecEntry> _entryRep;
  561. public AdoS0QmsProcessInspectionSpecsController(
  562. SqlSugarRepository<AdoS0QmsProcessInspectionSpec> rep,
  563. SqlSugarRepository<AdoS0QmsProcessInspectionSpecEntry> entryRep)
  564. {
  565. _rep = rep;
  566. _entryRep = entryRep;
  567. }
  568. [HttpGet]
  569. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0QualityPagedQueryDto q)
  570. {
  571. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  572. var query = _rep.AsQueryable()
  573. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword), x =>
  574. (x.FileNumber != null && x.FileNumber.Contains(q.Keyword!)) ||
  575. (x.ApplicableModel != null && x.ApplicableModel.Contains(q.Keyword!)) ||
  576. (x.MaterialCode != null && x.MaterialCode.Contains(q.Keyword!)));
  577. var total = await query.CountAsync();
  578. var list = await query.OrderByDescending(x => x.Id).Skip((q.Page - 1) * q.PageSize).Take(q.PageSize).ToListAsync();
  579. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  580. }
  581. [HttpGet("{id:long}")]
  582. public async Task<IActionResult> GetDetailAsync(long id)
  583. {
  584. var master = await _rep.GetByIdAsync(id);
  585. if (master == null) return NotFound();
  586. var items = await _entryRep.AsQueryable().Where(x => x.MasterId == id).OrderBy(x => x.Id).ToListAsync();
  587. return Ok(new { master, items });
  588. }
  589. [HttpPost]
  590. public async Task<IActionResult> CreateAsync([FromBody] AdoS0QmsProcessInspectionSpecUpsertDto dto)
  591. {
  592. var db = _rep.Context;
  593. await db.Ado.BeginTranAsync();
  594. try
  595. {
  596. var master = new AdoS0QmsProcessInspectionSpec();
  597. ApplyProcessInspectionSpec(master, dto);
  598. await _rep.AsInsertable(master).ExecuteReturnEntityAsync();
  599. await SyncProcessInspectionSpecEntriesAsync(master.Id, dto.Items);
  600. await db.Ado.CommitTranAsync();
  601. return await GetDetailAsync(master.Id);
  602. }
  603. catch
  604. {
  605. await db.Ado.RollbackTranAsync();
  606. throw;
  607. }
  608. }
  609. [HttpPut("{id:long}")]
  610. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0QmsProcessInspectionSpecUpsertDto dto)
  611. {
  612. var master = await _rep.GetByIdAsync(id);
  613. if (master == null) return NotFound();
  614. var db = _rep.Context;
  615. await db.Ado.BeginTranAsync();
  616. try
  617. {
  618. ApplyProcessInspectionSpec(master, dto);
  619. await _rep.AsUpdateable(master).ExecuteCommandAsync();
  620. await _entryRep.AsDeleteable().Where(x => x.MasterId == id).ExecuteCommandAsync();
  621. await SyncProcessInspectionSpecEntriesAsync(id, dto.Items);
  622. await db.Ado.CommitTranAsync();
  623. return await GetDetailAsync(id);
  624. }
  625. catch
  626. {
  627. await db.Ado.RollbackTranAsync();
  628. throw;
  629. }
  630. }
  631. [HttpDelete("{id:long}")]
  632. public async Task<IActionResult> DeleteAsync(long id)
  633. {
  634. var master = await _rep.GetByIdAsync(id);
  635. if (master == null) return NotFound();
  636. var db = _rep.Context;
  637. await db.Ado.BeginTranAsync();
  638. try
  639. {
  640. await _entryRep.AsDeleteable().Where(x => x.MasterId == id).ExecuteCommandAsync();
  641. await _rep.DeleteAsync(master);
  642. await db.Ado.CommitTranAsync();
  643. return Ok(new { message = "删除成功" });
  644. }
  645. catch
  646. {
  647. await db.Ado.RollbackTranAsync();
  648. throw;
  649. }
  650. }
  651. private static void ApplyProcessInspectionSpec(AdoS0QmsProcessInspectionSpec entity, AdoS0QmsProcessInspectionSpecUpsertDto dto)
  652. {
  653. entity.ApplicableModel = NullIfWhiteSpace(dto.ApplicableModel);
  654. entity.FileNumber = dto.FileNumber.Trim();
  655. entity.VersionNo = NullIfWhiteSpace(dto.VersionNo);
  656. entity.EffectiveDate = NullIfWhiteSpace(dto.EffectiveDate);
  657. entity.Attachment = NullIfWhiteSpace(dto.Attachment);
  658. entity.MaterialCode = NullIfWhiteSpace(dto.MaterialCode);
  659. entity.Attachment2 = NullIfWhiteSpace(dto.Attachment2);
  660. entity.Version = dto.Version;
  661. }
  662. private async Task SyncProcessInspectionSpecEntriesAsync(long masterId, IEnumerable<AdoS0QmsProcessInspectionSpecEntryDto> items)
  663. {
  664. foreach (var item in items)
  665. {
  666. var entity = new AdoS0QmsProcessInspectionSpecEntry
  667. {
  668. MasterId = masterId,
  669. OperationCode = NullIfWhiteSpace(item.OperationCode),
  670. OperationName = NullIfWhiteSpace(item.OperationName),
  671. InspectionItem = NullIfWhiteSpace(item.InspectionItem),
  672. InspectionMethod = NullIfWhiteSpace(item.InspectionMethod),
  673. InspectionSpec = NullIfWhiteSpace(item.InspectionSpec),
  674. ImageCategory = NullIfWhiteSpace(item.ImageCategory),
  675. InspectionFrequency = NullIfWhiteSpace(item.InspectionFrequency),
  676. TechnicalStandard = NullIfWhiteSpace(item.TechnicalStandard),
  677. PeelingForce = item.PeelingForce,
  678. UpperLimit = NullIfWhiteSpace(item.UpperLimit),
  679. LowerLimit = NullIfWhiteSpace(item.LowerLimit)
  680. };
  681. await _entryRep.AsInsertable(entity).ExecuteCommandAsync();
  682. }
  683. }
  684. }