using Admin.NET.Plugin.AiDOP.Dto.S0.Quality; using Admin.NET.Plugin.AiDOP.Entity.S0.Quality; using Admin.NET.Plugin.AiDOP.Infrastructure; using static Admin.NET.Plugin.AiDOP.Controllers.S0.Quality.AdoS0QmsControllerHelpers; namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Quality; [ApiController] [Route("api/s0/quality/fqc-inspection-specs")] [AllowAnonymous] [NonUnify] public class AdoS0QmsFqcInspectionSpecsController : ControllerBase { private readonly SqlSugarRepository _rep; private readonly SqlSugarRepository _entryRep; public AdoS0QmsFqcInspectionSpecsController( SqlSugarRepository rep, SqlSugarRepository entryRep) { _rep = rep; _entryRep = entryRep; } [HttpGet] public async Task GetPagedAsync([FromQuery] AdoS0QualityPagedQueryDto q) { (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize); var query = _rep.AsQueryable() .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword), x => (x.FileNumber != null && x.FileNumber.Contains(q.Keyword!)) || (x.ApplicableModel != null && x.ApplicableModel.Contains(q.Keyword!)) || (x.MaterialCode != null && x.MaterialCode.Contains(q.Keyword!))); var total = await query.CountAsync(); var list = await query.OrderByDescending(x => x.Id).Skip((q.Page - 1) * q.PageSize).Take(q.PageSize).ToListAsync(); return Ok(new { total, page = q.Page, pageSize = q.PageSize, list }); } [HttpGet("{id:long}")] public async Task GetDetailAsync(long id) { var master = await _rep.GetByIdAsync(id); if (master == null) return NotFound(); var items = await _entryRep.AsQueryable().Where(x => x.MasterId == id).OrderBy(x => x.Id).ToListAsync(); return Ok(new { master, items }); } [HttpPost] public async Task CreateAsync([FromBody] AdoS0QmsFqcInspectionSpecUpsertDto dto) { var db = _rep.Context; await db.Ado.BeginTranAsync(); try { var master = new AdoS0QmsFqcInspectionSpec(); ApplyFqcSpec(master, dto); await _rep.AsInsertable(master).ExecuteReturnEntityAsync(); await SyncFqcEntriesAsync(master.Id, dto.Items); await db.Ado.CommitTranAsync(); return await GetDetailAsync(master.Id); } catch { await db.Ado.RollbackTranAsync(); throw; } } [HttpPut("{id:long}")] public async Task UpdateAsync(long id, [FromBody] AdoS0QmsFqcInspectionSpecUpsertDto dto) { var master = await _rep.GetByIdAsync(id); if (master == null) return NotFound(); var db = _rep.Context; await db.Ado.BeginTranAsync(); try { ApplyFqcSpec(master, dto); await _rep.AsUpdateable(master).ExecuteCommandAsync(); await _entryRep.AsDeleteable().Where(x => x.MasterId == id).ExecuteCommandAsync(); await SyncFqcEntriesAsync(id, dto.Items); await db.Ado.CommitTranAsync(); return await GetDetailAsync(id); } catch { await db.Ado.RollbackTranAsync(); throw; } } [HttpDelete("{id:long}")] public async Task DeleteAsync(long id) { var master = await _rep.GetByIdAsync(id); if (master == null) return NotFound(); var db = _rep.Context; await db.Ado.BeginTranAsync(); try { await _entryRep.AsDeleteable().Where(x => x.MasterId == id).ExecuteCommandAsync(); await _rep.DeleteAsync(master); await db.Ado.CommitTranAsync(); return Ok(new { message = "删除成功" }); } catch { await db.Ado.RollbackTranAsync(); throw; } } private static void ApplyFqcSpec(AdoS0QmsFqcInspectionSpec entity, AdoS0QmsFqcInspectionSpecUpsertDto dto) { entity.ApplicableModel = NullIfWhiteSpace(dto.ApplicableModel); entity.FileNumber = dto.FileNumber.Trim(); entity.VersionNo = NullIfWhiteSpace(dto.VersionNo); entity.EffectiveDate = NullIfWhiteSpace(dto.EffectiveDate); entity.Attachment = NullIfWhiteSpace(dto.Attachment); entity.MaterialCode = NullIfWhiteSpace(dto.MaterialCode); entity.Attachment2 = NullIfWhiteSpace(dto.Attachment2); entity.Version = dto.Version; } private async Task SyncFqcEntriesAsync(long masterId, IEnumerable items) { foreach (var item in items) { var entity = new AdoS0QmsFqcInspectionSpecEntry { MasterId = masterId, OperationCode = NullIfWhiteSpace(item.OperationCode), OperationName = NullIfWhiteSpace(item.OperationName), InspectionItem = NullIfWhiteSpace(item.InspectionItem), InspectionMethod = NullIfWhiteSpace(item.InspectionMethod), InspectionSpec = NullIfWhiteSpace(item.InspectionSpec), ImageCategory = NullIfWhiteSpace(item.ImageCategory), InspectionFrequency = NullIfWhiteSpace(item.InspectionFrequency), TechnicalStandard = NullIfWhiteSpace(item.TechnicalStandard), PeelingForce = item.PeelingForce, UpperLimit = NullIfWhiteSpace(item.UpperLimit), LowerLimit = NullIfWhiteSpace(item.LowerLimit) }; await _entryRep.AsInsertable(entity).ExecuteCommandAsync(); } } } [ApiController] [Route("api/s0/quality/oqc-inspection-specs")] [AllowAnonymous] [NonUnify] public class AdoS0QmsOqcInspectionSpecsController : ControllerBase { private readonly SqlSugarRepository _rep; private readonly SqlSugarRepository _entryRep; public AdoS0QmsOqcInspectionSpecsController( SqlSugarRepository rep, SqlSugarRepository entryRep) { _rep = rep; _entryRep = entryRep; } [HttpGet] public async Task GetPagedAsync([FromQuery] AdoS0QualityPagedQueryDto q) { (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.PageSize); var query = _rep.AsQueryable() .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword), x => (x.FileNumber != null && x.FileNumber.Contains(q.Keyword!)) || (x.ApplicableModel != null && x.ApplicableModel.Contains(q.Keyword!)) || (x.MaterialCode != null && x.MaterialCode.Contains(q.Keyword!))); var total = await query.CountAsync(); var list = await query.OrderByDescending(x => x.Id).Skip((q.Page - 1) * q.PageSize).Take(q.PageSize).ToListAsync(); return Ok(new { total, page = q.Page, pageSize = q.PageSize, list }); } [HttpGet("{id:long}")] public async Task GetDetailAsync(long id) { var master = await _rep.GetByIdAsync(id); if (master == null) return NotFound(); var items = await _entryRep.AsQueryable().Where(x => x.MasterId == id).OrderBy(x => x.Id).ToListAsync(); return Ok(new { master, items }); } [HttpPost] public async Task CreateAsync([FromBody] AdoS0QmsOqcInspectionSpecUpsertDto dto) { var db = _rep.Context; await db.Ado.BeginTranAsync(); try { var master = new AdoS0QmsOqcInspectionSpec(); ApplyOqcSpec(master, dto); await _rep.AsInsertable(master).ExecuteReturnEntityAsync(); await SyncOqcEntriesAsync(master.Id, dto.Items); await db.Ado.CommitTranAsync(); return await GetDetailAsync(master.Id); } catch { await db.Ado.RollbackTranAsync(); throw; } } [HttpPut("{id:long}")] public async Task UpdateAsync(long id, [FromBody] AdoS0QmsOqcInspectionSpecUpsertDto dto) { var master = await _rep.GetByIdAsync(id); if (master == null) return NotFound(); var db = _rep.Context; await db.Ado.BeginTranAsync(); try { ApplyOqcSpec(master, dto); await _rep.AsUpdateable(master).ExecuteCommandAsync(); await _entryRep.AsDeleteable().Where(x => x.MasterId == id).ExecuteCommandAsync(); await SyncOqcEntriesAsync(id, dto.Items); await db.Ado.CommitTranAsync(); return await GetDetailAsync(id); } catch { await db.Ado.RollbackTranAsync(); throw; } } [HttpDelete("{id:long}")] public async Task DeleteAsync(long id) { var master = await _rep.GetByIdAsync(id); if (master == null) return NotFound(); var db = _rep.Context; await db.Ado.BeginTranAsync(); try { await _entryRep.AsDeleteable().Where(x => x.MasterId == id).ExecuteCommandAsync(); await _rep.DeleteAsync(master); await db.Ado.CommitTranAsync(); return Ok(new { message = "删除成功" }); } catch { await db.Ado.RollbackTranAsync(); throw; } } private static void ApplyOqcSpec(AdoS0QmsOqcInspectionSpec entity, AdoS0QmsOqcInspectionSpecUpsertDto dto) { entity.ApplicableModel = NullIfWhiteSpace(dto.ApplicableModel); entity.FileNumber = dto.FileNumber.Trim(); entity.VersionNo = NullIfWhiteSpace(dto.VersionNo); entity.EffectiveDate = NullIfWhiteSpace(dto.EffectiveDate); entity.Attachment = NullIfWhiteSpace(dto.Attachment); entity.MaterialCode = NullIfWhiteSpace(dto.MaterialCode); entity.Attachment2 = NullIfWhiteSpace(dto.Attachment2); entity.Version = dto.Version; } private async Task SyncOqcEntriesAsync(long masterId, IEnumerable items) { foreach (var item in items) { var entity = new AdoS0QmsOqcInspectionSpecEntry { MasterId = masterId, OperationCode = NullIfWhiteSpace(item.OperationCode), OperationName = NullIfWhiteSpace(item.OperationName), InspectionItem = NullIfWhiteSpace(item.InspectionItem), InspectionMethod = NullIfWhiteSpace(item.InspectionMethod), InspectionSpec = NullIfWhiteSpace(item.InspectionSpec), ImageCategory = NullIfWhiteSpace(item.ImageCategory), InspectionFrequency = NullIfWhiteSpace(item.InspectionFrequency), TechnicalStandard = NullIfWhiteSpace(item.TechnicalStandard), PeelingForce = item.PeelingForce, UpperLimit = NullIfWhiteSpace(item.UpperLimit), LowerLimit = NullIfWhiteSpace(item.LowerLimit) }; await _entryRep.AsInsertable(entity).ExecuteCommandAsync(); } } }