Fortlaufende Zyklus-Planung und Periodenlogik einführen
This commit is contained in:
@@ -11,12 +11,15 @@ func ensureSchema(db *sql.DB) error {
|
||||
id TINYINT PRIMARY KEY,
|
||||
days_per_month INT NOT NULL DEFAULT 2,
|
||||
current_month TINYINT NOT NULL DEFAULT 1,
|
||||
current_cycle INT NOT NULL DEFAULT 0,
|
||||
current_day TINYINT NOT NULL DEFAULT 1
|
||||
)`,
|
||||
`ALTER TABLE settings ADD COLUMN IF NOT EXISTS current_month TINYINT NOT NULL DEFAULT 1`,
|
||||
`ALTER TABLE settings ADD COLUMN IF NOT EXISTS current_cycle INT NOT NULL DEFAULT 0`,
|
||||
`ALTER TABLE settings ADD COLUMN IF NOT EXISTS current_day TINYINT NOT NULL DEFAULT 1`,
|
||||
`INSERT INTO settings(id,days_per_month,current_month,current_day) VALUES (1,2,1,1) ON DUPLICATE KEY UPDATE id=id`,
|
||||
`INSERT INTO settings(id,days_per_month,current_month,current_cycle,current_day) VALUES (1,2,1,0,1) ON DUPLICATE KEY UPDATE id=id`,
|
||||
`UPDATE settings SET current_month=1 WHERE current_month < 1 OR current_month > 12`,
|
||||
`UPDATE settings SET current_cycle=0 WHERE current_cycle < 0`,
|
||||
`UPDATE settings SET current_day=1 WHERE current_day < 1`,
|
||||
|
||||
`CREATE TABLE IF NOT EXISTS fields(
|
||||
@@ -71,6 +74,7 @@ func ensureSchema(db *sql.DB) error {
|
||||
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
||||
template_id BIGINT NULL,
|
||||
title VARCHAR(140) NOT NULL,
|
||||
task_period INT NOT NULL DEFAULT 0,
|
||||
month TINYINT NOT NULL,
|
||||
day TINYINT NOT NULL,
|
||||
year_offset SMALLINT NOT NULL DEFAULT 0,
|
||||
@@ -95,8 +99,10 @@ func ensureSchema(db *sql.DB) error {
|
||||
target_ref VARCHAR(80) NOT NULL DEFAULT '',
|
||||
target_name VARCHAR(140) NOT NULL DEFAULT '',
|
||||
crop_id BIGINT NOT NULL,
|
||||
start_period INT NOT NULL DEFAULT 0,
|
||||
start_month TINYINT NOT NULL,
|
||||
start_day TINYINT NOT NULL,
|
||||
harvest_period INT NOT NULL DEFAULT 0,
|
||||
harvest_month TINYINT NOT NULL,
|
||||
harvest_day TINYINT NOT NULL,
|
||||
notes VARCHAR(255) NOT NULL DEFAULT '',
|
||||
@@ -107,6 +113,12 @@ func ensureSchema(db *sql.DB) error {
|
||||
`ALTER TABLE plans MODIFY COLUMN field_id BIGINT NULL`,
|
||||
`ALTER TABLE plans ADD COLUMN IF NOT EXISTS target_ref VARCHAR(80) NOT NULL DEFAULT '' AFTER field_id`,
|
||||
`ALTER TABLE plans ADD COLUMN IF NOT EXISTS target_name VARCHAR(140) NOT NULL DEFAULT '' AFTER target_ref`,
|
||||
`ALTER TABLE plans ADD COLUMN IF NOT EXISTS start_period INT NOT NULL DEFAULT 0 AFTER crop_id`,
|
||||
`ALTER TABLE plans ADD COLUMN IF NOT EXISTS harvest_period INT NOT NULL DEFAULT 0 AFTER start_day`,
|
||||
`UPDATE plans SET start_period = start_month - 1 WHERE start_period = 0 AND start_month <> 1`,
|
||||
`UPDATE plans SET harvest_period = start_period + (CASE WHEN harvest_month >= start_month THEN harvest_month - start_month ELSE harvest_month + 12 - start_month END) WHERE harvest_period = 0`,
|
||||
`ALTER TABLE custom_tasks ADD COLUMN IF NOT EXISTS task_period INT NOT NULL DEFAULT 0 AFTER title`,
|
||||
`UPDATE custom_tasks SET task_period = (year_offset * 12) + (month - 1) WHERE task_period = 0 AND (year_offset <> 0 OR month <> 1)`,
|
||||
}
|
||||
for _, stmt := range stmts {
|
||||
if _, err := db.Exec(stmt); err != nil {
|
||||
@@ -118,8 +130,8 @@ func ensureSchema(db *sql.DB) error {
|
||||
|
||||
func (a *App) getSettings() (Settings, error) {
|
||||
var s Settings
|
||||
err := a.db.QueryRow(`SELECT days_per_month,current_month,current_day FROM settings WHERE id=1`).
|
||||
Scan(&s.DaysPerMonth, &s.CurrentMonth, &s.CurrentDay)
|
||||
err := a.db.QueryRow(`SELECT days_per_month,current_month,current_cycle,current_day FROM settings WHERE id=1`).
|
||||
Scan(&s.DaysPerMonth, &s.CurrentMonth, &s.CurrentCycle, &s.CurrentDay)
|
||||
return s, err
|
||||
}
|
||||
|
||||
@@ -199,10 +211,10 @@ func (a *App) listProducts() ([]Product, error) {
|
||||
|
||||
func (a *App) listPlans() ([]Plan, error) {
|
||||
rows, err := a.db.Query(`
|
||||
SELECT p.id,p.field_id,COALESCE(p.target_ref,''),COALESCE(p.target_name,''),p.crop_id,COALESCE(c.name,''),COALESCE(c.grow_months,1),p.start_month,p.start_day,p.harvest_month,p.harvest_day,COALESCE(p.notes,''),COALESCE(c.regrow_enabled,0),COALESCE(c.regrow_cycles,0)
|
||||
SELECT p.id,p.field_id,COALESCE(p.target_ref,''),COALESCE(p.target_name,''),p.crop_id,COALESCE(c.name,''),COALESCE(c.grow_months,1),p.start_period,p.start_month,p.start_day,p.harvest_period,p.harvest_month,p.harvest_day,COALESCE(p.notes,''),COALESCE(c.regrow_enabled,0),COALESCE(c.regrow_cycles,0)
|
||||
FROM plans p
|
||||
JOIN crops c ON c.id=p.crop_id
|
||||
ORDER BY p.start_month,p.start_day,p.id DESC`)
|
||||
ORDER BY p.start_period,p.start_day,p.id DESC`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -210,9 +222,11 @@ func (a *App) listPlans() ([]Plan, error) {
|
||||
var out []Plan
|
||||
for rows.Next() {
|
||||
var p Plan
|
||||
if err := rows.Scan(&p.ID, &p.FieldID, &p.TargetRef, &p.TargetName, &p.CropID, &p.CropName, &p.GrowMonths, &p.StartMonth, &p.StartDay, &p.HarvestMonth, &p.HarvestDay, &p.Notes, &p.RegrowEnabled, &p.RegrowCycles); err != nil {
|
||||
if err := rows.Scan(&p.ID, &p.FieldID, &p.TargetRef, &p.TargetName, &p.CropID, &p.CropName, &p.GrowMonths, &p.StartPeriod, &p.StartMonth, &p.StartDay, &p.HarvestPeriod, &p.HarvestMonth, &p.HarvestDay, &p.Notes, &p.RegrowEnabled, &p.RegrowCycles); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p.StartCycle = periodCycle(p.StartPeriod)
|
||||
p.HarvestCycle = periodCycle(p.HarvestPeriod)
|
||||
out = append(out, p)
|
||||
}
|
||||
return out, rows.Err()
|
||||
@@ -272,9 +286,9 @@ func (a *App) listCustomTaskTemplates() ([]CustomTaskTemplate, error) {
|
||||
|
||||
func (a *App) listCustomTasks() ([]CustomTask, error) {
|
||||
rows, err := a.db.Query(`
|
||||
SELECT id,template_id,title,month,day,year_offset,COALESCE(target_name,''),COALESCE(notes,'')
|
||||
SELECT id,template_id,title,task_period,month,day,year_offset,COALESCE(target_name,''),COALESCE(notes,'')
|
||||
FROM custom_tasks
|
||||
ORDER BY year_offset, month, day, title`)
|
||||
ORDER BY task_period, day, title`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -283,9 +297,11 @@ func (a *App) listCustomTasks() ([]CustomTask, error) {
|
||||
var out []CustomTask
|
||||
for rows.Next() {
|
||||
var t CustomTask
|
||||
if err := rows.Scan(&t.ID, &t.TemplateID, &t.Title, &t.Month, &t.Day, &t.YearOffset, &t.TargetName, &t.Notes); err != nil {
|
||||
if err := rows.Scan(&t.ID, &t.TemplateID, &t.Title, &t.TaskPeriod, &t.Month, &t.Day, &t.YearOffset, &t.TargetName, &t.Notes); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
t.Month = periodMonth(t.TaskPeriod)
|
||||
t.Cycle = periodCycle(t.TaskPeriod)
|
||||
out = append(out, t)
|
||||
}
|
||||
return out, rows.Err()
|
||||
|
||||
Reference in New Issue
Block a user