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.Entity.S0.Sales; using Admin.NET.Plugin.AiDOP.Infrastructure; namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Manufacturing; [ApiController] [Route("api/s0/manufacturing/material-process-elements")] [AllowAnonymous] [NonUnify] public class AdoS0MfgMaterialProcessElementsController : ControllerBase { private readonly SqlSugarRepository _rep; public AdoS0MfgMaterialProcessElementsController(SqlSugarRepository rep) { _rep = rep; } [HttpGet] public async Task GetPagedAsync([FromQuery] AdoS0MfgMaterialProcessElementQueryDto q) { var page = q.EffectivePage; var pageSize = q.PageSize; (page, pageSize) = PagingGuard.Normalize(page, pageSize); List? materialFilterIds = null; if (!string.IsNullOrWhiteSpace(q.MaterialCode) || !string.IsNullOrWhiteSpace(q.MaterialName)) { var mq = _rep.Context.Queryable() .WhereIF(q.CompanyRefId.HasValue, m => m.CompanyRefId == q.CompanyRefId!.Value) .WhereIF(q.FactoryRefId.HasValue, m => m.FactoryRefId == q.FactoryRefId!.Value) .WhereIF(!string.IsNullOrWhiteSpace(q.MaterialCode), m => m.Code.Contains(q.MaterialCode!)) .WhereIF(!string.IsNullOrWhiteSpace(q.MaterialName), m => m.Name.Contains(q.MaterialName!)) .Select(m => m.Id); materialFilterIds = await mq.ToListAsync(); if (materialFilterIds.Count == 0) return Ok(new { total = 0, page, pageSize, list = Array.Empty() }); } 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(materialFilterIds != null, x => materialFilterIds!.Contains(x.MaterialId)) .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword), x => (x.ElementCode != null && x.ElementCode.Contains(q.Keyword!)) || (x.ElementName != null && x.ElementName.Contains(q.Keyword!))) .WhereIF(!string.IsNullOrWhiteSpace(q.ElementType), x => x.ElementType == q.ElementType) .WhereIF(!string.IsNullOrWhiteSpace(q.ElementCode), x => x.ElementCode != null && x.ElementCode.Contains(q.ElementCode!)) .WhereIF(q.IsEnabled.HasValue, x => x.IsEnabled == q.IsEnabled!.Value); var total = await query.CountAsync(); var list = await query.OrderByDescending(x => x.CreatedAt).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] AdoS0MfgMaterialProcessElementUpsertDto dto) { var entity = new AdoS0MfgMaterialProcessElement { CompanyRefId = dto.CompanyRefId, FactoryRefId = dto.FactoryRefId, MaterialId = dto.MaterialId, OperationId = dto.OperationId, ProductionLineId = dto.ProductionLineId, ElementType = dto.ElementType, ElementCode = dto.ElementCode, ElementName = dto.ElementName, ElementParamId = dto.ElementParamId, ParamValue = dto.ParamValue, IsKeyElement = dto.IsKeyElement, Remark = dto.Remark, IsEnabled = dto.IsEnabled, CreatedAt = DateTime.Now }; await _rep.AsInsertable(entity).ExecuteReturnEntityAsync(); return Ok(entity); } [HttpPut("{id:long}")] public async Task UpdateAsync(long id, [FromBody] AdoS0MfgMaterialProcessElementUpsertDto dto) { var entity = await _rep.GetByIdAsync(id); if (entity == null) return NotFound(); entity.CompanyRefId = dto.CompanyRefId; entity.FactoryRefId = dto.FactoryRefId; entity.MaterialId = dto.MaterialId; entity.OperationId = dto.OperationId; entity.ProductionLineId = dto.ProductionLineId; entity.ElementType = dto.ElementType; entity.ElementCode = dto.ElementCode; entity.ElementName = dto.ElementName; entity.ElementParamId = dto.ElementParamId; entity.ParamValue = dto.ParamValue; entity.IsKeyElement = dto.IsKeyElement; entity.Remark = dto.Remark; entity.IsEnabled = dto.IsEnabled; 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 = "删除成功" }); } }