[e0cdea2] | 1 | using FarmatikoData.Models;
|
---|
[a55ef91] | 2 | using Newtonsoft.Json;
|
---|
| 3 | using Newtonsoft.Json.Linq;
|
---|
| 4 | using System;
|
---|
| 5 | using Microsoft.Extensions.Logging;
|
---|
| 6 | using System.Net;
|
---|
| 7 | using System.Linq;
|
---|
| 8 | using FarmatikoServices.FarmatikoServiceInterfaces;
|
---|
| 9 | using RestSharp;
|
---|
[c406ae5] | 10 | using System.Threading.Tasks;
|
---|
[1454207] | 11 | using OfficeOpenXml;
|
---|
| 12 | using System.IO;
|
---|
[e0cdea2] | 13 | using GemBox.Spreadsheet;
|
---|
| 14 | using System.Text;
|
---|
[f554983] | 15 | using FarmatikoData.FarmatikoRepoInterfaces;
|
---|
[a55ef91] | 16 |
|
---|
| 17 | namespace FarmatikoServices.Services
|
---|
| 18 | {
|
---|
| 19 | public class ProcessJSONService : IProcessJSONService
|
---|
| 20 | {
|
---|
[1454207] | 21 |
|
---|
[c406ae5] | 22 | private readonly ILogger _logger;
|
---|
[f554983] | 23 | private readonly IUpdateDataRepo _repo;
|
---|
[1454207] | 24 | private readonly IService _service;
|
---|
[f554983] | 25 | public ProcessJSONService(ILogger logger, IUpdateDataRepo repo, IService service)
|
---|
[a55ef91] | 26 | {
|
---|
[c406ae5] | 27 | _logger = logger;
|
---|
[f554983] | 28 | _repo = repo;
|
---|
[1454207] | 29 | _service = service;
|
---|
| 30 | }
|
---|
[d23bf72] | 31 | //Excel reader
|
---|
[e0cdea2] | 32 | private bool ReadPharmaciesFromExcel(string Path)
|
---|
[1454207] | 33 | {
|
---|
[e0cdea2] | 34 | string path = Directory.GetCurrentDirectory() + @"\ExcellDocs\1.xlsx";
|
---|
| 35 |
|
---|
| 36 | FileInfo fileInfo = new FileInfo(path);
|
---|
| 37 | if (fileInfo != null)
|
---|
[1454207] | 38 | {
|
---|
[e0cdea2] | 39 | using (var package = new ExcelPackage(fileInfo))
|
---|
[1454207] | 40 | {
|
---|
[e0cdea2] | 41 | var Sheet = package.Workbook.Worksheets.First();
|
---|
| 42 | //var table = Sheet.Tables.First();
|
---|
| 43 | int rowCount = Sheet.Dimension.End.Row;
|
---|
| 44 | for (int i = 2; i < rowCount; ++i)
|
---|
[1454207] | 45 | {
|
---|
[e0cdea2] | 46 | //Console.WriteLine();
|
---|
| 47 | Pharmacy pharmacy = new Pharmacy()
|
---|
| 48 | {
|
---|
| 49 | Name = Sheet.Cells[i, 2].Value.ToString(),
|
---|
| 50 | Address = Sheet.Cells[i, 3].Value.ToString(),
|
---|
| 51 | Location = Sheet.Cells[i, 4].Value.ToString(),
|
---|
| 52 | WorkAllTime = false
|
---|
| 53 | };
|
---|
| 54 | _service.AddPharmacy(pharmacy);
|
---|
| 55 | }
|
---|
[d23bf72] | 56 | return true;
|
---|
[1454207] | 57 | }
|
---|
[e0cdea2] | 58 |
|
---|
[d23bf72] | 59 | }
|
---|
[e0cdea2] | 60 |
|
---|
[d23bf72] | 61 | return false;
|
---|
[e0cdea2] | 62 |
|
---|
[d23bf72] | 63 | }
|
---|
[e0cdea2] | 64 | public void DownloadPharmaciesExcel()
|
---|
[d23bf72] | 65 | {
|
---|
| 66 | try
|
---|
| 67 | {
|
---|
| 68 | string pathToSave1 = Directory.GetCurrentDirectory() + @"\ExcellDocs\1.xlsx";
|
---|
[6f203af] | 69 |
|
---|
[d23bf72] | 70 | string pathToSave2 = Directory.GetCurrentDirectory() + @"\ExcellDocs\2.xlsx";
|
---|
| 71 | var client = new WebClient();
|
---|
[e0cdea2] | 72 | string url1 = "http://data.gov.mk/dataset/d84c31d9-e749-4b17-9faf-a5b4db3e7a70/resource/4806b744-f6f6-42d0-b0f0-d82e66d3b177/download/-.xlsxs";
|
---|
| 73 | /*string url2 = "http://data.gov.mk/dataset/d84c31d9-e749-4b17-9faf-a5b4db3e7a70/resource/a16379b4-ec81-4de7-994d-0ee503d71b55/download/registar-na-apteki-nadvor-od-mreza-na-fzo-12.08.2020.xlsx";*/
|
---|
[d23bf72] | 74 | Uri uri1 = new Uri(url1);
|
---|
[e0cdea2] | 75 | //Uri uri2 = new Uri(url2);
|
---|
| 76 | client.DownloadFileAsync(uri1, pathToSave1);
|
---|
| 77 | //client.DownloadFile(uri2, @pathToSave2);
|
---|
[d23bf72] | 78 |
|
---|
| 79 |
|
---|
[e0cdea2] | 80 | bool Success = ReadPharmaciesFromExcel(pathToSave1);
|
---|
[d23bf72] | 81 | _logger.LogInformation(Success.ToString() + "1");
|
---|
[f554983] | 82 |
|
---|
| 83 |
|
---|
[6f203af] | 84 | }
|
---|
[d23bf72] | 85 | catch (Exception e)
|
---|
[6f203af] | 86 | {
|
---|
| 87 | _logger.LogInformation(e.Message);
|
---|
[e0cdea2] | 88 | throw new Exception("Cannot process Pharmacies from Excel.");
|
---|
[1454207] | 89 | }
|
---|
[a55ef91] | 90 | }
|
---|
[c406ae5] | 91 |
|
---|
[d23bf72] | 92 | //Healthfacilities
|
---|
[f554983] | 93 | public void GetProcessedHealthFacilitiesFromJSON()
|
---|
[a55ef91] | 94 | {
|
---|
| 95 | try
|
---|
| 96 | {
|
---|
[c406ae5] | 97 | var client = new WebClient();
|
---|
[d23bf72] | 98 | var json = client.DownloadString(@"http://www.otvorenipodatoci.gov.mk/datastore/dump/505db453-4de2-4761-8a81-2800f7820b06?format=json");
|
---|
[a55ef91] | 99 |
|
---|
| 100 | var jsonResponse = JObject.Parse(json);
|
---|
| 101 | var records = JArray.Parse(jsonResponse.GetValue("records").ToString());
|
---|
| 102 |
|
---|
[c406ae5] | 103 | foreach (var rec in records)
|
---|
[a55ef91] | 104 | {
|
---|
| 105 | dynamic obj = JsonConvert.DeserializeObject(rec.ToString());
|
---|
| 106 | var Name = obj[2];
|
---|
| 107 | var Municipality = obj[6];
|
---|
| 108 | var Address = obj[9];
|
---|
| 109 | var Email = obj[10];
|
---|
| 110 | var Phone = obj[11];
|
---|
| 111 | var Type = obj[5];
|
---|
[d23bf72] | 112 | HealthFacility healthFacility = new HealthFacility();
|
---|
| 113 | //Name, Municipality, Address, Type, Email, Phone
|
---|
| 114 | healthFacility.Name = Convert.ToString(Name);
|
---|
| 115 | healthFacility.Municipality = Convert.ToString(Municipality);
|
---|
| 116 | healthFacility.Address = Convert.ToString(Address);
|
---|
| 117 | healthFacility.Type = Convert.ToString(Type);
|
---|
| 118 | healthFacility.Email = Convert.ToString(Email);
|
---|
| 119 | healthFacility.Phone = Convert.ToString(Phone);
|
---|
[f554983] | 120 | _service.AddFacility(healthFacility);
|
---|
[a55ef91] | 121 |
|
---|
[c406ae5] | 122 | }
|
---|
[f554983] | 123 |
|
---|
[c406ae5] | 124 | }
|
---|
| 125 | catch (Exception e)
|
---|
| 126 | {
|
---|
| 127 | _logger.LogInformation(e.Message);
|
---|
[d23bf72] | 128 | throw new Exception("Cannot process health facilities from JSON." + e.Message);
|
---|
[c406ae5] | 129 | }
|
---|
| 130 | }
|
---|
[f554983] | 131 |
|
---|
[d23bf72] | 132 | //Healthcare workers
|
---|
[f554983] | 133 | public void GetProcessedHealthcareWorkersFromJSON()
|
---|
[c406ae5] | 134 | {
|
---|
| 135 | try
|
---|
| 136 | {
|
---|
| 137 | var client = new WebClient();
|
---|
[d23bf72] | 138 | var jsonW = client.DownloadString(@"http://www.otvorenipodatoci.gov.mk/datastore/dump/5b661887-685b-4189-b6bb-9b52eb8ace16?format=json");
|
---|
[c73269d] | 139 |
|
---|
| 140 | var jsonResponseW = JObject.Parse(jsonW);
|
---|
| 141 | var recordsW = JArray.Parse(jsonResponseW.GetValue("records").ToString());
|
---|
| 142 |
|
---|
| 143 | foreach (var rec in recordsW)
|
---|
| 144 | {
|
---|
| 145 | dynamic obj = JsonConvert.DeserializeObject(rec.ToString());
|
---|
[d23bf72] | 146 | var Name = Convert.ToString(obj[4]);
|
---|
| 147 | var Branch = Convert.ToString(obj[2]);
|
---|
| 148 | var FacilityName = Convert.ToString(obj[1]);
|
---|
| 149 | var Title = Convert.ToString(obj[3]);
|
---|
| 150 |
|
---|
[f554983] | 151 | HealthFacility facility = _repo.GetFacilityJSON(Convert.ToString(FacilityName));
|
---|
[d23bf72] | 152 |
|
---|
[db484c9] | 153 | if (facility != null && facility != default)
|
---|
[d23bf72] | 154 | {
|
---|
| 155 | HealthFacility Facility = new HealthFacility(
|
---|
| 156 | facility.Name,
|
---|
| 157 | facility.Municipality,
|
---|
| 158 | facility.Address,
|
---|
| 159 | facility.Type,
|
---|
| 160 | facility.Email,
|
---|
| 161 | facility.Phone
|
---|
| 162 | );
|
---|
| 163 | HealthcareWorker healthcareWorker = new HealthcareWorker(Name, Branch, Facility, Title);
|
---|
[f554983] | 164 | _service.AddWorker(healthcareWorker);
|
---|
[d23bf72] | 165 | }
|
---|
| 166 | else
|
---|
| 167 | {
|
---|
| 168 | HealthFacility Facility = new HealthFacility(
|
---|
| 169 | Convert.ToString(FacilityName),
|
---|
| 170 | "",
|
---|
| 171 | "",
|
---|
| 172 | Convert.ToString(Branch),
|
---|
| 173 | "",
|
---|
| 174 | ""
|
---|
| 175 | );
|
---|
| 176 | HealthcareWorker healthcareWorker = new HealthcareWorker(Name, Branch, Facility, Title);
|
---|
[f554983] | 177 | _service.AddWorker(healthcareWorker);
|
---|
[d23bf72] | 178 | }
|
---|
| 179 |
|
---|
| 180 |
|
---|
[c73269d] | 181 | }
|
---|
[f554983] | 182 |
|
---|
[c406ae5] | 183 | }
|
---|
| 184 | catch (Exception e)
|
---|
| 185 | {
|
---|
| 186 | _logger.LogInformation(e.Message);
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
[d23bf72] | 189 | //Medicines
|
---|
[f554983] | 190 | public void GetProcessedMedicinesFromJSON()
|
---|
[c406ae5] | 191 | {
|
---|
| 192 | try
|
---|
| 193 | {
|
---|
| 194 | var client = new WebClient();
|
---|
[d23bf72] | 195 | var jsonM = client.DownloadString(@"http://www.otvorenipodatoci.gov.mk/datastore/dump/ecff2aef-9c8e-4efd-a557-96df4fff9adb?format=json");
|
---|
[c73269d] | 196 |
|
---|
| 197 | var jsonResponseM = JObject.Parse(jsonM);
|
---|
| 198 | var recordsM = JArray.Parse(jsonResponseM.GetValue("records").ToString());
|
---|
| 199 |
|
---|
| 200 | foreach (var rec in recordsM)
|
---|
| 201 | {
|
---|
| 202 | dynamic obj = JsonConvert.DeserializeObject(rec.ToString());
|
---|
| 203 | var Name = obj[1];
|
---|
| 204 | var Strength = obj[7];
|
---|
| 205 | var Form = obj[6];
|
---|
| 206 | var WayOfIssuing = obj[9];
|
---|
| 207 | var Manufacturer = obj[11];
|
---|
[d23bf72] | 208 | var Price = float.Parse(Convert.ToString(obj[17]));
|
---|
[c73269d] | 209 | var Packaging = obj[8];
|
---|
[d23bf72] | 210 | string price = Convert.ToString(Price);
|
---|
| 211 | Medicine medicine = new Medicine(Convert.ToString(Name), Convert.ToString(Strength), Convert.ToString(Form), Convert.ToString(WayOfIssuing), Convert.ToString(Manufacturer), Price, Convert.ToString(Packaging));
|
---|
| 212 |
|
---|
[f554983] | 213 | _service.AddMedicines(medicine);
|
---|
[c406ae5] | 214 | }
|
---|
[a55ef91] | 215 | }
|
---|
| 216 | catch (Exception e)
|
---|
| 217 | {
|
---|
[c406ae5] | 218 | _logger.LogInformation(e.Message);
|
---|
[d23bf72] | 219 | throw new Exception("medicine");
|
---|
[a55ef91] | 220 | }
|
---|
| 221 | }
|
---|
| 222 | }
|
---|
| 223 | }
|
---|