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/line-posts")] [AllowAnonymous] [NonUnify] public class AdoS0MfgLinePostsController : ControllerBase { private readonly SqlSugarRepository _postRep; private readonly SqlSugarRepository _skillRep; private readonly SqlSugarRepository _personSkillRep; public AdoS0MfgLinePostsController( SqlSugarRepository postRep, SqlSugarRepository skillRep, SqlSugarRepository personSkillRep) { _postRep = postRep; _skillRep = skillRep; _personSkillRep = personSkillRep; } [HttpGet] public async Task GetPagedAsync([FromQuery] AdoS0MfgLinePostQueryDto q) { var page = q.EffectivePage; var pageSize = q.PageSize; (page, pageSize) = PagingGuard.Normalize(page, pageSize); var query = _postRep.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.Code.Contains(q.Keyword!) || x.Name.Contains(q.Keyword!)) .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 GetDetailAsync(long id) { var header = await _postRep.GetByIdAsync(id); if (header == null) return NotFound(); var skills = await _skillRep.AsQueryable() .Where(x => x.LinePostId == id) .OrderBy(x => x.Id) .ToListAsync(); return Ok(new { header, linePostSkills = skills }); } [HttpPost] public async Task CreateAsync([FromBody] AdoS0MfgLinePostUpsertDto dto) { var fkErr = await ValidatePersonSkillsAsync(dto); if (fkErr != null) return BadRequest(new { message = fkErr }); var db = _postRep.Context; await db.Ado.BeginTranAsync(); try { var post = new AdoS0MfgLinePost { CompanyRefId = dto.CompanyRefId, FactoryRefId = dto.FactoryRefId, Code = dto.Code, Name = dto.Name, ProductionLineId = dto.ProductionLineId, Remark = dto.Remark, IsEnabled = dto.IsEnabled, CreatedAt = DateTime.Now }; post = await _postRep.AsInsertable(post).ExecuteReturnEntityAsync(); foreach (var s in dto.Skills) { var row = MapSkill(post.Id, dto.CompanyRefId, dto.FactoryRefId, s); await _skillRep.AsInsertable(row).ExecuteCommandAsync(); } await db.Ado.CommitTranAsync(); return await GetDetailAsync(post.Id); } catch { await db.Ado.RollbackTranAsync(); throw; } } [HttpPut("{id:long}")] public async Task UpdateAsync(long id, [FromBody] AdoS0MfgLinePostUpsertDto dto) { var header = await _postRep.GetByIdAsync(id); if (header == null) return NotFound(); var fkErr = await ValidatePersonSkillsAsync(dto); if (fkErr != null) return BadRequest(new { message = fkErr }); var db = _postRep.Context; await db.Ado.BeginTranAsync(); try { header.CompanyRefId = dto.CompanyRefId; header.FactoryRefId = dto.FactoryRefId; header.Code = dto.Code; header.Name = dto.Name; header.ProductionLineId = dto.ProductionLineId; header.Remark = dto.Remark; header.IsEnabled = dto.IsEnabled; header.UpdatedAt = DateTime.Now; await _postRep.AsUpdateable(header).ExecuteCommandAsync(); await _skillRep.AsDeleteable().Where(x => x.LinePostId == id).ExecuteCommandAsync(); foreach (var s in dto.Skills) { var row = MapSkill(id, dto.CompanyRefId, dto.FactoryRefId, s); await _skillRep.AsInsertable(row).ExecuteCommandAsync(); } await db.Ado.CommitTranAsync(); return await GetDetailAsync(id); } catch { await db.Ado.RollbackTranAsync(); throw; } } [HttpPatch("{id:long}/toggle-enabled")] public async Task ToggleEnabledAsync(long id, [FromBody] AdoS0ToggleEnabledDto dto) { var entity = await _postRep.GetByIdAsync(id); if (entity == null) return NotFound(); entity.IsEnabled = dto.IsEnabled; entity.UpdatedAt = DateTime.Now; await _postRep.AsUpdateable(entity).ExecuteCommandAsync(); return Ok(entity); } [HttpDelete("{id:long}")] public async Task DeleteAsync(long id) { var header = await _postRep.GetByIdAsync(id); if (header == null) return NotFound(); var db = _postRep.Context; await db.Ado.BeginTranAsync(); try { await _skillRep.AsDeleteable().Where(x => x.LinePostId == id).ExecuteCommandAsync(); await _postRep.DeleteAsync(header); await db.Ado.CommitTranAsync(); return Ok(new { message = "删除成功" }); } catch { await db.Ado.RollbackTranAsync(); throw; } } private async Task ValidatePersonSkillsAsync(AdoS0MfgLinePostUpsertDto dto) { if (dto.Skills == null || dto.Skills.Count == 0) return null; var ids = dto.Skills.Select(s => s.PersonSkillId).Distinct().ToList(); var cnt = await _personSkillRep.AsQueryable() .Where(p => ids.Contains(p.Id) && p.CompanyRefId == dto.CompanyRefId && p.FactoryRefId == dto.FactoryRefId) .CountAsync(); return cnt == ids.Count ? null : "存在无效的人员技能主数据引用"; } private static AdoS0MfgLinePostSkill MapSkill(long linePostId, long companyRefId, long factoryRefId, AdoS0MfgLinePostSkillUpsertDto s) { return new AdoS0MfgLinePostSkill { CompanyRefId = companyRefId, FactoryRefId = factoryRefId, LinePostId = linePostId, PersonSkillId = s.PersonSkillId, RequiredLevel = s.RequiredLevel, Remark = s.Remark, EffectiveDate = s.EffectiveDate, IsEnabled = s.IsEnabled, CreatedAt = DateTime.Now }; } }