Files
linqpad/Sole Hope/SoleHope Inventory refactoring.linq

275 lines
9.6 KiB
C#

<Query Kind="Program">
<Reference>&lt;ProgramFilesX64&gt;\Microsoft SDKs\Azure\.NET SDK\v2.9\bin\plugins\Diagnostics\Newtonsoft.Json.dll</Reference>
<Reference>&lt;RuntimeDirectory&gt;\System.Data.Entity.Design.dll</Reference>
<NuGetReference>Amazon.Lambda.S3Events</NuGetReference>
<NuGetReference>AWSSDK.Core</NuGetReference>
<NuGetReference>AWSSDK.S3</NuGetReference>
<NuGetReference>LINQtoCSV</NuGetReference>
<Namespace>Amazon</Namespace>
<Namespace>Amazon.Auth.AccessControlPolicy</Namespace>
<Namespace>Amazon.Auth.AccessControlPolicy.ActionIdentifiers</Namespace>
<Namespace>Amazon.Internal</Namespace>
<Namespace>Amazon.Lambda.S3Events</Namespace>
<Namespace>Amazon.MissingTypes</Namespace>
<Namespace>Amazon.Runtime</Namespace>
<Namespace>Amazon.Runtime.CredentialManagement</Namespace>
<Namespace>Amazon.Runtime.CredentialManagement.Internal</Namespace>
<Namespace>Amazon.Runtime.EventStreams</Namespace>
<Namespace>Amazon.Runtime.EventStreams.Internal</Namespace>
<Namespace>Amazon.Runtime.Internal</Namespace>
<Namespace>Amazon.Runtime.Internal.Auth</Namespace>
<Namespace>Amazon.Runtime.Internal.Settings</Namespace>
<Namespace>Amazon.Runtime.Internal.Transform</Namespace>
<Namespace>Amazon.Runtime.Internal.Util</Namespace>
<Namespace>Amazon.Runtime.SharedInterfaces</Namespace>
<Namespace>Amazon.Runtime.SharedInterfaces.Internal</Namespace>
<Namespace>Amazon.S3</Namespace>
<Namespace>Amazon.S3.Encryption</Namespace>
<Namespace>Amazon.S3.Encryption.Internal</Namespace>
<Namespace>Amazon.S3.Internal</Namespace>
<Namespace>Amazon.S3.IO</Namespace>
<Namespace>Amazon.S3.Model</Namespace>
<Namespace>Amazon.S3.Model.Internal.MarshallTransformations</Namespace>
<Namespace>Amazon.S3.Transfer</Namespace>
<Namespace>Amazon.S3.Util</Namespace>
<Namespace>Amazon.Util</Namespace>
<Namespace>Amazon.Util.Internal</Namespace>
<Namespace>Amazon.Util.Internal.PlatformServices</Namespace>
<Namespace>LINQPad.Extensibility.DataContext.DbSchema</Namespace>
<Namespace>LINQtoCSV</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.Globalization</Namespace>
<Namespace>System.IO.Compression</Namespace>
<Namespace>ThirdParty.BouncyCastle.Asn1</Namespace>
<Namespace>ThirdParty.BouncyCastle.Asn1.Utilities</Namespace>
<Namespace>ThirdParty.BouncyCastle.Math</Namespace>
<Namespace>ThirdParty.BouncyCastle.OpenSsl</Namespace>
<Namespace>ThirdParty.BouncyCastle.Utilities.IO.Pem</Namespace>
<Namespace>ThirdParty.Ionic.Zlib</Namespace>
<Namespace>ThirdParty.Json.LitJson</Namespace>
<Namespace>ThirdParty.MD5</Namespace>
<Namespace>System.Data.Entity.Design.PluralizationServices</Namespace>
</Query>
private static PluralizationService ps => PluralizationService.CreateService(new CultureInfo("en-US"));
void Main()
{
var path = @"C:\Users\tlamb\downloads";
var newPath = @"C:\Users\tlamb\downloads\Solehope 190705";
var file = "medical inventory July 5 2019.2.csv";
var filepath = Path.Combine(path, file);
CsvFileDescription inputFileDescription = new CsvFileDescription
{
SeparatorChar = ',',
FirstLineHasColumnNames = true,
IgnoreUnknownColumns = true
};
CsvFileDescription outputFileDescription = new CsvFileDescription
{
SeparatorChar = ',',
FirstLineHasColumnNames = true
};
CsvContext cc = new CsvContext();
IEnumerable<MedicationOut> medications;
IEnumerable<Inventory> inventory;
IEnumerable<Intake> intake;
var meds = cc.Read<Med>(filepath, inputFileDescription).ToList();
meds.Dump(file);
medications = meds.ConvertAll(med => (MedicationOut)med);
inventory = meds.ConvertAll(med => (Inventory)med);
inventory = inventory.OrderBy(i => i.Medication)
.Select((i, idx) => { i.InventoryId = idx.ToString(); return i; });
intake = meds.ConvertAll(med => (Intake)med);
intake = intake
.Join(inventory, x => x.MedicationId, inv => inv.MedicationId, (x, inv) => new { intake = x, inv }).ToList()
.ConvertAll(u => { u.intake.InventoryId = u.inv.InventoryId; return u.intake; });
var updates = cc.Read<Intake>(Path.Combine(@"C:\Users\tlamb\downloads\Solehope 190819", "intake2.csv"), inputFileDescription).ToList()
.Join(inventory, x => x.InventoryId, inv => inv.InventoryId, (x, inv) => new { intake = x, inv }).ToList()
.ConvertAll(u => { u.intake.MedicationId = u.inv.MedicationId; return u.intake; });
newPath.Dump("New Path");
medications.Dump("Medications");
inventory.Dump("Inventory");
intake.Dump("Intake - from Medications");
updates.Dump("Updates");
file = "medication.csv";
filepath = Path.Combine(newPath, file);
cc.Write(medications, filepath, outputFileDescription);
file = "inventory.csv";
filepath = Path.Combine(newPath, file);
cc.Write(inventory, filepath, outputFileDescription);
file = "intake.csv";
filepath = Path.Combine(newPath, file);
cc.Write(intake, filepath, outputFileDescription);
file = "updates.csv";
filepath = Path.Combine(newPath, file);
cc.Write(updates, filepath, outputFileDescription);
}
// Define other methods and classes here
public class Med
{
public string Id { get; set; }
public string Type { get; set; }
[CsvColumn(Name = "Medication")]
public string MedicationStr { get; set; }
internal string Medications => box.Match(MedicationStr.Trim()).Success ? box.Replace(MedicationStr.Trim(), "$1").Trim() : MedicationStr.Trim();
public string Strength { get; set; }
[CsvColumn(Name = "Quantity")]
public string Start { get; set; }
internal int StartCnt => Convert.ToInt32(regex.Replace(string.IsNullOrEmpty(Start) ? "0" : Start, ""));
[CsvColumn(Name = "Minimum")]
public string MinimumStr { get; set; }
internal int MinimumCnt => Convert.ToInt32(regex.Replace(string.IsNullOrEmpty(MinimumStr) ? "0" : MinimumStr, ""));
public string Unit { get; set; }
public string Package { get; set; }
[CsvColumn(Name = "Number")]
public string NumberStr { get; set; }
internal int NumberCnt => box.Match(MedicationStr.Trim()).Success ? Convert.ToInt32(box.Replace(MedicationStr.Trim(), "$2"))
: Convert.ToInt32(regex.Replace(string.IsNullOrEmpty(NumberStr) ? "0" : NumberStr, ""));
[CsvColumn(Name = "Expiration Date")]
public string Expiry { get; set; }
internal DateTime ExpiryDate => GetDateFromString(Expiry);
internal string Form => string.IsNullOrEmpty(Package) ? ""
: Package.Trim() == "Tube/Cream" ? "Cream" : ps.Singularize(Package.Trim());
internal string UnitOut => !string.IsNullOrEmpty(UnitNoBox)
? ps.Singularize(UnitNoBox)
: string.IsNullOrEmpty(Package)
? string.Empty
: Package.Trim() == "Tube/Cream"
? "Tube"
: string.Empty;
public string Label => $"{Medications} {Strength} {Form} {NumberCnt} / {UnitOut}";
internal string UnitNoBox => box.Match(MedicationStr.Trim()).Success ? "Box" : Unit?.Trim() ?? "";
internal Regex regex = new Regex(@"[^\d]");
internal Regex date = new Regex(@"([\d]{1,2})/([\d]{1,2})/([\d]{1,4})");
internal Regex shortDate = new Regex(@"([\d]{1,2})/([\d]{1,2})");
internal Regex box = new Regex(@"^(\D*)([\d]*)/Box$");
internal DateTime GetDateFromString(string theDate)
{
DateTime result = DateTime.MaxValue;
DateTime work;
if (string.IsNullOrEmpty(theDate) || theDate == "0/0")
work = result;
else if (date.IsMatch(theDate))
work = Convert.ToDateTime(theDate);
else if (shortDate.IsMatch(theDate))
work = Convert.ToDateTime(shortDate.Replace(theDate, "$1/5/$2"));
else
work = result;
work = new DateTime(work.Year, work.Month, DateTime.DaysInMonth(work.Year, work.Month));
return work;
}
}
public class MedicationOut
{
public string Id { get; set; }
public string Type { get; set; }
public string Medication { get; set; }
public int Minimum { get; set; }
public string Strength { get; set; }
public string Form { get; set; }
public int Number { get; set; }
public string Unit { get; set; }
public string Label { get; internal set; }
public static explicit operator MedicationOut(Med med)
{
return new MedicationOut
{
Id = med.Id,
Type = med.Type,
Medication = med.Medications,
Minimum = med.MinimumCnt,
Strength = med.Strength ?? "",
Form = med.Form,
Number = med.NumberCnt,
Unit = !string.IsNullOrEmpty(med.UnitNoBox)
? ps.Singularize(med.UnitNoBox)
: string.IsNullOrEmpty(med.Package)
? string.Empty
: med.Package.Trim() == "Tube/Cream"
? "Tube"
: string.Empty,
Label = med.Label
};
}
}
public class Inventory
{
public string MedicationId { get; set; }
public string InventoryId { get; set; }
public string Medication { get; set; }
public int Start => 0;
public string Label { get; internal set; }
public string Note { get; set; }
public static explicit operator Inventory(Med med)
{
return new Inventory
{
MedicationId = med.Id,
Medication = med.Medications,
Label = med.Label,
Note = string.Empty
};
}
}
public class Intake
{
public string MedicationId { get; set; }
public string InventoryId { get; set; }
public string Medication { get; set; }
public int Received { get; set; }
public DateTime Date { get; set; }
public DateTime Expiry { get; set; }
internal string MedLabel { get; set; }
public string Label => $"({Received}) {MedLabel}";
public static explicit operator Intake(Med med)
{
return new Intake
{
MedicationId = med.Id,
Medication = med.Medications,
Received = med.StartCnt,
Date = new DateTime(2019, 7, 5),
Expiry = med.ExpiryDate,
MedLabel = med.Label
};
}
}