AdoS0QualityAggregateControllers.cs 29 KB

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