Skip to content

Instantly share code, notes, and snippets.

@vmandic
Created October 25, 2018 22:27
Show Gist options
  • Save vmandic/3739bbdd7736133c25fe0d2ecf54033f to your computer and use it in GitHub Desktop.
Save vmandic/3739bbdd7736133c25fe0d2ecf54033f to your computer and use it in GitHub Desktop.
meds-processor, p3, s7
void ParseHzzoExcelDocuments(IEnumerable<HzzoMedsDownloadDto> filteredMeds, DrugListType listType, bool isListStartingWith2014)
{
HzzoMedsDownloadDto latestMed = null;
int latestRow = 0;
int latestCol = 0;
try
{
Parallel.ForEach(filteredMeds, med =>
{
latestMed = med;
using(var stream = File.Open(med.FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
var drugListSheet = OpenWorkbookSheetWithNpoi(stream, med, latestMed);
if (drugListSheet == null) return;
var totalRows = drugListSheet.LastRowNum;
int rowIndex = 1; // skips header row
int colIndex = 0;
int incColNumber() =>
latestCol = colIndex++;
string GetNextString() =>
drugListSheet.GetRow(rowIndex).GetCell(incColNumber()).ToString();
(bool, string) TryGetNextString()
{
var row = drugListSheet.GetRow(rowIndex);
var cIndex = incColNumber();
if (row == null || row.GetCell(cIndex) == null)
return (false, null);
return (true, drugListSheet.GetRow(rowIndex).GetCell(cIndex).ToString());
}
decimal? GetNextDecimal() =>
decimal.TryParse(
drugListSheet.GetRow(rowIndex).GetCell(incColNumber()).ToString(),
out decimal dec)
? dec
: new decimal?();
string GetEnumStrVal() =>
drugListSheet.GetRow(rowIndex).GetCell(incColNumber())
.ToString().Replace(@"\", string.Empty).Replace("/", string.Empty);
DrugApplicationType ParseNextDrugApplicationType()
{
var strVal = GetEnumStrVal();
return string.IsNullOrWhiteSpace(strVal)
? DrugApplicationType.Undefined
: EnumExtensions.Parse<DrugApplicationTypeShort, DrugApplicationType>(strVal);
}
DrugPrescriptionType ParseNextDrugPrescriptionType()
{
var strVal = GetEnumStrVal();
return string.IsNullOrWhiteSpace(strVal)
? DrugPrescriptionType.Unprescribed
: EnumExtensions.Parse<DrugPrescriptionTypeShort, DrugPrescriptionType>(strVal);
}
DrugApplicationTypeLimitation ParseNextDrugApplicationTypeLimitation()
{
var strVal = GetEnumStrVal();
return string.IsNullOrWhiteSpace(strVal)
? DrugApplicationTypeLimitation.Undefined
: EnumExtensions.Parse<DrugApplicationTypeLimitationShort, DrugApplicationTypeLimitation>(strVal);
}
for (; rowIndex <= totalRows; rowIndex++)
{
latestCol = colIndex = 0;
latestRow = rowIndex;
var (hasRow, atkCode) = TryGetNextString();
if (!hasRow) continue;
var importDto = new HzzoMedsImportDto();
importDto.RowId = rowIndex;
importDto.ListType = listType;
importDto.ValidFrom = med.ValidFrom;
importDto.AtkCode = atkCode;
importDto.ApplicationTypeLimitation = ParseNextDrugApplicationTypeLimitation();
importDto.GenericName = GetNextString();
importDto.UnitOfDistribution = GetNextString();
importDto.UnitOfDistributionPriceWithoutPDV = GetNextDecimal();
importDto.UnitOfDistributionPriceWithPDV = GetNextDecimal();
importDto.ApplicationType = ParseNextDrugApplicationType();
importDto.ApprovedBy = isListStartingWith2014 ? GetNextString() : null;
importDto.Manufacturer = GetNextString();
importDto.RegisteredName = GetNextString();
importDto.OriginalPackagingDescription = GetNextString();
importDto.OriginalPackagingSingleUnitPriceWithoutPdv = GetNextDecimal();
importDto.OriginalPackagingSingleUnitPriceWithPdv = GetNextDecimal();
importDto.OriginalPackagingPriceWithoutPdv = GetNextDecimal();
importDto.OriginalPackagingPriceWithPdv = GetNextDecimal();
// NOTE: supplementary prices
if (listType == DrugListType.Supplementary)
{
importDto.OriginalPackagingSingleUnitPricePaidByHzzoWithoutPdv = GetNextDecimal();
importDto.OriginalPackagingSingleUnitPricePaidByHzzoWithPdv = GetNextDecimal();
importDto.OriginalPackagingPricePaidByHzzoWithoutPdv = GetNextDecimal();
importDto.OriginalPackagingPricePaidByHzzoWithPdv = GetNextDecimal();
importDto.OriginalPackagingSingleUnitPriceExtraChargeWithoutPdv = GetNextDecimal();
importDto.OriginalPackagingSingleUnitPriceExtraChargeWithPdv = GetNextDecimal();
importDto.OriginalPackagingPriceExtraChargeWithoutPdv = GetNextDecimal();
importDto.OriginalPackagingPriceExtraChargeWithPdv = GetNextDecimal();
}
importDto.PrescriptionType = ParseNextDrugPrescriptionType();
importDto.IndicationsCode = GetNextString();
importDto.DirectionsCode = GetNextString();
importDto.DrugGroupCode = GetNextString();
importDto.DrugGroup = GetNextString();
importDto.DrugSubgroupCode = GetNextString();
importDto.DrugSubgroup = GetNextString();
med.MedsList.Add(importDto);
}
}
});
}
catch (Exception ex)
{
var str = new StringBuilder()
.AppendLine("latest med: ").Append(latestMed.FileName)
.AppendLine("latest row: ").Append(latestRow)
.AppendLine("latest col: ").Append(latestCol);
throw new InvalidOperationException(str.ToString(), ex);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment