AdoS0QualityAggregateControllers.cs 29 KB

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