237 lines
6.8 KiB
C#
237 lines
6.8 KiB
C#
<Query Kind="Program">
|
|
<Reference><ProgramFilesX64>\Microsoft SDKs\Azure\.NET SDK\v2.9\bin\plugins\Diagnostics\Newtonsoft.Json.dll</Reference>
|
|
<NuGetReference>AutoMapper</NuGetReference>
|
|
<Namespace>AutoMapper</Namespace>
|
|
<Namespace>Newtonsoft.Json</Namespace>
|
|
<Namespace>Newtonsoft.Json.Converters</Namespace>
|
|
<Namespace>Newtonsoft.Json.Linq</Namespace>
|
|
<Namespace>Newtonsoft.Json.Schema</Namespace>
|
|
<Namespace>Newtonsoft.Json.Serialization</Namespace>
|
|
<Namespace>System</Namespace>
|
|
<Namespace>System.Diagnostics</Namespace>
|
|
<Namespace>System.Diagnostics.Tracing</Namespace>
|
|
<Namespace>System.IO.Compression</Namespace>
|
|
<Namespace>AutoMapper.Configuration.Annotations</Namespace>
|
|
<Namespace>static UserQuery</Namespace>
|
|
</Query>
|
|
|
|
private IDResolver idResolver = new IDResolver();
|
|
|
|
protected MapperConfiguration config { get; } = new MapperConfiguration(cfg =>
|
|
{
|
|
cfg.AddMaps(Assembly.GetExecutingAssembly());
|
|
cfg.Advanced.AllowAdditiveTypeMapCreation = true;
|
|
cfg.ValidateInlineMaps = false;
|
|
});
|
|
|
|
void Main()
|
|
{
|
|
|
|
NameBrandExpiryComparer nameBrandExpiryEqC = new NameBrandExpiryComparer();
|
|
NameBrandComparer nameBrandEqC = new NameBrandComparer();
|
|
|
|
Mapper.Reset();
|
|
IMapper mapper = new Mapper(config);
|
|
mapper.ConfigurationProvider.CompileMappings();
|
|
mapper.ConfigurationProvider.AssertConfigurationIsValid();
|
|
|
|
var path = @"C:\Users\Tlamb\Downloads";
|
|
var json = "medications.json";
|
|
var data = LoadFile(Path.Combine(path, json));
|
|
|
|
var meds = mapper.Map<List<Medication>>(data.records);
|
|
var medBrandList = meds.Distinct(nameBrandEqC)
|
|
.OrderBy(m => m.Name).ThenBy(m => m.Brand)
|
|
.Select((m, i) => mapper.Map<MedBrand>(m, opt => opt.Items["id"] = i))
|
|
.ToList();
|
|
medBrandList.Dump("MedBrands");
|
|
|
|
var withLabels = medBrandList.Select(m => mapper.Map<MedBrandLabel>(m, opt => opt.Items["id"] = m.MedicineID)); //new MedBrandLabel(m));
|
|
withLabels.Dump("MedBrands with Labels");
|
|
|
|
var medList = meds.Distinct(nameBrandExpiryEqC)
|
|
.Join(medBrandList
|
|
, exp => new { exp.Name, exp.Brand }
|
|
, key => new { key.Name, key.Brand },
|
|
(exp, key) => new MedInventory
|
|
{
|
|
MedicineID = key.MedicineID,
|
|
Start = exp.Start,
|
|
Expiry = exp.Expiry,
|
|
})
|
|
.OrderBy(m => m.MedicineID).ThenBy(m => m.Expiry)
|
|
.ToList();
|
|
medList.Dump("Med List");
|
|
var medInv = medList.Select(ml => mapper.Map<MedInvLabel>(ml, opt => opt.Items["medBrands"] = withLabels.ToDictionary(key => key.MedicineID, label => label.Label)));
|
|
medInv.Dump("Med Inventory");
|
|
|
|
meds.OrderBy(m => m.Name).ThenBy(m => m.Brand).ThenBy(m => m.Expiry).Dump();
|
|
|
|
}
|
|
|
|
public RootObject LoadFile(string fileName)
|
|
{
|
|
var jsonFile = File.ReadAllText(fileName);
|
|
return JsonConvert.DeserializeObject<RootObject>(jsonFile);
|
|
}
|
|
|
|
// Define other methods and classes here
|
|
|
|
public class NameBrandExpiryComparer : IEqualityComparer<Medication>
|
|
{
|
|
public bool Equals(Medication med1, Medication med2)
|
|
{
|
|
if (med1 == null && med2 == null) return true;
|
|
else if (med1 == null || med2 == null) return false;
|
|
else if (med1.Name == med2.Name && med1.Brand == med2.Brand && med1.Expiry == med2.Expiry) return true;
|
|
else
|
|
return false;
|
|
}
|
|
|
|
public int GetHashCode(Medication med)
|
|
{
|
|
return $"{med.Name}{med.Brand}{med.Expiry:MMddyyyy}".GetHashCode();
|
|
}
|
|
}
|
|
|
|
public class NameBrandComparer : IEqualityComparer<Medication>
|
|
{
|
|
public bool Equals(Medication med1, Medication med2)
|
|
{
|
|
if (med1 == null && med2 == null) return true;
|
|
else if (med1 == null || med2 == null) return false;
|
|
else if (med1.Name == med2.Name && med1.Brand == med2.Brand) return true;
|
|
else
|
|
return false;
|
|
}
|
|
|
|
public int GetHashCode(Medication med)
|
|
{
|
|
return $"{med.Name}{med.Brand}".GetHashCode();
|
|
}
|
|
}
|
|
|
|
[AutoMap(typeof(Record))]
|
|
public class Medication
|
|
{
|
|
[SourceMember(nameof(Record.field_48_raw))]
|
|
public int MedicineID { get; set; }
|
|
[SourceMember(nameof(Record.field_49_raw))]
|
|
public string Name { get; set; }
|
|
[SourceMember(nameof(Record.field_50_raw))]
|
|
public string Brand { get; set; }
|
|
[SourceMember(nameof(Record.field_51_raw))]
|
|
public int Start { get; set; }
|
|
[SourceMember(nameof(Record.field_55_raw))]
|
|
public int Minimum { get; set; }
|
|
[SourceMember(nameof(Record.field_57_raw))]
|
|
public Field57Raw field57 { get; set; }
|
|
public DateTime Expiry => field57?.iso_timestamp ?? DateTime.MaxValue;
|
|
[Ignore]
|
|
public bool NeedsReorder => (Start < Minimum) || (Expiry <= DateTime.Now);
|
|
}
|
|
|
|
[AutoMap(typeof(Medication))]
|
|
public class MedBrand
|
|
{
|
|
[ValueResolver(typeof(IDResolver))]
|
|
public int MedicineID { get; set; }
|
|
public string Name { get; set; }
|
|
public string Brand { get; set; }
|
|
public int Minimum { get; set; }
|
|
[Ignore]
|
|
public string Dosage { get; set; }
|
|
[Ignore]
|
|
public int Package { get; set; }
|
|
[Ignore]
|
|
public string Per { get; set; }
|
|
[Ignore]
|
|
public string Unit { get; set; }
|
|
|
|
public MedBrand()
|
|
{
|
|
Minimum = 10;
|
|
Dosage = "50mg";
|
|
Unit = "Tablet";
|
|
Package = 250;
|
|
Per = "Bottle";
|
|
}
|
|
|
|
}
|
|
|
|
[AutoMap(typeof(MedBrand))]
|
|
public class MedBrandLabel : MedBrand
|
|
{
|
|
[Ignore]
|
|
public string Label => $"{Name} {Brand} {Dosage} {Package} {Per} / {Unit}";
|
|
}
|
|
|
|
public class MedInventory
|
|
{
|
|
public int MedicineID { get; set; }
|
|
public int Start { get; set; }
|
|
public DateTime Expiry { get; set; }
|
|
public bool NeedsReorder => Expiry <= DateTime.Now;
|
|
}
|
|
|
|
[AutoMap(typeof(MedInventory))]
|
|
public class MedInvLabel : MedInventory
|
|
{
|
|
[ValueResolver(typeof(BrandResolver))]
|
|
public string MedicationBrand { get; set; }
|
|
[Ignore]
|
|
public string Label => $"{MedicationBrand} {Expiry:MM/dd/yyyy}";
|
|
}
|
|
|
|
public class Field57Raw
|
|
{
|
|
public string date { get; set; }
|
|
public string date_formatted { get; set; }
|
|
public string hours { get; set; }
|
|
public string minutes { get; set; }
|
|
public string am_pm { get; set; }
|
|
public object unix_timestamp { get; set; }
|
|
public DateTime iso_timestamp { get; set; }
|
|
public string timestamp { get; set; }
|
|
public int time { get; set; }
|
|
}
|
|
|
|
public class Record
|
|
{
|
|
public string id { get; set; }
|
|
public int field_48_raw { get; set; }
|
|
public string field_49_raw { get; set; }
|
|
public string field_50_raw { get; set; }
|
|
public int field_51_raw { get; set; }
|
|
public int field_52_raw { get; set; }
|
|
public int field_53_raw { get; set; }
|
|
public int field_54_raw { get; set; }
|
|
public int field_55_raw { get; set; }
|
|
public bool field_56_raw { get; set; }
|
|
public Field57Raw field_57_raw { get; set; }
|
|
public string field_58_raw { get; set; }
|
|
public string field_63_raw { get; set; }
|
|
}
|
|
|
|
public class RootObject
|
|
{
|
|
public List<Record> records { get; set; }
|
|
}
|
|
|
|
public class IDResolver : IValueResolver<object, object, int>
|
|
{
|
|
public int Resolve(object source, object destination, int id, ResolutionContext context)
|
|
{
|
|
id = (int)context.Items["id"];
|
|
return id;
|
|
}
|
|
}
|
|
|
|
public class BrandResolver : IValueResolver<MedInventory, object, string>
|
|
{
|
|
public string Resolve(MedInventory source, object destination, string label, ResolutionContext context)
|
|
{
|
|
label = ((Dictionary<int, string>)context.Items["medBrands"])[source.MedicineID];
|
|
return label;
|
|
}
|
|
} |