using Admin.NET.Plugin.AiDOP.Dto.S0.Quality; using Admin.NET.Plugin.AiDOP.Entity.S0.Quality; using Admin.NET.Plugin.AiDOP.Infrastructure; using System.Linq.Expressions; using static Admin.NET.Plugin.AiDOP.Controllers.S0.Quality.AdoS0QmsControllerHelpers; namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Quality; [ApiController] [Route("api/s0/quality/raw-whitelists")] [AllowAnonymous] [NonUnify] public class AdoS0QmsRawWhitelistsController : ControllerBase { private readonly SqlSugarRepository _rep; public AdoS0QmsRawWhitelistsController(SqlSugarRepository rep) { _rep = rep; } [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.SupplierCode != null && x.SupplierCode.Contains(q.Keyword!)) || (x.SupplierName != null && x.SupplierName.Contains(q.Keyword!))); var total = await query.CountAsync(); var list = await query.OrderBy(x => x.SupplierCode).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 GetAsync(long id) => OkOrNotFound(await _rep.GetByIdAsync(id)); [HttpGet("options")] public async Task GetOptionsAsync() { var list = await _rep.AsQueryable() .OrderBy(x => x.SupplierCode) .Select(x => new AdoS0QualitySimpleOptionDto { Value = x.Id, Label = (x.SupplierCode ?? "") + " / " + (x.SupplierName ?? "") }) .ToListAsync(); return Ok(list); } [HttpPost] public async Task CreateAsync([FromBody] AdoS0QmsRawWhitelistUpsertDto dto) { var entity = new AdoS0QmsRawWhitelist { SupplierCode = dto.SupplierCode.Trim(), SupplierName = dto.SupplierName.Trim() }; await _rep.AsInsertable(entity).ExecuteReturnEntityAsync(); return Ok(entity); } [HttpPut("{id:long}")] public async Task UpdateAsync(long id, [FromBody] AdoS0QmsRawWhitelistUpsertDto dto) { var entity = await _rep.GetByIdAsync(id); if (entity == null) return NotFound(); entity.SupplierCode = dto.SupplierCode.Trim(); entity.SupplierName = dto.SupplierName.Trim(); await _rep.AsUpdateable(entity).ExecuteCommandAsync(); return Ok(entity); } [HttpDelete("{id:long}")] public async Task DeleteAsync(long id) => await DeleteByIdAsync(_rep, id); } [ApiController] [Route("api/s0/quality/sampling-schemes")] [AllowAnonymous] [NonUnify] public class AdoS0QmsSamplingSchemesController : ControllerBase { private readonly SqlSugarRepository _rep; public AdoS0QmsSamplingSchemesController(SqlSugarRepository rep) { _rep = rep; } [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.Number != null && x.Number.Contains(q.Keyword!)) || (x.Name != null && x.Name.Contains(q.Keyword!))); var total = await query.CountAsync(); var list = await query.OrderBy(x => x.Number).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 GetAsync(long id) => OkOrNotFound(await _rep.GetByIdAsync(id)); [HttpGet("options")] public async Task GetOptionsAsync() => Ok(await BuildOptionsAsync(_rep.AsQueryable(), x => x.Id, x => $"{x.Number} / {x.Name}")); [HttpPost] public async Task CreateAsync([FromBody] AdoS0QmsSamplingSchemeUpsertDto dto) { var entity = new AdoS0QmsSamplingScheme(); ApplySamplingScheme(entity, dto, true); await _rep.AsInsertable(entity).ExecuteReturnEntityAsync(); return Ok(entity); } [HttpPut("{id:long}")] public async Task UpdateAsync(long id, [FromBody] AdoS0QmsSamplingSchemeUpsertDto dto) { var entity = await _rep.GetByIdAsync(id); if (entity == null) return NotFound(); ApplySamplingScheme(entity, dto, false); await _rep.AsUpdateable(entity).ExecuteCommandAsync(); return Ok(entity); } [HttpDelete("{id:long}")] public async Task DeleteAsync(long id) => await DeleteByIdAsync(_rep, id); private static void ApplySamplingScheme(AdoS0QmsSamplingScheme entity, AdoS0QmsSamplingSchemeUpsertDto dto, bool isCreate) { entity.Number = dto.Number.Trim(); entity.Name = dto.Name.Trim(); entity.SamplingType = NullIfWhiteSpace(dto.SamplingType); entity.InspectionLevel = NullIfWhiteSpace(dto.InspectionLevel); entity.Strictness = NullIfWhiteSpace(dto.Strictness); entity.AqlValue = NullIfWhiteSpace(dto.AqlValue); entity.InspectionType = NullIfWhiteSpace(dto.InspectionType); entity.InspectOrgId = dto.InspectOrgId; entity.InspectUserId = NullIfWhiteSpace(dto.InspectUserId); entity.Status = NullIfWhiteSpace(dto.Status); entity.EnableStatus = NullIfWhiteSpace(dto.EnableStatus); entity.Comment = NullIfWhiteSpace(dto.Comment); if (isCreate) entity.CreateTime = DateTime.Now; entity.ModifyTime = DateTime.Now; } } [ApiController] [Route("api/s0/quality/instruments")] [AllowAnonymous] [NonUnify] public class AdoS0QmsInspectionInstrumentsController : ControllerBase { private readonly SqlSugarRepository _rep; public AdoS0QmsInspectionInstrumentsController(SqlSugarRepository rep) { _rep = rep; } [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.Number != null && x.Number.Contains(q.Keyword!)) || (x.Name != null && x.Name.Contains(q.Keyword!)) || (x.Model != null && x.Model.Contains(q.Keyword!))); var total = await query.CountAsync(); var list = await query.OrderBy(x => x.Number).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 GetAsync(long id) => OkOrNotFound(await _rep.GetByIdAsync(id)); [HttpGet("options")] public async Task GetOptionsAsync() => Ok(await BuildOptionsAsync(_rep.AsQueryable(), x => x.Id, x => $"{x.Number} / {x.Name}")); [HttpPost] public async Task CreateAsync([FromBody] AdoS0QmsInspectionInstrumentUpsertDto dto) { var entity = new AdoS0QmsInspectionInstrument(); ApplyInspectionInstrument(entity, dto, true); await _rep.AsInsertable(entity).ExecuteReturnEntityAsync(); return Ok(entity); } [HttpPut("{id:long}")] public async Task UpdateAsync(long id, [FromBody] AdoS0QmsInspectionInstrumentUpsertDto dto) { var entity = await _rep.GetByIdAsync(id); if (entity == null) return NotFound(); ApplyInspectionInstrument(entity, dto, false); await _rep.AsUpdateable(entity).ExecuteCommandAsync(); return Ok(entity); } [HttpDelete("{id:long}")] public async Task DeleteAsync(long id) => await DeleteByIdAsync(_rep, id); private static void ApplyInspectionInstrument(AdoS0QmsInspectionInstrument entity, AdoS0QmsInspectionInstrumentUpsertDto dto, bool isCreate) { entity.Number = dto.Number.Trim(); entity.Name = dto.Name.Trim(); entity.Model = NullIfWhiteSpace(dto.Model); entity.Specification = NullIfWhiteSpace(dto.Specification); entity.Manufacturer = NullIfWhiteSpace(dto.Manufacturer); entity.Status = NullIfWhiteSpace(dto.Status); entity.EnableStatus = NullIfWhiteSpace(dto.EnableStatus); entity.Comment = NullIfWhiteSpace(dto.Comment); if (isCreate) entity.CreateTime = DateTime.Now; entity.ModifyTime = DateTime.Now; } } [ApiController] [Route("api/s0/quality/inspection-methods")] [AllowAnonymous] [NonUnify] public class AdoS0QmsInspectionMethodsController : ControllerBase { private readonly SqlSugarRepository _rep; public AdoS0QmsInspectionMethodsController(SqlSugarRepository rep) { _rep = rep; } [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.Number != null && x.Number.Contains(q.Keyword!)) || (x.Name != null && x.Name.Contains(q.Keyword!))); var total = await query.CountAsync(); var list = await query.OrderBy(x => x.Number).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 GetAsync(long id) => OkOrNotFound(await _rep.GetByIdAsync(id)); [HttpGet("options")] public async Task GetOptionsAsync() => Ok(await BuildOptionsAsync(_rep.AsQueryable(), x => x.Id, x => $"{x.Number} / {x.Name}")); [HttpPost] public async Task CreateAsync([FromBody] AdoS0QmsInspectionMethodUpsertDto dto) { var entity = new AdoS0QmsInspectionMethod(); ApplyInspectionMethod(entity, dto, true); await _rep.AsInsertable(entity).ExecuteReturnEntityAsync(); return Ok(entity); } [HttpPut("{id:long}")] public async Task UpdateAsync(long id, [FromBody] AdoS0QmsInspectionMethodUpsertDto dto) { var entity = await _rep.GetByIdAsync(id); if (entity == null) return NotFound(); ApplyInspectionMethod(entity, dto, false); await _rep.AsUpdateable(entity).ExecuteCommandAsync(); return Ok(entity); } [HttpDelete("{id:long}")] public async Task DeleteAsync(long id) => await DeleteByIdAsync(_rep, id); private static void ApplyInspectionMethod(AdoS0QmsInspectionMethod entity, AdoS0QmsInspectionMethodUpsertDto dto, bool isCreate) { entity.Number = dto.Number.Trim(); entity.Name = dto.Name.Trim(); entity.ControlStrategy = NullIfWhiteSpace(dto.ControlStrategy); entity.Status = NullIfWhiteSpace(dto.Status); entity.EnableStatus = NullIfWhiteSpace(dto.EnableStatus); entity.Comment = NullIfWhiteSpace(dto.Comment); if (isCreate) entity.CreateTime = DateTime.Now; entity.ModifyTime = DateTime.Now; } } [ApiController] [Route("api/s0/quality/inspection-items")] [AllowAnonymous] [NonUnify] public class AdoS0QmsInspectionItemsController : ControllerBase { private readonly SqlSugarRepository _rep; public AdoS0QmsInspectionItemsController(SqlSugarRepository rep) { _rep = rep; } [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.Number != null && x.Number.Contains(q.Keyword!)) || (x.Name != null && x.Name.Contains(q.Keyword!))); var total = await query.CountAsync(); var list = await query.OrderBy(x => x.Number).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 GetAsync(long id) => OkOrNotFound(await _rep.GetByIdAsync(id)); [HttpGet("options")] public async Task GetOptionsAsync() => Ok(await BuildOptionsAsync(_rep.AsQueryable(), x => x.Id, x => $"{x.Number} / {x.Name}")); [HttpPost] public async Task CreateAsync([FromBody] AdoS0QmsInspectionItemUpsertDto dto) { var entity = new AdoS0QmsInspectionItem(); ApplyInspectionItem(entity, dto, true); await _rep.AsInsertable(entity).ExecuteReturnEntityAsync(); return Ok(entity); } [HttpPut("{id:long}")] public async Task UpdateAsync(long id, [FromBody] AdoS0QmsInspectionItemUpsertDto dto) { var entity = await _rep.GetByIdAsync(id); if (entity == null) return NotFound(); ApplyInspectionItem(entity, dto, false); await _rep.AsUpdateable(entity).ExecuteCommandAsync(); return Ok(entity); } [HttpDelete("{id:long}")] public async Task DeleteAsync(long id) => await DeleteByIdAsync(_rep, id); private static void ApplyInspectionItem(AdoS0QmsInspectionItem entity, AdoS0QmsInspectionItemUpsertDto dto, bool isCreate) { entity.Number = dto.Number.Trim(); entity.Name = dto.Name.Trim(); entity.CheckMethodId = dto.CheckMethodId; entity.CheckBasisId = dto.CheckBasisId; entity.CheckInstructId = dto.CheckInstructId; entity.RadioGroupField = NullIfWhiteSpace(dto.RadioGroupField); entity.RadioGroupField1 = NullIfWhiteSpace(dto.RadioGroupField1); entity.MetricType = dto.MetricType; entity.Status = NullIfWhiteSpace(dto.Status); entity.EnableStatus = NullIfWhiteSpace(dto.EnableStatus); entity.Comment = NullIfWhiteSpace(dto.Comment); if (isCreate) entity.CreateTime = DateTime.Now; entity.ModifyTime = DateTime.Now; } } [ApiController] [Route("api/s0/quality/inspection-frequencies")] [AllowAnonymous] [NonUnify] public class AdoS0QmsInspectionFrequenciesController : ControllerBase { private readonly SqlSugarRepository _rep; public AdoS0QmsInspectionFrequenciesController(SqlSugarRepository rep) { _rep = rep; } [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.Number != null && x.Number.Contains(q.Keyword!)) || (x.Name != null && x.Name.Contains(q.Keyword!))); var total = await query.CountAsync(); var list = await query.OrderBy(x => x.Number).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 GetAsync(long id) => OkOrNotFound(await _rep.GetByIdAsync(id)); [HttpGet("options")] public async Task GetOptionsAsync() => Ok(await BuildOptionsAsync(_rep.AsQueryable(), x => x.Id, x => $"{x.Number} / {x.Name}")); [HttpPost] public async Task CreateAsync([FromBody] AdoS0QmsInspectionFrequencyUpsertDto dto) { var entity = new AdoS0QmsInspectionFrequency(); ApplyInspectionFrequency(entity, dto, true); await _rep.AsInsertable(entity).ExecuteReturnEntityAsync(); return Ok(entity); } [HttpPut("{id:long}")] public async Task UpdateAsync(long id, [FromBody] AdoS0QmsInspectionFrequencyUpsertDto dto) { var entity = await _rep.GetByIdAsync(id); if (entity == null) return NotFound(); ApplyInspectionFrequency(entity, dto, false); await _rep.AsUpdateable(entity).ExecuteCommandAsync(); return Ok(entity); } [HttpDelete("{id:long}")] public async Task DeleteAsync(long id) => await DeleteByIdAsync(_rep, id); private static void ApplyInspectionFrequency(AdoS0QmsInspectionFrequency entity, AdoS0QmsInspectionFrequencyUpsertDto dto, bool isCreate) { entity.Number = dto.Number.Trim(); entity.Name = dto.Name.Trim(); entity.Remark = NullIfWhiteSpace(dto.Remark); entity.Status = NullIfWhiteSpace(dto.Status); entity.EnableStatus = NullIfWhiteSpace(dto.EnableStatus); if (isCreate) entity.CreateTime = DateTime.Now; entity.ModifyTime = DateTime.Now; } } internal static partial class AdoS0QmsControllerHelpers { internal static async Task DeleteByIdAsync(SqlSugarRepository rep, long id) where TEntity : class, new() { var item = await rep.GetByIdAsync(id); if (item == null) return new NotFoundResult(); await rep.DeleteAsync(item); return new OkObjectResult(new { message = "删除成功" }); } internal static IActionResult OkOrNotFound(TEntity? entity) where TEntity : class => entity == null ? new NotFoundResult() : new OkObjectResult(entity); internal static string? NullIfWhiteSpace(string? value) => string.IsNullOrWhiteSpace(value) ? null : value.Trim(); internal static async Task> BuildOptionsAsync( ISugarQueryable query, Expression> valueExp, Expression> labelExp) where TEntity : class, new() { var rows = await query.ToListAsync(); var valueGetter = valueExp.Compile(); var labelGetter = labelExp.Compile(); return rows.Select(x => new AdoS0QualitySimpleOptionDto { Value = valueGetter(x), Label = labelGetter(x) }).OrderBy(x => x.Label).ToList(); } }