[#14] Models: Add DeliveryAncmt

This commit is contained in:
2024-08-02 21:14:08 +02:00
parent 2333077aa5
commit 2d737e2780
13 changed files with 297 additions and 13 deletions

View File

@ -51,6 +51,9 @@ namespace Elwig.Helpers {
public DbSet<MemberHistory> MemberHistory { get; private set; } public DbSet<MemberHistory> MemberHistory { get; private set; }
public DbSet<AreaCom> AreaCommitments { get; private set; } public DbSet<AreaCom> AreaCommitments { get; private set; }
public DbSet<Season> Seasons { get; private set; } public DbSet<Season> Seasons { get; private set; }
public DbSet<DeliverySchedule> DeliverySchedules { get; private set; }
public DbSet<DeliveryScheduleWineVar> DeliveryScheduleWineVarieties { get; private set; }
public DbSet<DeliveryAncmt> DeliveryAnnouncements { get; private set; }
public DbSet<Modifier> Modifiers { get; private set; } public DbSet<Modifier> Modifiers { get; private set; }
public DbSet<Delivery> Deliveries { get; private set; } public DbSet<Delivery> Deliveries { get; private set; }
public DbSet<DeliveryPart> DeliveryParts { get; private set; } public DbSet<DeliveryPart> DeliveryParts { get; private set; }

View File

@ -9,7 +9,7 @@ namespace Elwig.Helpers {
public static class AppDbUpdater { public static class AppDbUpdater {
// Don't forget to update value in Tests/fetch-resources.bat! // Don't forget to update value in Tests/fetch-resources.bat!
public static readonly int RequiredSchemaVersion = 25; public static readonly int RequiredSchemaVersion = 26;
private static int VersionOffset = 0; private static int VersionOffset = 0;

View File

@ -1,5 +1,6 @@
using Elwig.Helpers; using Elwig.Helpers;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations.Schema;
@ -39,6 +40,16 @@ namespace Elwig.Models.Entities {
[Column("comment")] [Column("comment")]
public string? Comment { get; set; } public string? Comment { get; set; }
[Column("ctime"), DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public long CTime { get; private set; }
[NotMapped]
public DateTime CreatedTimestamp => DateTimeOffset.FromUnixTimeSeconds(CTime).LocalDateTime;
[Column("mtime"), DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public long MTime { get; private set; }
[NotMapped]
public DateTime ModifiedTimestamp => DateTimeOffset.FromUnixTimeSeconds(MTime).LocalDateTime;
[ForeignKey("MgNr")] [ForeignKey("MgNr")]
public virtual Member Member { get; private set; } = null!; public virtual Member Member { get; private set; } = null!;

View File

@ -83,15 +83,15 @@ namespace Elwig.Models.Entities {
get => Utils.DecFromDb(AmountValue, 2); get => Utils.DecFromDb(AmountValue, 2);
} }
[Column("ctime")] [Column("ctime"), DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public long CTime { get; private set; } public long CTime { get; private set; }
[NotMapped] [NotMapped]
public DateTime CreatedTimestamp => DateTimeOffset.FromUnixTimeSeconds(CTime).LocalDateTime; public DateTime CreatedTimestamp => DateTimeOffset.FromUnixTimeSeconds(CTime).LocalDateTime;
[Column("mtime")] [Column("mtime"), DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public long MTime { get; private set; } public long MTime { get; private set; }
[NotMapped] [NotMapped]
public DateTime ModifiedTimestamp => DateTimeOffset.FromUnixTimeSeconds(CTime).LocalDateTime; public DateTime ModifiedTimestamp => DateTimeOffset.FromUnixTimeSeconds(MTime).LocalDateTime;
[ForeignKey("Year, AvNr, MgNr")] [ForeignKey("Year, AvNr, MgNr")]
public virtual PaymentMember Payment { get; private set; } = null!; public virtual PaymentMember Payment { get; private set; } = null!;

View File

@ -17,7 +17,6 @@ namespace Elwig.Models.Entities {
[Column("date")] [Column("date")]
public required string DateString { get; set; } public required string DateString { get; set; }
[NotMapped] [NotMapped]
public DateOnly Date { public DateOnly Date {
get => DateOnly.ParseExact(DateString, "yyyy-MM-dd"); get => DateOnly.ParseExact(DateString, "yyyy-MM-dd");
@ -26,7 +25,6 @@ namespace Elwig.Models.Entities {
[Column("time")] [Column("time")]
public string? TimeString { get; set; } public string? TimeString { get; set; }
[NotMapped] [NotMapped]
public TimeOnly? Time { public TimeOnly? Time {
get => (TimeString == null) ? null : TimeOnly.ParseExact(TimeString, "HH:mm:ss"); get => (TimeString == null) ? null : TimeOnly.ParseExact(TimeString, "HH:mm:ss");
@ -63,6 +61,16 @@ namespace Elwig.Models.Entities {
[Column("comment")] [Column("comment")]
public string? Comment { get; set; } public string? Comment { get; set; }
[Column("ctime"), DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public long CTime { get; private set; }
[NotMapped]
public DateTime CreatedTimestamp => DateTimeOffset.FromUnixTimeSeconds(CTime).LocalDateTime;
[Column("mtime"), DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public long MTime { get; private set; }
[NotMapped]
public DateTime ModifiedTimestamp => DateTimeOffset.FromUnixTimeSeconds(MTime).LocalDateTime;
[ForeignKey("Year")] [ForeignKey("Year")]
public virtual Season Season { get; private set; } = null!; public virtual Season Season { get; private set; } = null!;

View File

@ -0,0 +1,56 @@
using Elwig.Helpers;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
namespace Elwig.Models.Entities {
[Table("delivery_announcement"), PrimaryKey("Year", "DsNr", "MgNr", "SortId")]
public class DeliveryAncmt {
[Column("year")]
public int Year { get; set; }
[Column("dsnr")]
public int DsNr { get; set; }
[Column("mgnr")]
public int MgNr { get; set; }
[Column("sortid")]
public required string SortId { get; set; }
[Column("weight")]
public int Weight { get; set; }
[Column("type")]
public required string Type { get; set; }
[Column("ctime"), DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public long CTime { get; private set; }
[NotMapped]
public DateTime CreatedTimestamp => DateTimeOffset.FromUnixTimeSeconds(CTime).LocalDateTime;
[Column("mtime"), DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public long MTime { get; private set; }
[NotMapped]
public DateTime ModifiedTimestamp => DateTimeOffset.FromUnixTimeSeconds(MTime).LocalDateTime;
[ForeignKey("Year, DsNr")]
public virtual DeliverySchedule Schedule { get; private set; } = null!;
[ForeignKey("MgNr")]
public virtual Member Member { get; private set; } = null!;
[ForeignKey("SortId")]
public virtual WineVar Variety { get; private set; } = null!;
public int SearchScore(IEnumerable<string> keywords) {
return Utils.GetSearchScore([
Schedule.Description,
Member.FamilyName, Member.MiddleName, Member.GivenName,
Member.BillingAddress?.Name,
], keywords);
}
}
}

View File

@ -128,6 +128,16 @@ namespace Elwig.Models.Entities {
[Column("comment")] [Column("comment")]
public string? Comment { get; set; } public string? Comment { get; set; }
[Column("ctime"), DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public long CTime { get; private set; }
[NotMapped]
public DateTime CreatedTimestamp => DateTimeOffset.FromUnixTimeSeconds(CTime).LocalDateTime;
[Column("mtime"), DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public long MTime { get; private set; }
[NotMapped]
public DateTime ModifiedTimestamp => DateTimeOffset.FromUnixTimeSeconds(MTime).LocalDateTime;
[InverseProperty(nameof(DeliveryPartModifier.Part))] [InverseProperty(nameof(DeliveryPartModifier.Part))]
public virtual ICollection<DeliveryPartModifier> PartModifiers { get; private set; } = null!; public virtual ICollection<DeliveryPartModifier> PartModifiers { get; private set; } = null!;

View File

@ -0,0 +1,63 @@
using Elwig.Helpers;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
namespace Elwig.Models.Entities {
[Table("delivery_schedule"), PrimaryKey("Year", "DsNr")]
public class DeliverySchedule {
[Column("year")]
public int Year { get; set; }
[Column("dsnr")]
public int DsNr { get; set; }
[Column("date")]
public required string DateString { get; set; }
[NotMapped]
public DateOnly Date {
get => DateOnly.ParseExact(DateString, "yyyy-MM-dd");
set => DateString = value.ToString("yyyy-MM-dd");
}
[Column("zwstid")]
public required string ZwstId { get; set; }
[Column("description")]
public required string Description { get; set; }
[Column("max_weight")]
public int? MaxWeight { get; set; }
[Column("ancmt_from")]
public long? AncmtFromUnix { get; set; }
[NotMapped]
public DateTime? AncmtFrom {
get => AncmtFromUnix != null ? DateTimeOffset.FromUnixTimeSeconds(AncmtFromUnix.Value).LocalDateTime : null;
set => AncmtFromUnix = value != null ? new DateTimeOffset(value.Value).ToUnixTimeSeconds() : null;
}
[Column("ancmt_to")]
public long? AncmtToUnix { get; set; }
[NotMapped]
public DateTime? AncmtTo {
get => AncmtToUnix != null ? DateTimeOffset.FromUnixTimeSeconds(AncmtToUnix.Value).LocalDateTime : null;
set => AncmtToUnix = value != null ? new DateTimeOffset(value.Value).ToUnixTimeSeconds() : null;
}
[ForeignKey("Year")]
public virtual Season Season { get; private set; } = null!;
[ForeignKey("ZwstId")]
public virtual Branch Branch { get; private set; } = null!;
[InverseProperty(nameof(DeliveryScheduleWineVar.Schedule))]
public virtual ICollection<DeliveryScheduleWineVar> Varieties { get; private set; } = null!;
public int SearchScore(IEnumerable<string> keywords) {
return Utils.GetSearchScore([Description], keywords);
}
}
}

View File

@ -0,0 +1,26 @@
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations.Schema;
namespace Elwig.Models.Entities {
[Table("delivery_schedule_wine_variety"), PrimaryKey("Year", "DsNr", "SortId")]
public class DeliveryScheduleWineVar {
[Column("year")]
public int Year { get; set; }
[Column("dsnr")]
public int DsNr { get; set; }
[Column("sortid")]
public required string SortId { get; set; }
[Column("priority")]
public int Priority { get; set; }
[ForeignKey("Year, DsNr")]
public virtual DeliverySchedule Schedule { get; private set; } = null!;
[ForeignKey("SortId")]
public virtual WineVar Variety { get; private set; } = null!;
}
}

View File

@ -22,7 +22,6 @@ namespace Elwig.Models.Entities {
[Column("middle_names")] [Column("middle_names")]
public string? MiddleName { get; set; } public string? MiddleName { get; set; }
[NotMapped] [NotMapped]
public string[] MiddleNames { public string[] MiddleNames {
get => (MiddleName != null) ? MiddleName.Split(" ") : []; get => (MiddleName != null) ? MiddleName.Split(" ") : [];
@ -59,7 +58,6 @@ namespace Elwig.Models.Entities {
[Column("entry_date")] [Column("entry_date")]
public string? EntryDateString { get; set; } public string? EntryDateString { get; set; }
[NotMapped] [NotMapped]
public DateOnly? EntryDate { public DateOnly? EntryDate {
get => EntryDateString != null ? DateOnly.ParseExact(EntryDateString, "yyyy-MM-dd") : null; get => EntryDateString != null ? DateOnly.ParseExact(EntryDateString, "yyyy-MM-dd") : null;
@ -68,7 +66,6 @@ namespace Elwig.Models.Entities {
[Column("exit_date")] [Column("exit_date")]
public string? ExitDateString { get; set; } public string? ExitDateString { get; set; }
[NotMapped] [NotMapped]
public DateOnly? ExitDate { public DateOnly? ExitDate {
get => ExitDateString != null ? DateOnly.ParseExact(ExitDateString, "yyyy-MM-dd") : null; get => ExitDateString != null ? DateOnly.ParseExact(ExitDateString, "yyyy-MM-dd") : null;
@ -146,10 +143,19 @@ namespace Elwig.Models.Entities {
[ForeignKey("DefaultKgNr")] [ForeignKey("DefaultKgNr")]
public virtual WbKg? DefaultWbKg { get; private set; } public virtual WbKg? DefaultWbKg { get; private set; }
[NotMapped] [NotMapped]
public AT_Kg? DefaultKg => DefaultWbKg?.AtKg; public AT_Kg? DefaultKg => DefaultWbKg?.AtKg;
[Column("ctime"), DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public long CTime { get; private set; }
[NotMapped]
public DateTime CreatedTimestamp => DateTimeOffset.FromUnixTimeSeconds(CTime).LocalDateTime;
[Column("mtime"), DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public long MTime { get; private set; }
[NotMapped]
public DateTime ModifiedTimestamp => DateTimeOffset.FromUnixTimeSeconds(MTime).LocalDateTime;
[ForeignKey("ZwstId")] [ForeignKey("ZwstId")]
public virtual Branch? Branch { get; private set; } public virtual Branch? Branch { get; private set; }

View File

@ -35,9 +35,9 @@ namespace Elwig.Models.Entities {
public bool TestVariant { get; set; } public bool TestVariant { get; set; }
[Column("calc_time")] [Column("calc_time")]
public int? CalcTimeUnix { get; set; } public long? CalcTimeUnix { get; set; }
[NotMapped] [NotMapped]
public DateTime? CalcTime => CalcTimeUnix != null ? DateTimeOffset.FromUnixTimeSeconds((long)CalcTimeUnix).UtcDateTime.ToLocalTime() : null; public DateTime? CalcTime => CalcTimeUnix != null ? DateTimeOffset.FromUnixTimeSeconds(CalcTimeUnix.Value).LocalDateTime : null;
[Column("comment")] [Column("comment")]
public string? Comment { get; set; } public string? Comment { get; set; }
@ -45,6 +45,16 @@ namespace Elwig.Models.Entities {
[Column("data")] [Column("data")]
public required string Data { get; set; } public required string Data { get; set; }
[Column("ctime"), DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public long CTime { get; private set; }
[NotMapped]
public DateTime CreatedTimestamp => DateTimeOffset.FromUnixTimeSeconds(CTime).LocalDateTime;
[Column("mtime"), DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public long MTime { get; private set; }
[NotMapped]
public DateTime ModifiedTimestamp => DateTimeOffset.FromUnixTimeSeconds(MTime).LocalDateTime;
[ForeignKey("Year")] [ForeignKey("Year")]
public virtual Season Season { get; private set; } = null!; public virtual Season Season { get; private set; } = null!;

View File

@ -0,0 +1,91 @@
-- schema version 24 to 25
CREATE TABLE delivery_schedule (
year INTEGER NOT NULL,
dsnr INTEGER NOT NULL,
date TEXT NOT NULL CHECK (date LIKE year || '-%' AND date REGEXP '^[1-9][0-9]{3}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$'),
zwstid TEXT NOT NULL,
description TEXT NOT NULL,
max_weight INTEGER,
ancmt_from INTEGER,
ancmt_to INTEGER,
CONSTRAINT pk_delivery_schedule PRIMARY KEY (year, dsnr),
CONSTRAINT fk_delivery_schedule_season FOREIGN KEY (year) REFERENCES season (year)
ON UPDATE CASCADE
ON DELETE RESTRICT,
CONSTRAINT fk_delivery_schedule_branch FOREIGN KEY (zwstid) REFERENCES branch (zwstid)
ON UPDATE CASCADE
ON DELETE RESTRICT
) STRICT;
CREATE TABLE delivery_schedule_wine_variety (
year INTEGER NOT NULL,
dsnr INTEGER NOT NULL,
sortid TEXT NOT NULL,
priority INTEGER NOT NULL DEFAULT 1,
CONSTRAINT pk_delivery_schedule_wine_variety PRIMARY KEY (year, dsnr, sortid),
CONSTRAINT fk_delivery_schedule_wine_variety_delivery_schedule FOREIGN KEY (year, dsnr) REFERENCES delivery_schedule (year, dsnr)
ON UPDATE CASCADE
ON DELETE CASCADE,
CONSTRAINT fk_delivery_schedule_wine_variety_wine_variety FOREIGN KEY (sortid) REFERENCES wine_variety (sortid)
ON UPDATE CASCADE
ON DELETE RESTRICT
) STRICT;
CREATE TABLE delivery_announcement (
year INTEGER NOT NULL,
dsnr INTEGER NOT NULL,
mgnr INTEGER NOT NULL,
sortid TEXT NOT NULL,
weight INTEGER NOT NULL,
type TEXT NOT NULL,
ctime INTEGER NOT NULL DEFAULT (UNIXEPOCH()),
mtime INTEGER NOT NULL DEFAULT (UNIXEPOCH()),
CONSTRAINT pk_delivery_announcement PRIMARY KEY (year, dsnr, mgnr, sortid),
CONSTRAINT fk_delivery_announcement_delivery_schedule FOREIGN KEY (year, dsnr) REFERENCES delivery_schedule (year, dsnr)
ON UPDATE CASCADE
ON DELETE CASCADE,
CONSTRAINT fk_delivery_announcement_member FOREIGN KEY (mgnr) REFERENCES member (mgnr)
ON UPDATE CASCADE
ON DELETE CASCADE,
CONSTRAINT fk_delivery_announcement_wine_variety FOREIGN KEY (sortid) REFERENCES wine_variety (sortid)
ON UPDATE CASCADE
ON DELETE RESTRICT
) STRICT;
CREATE TRIGGER t_delivery_announcement_i_ctime
AFTER INSERT ON delivery_announcement FOR EACH ROW
WHEN NEW.ctime != UNIXEPOCH()
BEGIN
UPDATE delivery_announcement SET ctime = UNIXEPOCH() WHERE (year, dsnr, mgnr, sortid) = (NEW.year, NEW.dsnr, NEW.mgnr, NEW.sortid);
END;
CREATE TRIGGER t_delivery_announcement_u_ctime
BEFORE UPDATE ON delivery_announcement FOR EACH ROW
WHEN OLD.ctime != NEW.ctime
BEGIN
SELECT RAISE(ABORT, 'It is not allowed to change ctime');
END;
CREATE TRIGGER t_delivery_announcement_i_mtime
AFTER INSERT ON delivery_announcement FOR EACH ROW
WHEN NEW.mtime != UNIXEPOCH()
BEGIN
UPDATE delivery_announcement SET mtime = UNIXEPOCH() WHERE (year, dsnr, mgnr, sortid) = (NEW.year, NEW.dsnr, NEW.mgnr, NEW.sortid);
END;
CREATE TRIGGER t_delivery_announcement_u_mtime
AFTER UPDATE ON delivery_announcement FOR EACH ROW
WHEN NEW.mtime != UNIXEPOCH()
BEGIN
UPDATE delivery_announcement SET mtime = UNIXEPOCH() WHERE (year, dsnr, mgnr, sortid) = (NEW.year, NEW.dsnr, NEW.mgnr, NEW.sortid);
END;

View File

@ -1 +1 @@
curl --fail -s -L "https://elwig.at/files/create.sql?v=25" -u "elwig:ganzGeheim123!" -o "Resources\Sql\Create.sql" curl --fail -s -L "https://elwig.at/files/create.sql?v=26" -u "elwig:ganzGeheim123!" -o "Resources\Sql\Create.sql"