This commit is contained in:
@ -594,5 +594,76 @@ namespace Elwig.Helpers {
|
||||
public static IEnumerable<AreaCom> ActiveAreaCommitments(IEnumerable<AreaCom> query) => ActiveAreaCommitments(query, CurrentYear);
|
||||
public static IEnumerable<AreaCom> ActiveAreaCommitments(IEnumerable<AreaCom> query, int year) =>
|
||||
query.Where(c => ActiveAreaCommitments(year).Invoke(c));
|
||||
|
||||
public static async Task<(DateTime DateTime, string Type, int MgNr, string Name, string[] Addresses, string Subject, string[] Attachments)[]> GetSentMails(DateOnly? fromDate = null, DateOnly? toDate = null) {
|
||||
var f = $"{fromDate:yyyy-MM-dd}";
|
||||
var t = $"{toDate:yyyy-MM-dd}";
|
||||
try {
|
||||
var rows = new List<(DateTime, string, int, string, string[], string, string[])>();
|
||||
var filenames = Directory.GetFiles(App.MailsPath, "????.csv")
|
||||
.Where(n => fromDate == null || Path.GetFileName(n).CompareTo($"{fromDate.Value.Year}.csv") >= 0)
|
||||
.Where(n => toDate == null || Path.GetFileName(n).CompareTo($"{toDate.Value.Year}.csv") <= 0)
|
||||
.Order();
|
||||
foreach (var filename in filenames) {
|
||||
using var reader = new StreamReader(filename, Utils.UTF8);
|
||||
string? line;
|
||||
while ((line = await reader.ReadLineAsync()) != null) {
|
||||
try {
|
||||
if (line.Length < 20 || line[10] != ';' || line[19] != ';')
|
||||
continue;
|
||||
var date = line[..10];
|
||||
if (fromDate != null && date.CompareTo(f) < 0) {
|
||||
continue;
|
||||
} else if (toDate != null && date.CompareTo(t) > 0) {
|
||||
break;
|
||||
}
|
||||
var p = line.Split(';');
|
||||
rows.Add((
|
||||
DateOnly.ParseExact(p[0], "yyyy-MM-dd").ToDateTime(TimeOnly.ParseExact(p[1], "HH:mm:ss")),
|
||||
p[2],
|
||||
int.Parse(p[3]),
|
||||
p[4],
|
||||
p[5].Split(',').Select(a => a.Replace(" | ", "\n")).ToArray(),
|
||||
p[6],
|
||||
p[7].Split(',')
|
||||
));
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
return [.. rows];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task AddSentMails(IEnumerable<(string Type, int MgNr, string Name, string[] Addresses, string Subject, string[] Attachments)> data) {
|
||||
var now = DateTime.Now;
|
||||
var filename = Path.Combine(App.MailsPath, $"{now.Year}.csv");
|
||||
await File.AppendAllLinesAsync(filename, data.Select(d =>
|
||||
$"{now:yyyy-MM-dd;HH:mm:ss};{d.Type};" +
|
||||
$"{d.MgNr};{d.Name.Replace(';', ' ')};" +
|
||||
$"{string.Join(',', d.Addresses.Select(a => a.Replace(';', ' ').Replace(',', ' ').Replace("\n", " | ")))};" +
|
||||
$"{d.Subject.Replace(';', ' ')};" +
|
||||
$"{string.Join(',', d.Attachments.Select(a => a.Replace(';', ' ').Replace(',', ' ')))}"
|
||||
), Utils.UTF8);
|
||||
}
|
||||
|
||||
public static async Task<string?> FindSentMailBody(DateTime target) {
|
||||
var dt = $"{target:yyyy-MM-dd_HH-mm-ss}_";
|
||||
var filename = Directory.GetFiles(App.MailsPath, "????-??-??_??-??-??_*.txt")
|
||||
.Where(n => Path.GetFileName(n).CompareTo(dt) <= 0)
|
||||
.Order()
|
||||
.LastOrDefault();
|
||||
if (filename == null)
|
||||
return null;
|
||||
return await File.ReadAllTextAsync(filename, Utils.UTF8);
|
||||
}
|
||||
|
||||
public static async Task AddSentMailBody(string subject, string body, int recipients) {
|
||||
var filename = Path.Combine(App.MailsPath, $"{DateTime.Now:yyyy-MM-dd_HH-mm-ss}_{NormalizeFileName(subject)}.txt");
|
||||
await File.WriteAllTextAsync(filename, $"# {subject}\r\n# Vorgesehene Empfänger: {recipients}\r\n\r\n" + body, Utils.UTF8);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user