using Admin.NET.Plugin.AiDOP.Dto.S0.Manufacturing; using Admin.NET.Plugin.AiDOP.Dto.S0.Sales; using Admin.NET.Plugin.AiDOP.Entity.S0.Manufacturing; using Admin.NET.Plugin.AiDOP.Infrastructure; namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Manufacturing; [ApiController] [Route("api/s0/manufacturing/routing-op-details")] [AllowAnonymous] [NonUnify] public class AdoS0MfgRoutingOpDetailsController : ControllerBase { private readonly SqlSugarRepository _rep; public AdoS0MfgRoutingOpDetailsController(SqlSugarRepository rep) { _rep = rep; } [HttpGet] public async Task GetPagedAsync([FromQuery] AdoS0MfgRoutingOpDetailQueryDto q) { var page = q.EffectivePage; var pageSize = q.PageSize; (page, pageSize) = PagingGuard.Normalize(page, pageSize); var query = _rep.AsQueryable() .WhereIF(q.CompanyRefId.HasValue, x => x.CompanyRefId == q.CompanyRefId!.Value) .WhereIF(q.FactoryRefId.HasValue, x => x.FactoryRefId == q.FactoryRefId!.Value) .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword), x => x.RouteCode.Contains(q.Keyword!) || x.RouteName.Contains(q.Keyword!) || x.MaterialCode.Contains(q.Keyword!) || x.OperationCode.Contains(q.Keyword!) || x.OperationDescription.Contains(q.Keyword!) || x.WorkCenterCode.Contains(q.Keyword!)) .WhereIF(q.IsEnabled.HasValue, x => x.IsEnabled == q.IsEnabled!.Value); var total = await query.CountAsync(); var list = await query.OrderBy(x => x.RouteCode).OrderBy(x => x.SortNo).OrderBy(x => x.Id) .Skip((page - 1) * pageSize).Take(pageSize).ToListAsync(); return Ok(new { total, page, pageSize, list }); } [HttpGet("{id:long}")] public async Task GetAsync(long id) { var item = await _rep.GetByIdAsync(id); return item == null ? NotFound() : Ok(item); } [HttpPost] public async Task CreateAsync([FromBody] AdoS0MfgRoutingOpDetailUpsertDto dto) { var err = ValidateUpsert(dto); if (err != null) return BadRequest(new { message = err }); var entity = MapToEntity(dto); entity.CreatedAt = DateTime.Now; await _rep.AsInsertable(entity).ExecuteReturnEntityAsync(); return Ok(entity); } [HttpPut("{id:long}")] public async Task UpdateAsync(long id, [FromBody] AdoS0MfgRoutingOpDetailUpsertDto dto) { var err = ValidateUpsert(dto); if (err != null) return BadRequest(new { message = err }); var entity = await _rep.GetByIdAsync(id); if (entity == null) return NotFound(); ApplyUpsert(entity, dto); entity.UpdatedAt = DateTime.Now; await _rep.AsUpdateable(entity).ExecuteCommandAsync(); return Ok(entity); } [HttpPatch("{id:long}/toggle-enabled")] public async Task ToggleEnabledAsync(long id, [FromBody] AdoS0ToggleEnabledDto dto) { var entity = await _rep.GetByIdAsync(id); if (entity == null) return NotFound(); entity.IsEnabled = dto.IsEnabled; entity.UpdatedAt = DateTime.Now; await _rep.AsUpdateable(entity).ExecuteCommandAsync(); return Ok(entity); } [HttpDelete("{id:long}")] public async Task DeleteAsync(long id) { var item = await _rep.GetByIdAsync(id); if (item == null) return NotFound(); await _rep.DeleteAsync(item); return Ok(new { message = "删除成功" }); } private static string? ValidateUpsert(AdoS0MfgRoutingOpDetailUpsertDto dto) { if (string.IsNullOrWhiteSpace(dto.RouteCode)) return "工艺路线编码不能为空"; if (string.IsNullOrWhiteSpace(dto.RouteName)) return "工艺路线名称不能为空"; if (string.IsNullOrWhiteSpace(dto.MaterialCode)) return "物料编码不能为空"; if (string.IsNullOrWhiteSpace(dto.OperationCode)) return "工序编码不能为空"; return null; } private static AdoS0MfgRoutingOpDetail MapToEntity(AdoS0MfgRoutingOpDetailUpsertDto dto) { var e = new AdoS0MfgRoutingOpDetail(); ApplyUpsert(e, dto); return e; } private static void ApplyUpsert(AdoS0MfgRoutingOpDetail e, AdoS0MfgRoutingOpDetailUpsertDto dto) { e.CompanyRefId = dto.CompanyRefId; e.FactoryRefId = dto.FactoryRefId; e.RouteCode = dto.RouteCode.Trim(); e.RouteName = dto.RouteName.Trim(); e.MaterialCode = dto.MaterialCode.Trim(); e.OperationCode = dto.OperationCode.Trim(); e.OperationDescription = (dto.OperationDescription ?? string.Empty).Trim(); e.WorkCenterCode = (dto.WorkCenterCode ?? string.Empty).Trim(); e.MilestoneOperation = string.IsNullOrWhiteSpace(dto.MilestoneOperation) ? null : dto.MilestoneOperation.Trim(); e.Ufld1 = dto.Ufld1; e.Ufld2 = dto.Ufld2; e.UDeci1 = dto.UDeci1; e.Ufld3 = dto.Ufld3; e.Ufld4 = dto.Ufld4; e.Ufld5 = dto.Ufld5; e.UDeci2 = dto.UDeci2; e.Ufld6 = dto.Ufld6; e.Ufld7 = dto.Ufld7; e.Ufld8 = dto.Ufld8; e.UDeci3 = dto.UDeci3; e.Ufld9 = dto.Ufld9; e.UDeci4 = dto.UDeci4; e.UDeci5 = dto.UDeci5; e.SupplierCode = string.IsNullOrWhiteSpace(dto.SupplierCode) ? null : dto.SupplierCode.Trim(); e.OutsourcedLeadTime = dto.OutsourcedLeadTime; e.SortNo = dto.SortNo; e.IsEnabled = dto.IsEnabled; e.IsConfirm = dto.IsConfirm; e.CommentIndex = dto.CommentIndex; e.CreateUser = dto.CreateUser; e.UpdateUser = dto.UpdateUser; } }