using System; using System.Collections.Generic; using System.Linq; using CncService.Interface; using CncModels.Dto; using CncModels.Dto.Worker; using CncModels.Entity; using CncModels.Constants; using CncRepository.Interface; namespace CncService.Impl { /// /// 员工管理实现 /// public class WorkerService : IWorkerService { private readonly IWorkerRepository _workerRepository; private readonly IWorkerMachineRepository _workerMachineRepository; private readonly IMachineRepository _machineRepository; public WorkerService( IWorkerRepository workerRepository, IWorkerMachineRepository workerMachineRepository, IMachineRepository machineRepository) { _workerRepository = workerRepository ?? throw new ArgumentNullException(nameof(workerRepository)); _workerMachineRepository = workerMachineRepository ?? throw new ArgumentNullException(nameof(workerMachineRepository)); _machineRepository = machineRepository ?? throw new ArgumentNullException(nameof(machineRepository)); } /// public PagedResult GetList(WorkerQuery query) { if (query == null) throw new BusinessException(ErrorCode.BadRequest, "查询参数不能为空"); return _workerRepository.GetList(query); } /// public WorkerDetailResponse GetById(int id) { if (id <= 0) throw new BusinessException(ErrorCode.BadRequest, "无效的员工ID"); var w = _workerRepository.GetById(id); if (w == null) throw new BusinessException(ErrorCode.NotFound, "员工未找到"); var bindings = _workerMachineRepository.GetByWorkerId(id); var machineNames = new List(); foreach (var b in bindings) { var m = _machineRepository.GetById(b.MachineId); if (m != null) machineNames.Add(m.Name ?? m.DeviceCode); } return new WorkerDetailResponse { Id = w.Id, Code = w.Code, Name = w.Name, IsEnabled = w.IsEnabled, MachineCount = bindings.Count, MachineNames = string.Join(", ", machineNames) }; } /// public int Create(CreateWorkerRequest request) { if (request == null) throw new BusinessException(ErrorCode.BadRequest, "请求参数不能为空"); if (string.IsNullOrWhiteSpace(request.Code)) throw new BusinessException(ErrorCode.BadRequest, "工号不能为空"); // 唯一性校验 var existing = _workerRepository.GetByCode(request.Code); if (existing != null) throw new BusinessException(ErrorCode.Conflict, "工号已存在"); var entity = new Worker { Name = request.Name, Code = request.Code, IsEnabled = 1, CreatedAt = DateTime.Now, UpdatedAt = DateTime.Now }; return _workerRepository.Create(entity); } /// public bool Update(int id, UpdateWorkerRequest request) { if (id <= 0) throw new BusinessException(ErrorCode.BadRequest, "无效的员工ID"); if (request == null) throw new BusinessException(ErrorCode.BadRequest, "请求参数不能为空"); var entity = _workerRepository.GetById(id); if (entity == null) throw new BusinessException(ErrorCode.NotFound, "员工未找到"); entity.Name = request.Name ?? entity.Name; entity.UpdatedAt = DateTime.Now; return _workerRepository.Update(entity); } /// public bool Delete(int id) { if (id <= 0) throw new BusinessException(ErrorCode.BadRequest, "无效的员工ID"); // 解绑所有机床 _workerMachineRepository.DeleteByWorkerId(id); return _workerRepository.Delete(id); } /// public bool ToggleEnabled(int id) { if (id <= 0) throw new BusinessException(ErrorCode.BadRequest, "无效的员工ID"); return _workerRepository.ToggleEnabled(id); } /// public bool BindMachine(int workerId, int machineId) { if (workerId <= 0 || machineId <= 0) throw new BusinessException(ErrorCode.BadRequest, "无效的参数"); // 检查是否已绑定 var existing = _workerMachineRepository.GetByMachineId(machineId); if (existing != null) throw new BusinessException(ErrorCode.Conflict, "该机床已绑定其他工人"); _workerMachineRepository.Create(workerId, machineId); return true; } /// public bool UnbindMachine(int workerId, int machineId) { if (workerId <= 0 || machineId <= 0) throw new BusinessException(ErrorCode.BadRequest, "无效的参数"); return _workerMachineRepository.DeleteByMachineId(machineId); } /// public List GetMachines(int workerId) { if (workerId <= 0) throw new BusinessException(ErrorCode.BadRequest, "无效的员工ID"); var bindings = _workerMachineRepository.GetByWorkerId(workerId); var result = new List(); foreach (var b in bindings) { var m = _machineRepository.GetById(b.MachineId); if (m == null) continue; string workshopName = null; string brandName = null; // 车间名称需查关联表,但当前简单返回null即可 result.Add(new WorkerMachineItem { MachineId = m.Id, MachineName = m.Name ?? m.DeviceCode, DeviceCode = m.DeviceCode, WorkshopName = workshopName, BrandName = brandName, IsOnline = m.IsOnline == 1, ProgramName = m.LastProgramName }); } return result; } /// public List GetTodayProduction(int workerId) { if (workerId <= 0) throw new BusinessException(ErrorCode.BadRequest, "无效的员工ID"); // 采集服务未运行,返回空列表 return new List(); } /// public List GetProductionTrend(int workerId) { if (workerId <= 0) throw new BusinessException(ErrorCode.BadRequest, "无效的员工ID"); // 采集服务未运行,返回7天空数据 var result = new List(); for (int i = 6; i >= 0; i--) { result.Add(new WorkerTrendItem { Date = DateTime.Today.AddDays(-i).ToString("yyyy-MM-dd"), Quantity = 0 }); } return result; } /// public List GetAvailableMachines() { // 获取所有在线且启用的机床,排除已绑定的 var allMachines = _machineRepository.GetEnabledOnline(); var result = new List(); foreach (var m in allMachines) { var binding = _workerMachineRepository.GetByMachineId(m.Id); if (binding == null) { result.Add(new WorkerAvailableMachineItem { Id = m.Id, Name = m.Name ?? m.DeviceCode, DeviceCode = m.DeviceCode }); } } return result; } } }