AdoS0QualityFqcOqcControllers.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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().Where(x => x.TenantId == QmsTenantScope.Current())
  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. master.TenantId = QmsTenantScope.Current();
  52. await _rep.AsInsertable(master).ExecuteReturnEntityAsync();
  53. await SyncFqcEntriesAsync(master.Id, dto.Items);
  54. await db.Ado.CommitTranAsync();
  55. return await GetDetailAsync(master.Id);
  56. }
  57. catch
  58. {
  59. await db.Ado.RollbackTranAsync();
  60. throw;
  61. }
  62. }
  63. [HttpPut("{id:long}")]
  64. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0QmsFqcInspectionSpecUpsertDto dto)
  65. {
  66. var master = await _rep.GetByIdAsync(id);
  67. if (master == null) return NotFound();
  68. var db = _rep.Context;
  69. await db.Ado.BeginTranAsync();
  70. try
  71. {
  72. ApplyFqcSpec(master, dto);
  73. await _rep.AsUpdateable(master).ExecuteCommandAsync();
  74. await _entryRep.AsDeleteable().Where(x => x.MasterId == id).ExecuteCommandAsync();
  75. await SyncFqcEntriesAsync(id, dto.Items);
  76. await db.Ado.CommitTranAsync();
  77. return await GetDetailAsync(id);
  78. }
  79. catch
  80. {
  81. await db.Ado.RollbackTranAsync();
  82. throw;
  83. }
  84. }
  85. [HttpDelete("{id:long}")]
  86. public async Task<IActionResult> DeleteAsync(long id)
  87. {
  88. var master = await _rep.GetByIdAsync(id);
  89. if (master == null) return NotFound();
  90. var db = _rep.Context;
  91. await db.Ado.BeginTranAsync();
  92. try
  93. {
  94. await _entryRep.AsDeleteable().Where(x => x.MasterId == id).ExecuteCommandAsync();
  95. await _rep.DeleteAsync(master);
  96. await db.Ado.CommitTranAsync();
  97. return Ok(new { message = "删除成功" });
  98. }
  99. catch
  100. {
  101. await db.Ado.RollbackTranAsync();
  102. throw;
  103. }
  104. }
  105. private static void ApplyFqcSpec(AdoS0QmsFqcInspectionSpec entity, AdoS0QmsFqcInspectionSpecUpsertDto dto)
  106. {
  107. entity.ApplicableModel = NullIfWhiteSpace(dto.ApplicableModel);
  108. entity.FileNumber = dto.FileNumber.Trim();
  109. entity.VersionNo = NullIfWhiteSpace(dto.VersionNo);
  110. entity.EffectiveDate = NullIfWhiteSpace(dto.EffectiveDate);
  111. entity.Attachment = NullIfWhiteSpace(dto.Attachment);
  112. entity.MaterialCode = NullIfWhiteSpace(dto.MaterialCode);
  113. entity.Attachment2 = NullIfWhiteSpace(dto.Attachment2);
  114. entity.Version = dto.Version;
  115. }
  116. private async Task SyncFqcEntriesAsync(long masterId, IEnumerable<AdoS0QmsFqcInspectionSpecEntryDto> items)
  117. {
  118. foreach (var item in items)
  119. {
  120. var entity = new AdoS0QmsFqcInspectionSpecEntry
  121. {
  122. MasterId = masterId,
  123. OperationCode = NullIfWhiteSpace(item.OperationCode),
  124. OperationName = NullIfWhiteSpace(item.OperationName),
  125. InspectionItem = NullIfWhiteSpace(item.InspectionItem),
  126. InspectionMethod = NullIfWhiteSpace(item.InspectionMethod),
  127. InspectionSpec = NullIfWhiteSpace(item.InspectionSpec),
  128. ImageCategory = NullIfWhiteSpace(item.ImageCategory),
  129. InspectionFrequency = NullIfWhiteSpace(item.InspectionFrequency),
  130. TechnicalStandard = NullIfWhiteSpace(item.TechnicalStandard),
  131. PeelingForce = item.PeelingForce,
  132. UpperLimit = NullIfWhiteSpace(item.UpperLimit),
  133. LowerLimit = NullIfWhiteSpace(item.LowerLimit)
  134. };
  135. await _entryRep.AsInsertable(entity).ExecuteCommandAsync();
  136. }
  137. }
  138. }
  139. [ApiController]
  140. [Route("api/s0/quality/oqc-inspection-specs")]
  141. [AllowAnonymous]
  142. [NonUnify]
  143. public class AdoS0QmsOqcInspectionSpecsController : ControllerBase
  144. {
  145. private readonly SqlSugarRepository<AdoS0QmsOqcInspectionSpec> _rep;
  146. private readonly SqlSugarRepository<AdoS0QmsOqcInspectionSpecEntry> _entryRep;
  147. public AdoS0QmsOqcInspectionSpecsController(
  148. SqlSugarRepository<AdoS0QmsOqcInspectionSpec> rep,
  149. SqlSugarRepository<AdoS0QmsOqcInspectionSpecEntry> 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.FileNumber != null && x.FileNumber.Contains(q.Keyword!))
  161. || (x.ApplicableModel != null && x.ApplicableModel.Contains(q.Keyword!))
  162. || (x.MaterialCode != null && x.MaterialCode.Contains(q.Keyword!)));
  163. var total = await query.CountAsync();
  164. var list = await query.OrderByDescending(x => x.Id).Skip((q.Page - 1) * q.PageSize).Take(q.PageSize).ToListAsync();
  165. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  166. }
  167. [HttpGet("{id:long}")]
  168. public async Task<IActionResult> GetDetailAsync(long id)
  169. {
  170. var master = await _rep.GetByIdAsync(id);
  171. if (master == null) return NotFound();
  172. var items = await _entryRep.AsQueryable().Where(x => x.MasterId == id).OrderBy(x => x.Id).ToListAsync();
  173. return Ok(new { master, items });
  174. }
  175. [HttpPost]
  176. public async Task<IActionResult> CreateAsync([FromBody] AdoS0QmsOqcInspectionSpecUpsertDto dto)
  177. {
  178. var db = _rep.Context;
  179. await db.Ado.BeginTranAsync();
  180. try
  181. {
  182. var master = new AdoS0QmsOqcInspectionSpec();
  183. ApplyOqcSpec(master, dto);
  184. master.TenantId = QmsTenantScope.Current();
  185. await _rep.AsInsertable(master).ExecuteReturnEntityAsync();
  186. await SyncOqcEntriesAsync(master.Id, dto.Items);
  187. await db.Ado.CommitTranAsync();
  188. return await GetDetailAsync(master.Id);
  189. }
  190. catch
  191. {
  192. await db.Ado.RollbackTranAsync();
  193. throw;
  194. }
  195. }
  196. [HttpPut("{id:long}")]
  197. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0QmsOqcInspectionSpecUpsertDto dto)
  198. {
  199. var master = await _rep.GetByIdAsync(id);
  200. if (master == null) return NotFound();
  201. var db = _rep.Context;
  202. await db.Ado.BeginTranAsync();
  203. try
  204. {
  205. ApplyOqcSpec(master, dto);
  206. await _rep.AsUpdateable(master).ExecuteCommandAsync();
  207. await _entryRep.AsDeleteable().Where(x => x.MasterId == id).ExecuteCommandAsync();
  208. await SyncOqcEntriesAsync(id, dto.Items);
  209. await db.Ado.CommitTranAsync();
  210. return await GetDetailAsync(id);
  211. }
  212. catch
  213. {
  214. await db.Ado.RollbackTranAsync();
  215. throw;
  216. }
  217. }
  218. [HttpDelete("{id:long}")]
  219. public async Task<IActionResult> DeleteAsync(long id)
  220. {
  221. var master = await _rep.GetByIdAsync(id);
  222. if (master == null) return NotFound();
  223. var db = _rep.Context;
  224. await db.Ado.BeginTranAsync();
  225. try
  226. {
  227. await _entryRep.AsDeleteable().Where(x => x.MasterId == id).ExecuteCommandAsync();
  228. await _rep.DeleteAsync(master);
  229. await db.Ado.CommitTranAsync();
  230. return Ok(new { message = "删除成功" });
  231. }
  232. catch
  233. {
  234. await db.Ado.RollbackTranAsync();
  235. throw;
  236. }
  237. }
  238. private static void ApplyOqcSpec(AdoS0QmsOqcInspectionSpec entity, AdoS0QmsOqcInspectionSpecUpsertDto dto)
  239. {
  240. entity.ApplicableModel = NullIfWhiteSpace(dto.ApplicableModel);
  241. entity.FileNumber = dto.FileNumber.Trim();
  242. entity.VersionNo = NullIfWhiteSpace(dto.VersionNo);
  243. entity.EffectiveDate = NullIfWhiteSpace(dto.EffectiveDate);
  244. entity.Attachment = NullIfWhiteSpace(dto.Attachment);
  245. entity.MaterialCode = NullIfWhiteSpace(dto.MaterialCode);
  246. entity.Attachment2 = NullIfWhiteSpace(dto.Attachment2);
  247. entity.Version = dto.Version;
  248. }
  249. private async Task SyncOqcEntriesAsync(long masterId, IEnumerable<AdoS0QmsOqcInspectionSpecEntryDto> items)
  250. {
  251. foreach (var item in items)
  252. {
  253. var entity = new AdoS0QmsOqcInspectionSpecEntry
  254. {
  255. MasterId = masterId,
  256. OperationCode = NullIfWhiteSpace(item.OperationCode),
  257. OperationName = NullIfWhiteSpace(item.OperationName),
  258. InspectionItem = NullIfWhiteSpace(item.InspectionItem),
  259. InspectionMethod = NullIfWhiteSpace(item.InspectionMethod),
  260. InspectionSpec = NullIfWhiteSpace(item.InspectionSpec),
  261. ImageCategory = NullIfWhiteSpace(item.ImageCategory),
  262. InspectionFrequency = NullIfWhiteSpace(item.InspectionFrequency),
  263. TechnicalStandard = NullIfWhiteSpace(item.TechnicalStandard),
  264. PeelingForce = item.PeelingForce,
  265. UpperLimit = NullIfWhiteSpace(item.UpperLimit),
  266. LowerLimit = NullIfWhiteSpace(item.LowerLimit)
  267. };
  268. await _entryRep.AsInsertable(entity).ExecuteCommandAsync();
  269. }
  270. }
  271. }