public static List <StaffCombination> GetFilteredStaffForAppointmentTypeBySkills(List <Staff> practiceStaff, AppointmentType appointmentType) { List <StaffCombination> retVal = new List <StaffCombination>(); //potrebno osoblje, konkretno oznacneo var reqStaffByName = practiceStaff.Where(s => appointmentType.RequiredStaff.Select(selectStaff => selectStaff.StaffID).Contains(s.ID)).ToArray(); //ako su svi raspolozivi if (reqStaffByName.Length == appointmentType.RequiredStaff.Count) { Dictionary <Guid, SelectedStaffPerAbility> usedStaffPerAbility = new Dictionary <Guid, SelectedStaffPerAbility>(); foreach (var reqSA in appointmentType.ReqStaffByAbillities) { //ako nema, iniciraj value if (!usedStaffPerAbility.ContainsKey(reqSA.ID)) { usedStaffPerAbility.Add(reqSA.ID, new SelectedStaffPerAbility()); usedStaffPerAbility[reqSA.ID].StillNeed = reqSA.Count; } //rasporedjeni zapolseni koji su odabrani po imenu List <Guid> allreadySelectedStaffIDs = new List <Guid>(); foreach (var value in usedStaffPerAbility.Values) { allreadySelectedStaffIDs.AddRange(value.GetStaffIDs()); } //ne rasporedjeni zapolseni koji su odabrani po imenu var otherReqStaffByName = reqStaffByName.Where(s => !allreadySelectedStaffIDs.Contains(s.ID)).ToList(); var selectedStaff = GetAllStaffByStaffReqs(otherReqStaffByName, reqSA); //dodati ga if (selectedStaff.Count > reqSA.Count) { //uzeti prvih X TODO: napraviti bolji odabir usedStaffPerAbility[reqSA.ID].AddStaff(selectedStaff.Take(reqSA.Count).ToList()); usedStaffPerAbility[reqSA.ID].StillNeed = 0; } else { usedStaffPerAbility[reqSA.ID].AddStaff(selectedStaff); usedStaffPerAbility[reqSA.ID].StillNeed = reqSA.Count - selectedStaff.Count; } } var unusedStaff = practiceStaff.Where(s => !reqStaffByName.Select(rss => rss.ID).Contains(s.ID)).ToList(); Dictionary <Guid, SelectedStaffPerAbility> otherStaffPerAbility = new Dictionary <Guid, SelectedStaffPerAbility>(); foreach (var usa in usedStaffPerAbility) { var reqSA = appointmentType.ReqStaffByAbillities.First(s => s.ID == usa.Key); if (usa.Value.StillNeed > 0) { otherStaffPerAbility.Add(reqSA.ID, new SelectedStaffPerAbility()); otherStaffPerAbility[reqSA.ID].AddStaff(GetAllStaffByStaffReqs(unusedStaff, reqSA)); otherStaffPerAbility[reqSA.ID].StillNeed = usa.Value.StillNeed; if (otherStaffPerAbility[reqSA.ID].Staff.Count < reqSA.Count) { return(retVal); // nema ih dovoljno za taj requ, kraj } } } var combinationPerMappedReqSA = new Dictionary <Guid, List <TemplateRequiredStaffInfo> >(); var mappingKeysToRealRequs = new Dictionary <Guid, Guid>(); foreach (var other in otherStaffPerAbility) { for (int i = 0; i < other.Value.StillNeed; i++) { var tempKey = Guid.NewGuid(); mappingKeysToRealRequs.Add(tempKey, other.Key); List <TemplateRequiredStaffInfo> tempList = new List <TemplateRequiredStaffInfo>(); foreach (var s in other.Value.Staff) { tempList.Add(new TemplateRequiredStaffInfo() { ID = tempKey, Staff = s }); } combinationPerMappedReqSA.Add(tempKey, tempList); } } TreeNode tree = new TreeNode(Guid.Empty, Guid.Empty); int reqSkillsCounter = 0; foreach (var key in combinationPerMappedReqSA.Keys) { var staff2ReqList = combinationPerMappedReqSA[key]; var leafs = TreeUtils.GetLeafs(tree); foreach (var leaf in leafs) { foreach (var staff2Req in staff2ReqList) { if (!TreeUtils.IsAncestorsContainsID(leaf, staff2Req.Staff.ID) && TreeUtils.GetNodeDepth(leaf) == reqSkillsCounter) { leaf.Add(new TreeNode(staff2Req.Staff.ID, key)); } } } reqSkillsCounter++; } var staffCombinationNodes = TreeUtils.GetBranchesCurrentSize(tree, combinationPerMappedReqSA.Keys.Count); if (staffCombinationNodes.Count > 0) { foreach (var combination in staffCombinationNodes) { var staff2reqConnections = new List <StaffID2ReqID>(); foreach (var c in combination) { if (c.StaffReqID != Guid.Empty) { staff2reqConnections.Add(new StaffID2ReqID() { ReqID = mappingKeysToRealRequs[c.StaffReqID], StaffID = c.StaffID }); } } var staffCombination = new List <TemplateRequiredStaffInfo>(); foreach (var staff in reqStaffByName) { staffCombination.Add(new TemplateRequiredStaffInfo() { Staff = staff, ID = appointmentType.RequiredStaff.First(f => f.StaffID == staff.ID).ID }); } var staffCombinationFromTree = practiceStaff.Where(s => staff2reqConnections.Select(select => select.StaffID).Contains(s.ID)).ToList(); foreach (var staff in staffCombinationFromTree) { staffCombination.Add(new TemplateRequiredStaffInfo() { ID = staff2reqConnections.First(f => f.StaffID == staff.ID).ReqID, Staff = staff }); } retVal.Add(new StaffCombination() { Data = staffCombination }); } } } return(retVal); }
public static List <DeviceCombination> GetFilteredDeviceForAppointmentType(List <DeviceInstance> practiceDeviceInstances, AppointmentType appointmentType) { List <DeviceCombination> retVal = new List <DeviceCombination>();; Dictionary <Guid, List <DeviceInstance> > instancesPerReq = new Dictionary <Guid, List <DeviceInstance> >(); foreach (var reqDeviceType in appointmentType.RequiredDeviceTypes) { var devicesByDeviceType = practiceDeviceInstances.Where(d => d.DeviceId == reqDeviceType.ID).ToList(); if (devicesByDeviceType.Count < reqDeviceType.Count) { return(retVal); } instancesPerReq.Add(reqDeviceType.ID, devicesByDeviceType); } var combinationPerMappedReq = new Dictionary <Guid, List <DeviceInstance> >(); var mappingKeysToRealRequs = new Dictionary <Guid, Guid>(); foreach (var ipr in instancesPerReq) { for (int i = 0; i < appointmentType.RequiredDeviceTypes.First(r => r.ID == ipr.Key).Count; i++) { var tempKey = Guid.NewGuid(); mappingKeysToRealRequs.Add(tempKey, ipr.Key); combinationPerMappedReq.Add(tempKey, new List <DeviceInstance>(ipr.Value)); } } TreeNode tree = new TreeNode(Guid.Empty, Guid.Empty); int reqSkillsCounter = 0; foreach (var key in combinationPerMappedReq.Keys) { var device2ReqList = combinationPerMappedReq[key]; var leafs = TreeUtils.GetLeafs(tree); foreach (var leaf in leafs) { foreach (var device2Req in device2ReqList) { if (!TreeUtils.IsAncestorsContainsID(leaf, device2Req.ID) && TreeUtils.GetNodeDepth(leaf) == reqSkillsCounter) { leaf.Add(new TreeNode(device2Req.ID, key)); } } } reqSkillsCounter++; } var instanceCombinationNodes = TreeUtils.GetBranchesCurrentSize(tree, combinationPerMappedReq.Keys.Count); if (instanceCombinationNodes.Count > 0) { foreach (var combination in instanceCombinationNodes) { var instances = new List <DeviceInstance>(); foreach (var c in combination) { if (c.StaffReqID != Guid.Empty) { instances.Add(practiceDeviceInstances.First(f => f.ID == c.StaffID)); } } retVal.Add(new DeviceCombination() { Data = instances }); } } return(retVal); }