AdoS0QualityFqcOqcControllers.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. [NonUnify]
  9. public class AdoS0QmsFqcInspectionSpecsController : ControllerBase
  10. {
  11. private readonly SqlSugarRepository<AdoS0QmsFqcInspectionSpec> _rep;
  12. private readonly SqlSugarRepository<AdoS0QmsFqcInspectionSpecEntry> _entryRep;
  13. public AdoS0QmsFqcInspectionSpecsController(
  14. SqlSugarRepository<AdoS0QmsFqcInspectionSpec> rep,
  15. SqlSugarRepository<AdoS0QmsFqcInspectionSpecEntry> entryRep)
  16. {
  17. _rep = rep;
  18. _entryRep = entryRep;
  19. }
  20. [HttpGet]
  21. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0QualityPagedQueryDto q)
  22. {
  23. if (!AdoS0TenantScope.TryResolveRequired(out var tenantId, out var tenantError)) return tenantError!;
  24. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  25. var query = _rep.AsQueryable().Where(x => x.TenantId == tenantId)
  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. if (!AdoS0TenantScope.TryResolveRequired(out var tenantId, out var tenantError)) return tenantError!;
  38. var master = await _rep.ByIdScopedByColumnAsync(id, tenantId);
  39. if (master == null) return NotFound();
  40. var items = await _entryRep.AsQueryable().Where(x => x.MasterId == id).OrderBy(x => x.Id).ToListAsync();
  41. return Ok(new { master, items });
  42. }
  43. [HttpPost]
  44. public async Task<IActionResult> CreateAsync([FromBody] AdoS0QmsFqcInspectionSpecUpsertDto dto)
  45. {
  46. if (!AdoS0TenantScope.TryResolveRequired(out var tenantId, out var tenantError)) return tenantError!;
  47. var db = _rep.Context;
  48. await db.Ado.BeginTranAsync();
  49. try
  50. {
  51. var master = new AdoS0QmsFqcInspectionSpec();
  52. ApplyFqcSpec(master, dto);
  53. master.TenantId = tenantId;
  54. await _rep.AsInsertable(master).ExecuteReturnEntityAsync();
  55. await SyncFqcEntriesAsync(master.Id, dto.Items, tenantId);
  56. await db.Ado.CommitTranAsync();
  57. return await GetDetailAsync(master.Id);
  58. }
  59. catch
  60. {
  61. await db.Ado.RollbackTranAsync();
  62. throw;
  63. }
  64. }
  65. [HttpPut("{id:long}")]
  66. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0QmsFqcInspectionSpecUpsertDto dto)
  67. {
  68. if (!AdoS0TenantScope.TryResolveRequired(out var tenantId, out var tenantError)) return tenantError!;
  69. var master = await _rep.ByIdScopedByColumnAsync(id, tenantId);
  70. if (master == null) return NotFound();
  71. var db = _rep.Context;
  72. await db.Ado.BeginTranAsync();
  73. try
  74. {
  75. ApplyFqcSpec(master, dto);
  76. await _rep.AsUpdateable(master).ExecuteCommandAsync();
  77. await _entryRep.AsDeleteable().Where(x => x.MasterId == id).ExecuteCommandAsync();
  78. await SyncFqcEntriesAsync(id, dto.Items, tenantId);
  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. if (!AdoS0TenantScope.TryResolveRequired(out var tenantId, out var tenantError)) return tenantError!;
  92. var master = await _rep.ByIdScopedByColumnAsync(id, tenantId);
  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 ApplyFqcSpec(AdoS0QmsFqcInspectionSpec entity, AdoS0QmsFqcInspectionSpecUpsertDto dto)
  110. {
  111. entity.ApplicableModel = NullIfWhiteSpace(dto.ApplicableModel);
  112. entity.FileNumber = dto.FileNumber.Trim();
  113. entity.VersionNo = NullIfWhiteSpace(dto.VersionNo);
  114. entity.EffectiveDate = NullIfWhiteSpace(dto.EffectiveDate);
  115. entity.Attachment = NullIfWhiteSpace(dto.Attachment);
  116. entity.MaterialCode = NullIfWhiteSpace(dto.MaterialCode);
  117. entity.Attachment2 = NullIfWhiteSpace(dto.Attachment2);
  118. entity.Version = dto.Version;
  119. }
  120. private async Task SyncFqcEntriesAsync(long masterId, IEnumerable<AdoS0QmsFqcInspectionSpecEntryDto> items, long tenantId)
  121. {
  122. foreach (var item in items)
  123. {
  124. var entity = new AdoS0QmsFqcInspectionSpecEntry
  125. {
  126. TenantId = tenantId,
  127. MasterId = masterId,
  128. OperationCode = NullIfWhiteSpace(item.OperationCode),
  129. OperationName = NullIfWhiteSpace(item.OperationName),
  130. InspectionItem = NullIfWhiteSpace(item.InspectionItem),
  131. InspectionMethod = NullIfWhiteSpace(item.InspectionMethod),
  132. InspectionSpec = NullIfWhiteSpace(item.InspectionSpec),
  133. ImageCategory = NullIfWhiteSpace(item.ImageCategory),
  134. InspectionFrequency = NullIfWhiteSpace(item.InspectionFrequency),
  135. TechnicalStandard = NullIfWhiteSpace(item.TechnicalStandard),
  136. PeelingForce = item.PeelingForce,
  137. UpperLimit = NullIfWhiteSpace(item.UpperLimit),
  138. LowerLimit = NullIfWhiteSpace(item.LowerLimit)
  139. };
  140. await _entryRep.AsInsertable(entity).ExecuteCommandAsync();
  141. }
  142. }
  143. }
  144. [ApiController]
  145. [Route("api/s0/quality/oqc-inspection-specs")]
  146. [NonUnify]
  147. public class AdoS0QmsOqcInspectionSpecsController : ControllerBase
  148. {
  149. private readonly SqlSugarRepository<AdoS0QmsOqcInspectionSpec> _rep;
  150. private readonly SqlSugarRepository<AdoS0QmsOqcInspectionSpecEntry> _entryRep;
  151. public AdoS0QmsOqcInspectionSpecsController(
  152. SqlSugarRepository<AdoS0QmsOqcInspectionSpec> rep,
  153. SqlSugarRepository<AdoS0QmsOqcInspectionSpecEntry> entryRep)
  154. {
  155. _rep = rep;
  156. _entryRep = entryRep;
  157. }
  158. [HttpGet]
  159. public async Task<IActionResult> GetPagedAsync([FromQuery] AdoS0QualityPagedQueryDto q)
  160. {
  161. if (!AdoS0TenantScope.TryResolveRequired(out var tenantId, out var tenantError)) return tenantError!;
  162. (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize);
  163. var query = _rep.AsQueryable().Where(x => x.TenantId == tenantId)
  164. .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword), x =>
  165. (x.FileNumber != null && x.FileNumber.Contains(q.Keyword!))
  166. || (x.ApplicableModel != null && x.ApplicableModel.Contains(q.Keyword!))
  167. || (x.MaterialCode != null && x.MaterialCode.Contains(q.Keyword!)));
  168. var total = await query.CountAsync();
  169. var list = await query.OrderByDescending(x => x.Id).Skip((q.Page - 1) * q.PageSize).Take(q.PageSize).ToListAsync();
  170. return Ok(new { total, page = q.Page, pageSize = q.PageSize, list });
  171. }
  172. [HttpGet("{id:long}")]
  173. public async Task<IActionResult> GetDetailAsync(long id)
  174. {
  175. if (!AdoS0TenantScope.TryResolveRequired(out var tenantId, out var tenantError)) return tenantError!;
  176. var master = await _rep.ByIdScopedByColumnAsync(id, tenantId);
  177. if (master == null) return NotFound();
  178. var items = await _entryRep.AsQueryable().Where(x => x.MasterId == id).OrderBy(x => x.Id).ToListAsync();
  179. return Ok(new { master, items });
  180. }
  181. [HttpPost]
  182. public async Task<IActionResult> CreateAsync([FromBody] AdoS0QmsOqcInspectionSpecUpsertDto dto)
  183. {
  184. if (!AdoS0TenantScope.TryResolveRequired(out var tenantId, out var tenantError)) return tenantError!;
  185. var db = _rep.Context;
  186. await db.Ado.BeginTranAsync();
  187. try
  188. {
  189. var master = new AdoS0QmsOqcInspectionSpec();
  190. ApplyOqcSpec(master, dto);
  191. master.TenantId = tenantId;
  192. await _rep.AsInsertable(master).ExecuteReturnEntityAsync();
  193. await SyncOqcEntriesAsync(master.Id, dto.Items, tenantId);
  194. await db.Ado.CommitTranAsync();
  195. return await GetDetailAsync(master.Id);
  196. }
  197. catch
  198. {
  199. await db.Ado.RollbackTranAsync();
  200. throw;
  201. }
  202. }
  203. [HttpPut("{id:long}")]
  204. public async Task<IActionResult> UpdateAsync(long id, [FromBody] AdoS0QmsOqcInspectionSpecUpsertDto dto)
  205. {
  206. if (!AdoS0TenantScope.TryResolveRequired(out var tenantId, out var tenantError)) return tenantError!;
  207. var master = await _rep.ByIdScopedByColumnAsync(id, tenantId);
  208. if (master == null) return NotFound();
  209. var db = _rep.Context;
  210. await db.Ado.BeginTranAsync();
  211. try
  212. {
  213. ApplyOqcSpec(master, dto);
  214. await _rep.AsUpdateable(master).ExecuteCommandAsync();
  215. await _entryRep.AsDeleteable().Where(x => x.MasterId == id).ExecuteCommandAsync();
  216. await SyncOqcEntriesAsync(id, dto.Items, tenantId);
  217. await db.Ado.CommitTranAsync();
  218. return await GetDetailAsync(id);
  219. }
  220. catch
  221. {
  222. await db.Ado.RollbackTranAsync();
  223. throw;
  224. }
  225. }
  226. [HttpDelete("{id:long}")]
  227. public async Task<IActionResult> DeleteAsync(long id)
  228. {
  229. if (!AdoS0TenantScope.TryResolveRequired(out var tenantId, out var tenantError)) return tenantError!;
  230. var master = await _rep.ByIdScopedByColumnAsync(id, tenantId);
  231. if (master == null) return NotFound();
  232. var db = _rep.Context;
  233. await db.Ado.BeginTranAsync();
  234. try
  235. {
  236. await _entryRep.AsDeleteable().Where(x => x.MasterId == id).ExecuteCommandAsync();
  237. await _rep.DeleteAsync(master);
  238. await db.Ado.CommitTranAsync();
  239. return Ok(new { message = "删除成功" });
  240. }
  241. catch
  242. {
  243. await db.Ado.RollbackTranAsync();
  244. throw;
  245. }
  246. }
  247. private static void ApplyOqcSpec(AdoS0QmsOqcInspectionSpec entity, AdoS0QmsOqcInspectionSpecUpsertDto dto)
  248. {
  249. entity.ApplicableModel = NullIfWhiteSpace(dto.ApplicableModel);
  250. entity.FileNumber = dto.FileNumber.Trim();
  251. entity.VersionNo = NullIfWhiteSpace(dto.VersionNo);
  252. entity.EffectiveDate = NullIfWhiteSpace(dto.EffectiveDate);
  253. entity.Attachment = NullIfWhiteSpace(dto.Attachment);
  254. entity.MaterialCode = NullIfWhiteSpace(dto.MaterialCode);
  255. entity.Attachment2 = NullIfWhiteSpace(dto.Attachment2);
  256. entity.Version = dto.Version;
  257. }
  258. private async Task SyncOqcEntriesAsync(long masterId, IEnumerable<AdoS0QmsOqcInspectionSpecEntryDto> items, long tenantId)
  259. {
  260. foreach (var item in items)
  261. {
  262. var entity = new AdoS0QmsOqcInspectionSpecEntry
  263. {
  264. TenantId = tenantId,
  265. MasterId = masterId,
  266. OperationCode = NullIfWhiteSpace(item.OperationCode),
  267. OperationName = NullIfWhiteSpace(item.OperationName),
  268. InspectionItem = NullIfWhiteSpace(item.InspectionItem),
  269. InspectionMethod = NullIfWhiteSpace(item.InspectionMethod),
  270. InspectionSpec = NullIfWhiteSpace(item.InspectionSpec),
  271. ImageCategory = NullIfWhiteSpace(item.ImageCategory),
  272. InspectionFrequency = NullIfWhiteSpace(item.InspectionFrequency),
  273. TechnicalStandard = NullIfWhiteSpace(item.TechnicalStandard),
  274. PeelingForce = item.PeelingForce,
  275. UpperLimit = NullIfWhiteSpace(item.UpperLimit),
  276. LowerLimit = NullIfWhiteSpace(item.LowerLimit)
  277. };
  278. await _entryRep.AsInsertable(entity).ExecuteCommandAsync();
  279. }
  280. }
  281. }