UI verbessert, 2. PDF als kompakte Variante
This commit is contained in:
1 parent
1bf3283029
commit
f9e3e94629
23 files changed
+1009
-146
No files matched your search
@@ -39,7 +39,9 @@ public struct ServicePlanner {
|
||||
|
||||
var totalByPerson: [String: Int] = [:]
|
||||
var monthByPerson: [String: Int] = [:]
|
||||
var dayByPerson: [String: Int] = [:]
|
||||
// Count distinct services per day. Both roles at the same service do not
|
||||
// count as a double booking, so one person can be lector and helper there.
|
||||
var dayServicesByPerson: [String: Set<String>] = [:]
|
||||
var churchByPerson: [String: [String: Int]] = [:]
|
||||
var lastDateByPerson: [String: Date] = [:]
|
||||
|
||||
@@ -48,7 +50,7 @@ public struct ServicePlanner {
|
||||
let month = DayMath.monthKey(service.date)
|
||||
totalByPerson[assignment.personID, default: 0] += 1
|
||||
monthByPerson["\(assignment.personID)|\(month)", default: 0] += 1
|
||||
dayByPerson["\(assignment.personID)|\(service.dateKey)", default: 0] += 1
|
||||
dayServicesByPerson["\(assignment.personID)|\(service.dateKey)", default: []].insert(service.id)
|
||||
churchByPerson[assignment.personID, default: [:]][service.churchID, default: 0] += 1
|
||||
let previous = lastDateByPerson[assignment.personID]
|
||||
if previous == nil || service.date > previous! {
|
||||
@@ -66,6 +68,13 @@ public struct ServicePlanner {
|
||||
var newAssignments: [Assignment] = []
|
||||
|
||||
for service in sortedServices {
|
||||
// Roles already covered at this service. A person may appear in both
|
||||
// roles, but not twice in the same role.
|
||||
var assignedRolesByPerson: [String: Set<Role>] = [:]
|
||||
for assignment in working where assignment.serviceID == service.id {
|
||||
assignedRolesByPerson[assignment.personID, default: []].insert(assignment.role)
|
||||
}
|
||||
|
||||
for role in Role.allCases {
|
||||
let required = service.requiredCount(for: role)
|
||||
guard required > 0 else { continue }
|
||||
@@ -73,7 +82,6 @@ public struct ServicePlanner {
|
||||
if existingForRole >= required { continue }
|
||||
|
||||
for _ in existingForRole..<required {
|
||||
let alreadyAssigned = Set(working.filter { $0.serviceID == service.id }.map(\.personID))
|
||||
let result = bestCandidate(
|
||||
for: service,
|
||||
role: role,
|
||||
@@ -82,10 +90,10 @@ public struct ServicePlanner {
|
||||
absencesByPerson: absencesByPerson,
|
||||
totalByPerson: totalByPerson,
|
||||
monthByPerson: monthByPerson,
|
||||
dayByPerson: dayByPerson,
|
||||
dayServicesByPerson: dayServicesByPerson,
|
||||
churchByPerson: churchByPerson,
|
||||
lastDateByPerson: lastDateByPerson,
|
||||
alreadyAssigned: alreadyAssigned
|
||||
assignedRolesByPerson: assignedRolesByPerson
|
||||
)
|
||||
switch result {
|
||||
case .success(let person):
|
||||
@@ -98,9 +106,10 @@ public struct ServicePlanner {
|
||||
working.append(assignment)
|
||||
totalByPerson[person.id, default: 0] += 1
|
||||
monthByPerson["\(person.id)|\(DayMath.monthKey(service.date))", default: 0] += 1
|
||||
dayByPerson["\(person.id)|\(service.dateKey)", default: 0] += 1
|
||||
dayServicesByPerson["\(person.id)|\(service.dateKey)", default: []].insert(service.id)
|
||||
churchByPerson[person.id, default: [:]][service.churchID, default: 0] += 1
|
||||
lastDateByPerson[person.id] = service.date
|
||||
assignedRolesByPerson[person.id, default: []].insert(role)
|
||||
case .failure(let reason):
|
||||
unfilled.append(UnfilledSlot(
|
||||
service: service,
|
||||
@@ -144,17 +153,18 @@ public struct ServicePlanner {
|
||||
absencesByPerson: [String: [Absence]],
|
||||
totalByPerson: [String: Int],
|
||||
monthByPerson: [String: Int],
|
||||
dayByPerson: [String: Int],
|
||||
dayServicesByPerson: [String: Set<String>],
|
||||
churchByPerson: [String: [String: Int]],
|
||||
lastDateByPerson: [String: Date],
|
||||
alreadyAssigned: Set<String>
|
||||
assignedRolesByPerson: [String: Set<Role>]
|
||||
) -> CandidateResult {
|
||||
var candidates: [(Person, Double)] = []
|
||||
var blockedReasons: [String: Int] = [:]
|
||||
var eligiblePeople: [Person] = []
|
||||
|
||||
for person in people where person.canFill(role) {
|
||||
guard !alreadyAssigned.contains(person.id) else {
|
||||
blockedReasons["bereits im Gottesdienst", default: 0] += 1
|
||||
let assignedRoles = assignedRolesByPerson[person.id] ?? []
|
||||
guard !assignedRoles.contains(role) else {
|
||||
blockedReasons["bereits für diesen Dienst eingeteilt", default: 0] += 1
|
||||
continue
|
||||
}
|
||||
if let absences = absencesByPerson[person.id], absences.contains(where: { $0.overlaps(service.date) }) {
|
||||
@@ -173,13 +183,41 @@ public struct ServicePlanner {
|
||||
blockedReasons["Monatslimit erreicht", default: 0] += 1
|
||||
continue
|
||||
}
|
||||
let usedThisDay = dayByPerson["\(person.id)|\(service.dateKey)", default: 0]
|
||||
if usedThisDay >= options.maxAssignmentsPerDay {
|
||||
let usedServicesThisDay = dayServicesByPerson["\(person.id)|\(service.dateKey)", default: []]
|
||||
// A second role at the very same service is not a double booking.
|
||||
if usedServicesThisDay.count >= options.maxAssignmentsPerDay,
|
||||
!usedServicesThisDay.isSubset(of: [service.id]) {
|
||||
blockedReasons["bereits an diesem Tag", default: 0] += 1
|
||||
continue
|
||||
}
|
||||
|
||||
eligiblePeople.append(person)
|
||||
}
|
||||
|
||||
// The dual-role preference is a tie-breaker, not a way to concentrate
|
||||
// assignments on one person. If another suitable person has noticeably
|
||||
// fewer assignments, fairness takes priority over giving both roles to
|
||||
// the same person.
|
||||
let minimumTotal = eligiblePeople.map { totalByPerson[$0.id, default: 0] }.min() ?? 0
|
||||
let fairnessSlack = 1
|
||||
|
||||
var candidates: [(Person, Double)] = []
|
||||
for person in eligiblePeople {
|
||||
let assignedRoles = assignedRolesByPerson[person.id] ?? []
|
||||
let total = totalByPerson[person.id, default: 0]
|
||||
let withinFairnessReach = total - minimumTotal <= fairnessSlack
|
||||
|
||||
var score = 10.0
|
||||
if withinFairnessReach, assignedRoles.contains(role.complementary) {
|
||||
// The same person already covers the other role here — prefer
|
||||
// keeping both duties with them while the distribution stays fair.
|
||||
score += options.weightDualRolePreference
|
||||
} else if withinFairnessReach,
|
||||
service.rolesNeeded.count > 1,
|
||||
person.roles.contains(where: { $0 != role && service.requiredCount(for: $0) > 0 }) {
|
||||
// On services needing both roles, favour people who can cover both.
|
||||
score += options.weightDualRolePreference
|
||||
}
|
||||
if person.preferredChurchIDs.contains(service.churchID) {
|
||||
score += options.weightChurchPreference
|
||||
}
|
||||
@@ -197,7 +235,6 @@ public struct ServicePlanner {
|
||||
if person.preferredWeekdays.contains(DayMath.weekday(service.date)) {
|
||||
score += 3
|
||||
}
|
||||
let total = totalByPerson[person.id, default: 0]
|
||||
score -= Double(total) * options.weightFairness
|
||||
let churchCount = churchByPerson[person.id]?[service.churchID, default: 0] ?? 0
|
||||
score -= Double(churchCount) * 0.4
|
||||
|
||||
Reference in new issue
Block a user