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/base-types")] [AllowAnonymous] [NonUnify] public class AdoS0QmsQualityBaseTypesController : ControllerBase { private readonly SqlSugarRepository _rep; public AdoS0QmsQualityBaseTypesController(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.TypeCategory.Contains(q.Keyword!) || x.TypeCode.Contains(q.Keyword!) || x.ShortName.Contains(q.Keyword!) || (x.FullName != null && x.FullName.Contains(q.Keyword!))); var total = await query.CountAsync(); var list = await query.OrderBy(x => x.TypeCategory).OrderBy(x => x.TypeCode) .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)); [HttpPost] public async Task CreateAsync([FromBody] AdoS0QmsQualityBaseTypeUpsertDto dto) { var entity = new AdoS0QmsQualityBaseType(); ApplyQualityBaseType(entity, dto, true); await _rep.AsInsertable(entity).ExecuteReturnEntityAsync(); return Ok(entity); } [HttpPut("{id:long}")] public async Task UpdateAsync(long id, [FromBody] AdoS0QmsQualityBaseTypeUpsertDto dto) { var entity = await _rep.GetByIdAsync(id); if (entity == null) return NotFound(); ApplyQualityBaseType(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 ApplyQualityBaseType(AdoS0QmsQualityBaseType entity, AdoS0QmsQualityBaseTypeUpsertDto dto, bool isCreate) { entity.TypeCategory = dto.TypeCategory.Trim(); entity.TypeCode = dto.TypeCode.Trim(); entity.ShortName = dto.ShortName.Trim(); entity.FullName = NullIfWhiteSpace(dto.FullName); entity.IsActive = dto.IsActive; entity.Remark = NullIfWhiteSpace(dto.Remark); if (isCreate) entity.CreateTime = DateTime.Now; entity.ModifyTime = DateTime.Now; } } [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!)) || (x.MaterialCode != null && x.MaterialCode.Contains(q.Keyword!)) || (x.MaterialName != null && x.MaterialName.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 err = ValidateRawWhitelist(dto, out var dimensionType); if (err != null) return AdoS0ApiErrors.InvalidRequest(err); var entity = new AdoS0QmsRawWhitelist { SupplierCode = NullIfWhiteSpace(dto.SupplierCode), SupplierName = NullIfWhiteSpace(dto.SupplierName), MaterialCode = NullIfWhiteSpace(dto.MaterialCode), MaterialName = NullIfWhiteSpace(dto.MaterialName), DimensionType = dimensionType }; 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(); var err = ValidateRawWhitelist(dto, out var dimensionType); if (err != null) return AdoS0ApiErrors.InvalidRequest(err); entity.SupplierCode = NullIfWhiteSpace(dto.SupplierCode); entity.SupplierName = NullIfWhiteSpace(dto.SupplierName); entity.MaterialCode = NullIfWhiteSpace(dto.MaterialCode); entity.MaterialName = NullIfWhiteSpace(dto.MaterialName); entity.DimensionType = dimensionType; await _rep.AsUpdateable(entity).ExecuteCommandAsync(); return Ok(entity); } [HttpDelete("{id:long}")] public async Task DeleteAsync(long id) => await DeleteByIdAsync(_rep, id); private static string? ValidateRawWhitelist(AdoS0QmsRawWhitelistUpsertDto dto, out string dimensionType) { dimensionType = string.IsNullOrWhiteSpace(dto.DimensionType) ? "supplier" : dto.DimensionType.Trim().ToLowerInvariant(); if (dimensionType is not ("supplier" or "material" or "material_supplier")) return "维度类型仅支持 supplier/material/material_supplier"; if ((dimensionType == "supplier" || dimensionType == "material_supplier") && string.IsNullOrWhiteSpace(dto.SupplierCode)) return "供应商维度下,供应商编码不能为空"; if ((dimensionType == "supplier" || dimensionType == "material_supplier") && string.IsNullOrWhiteSpace(dto.SupplierName)) return "供应商维度下,供应商名称不能为空"; if ((dimensionType == "material" || dimensionType == "material_supplier") && string.IsNullOrWhiteSpace(dto.MaterialCode)) return "物料维度下,物料编码不能为空"; return null; } } [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); entity.FixedSamplingRate = dto.FixedSamplingRate; 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); entity.GaugeCategory = NullIfWhiteSpace(dto.GaugeCategory); entity.CalibrationCycleDays = dto.CalibrationCycleDays; entity.NextCalibrationDate = NullIfWhiteSpace(dto.NextCalibrationDate); 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(); } }