using Admin.NET.Plugin.AiDOP.Dto.S0.Warehouse; using Admin.NET.Plugin.AiDOP.Entity.S0.Warehouse; using Admin.NET.Plugin.AiDOP.Infrastructure; namespace Admin.NET.Plugin.AiDOP.Controllers.S0.Warehouse; /// /// S0 雇员主数据(EmployeeMaster 语义) /// [ApiController] [Route("api/s0/warehouse/employees")] [AllowAnonymous] [NonUnify] public class AdoS0EmployeesController : ControllerBase { private readonly SqlSugarRepository _rep; private readonly SqlSugarRepository _deptRep; public AdoS0EmployeesController( SqlSugarRepository rep, SqlSugarRepository deptRep) { _rep = rep; _deptRep = deptRep; } [HttpGet] public async Task GetPagedAsync([FromQuery] AdoS0EmployeeQueryDto q) { (q.Page, q.PageSize) = PagingGuard.Normalize(q.Page, q.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.DomainCode), x => x.DomainCode == q.DomainCode) .WhereIF(!string.IsNullOrWhiteSpace(q.Department), x => x.Department == q.Department) .WhereIF(q.IsActive.HasValue, x => x.IsActive == q.IsActive!.Value) .WhereIF(!string.IsNullOrWhiteSpace(q.Keyword), x => x.Employee.Contains(q.Keyword!) || (x.Name != null && x.Name.Contains(q.Keyword!))); var total = await query.CountAsync(); var list = await query .OrderBy(x => x.Employee) .Skip((q.Page - 1) * q.PageSize) .Take(q.PageSize) .ToListAsync(); await ApplyDeptDescrAsync(list); return Ok(new { total, page = q.Page, pageSize = q.PageSize, list }); } [HttpGet("{id:long}")] public async Task GetAsync(long id) { var item = await _rep.GetByIdAsync(id); if (item == null) return NotFound(); await ApplyDeptDescrAsync(new List { item }); return Ok(item); } [HttpPost] public async Task CreateAsync([FromBody] AdoS0EmployeeUpsertDto dto) { if (await _rep.IsAnyAsync(x => x.FactoryRefId == dto.FactoryRefId && x.Employee == dto.Employee)) return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.DuplicateCode, "员工编码已存在"); var entity = MapToEntity(dto); entity.CreateTime = DateTime.Now; await _rep.AsInsertable(entity).ExecuteReturnEntityAsync(); return Ok(entity); } [HttpPut("{id:long}")] public async Task UpdateAsync(long id, [FromBody] AdoS0EmployeeUpsertDto dto) { var entity = await _rep.GetByIdAsync(id); if (entity == null) return NotFound(); if (await _rep.IsAnyAsync(x => x.Id != id && x.FactoryRefId == dto.FactoryRefId && x.Employee == dto.Employee)) return AdoS0ApiErrors.Conflict(AdoS0ErrorCodes.DuplicateCode, "员工编码已存在"); entity.CompanyRefId = dto.CompanyRefId; entity.FactoryRefId = dto.FactoryRefId; entity.DomainCode = dto.DomainCode ?? string.Empty; entity.Employee = dto.Employee; entity.Name = dto.Name; entity.Sex = dto.Sex; entity.Phone = dto.Phone; entity.Email = dto.Email; entity.BirthDate = dto.BirthDate; entity.Department = dto.Department; entity.DateEmployed = dto.DateEmployed; entity.EmploymentStatus = dto.EmploymentStatus; entity.MaritalStatus = dto.MaritalStatus; entity.JobTitle = dto.JobTitle; entity.DateTerminated = dto.DateTerminated; entity.WorkCtr = dto.WorkCtr; entity.CarId = dto.CarId; entity.DefaultWorkLocation = dto.DefaultWorkLocation; entity.IsActive = dto.IsActive; entity.UpdateUser = dto.UpdateUser; entity.UpdateTime = 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 AdoS0EmployeeMaster MapToEntity(AdoS0EmployeeUpsertDto dto) => new() { CompanyRefId = dto.CompanyRefId, FactoryRefId = dto.FactoryRefId, DomainCode = dto.DomainCode ?? string.Empty, Employee = dto.Employee, Name = dto.Name, Sex = dto.Sex, Phone = dto.Phone, Email = dto.Email, BirthDate = dto.BirthDate, Department = dto.Department, DateEmployed = dto.DateEmployed, EmploymentStatus = dto.EmploymentStatus, MaritalStatus = dto.MaritalStatus, JobTitle = dto.JobTitle, DateTerminated = dto.DateTerminated, WorkCtr = dto.WorkCtr, CarId = dto.CarId, DefaultWorkLocation = dto.DefaultWorkLocation, IsActive = dto.IsActive, CreateUser = dto.CreateUser }; private async Task ApplyDeptDescrAsync(List employees) { if (employees.Count == 0) return; var domainCodes = employees.Select(e => e.DomainCode).Distinct().ToList(); var depts = await _deptRep.AsQueryable() .Where(d => domainCodes.Contains(d.DomainCode)) .ToListAsync(); foreach (var emp in employees) { var dept = depts.Find(d => string.Equals(d.DomainCode, emp.DomainCode, StringComparison.OrdinalIgnoreCase) && string.Equals(d.Department, emp.Department, StringComparison.OrdinalIgnoreCase)); emp.DepartDescr = dept?.Descr; // JobTitle 直接使用存储值作为展示(GeneralizedCodeMaster 暂不关联) emp.JobTitleDisplay = emp.JobTitle; } } }