58 lines
2.9 KiB
C#
58 lines
2.9 KiB
C#
using File_Folder_Helper.Models;
|
|
using Microsoft.Extensions.Logging;
|
|
using System.Globalization;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace File_Folder_Helper.ADO2024.PI2;
|
|
|
|
internal static partial class Helper20240518 {
|
|
|
|
internal static void PersonKeyToImmichImport(ILogger<Worker> logger, List<string> args) {
|
|
string json;
|
|
string name;
|
|
Person person;
|
|
string birthDate;
|
|
string ownerId = args[5];
|
|
List<string> inserts = [];
|
|
string tableName = args[3];
|
|
string birthDateFormat = args[6];
|
|
string[] columns = args[4].Split(',');
|
|
string root = Path.GetFullPath(args[0]);
|
|
Dictionary<string, string> keyValuePairs = [];
|
|
json = File.ReadAllText(Path.Combine(root, args[2]));
|
|
DateOnly minimumBirthDate = DateOnly.ParseExact(args[7], birthDateFormat, CultureInfo.InvariantCulture);
|
|
Dictionary<long, Person> people = JsonSerializer.Deserialize(json, PeopleSourceGenerationContext.Default.DictionaryInt64Person) ??
|
|
throw new NullReferenceException();
|
|
foreach (KeyValuePair<long, Person> keyValuePair in people) {
|
|
person = keyValuePair.Value;
|
|
if (string.IsNullOrEmpty(person.Birth?.Note) || string.IsNullOrEmpty(person.Name?.ForwardSlashFull) || person.Birth.Date < minimumBirthDate) {
|
|
continue;
|
|
}
|
|
name = string.IsNullOrEmpty(person.Name.Suffix) ? person.Name.ForwardSlashFull : $"{person.Name.ForwardSlashFull} {person.Name.Suffix}";
|
|
keyValuePairs.Add(person.Birth.Note, name);
|
|
}
|
|
json = JsonSerializer.Serialize(keyValuePairs, DictionaryStringStringSourceGenerationContext.Default.DictionaryStringString);
|
|
string jsonFile = Path.Combine(root, $"{DateTime.Now.Ticks}.json");
|
|
logger.LogInformation("<{file}> saved", jsonFile);
|
|
File.WriteAllText(jsonFile, json);
|
|
foreach (KeyValuePair<long, Person> keyValuePair in people) {
|
|
person = keyValuePair.Value;
|
|
if (string.IsNullOrEmpty(person.Birth?.Note) || string.IsNullOrEmpty(person.Name?.ForwardSlashFull) || person.Birth?.Date is null) {
|
|
continue;
|
|
}
|
|
birthDate = person.Birth.Date.Value.ToString(birthDateFormat);
|
|
name = person.Name.ForwardSlashFull.Replace("/", string.Empty);
|
|
inserts.Add($"insert into \"{tableName}\" (\"{string.Join("\", \"", columns)}\") values ('{ownerId}', '{name}', '{birthDate}');");
|
|
}
|
|
string sqlFile = Path.Combine(root, $"{DateTime.Now.Ticks}.sql");
|
|
logger.LogInformation("<{file}> saved", sqlFile);
|
|
File.WriteAllLines(sqlFile, inserts);
|
|
}
|
|
|
|
}
|
|
|
|
[JsonSourceGenerationOptions(WriteIndented = true, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
|
|
[JsonSerializable(typeof(Dictionary<string, string>))]
|
|
internal partial class DictionaryStringStringSourceGenerationContext : JsonSerializerContext {
|
|
} |