AdoS0QualityFqcOqcControllers.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. var tenantId = QmsTenantScope.Current();
  119. foreach (var item in items)
  120. {
  121. var entity = new AdoS0QmsFqcInspectionSpecEntry
  122. {
  123. TenantId = tenantId,
  124. MasterId = masterId,
  125. OperationCode = NullIfWhiteSpace(item.OperationCode),
  126. OperationName = NullIfWhiteSpace(item.OperationName),
  127. InspectionItem = NullIfWhiteSpace(item.InspectionItem),
  128. InspectionMethod = NullIfWhiteSpace(item.InspectionMethod),
  129. InspectionSpec = NullIfWhiteSpace(item.InspectionSpec),
  130. ImageCategory = NullIfWhiteSpace(item.ImageCategory),
  131. InspectionFrequency = NullIfWhiteSpace(item.InspectionFrequency),
  132. TechnicalStandard = NullIfWhiteSpace(item.TechnicalStandard),
  133. PeelingForce = item.PeelingForce,
  134. UpperLimit = NullIfWhiteSpace(item.UpperLimit),
  135. LowerLimit = NullIfWhiteSpace(item.LowerLimit)
  136. };
  137. await _entryRep.AsInsertable(entity).ExecuteCommandAsync();
  138. }
  139. }
  140. }
  141. [ApiController]
  142. [Route("api/s0/quality/oqc-inspection-specs")]
  143. [AllowAnonymous]
  144. [NonUnify]
  145. public class AdoS0QmsOqcInspectionSpecsController : ControllerBase
  146. {
  147. private readonly SqlSugarRepository<AdoS0QmsOqcInspectionSpec> _rep;
  148. private readonly SqlSugarRepository<AdoS0QmsOqcInspectionSpecEntry> _entryRep;
  149. public AdoS0QmsOqcInspectionSpecsController(
  150. SqlSugarRepository<AdoS0QmsOqcInspectionSpec> rep,
  151. SqlSugarRepository<AdoS0QmsOqcInspectionSpecEntry> 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.FileNumber != null && x.FileNumber.Contains(q.Keyword!))
  163. || (x.ApplicableModel != null && x.ApplicableModel.Contains(q.Keyword!))
  164. || (x.MaterialCode != null && x.MaterialCode.Contains(q.Keyword!)));
  165. var total = await query.CountAsync();
  166. var list = await query.OrderByDescending(x => x.Id).Skip((q.Page - 1) * q.PageSize).Take(q.PageSize).ToListAsync();
  167. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  168. }
  169. [HttpGet("{id:long}")]
  170. public async Task<IActionResult> GetDetailAsync(long id)
  171. {
  172. var master = await _rep.GetByIdAsync(id);
  173. if (master == null) return NotFound();
  174. var items = await _entryRep.AsQueryable().Where(x => x.MasterId == id).OrderBy(x => x.Id).ToListAsync();
  175. return Ok(new { master, items });
  176. }
  177. [HttpPost]
  178. public async Task<IActionResult> CreateAsync([FromBody] AdoS0QmsOqcInspectionSpecUpsertDto dto)
  179. {
  180. var db = _rep.Context;
  181. await db.Ado.BeginTranAsync();
  182. try
  183. {
  184. var master = new AdoS0QmsOqcInspectionSpec();
  185. ApplyOqcSpec(master, dto);
  186. master.TenantId = QmsTenantScope.Current();
  187. await _rep.AsInsertable(master).ExecuteReturnEntityAsync();
  188. await SyncOqcEntriesAsync(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] AdoS0QmsOqcInspectionSpecUpsertDto 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. ApplyOqcSpec(master, dto);
  208. await _rep.AsUpdateable(master).ExecuteCommandAsync();
  209. await _entryRep.AsDeleteable().Where(x => x.MasterId == id).ExecuteCommandAsync();
  210. await SyncOqcEntriesAsync(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 ApplyOqcSpec(AdoS0QmsOqcInspectionSpec entity, AdoS0QmsOqcInspectionSpecUpsertDto dto)
  241. {
  242. entity.ApplicableModel = NullIfWhiteSpace(dto.ApplicableModel);
  243. entity.FileNumber = dto.FileNumber.Trim();
  244. entity.VersionNo = NullIfWhiteSpace(dto.VersionNo);
  245. entity.EffectiveDate = NullIfWhiteSpace(dto.EffectiveDate);
  246. entity.Attachment = NullIfWhiteSpace(dto.Attachment);
  247. entity.MaterialCode = NullIfWhiteSpace(dto.MaterialCode);
  248. entity.Attachment2 = NullIfWhiteSpace(dto.Attachment2);
  249. entity.Version = dto.Version;
  250. }
  251. private async Task SyncOqcEntriesAsync(long masterId, IEnumerable<AdoS0QmsOqcInspectionSpecEntryDto> items)
  252. {
  253. var tenantId = QmsTenantScope.Current();
  254. foreach (var item in items)
  255. {
  256. var entity = new AdoS0QmsOqcInspectionSpecEntry
  257. {
  258. TenantId = tenantId,
  259. MasterId = masterId,
  260. OperationCode = NullIfWhiteSpace(item.OperationCode),
  261. OperationName = NullIfWhiteSpace(item.OperationName),
  262. InspectionItem = NullIfWhiteSpace(item.InspectionItem),
  263. InspectionMethod = NullIfWhiteSpace(item.InspectionMethod),
  264. InspectionSpec = NullIfWhiteSpace(item.InspectionSpec),
  265. ImageCategory = NullIfWhiteSpace(item.ImageCategory),
  266. InspectionFrequency = NullIfWhiteSpace(item.InspectionFrequency),
  267. TechnicalStandard = NullIfWhiteSpace(item.TechnicalStandard),
  268. PeelingForce = item.PeelingForce,
  269. UpperLimit = NullIfWhiteSpace(item.UpperLimit),
  270. LowerLimit = NullIfWhiteSpace(item.LowerLimit)
  271. };
  272. await _entryRep.AsInsertable(entity).ExecuteCommandAsync();
  273. }
  274. }
  275. }