Fortlaufende Zyklus-Planung und Periodenlogik einführen

This commit is contained in:
Kai
2026-02-16 15:06:34 +01:00
parent 2255318993
commit 67bd87e12a
9 changed files with 138 additions and 63 deletions

View File

@@ -13,14 +13,16 @@ func buildTasksForDay(plans []Plan, crops []Crop, products []Product, stepMap ma
return tasks
}
func buildCalendar(plans []Plan, crops []Crop, products []Product, stepMap map[int64][]CropStep, customTasks []CustomTask, doneMap map[string]bool, startMonth, startDay, daysPerMonth, spanMonths int) []CalendarMonth {
func buildCalendar(plans []Plan, crops []Crop, products []Product, stepMap map[int64][]CropStep, customTasks []CustomTask, doneMap map[string]bool, startPeriod, startDay, daysPerMonth, spanMonths int) []CalendarMonth {
var out []CalendarMonth
startCycle := periodCycle(startPeriod)
for offset := 0; offset < spanMonths; offset++ {
month := wrapMonth(startMonth + offset)
yearOffset := (startMonth - 1 + offset) / 12
period := startPeriod + offset
month := periodMonth(period)
yearOffset := periodCycle(period) - startCycle
label := monthNames[month-1]
if yearOffset > 0 {
label = fmt.Sprintf("%s (+%d Jahr)", label, yearOffset)
label = fmt.Sprintf("%s (+%d Zyklus)", label, yearOffset)
}
fromDay := 1
@@ -30,7 +32,7 @@ func buildCalendar(plans []Plan, crops []Crop, products []Product, stepMap map[i
var days []CalendarDay
for d := fromDay; d <= daysPerMonth; d++ {
items := collectTasksForDate(plans, crops, products, stepMap, customTasks, month, d, yearOffset)
items := collectTasksForDate(plans, crops, products, stepMap, customTasks, period, d, yearOffset)
applyCompletion(items, doneMap)
sortTasks(items)
days = append(days, CalendarDay{Day: d, Tasks: items, Groups: groupTasksByField(items)})
@@ -65,11 +67,12 @@ func groupTasksByField(tasks []Task) []FieldTaskGroup {
return out
}
func collectTasksForDate(plans []Plan, crops []Crop, products []Product, stepMap map[int64][]CropStep, customTasks []CustomTask, month, day, yearOffset int) []Task {
func collectTasksForDate(plans []Plan, crops []Crop, products []Product, stepMap map[int64][]CropStep, customTasks []CustomTask, period, day, yearOffset int) []Task {
var tasks []Task
month := periodMonth(period)
for _, p := range plans {
tasks = append(tasks, expandPlanDayTasks(p, stepMap[p.CropID], month, day, yearOffset)...)
tasks = append(tasks, expandPlanDayTasks(p, stepMap[p.CropID], period, day, yearOffset)...)
}
if day == 1 {
for _, c := range crops {
@@ -102,7 +105,7 @@ func collectTasksForDate(plans []Plan, crops []Crop, products []Product, stepMap
}
}
for _, c := range customTasks {
if c.Month == month && c.Day == day && c.YearOffset == yearOffset {
if c.TaskPeriod == period && c.Day == day {
field := c.TargetName
if strings.TrimSpace(field) == "" {
field = "Allgemein"
@@ -113,13 +116,13 @@ func collectTasksForDate(plans []Plan, crops []Crop, products []Product, stepMap
}
tasks = append(tasks, Task{
UID: fmt.Sprintf("custom:%d", c.ID),
Type: "Aufgabe",
Field: field,
Message: msg,
Month: month,
Day: day,
YearOffset: yearOffset,
SortOrder: 40,
Type: "Aufgabe",
Field: field,
Message: msg,
Month: periodMonth(period),
Day: day,
YearOffset: yearOffset,
SortOrder: 40,
})
}
}
@@ -127,18 +130,18 @@ func collectTasksForDate(plans []Plan, crops []Crop, products []Product, stepMap
return tasks
}
func expandPlanDayTasks(p Plan, steps []CropStep, month, day, yearOffset int) []Task {
func expandPlanDayTasks(p Plan, steps []CropStep, period, day, yearOffset int) []Task {
field := p.TargetName
if strings.TrimSpace(field) == "" {
field = "Unbekanntes Feld"
}
month := periodMonth(period)
var out []Task
baseHarvest := p.HarvestMonth
if p.StartMonth == month && p.StartDay == day && yearOffset == 0 {
if p.StartPeriod == period && p.StartDay == day {
out = append(out, Task{
UID: fmt.Sprintf("plan:%d:sow:0", p.ID),
UID: fmt.Sprintf("plan:%d:sow:%d", p.ID, p.StartPeriod),
Type: "Aussaat",
Field: field,
Message: withOptionalNote(fmt.Sprintf("Aussaat %s", p.CropName), p.Notes),
@@ -149,21 +152,21 @@ func expandPlanDayTasks(p Plan, steps []CropStep, month, day, yearOffset int) []
})
}
harvestMonths := []int{baseHarvest}
harvestPeriods := []int{p.HarvestPeriod}
if p.RegrowEnabled {
maxExtra := p.RegrowCycles
if maxExtra == 0 {
maxExtra = 24
}
for i := 1; i <= maxExtra; i++ {
harvestMonths = append(harvestMonths, wrapMonth(baseHarvest+(i*p.HarvestDistance())))
harvestPeriods = append(harvestPeriods, p.HarvestPeriod+(i*p.HarvestDistance()))
}
}
harvestMonths = uniqueMonths(harvestMonths)
harvestPeriods = uniquePeriods(harvestPeriods)
for idx, hm := range harvestMonths {
if hm == month && p.HarvestDay == day {
uid := fmt.Sprintf("plan:%d:harvest:%d", p.ID, idx)
for _, harvestPeriod := range harvestPeriods {
if harvestPeriod == period && p.HarvestDay == day {
uid := fmt.Sprintf("plan:%d:harvest:%d", p.ID, harvestPeriod)
out = append(out, Task{
UID: uid,
Type: "Ernte",
@@ -180,10 +183,10 @@ func expandPlanDayTasks(p Plan, steps []CropStep, month, day, yearOffset int) []
for _, s := range steps {
switch s.Phase {
case "pre":
taskMonth := wrapMonth(p.StartMonth - s.MonthOffset)
if taskMonth == month && p.StartDay == day && yearOffset == 0 {
taskPeriod := p.StartPeriod - s.MonthOffset
if taskPeriod == period && p.StartDay == day {
out = append(out, Task{
UID: fmt.Sprintf("plan:%d:pre:%d", p.ID, s.ID),
UID: fmt.Sprintf("plan:%d:pre:%d:%d", p.ID, s.ID, taskPeriod),
Type: "Vorbereitung",
Field: field,
Message: s.Title,
@@ -194,11 +197,11 @@ func expandPlanDayTasks(p Plan, steps []CropStep, month, day, yearOffset int) []
})
}
case "post":
for idx, hm := range harvestMonths {
taskMonth := wrapMonth(hm + s.MonthOffset)
if taskMonth == month && p.HarvestDay == day {
for _, harvestPeriod := range harvestPeriods {
taskPeriod := harvestPeriod + s.MonthOffset
if taskPeriod == period && p.HarvestDay == day {
out = append(out, Task{
UID: fmt.Sprintf("plan:%d:post:%d:%d", p.ID, s.ID, idx),
UID: fmt.Sprintf("plan:%d:post:%d:%d", p.ID, s.ID, taskPeriod),
Type: "Nachbereitung",
Field: field,
Message: s.Title,
@@ -252,7 +255,7 @@ func sortTasks(tasks []Task) {
})
}
func uniqueMonths(values []int) []int {
func uniquePeriods(values []int) []int {
seen := make(map[int]bool)
var out []int
for _, v := range values {