Advertisement
Guest User

Untitled

a guest
May 21st, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using MedsProcessor.Common;
  8. using MedsProcessor.Common.Extensions;
  9. using MedsProcessor.Common.Models;
  10. using NPOI.HSSF.UserModel;
  11. using NPOI.SS.UserModel;
  12. using NPOI.XSSF.UserModel;
  13. using Microsoft.Extensions.Logging;
  14.  
  15. namespace MedsProcessor.Parser
  16. {
  17. public class HzzoExcelParser
  18. {
  19. private readonly ILogger<HzzoExcelParser> _logger;
  20. private int _medsCount;
  21. private int _parsedCount;
  22.  
  23. public HzzoExcelParser(ILogger<HzzoExcelParser> logger)
  24. {
  25. _logger = logger;
  26. }
  27.  
  28. public ISet<HzzoMedsDownloadDto> Run(ISet<HzzoMedsDownloadDto> meds)
  29. {
  30. _medsCount = meds.Count;
  31. _parsedCount = 0;
  32.  
  33. Parallel.ForEach(new Action<ISet<HzzoMedsDownloadDto>>[]
  34. {
  35. ParsePrimaryListsStartingWith2014_02,
  36. ParseSupplementaryListsStartingWith2014_02,
  37. ParsePrimaryListsUpTo2014_01,
  38. ParseSupplementaryListsUpTo2014_01
  39. }, parser => parser(meds));
  40.  
  41. _logger.LogInformation("Parser complete!");
  42. return meds;
  43. }
  44.  
  45. ISheet OpenWorkbookSheetWithNpoi(
  46. FileStream stream,
  47. HzzoMedsDownloadDto med,
  48. HzzoMedsDownloadDto latestMed)
  49. {
  50. ISheet drugListSheet = null;
  51.  
  52. try
  53. {
  54. if (med.FileName.ToLowerInvariant().EndsWith(".xls"))
  55. {
  56. var hssfWorkbook = new HSSFWorkbook(stream);
  57. drugListSheet = hssfWorkbook.GetSheetAt(0);
  58. }
  59. else
  60. {
  61. var xssfWorkbook = new XSSFWorkbook(stream);
  62. drugListSheet = xssfWorkbook.GetSheetAt(0);
  63. }
  64. }
  65. catch(Exception ex)
  66. {
  67. latestMed.Href += " - WORKSHEET COULD NOT BE PARSED";
  68. _logger.LogError(ex, latestMed.Href);
  69. }
  70.  
  71. return drugListSheet;
  72. }
  73.  
  74. void ParseHzzoExcelDocuments(IEnumerable<HzzoMedsDownloadDto> filteredMeds, DrugListType listType, bool isListStartingWith2014)
  75. {
  76. HzzoMedsDownloadDto latestMed = null;
  77. int latestRow = 0;
  78. int latestCol = 0;
  79.  
  80. try
  81. {
  82. foreach (var med in filteredMeds)
  83. {
  84. latestMed = med;
  85. using(var stream = File.Open(med.FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
  86. {
  87. var drugListSheet = OpenWorkbookSheetWithNpoi(stream, med, latestMed);
  88. if (drugListSheet == null) return;
  89.  
  90. var totalRows = drugListSheet.LastRowNum;
  91. int rowIndex = 1; // skips header row
  92. int colIndex = 0;
  93.  
  94. int incColNumber() =>
  95. latestCol = colIndex++;
  96.  
  97. string GetNextString() =>
  98. drugListSheet.GetRow(rowIndex).GetCell(incColNumber()).ToString();
  99.  
  100. (bool, string) TryGetNextString()
  101. {
  102. var row = drugListSheet.GetRow(rowIndex);
  103. var cIndex = incColNumber();
  104.  
  105. if (row == null || row.GetCell(cIndex) == null)
  106. return (false, null);
  107.  
  108. return (true, drugListSheet.GetRow(rowIndex).GetCell(cIndex).ToString());
  109. }
  110.  
  111. decimal? GetNextDecimal() =>
  112. decimal.TryParse(
  113. drugListSheet.GetRow(rowIndex).GetCell(incColNumber()).ToString(),
  114. out decimal dec)
  115. ? dec
  116. : new decimal?();
  117.  
  118. string GetEnumStrVal() =>
  119. drugListSheet.GetRow(rowIndex).GetCell(incColNumber())
  120. .ToString().Replace(@"\", string.Empty).Replace("/", string.Empty);
  121.  
  122. DrugApplicationType ParseNextDrugApplicationType()
  123. {
  124. var strVal = GetEnumStrVal();
  125.  
  126. return string.IsNullOrWhiteSpace(strVal)
  127. ? DrugApplicationType.Undefined
  128. : EnumExtensions.Parse<DrugApplicationTypeShort, DrugApplicationType>(strVal);
  129. }
  130.  
  131. DrugPrescriptionType ParseNextDrugPrescriptionType()
  132. {
  133. var strVal = GetEnumStrVal();
  134.  
  135. return string.IsNullOrWhiteSpace(strVal)
  136. ? DrugPrescriptionType.Unprescribed
  137. : EnumExtensions.Parse<DrugPrescriptionTypeShort, DrugPrescriptionType>(strVal);
  138. }
  139.  
  140. DrugApplicationTypeLimitation ParseNextDrugApplicationTypeLimitation()
  141. {
  142. var strVal = GetEnumStrVal();
  143.  
  144. return string.IsNullOrWhiteSpace(strVal)
  145. ? DrugApplicationTypeLimitation.Undefined
  146. : EnumExtensions.Parse<DrugApplicationTypeLimitationShort, DrugApplicationTypeLimitation>(strVal);
  147. }
  148.  
  149. for (; rowIndex <= totalRows; rowIndex++)
  150. {
  151. latestCol = colIndex = 0;
  152. latestRow = rowIndex;
  153.  
  154. var (hasRow, atkCode) = TryGetNextString();
  155.  
  156. if (!hasRow) continue;
  157.  
  158. var importDto = new HzzoMedsImportDto();
  159.  
  160. importDto.RowId = rowIndex;
  161. importDto.ListType = listType;
  162. importDto.ValidFrom = med.ValidFrom;
  163.  
  164. importDto.AtkCode = atkCode;
  165. importDto.ApplicationTypeLimitation = ParseNextDrugApplicationTypeLimitation();
  166. importDto.GenericName = GetNextString();
  167. importDto.UnitOfDistribution = GetNextString();
  168. importDto.UnitOfDistributionPriceWithoutPDV = GetNextDecimal();
  169. importDto.UnitOfDistributionPriceWithPDV = GetNextDecimal();
  170. importDto.ApplicationType = ParseNextDrugApplicationType();
  171. importDto.ApprovedBy = isListStartingWith2014 ? GetNextString() : null;
  172. importDto.Manufacturer = GetNextString();
  173. importDto.RegisteredName = GetNextString();
  174. importDto.OriginalPackagingDescription = GetNextString();
  175. importDto.OriginalPackagingSingleUnitPriceWithoutPdv = GetNextDecimal();
  176. importDto.OriginalPackagingSingleUnitPriceWithPdv = GetNextDecimal();
  177. importDto.OriginalPackagingPriceWithoutPdv = GetNextDecimal();
  178. importDto.OriginalPackagingPriceWithPdv = GetNextDecimal();
  179.  
  180. // NOTE: supplementary prices
  181. if (listType == DrugListType.Supplementary)
  182. {
  183. importDto.OriginalPackagingSingleUnitPricePaidByHzzoWithoutPdv = GetNextDecimal();
  184. importDto.OriginalPackagingSingleUnitPricePaidByHzzoWithPdv = GetNextDecimal();
  185. importDto.OriginalPackagingPricePaidByHzzoWithoutPdv = GetNextDecimal();
  186. importDto.OriginalPackagingPricePaidByHzzoWithPdv = GetNextDecimal();
  187. importDto.OriginalPackagingSingleUnitPriceExtraChargeWithoutPdv = GetNextDecimal();
  188. importDto.OriginalPackagingSingleUnitPriceExtraChargeWithPdv = GetNextDecimal();
  189. importDto.OriginalPackagingPriceExtraChargeWithoutPdv = GetNextDecimal();
  190. importDto.OriginalPackagingPriceExtraChargeWithPdv = GetNextDecimal();
  191. }
  192.  
  193. importDto.PrescriptionType = ParseNextDrugPrescriptionType();
  194. importDto.IndicationsCode = GetNextString();
  195. importDto.DirectionsCode = GetNextString();
  196. importDto.DrugGroupCode = GetNextString();
  197. importDto.DrugGroup = GetNextString();
  198. importDto.DrugSubgroupCode = GetNextString();
  199. importDto.DrugSubgroup = GetNextString();
  200.  
  201. med.MedsList.Add(importDto);
  202. }
  203. }
  204.  
  205. med.MarkAsParsed();
  206.  
  207. _logger.LogInformation(
  208. "Parsed document ({parsedCount}/{medsCount}): '{filename}'",
  209. ++_parsedCount,
  210. _medsCount,
  211. med.FileName);
  212. };
  213. }
  214. catch (Exception ex)
  215. {
  216. var str = new StringBuilder()
  217. .AppendLine(" latest med: ").Append(latestMed.FileName)
  218. .AppendLine(" latest row: ").Append(latestRow)
  219. .AppendLine(" latest col: ").Append(latestCol);
  220.  
  221. throw new InvalidOperationException(str.ToString(), ex);
  222. }
  223. }
  224.  
  225. static readonly DateTime filterDtStartWith2014 = new DateTime(2014, 1, 3);
  226.  
  227. void ParseSupplementaryListsUpTo2014_01(ISet<HzzoMedsDownloadDto> meds) =>
  228. ParseHzzoExcelDocuments(meds.Where(x =>
  229. x.ValidFrom <= filterDtStartWith2014 &&
  230. (
  231. x.FileName.ToLowerInvariant().Contains("dopunska") ||
  232. x.FileName.ToLowerInvariant().Contains("dll")
  233. )), DrugListType.Supplementary, false);
  234.  
  235. void ParsePrimaryListsUpTo2014_01(ISet<HzzoMedsDownloadDto> meds) =>
  236. ParseHzzoExcelDocuments(meds.Where(x =>
  237. x.ValidFrom <= filterDtStartWith2014 &&
  238. (
  239. x.FileName.ToLowerInvariant().Contains("osnovna") ||
  240. x.FileName.ToLowerInvariant().Contains("oll")
  241. )), DrugListType.Primary, false);
  242.  
  243. void ParseSupplementaryListsStartingWith2014_02(ISet<HzzoMedsDownloadDto> meds) =>
  244. ParseHzzoExcelDocuments(meds.Where(x =>
  245. x.ValidFrom > filterDtStartWith2014 &&
  246. (
  247. x.FileName.ToLowerInvariant().Contains("dopunska") ||
  248. x.FileName.ToLowerInvariant().Contains("dll")
  249. )), DrugListType.Supplementary, true);
  250.  
  251. void ParsePrimaryListsStartingWith2014_02(ISet<HzzoMedsDownloadDto> meds) =>
  252. ParseHzzoExcelDocuments(meds.Where(x =>
  253. x.ValidFrom > filterDtStartWith2014 &&
  254. (
  255. x.FileName.ToLowerInvariant().Contains("osnovna") ||
  256. x.FileName.ToLowerInvariant().Contains("oll")
  257. )), DrugListType.Primary, true);
  258. }
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement