1 | using FarmatikoData.FarmatikoRepoInterfaces;
|
---|
2 | using FarmatikoData.Models;
|
---|
3 | using System.Collections.Generic;
|
---|
4 | using Newtonsoft.Json;
|
---|
5 | using Newtonsoft.Json.Linq;
|
---|
6 | using System;
|
---|
7 | using Microsoft.Extensions.Logging;
|
---|
8 | using System.Net;
|
---|
9 | using System.Linq;
|
---|
10 | using FarmatikoServices.FarmatikoServiceInterfaces;
|
---|
11 | using RestSharp;
|
---|
12 | using System.Threading.Tasks;
|
---|
13 | using OfficeOpenXml;
|
---|
14 | using System.IO;
|
---|
15 | using Microsoft.VisualBasic;
|
---|
16 |
|
---|
17 | namespace FarmatikoServices.Services
|
---|
18 | {
|
---|
19 | public class ProcessJSONService : IProcessJSONService
|
---|
20 | {
|
---|
21 |
|
---|
22 | private readonly ILogger _logger;
|
---|
23 | private readonly IService _service;
|
---|
24 | public ProcessJSONService(ILogger logger, IService service)
|
---|
25 | {
|
---|
26 | _logger = logger;
|
---|
27 | _service = service;
|
---|
28 | }
|
---|
29 | //Excel reader
|
---|
30 | private async Task<bool> ReadPharmaciesFromExcel(string Path)
|
---|
31 | {
|
---|
32 | using (var package = new ExcelPackage(new FileInfo(Path)))
|
---|
33 | {
|
---|
34 | var Sheet = package.Workbook.Worksheets[1];
|
---|
35 | for (int i = 2; i < Sheet.Dimension.End.Row; ++i)
|
---|
36 | {
|
---|
37 | Pharmacy pharmacy = new Pharmacy()
|
---|
38 | {
|
---|
39 | Name = Sheet.Cells[i, 2].Value.ToString(),
|
---|
40 | Address = Sheet.Cells[i, 3].Value.ToString(),
|
---|
41 | Location = Sheet.Cells[i, 4].Value.ToString(),
|
---|
42 | WorkAllTime = false
|
---|
43 | };
|
---|
44 | await _service.AddPharmacy(pharmacy);
|
---|
45 | return true;
|
---|
46 | }
|
---|
47 | }
|
---|
48 | return false;
|
---|
49 | }
|
---|
50 | public async void DownloadPharmaciesExcel()
|
---|
51 | {
|
---|
52 | try
|
---|
53 | {
|
---|
54 | string pathToSave1 = Directory.GetCurrentDirectory() + @"\ExcellDocs\1.xlsx";
|
---|
55 |
|
---|
56 | string pathToSave2 = Directory.GetCurrentDirectory() + @"\ExcellDocs\2.xlsx";
|
---|
57 | var client = new WebClient();
|
---|
58 | string url1 = "http://data.gov.mk/dataset/d84c31d9-e749-4b17-9faf-a5b4db3e7a70/resource/ce446f5c-e541-46f6-9e8c-67568059cbc6/download/registar-na-apteki-vnatre-vo-mreza-na-fzo-12.08.2020.xlsx";
|
---|
59 | 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";
|
---|
60 | Uri uri1 = new Uri(url1);
|
---|
61 | Uri uri2 = new Uri(url2);
|
---|
62 | client.DownloadFile(uri1, @pathToSave1);
|
---|
63 | client.DownloadFile(uri2, @pathToSave2);
|
---|
64 |
|
---|
65 |
|
---|
66 | bool Success = await ReadPharmaciesFromExcel(pathToSave1);
|
---|
67 | _logger.LogInformation(Success.ToString() + "1");
|
---|
68 | Success = await ReadPharmaciesFromExcel(pathToSave2);
|
---|
69 | _logger.LogInformation(Success.ToString() + "2");
|
---|
70 | }
|
---|
71 | catch (Exception e)
|
---|
72 | {
|
---|
73 | _logger.LogInformation(e.Message);
|
---|
74 | throw new Exception("Cannot process Medicines from Excel.");
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | //Healthfacilities
|
---|
79 | public async void GetProcessedHealthFacilitiesFromJSON()
|
---|
80 | {
|
---|
81 | try
|
---|
82 | {
|
---|
83 | var client = new WebClient();
|
---|
84 | var json = client.DownloadString(@"http://www.otvorenipodatoci.gov.mk/datastore/dump/505db453-4de2-4761-8a81-2800f7820b06?format=json");
|
---|
85 |
|
---|
86 | var jsonResponse = JObject.Parse(json);
|
---|
87 | var records = JArray.Parse(jsonResponse.GetValue("records").ToString());
|
---|
88 |
|
---|
89 | foreach (var rec in records)
|
---|
90 | {
|
---|
91 | dynamic obj = JsonConvert.DeserializeObject(rec.ToString());
|
---|
92 | var Name = obj[2];
|
---|
93 | var Municipality = obj[6];
|
---|
94 | var Address = obj[9];
|
---|
95 | var Email = obj[10];
|
---|
96 | var Phone = obj[11];
|
---|
97 | var Type = obj[5];
|
---|
98 | HealthFacility healthFacility = new HealthFacility();
|
---|
99 | //Name, Municipality, Address, Type, Email, Phone
|
---|
100 | healthFacility.Name = Convert.ToString(Name);
|
---|
101 | healthFacility.Municipality = Convert.ToString(Municipality);
|
---|
102 | healthFacility.Address = Convert.ToString(Address);
|
---|
103 | healthFacility.Type = Convert.ToString(Type);
|
---|
104 | healthFacility.Email = Convert.ToString(Email);
|
---|
105 | healthFacility.Phone = Convert.ToString(Phone);
|
---|
106 | await _service.AddFacility(healthFacility);
|
---|
107 |
|
---|
108 | }
|
---|
109 |
|
---|
110 | }
|
---|
111 | catch (Exception e)
|
---|
112 | {
|
---|
113 | _logger.LogInformation(e.Message);
|
---|
114 | throw new Exception("Cannot process health facilities from JSON." + e.Message);
|
---|
115 | }
|
---|
116 | }
|
---|
117 | //Pandemics
|
---|
118 | public async void GetProcessedPandemicsFromJSONApi()
|
---|
119 | {
|
---|
120 | try
|
---|
121 | {
|
---|
122 | var Date = DateTime.UtcNow.ToString("yyyy-MM-dd");
|
---|
123 | var client = new RestClient($"https://api.covid19tracking.narrativa.com/api/{Date}/country/north_macedonia");
|
---|
124 | var response = client.Execute(new RestRequest());
|
---|
125 | string original = response.Content;
|
---|
126 | var jsonResponsePandemic = JObject.Parse(original);
|
---|
127 | var global = JObject.Parse(jsonResponsePandemic.GetValue("total").ToString());
|
---|
128 | var TotalConfirmed = long.Parse(global.GetValue("today_confirmed").ToString());
|
---|
129 | var TotalDeaths = long.Parse(global.GetValue("today_deaths").ToString());
|
---|
130 | var TotalRecovered = long.Parse(global.GetValue("today_new_recovered").ToString());
|
---|
131 |
|
---|
132 | var mk = JObject.Parse(jsonResponsePandemic.GetValue("dates").ToString());
|
---|
133 |
|
---|
134 | var date = JObject.Parse(mk.GetValue(Date).ToString());
|
---|
135 | var country = JObject.Parse(date.GetValue("countries").ToString());
|
---|
136 | var mkd = JObject.Parse(country.GetValue("North Macedonia").ToString());
|
---|
137 | dynamic objP = mkd;
|
---|
138 | var TotalMk = Int32.Parse(objP.GetValue("today_confirmed").ToString());
|
---|
139 | var TotalDeathsMK = Int32.Parse(objP.GetValue("today_deaths").ToString());
|
---|
140 | var TotalRecoveredMK = Int32.Parse(objP.GetValue("today_recovered").ToString());
|
---|
141 | var NewMK = Int32.Parse(objP.GetValue("today_new_confirmed").ToString());
|
---|
142 |
|
---|
143 | var Name = "Coronavirus";
|
---|
144 | var ActiveMk = TotalMk - (TotalRecoveredMK + TotalDeathsMK);
|
---|
145 | var ActiveGlobal = TotalConfirmed - (TotalRecovered + TotalDeaths);
|
---|
146 |
|
---|
147 | Pandemic pandemic = new Pandemic(Name, TotalMk, ActiveMk, TotalDeathsMK, NewMK, TotalConfirmed, TotalDeaths, ActiveGlobal);
|
---|
148 | await _service.AddPandemic(pandemic);
|
---|
149 | }
|
---|
150 | catch (Exception e)
|
---|
151 | {
|
---|
152 | _logger.LogInformation(e.Message);
|
---|
153 | }
|
---|
154 | }
|
---|
155 | //Healthcare workers
|
---|
156 | public async void GetProcessedHealthcareWorkersFromJSON()
|
---|
157 | {
|
---|
158 | try
|
---|
159 | {
|
---|
160 | var client = new WebClient();
|
---|
161 | var jsonW = client.DownloadString(@"http://www.otvorenipodatoci.gov.mk/datastore/dump/5b661887-685b-4189-b6bb-9b52eb8ace16?format=json");
|
---|
162 |
|
---|
163 | var jsonResponseW = JObject.Parse(jsonW);
|
---|
164 | var recordsW = JArray.Parse(jsonResponseW.GetValue("records").ToString());
|
---|
165 |
|
---|
166 | foreach (var rec in recordsW)
|
---|
167 | {
|
---|
168 | dynamic obj = JsonConvert.DeserializeObject(rec.ToString());
|
---|
169 | var Name = Convert.ToString(obj[4]);
|
---|
170 | var Branch = Convert.ToString(obj[2]);
|
---|
171 | var FacilityName = Convert.ToString(obj[1]);
|
---|
172 | var Title = Convert.ToString(obj[3]);
|
---|
173 |
|
---|
174 | HealthFacility facility = _service.GetFacilityJSON(Convert.ToString(FacilityName));
|
---|
175 |
|
---|
176 | if (facility != null && facility != default)
|
---|
177 | {
|
---|
178 | HealthFacility Facility = new HealthFacility(
|
---|
179 | facility.Name,
|
---|
180 | facility.Municipality,
|
---|
181 | facility.Address,
|
---|
182 | facility.Type,
|
---|
183 | facility.Email,
|
---|
184 | facility.Phone
|
---|
185 | );
|
---|
186 | HealthcareWorker healthcareWorker = new HealthcareWorker(Name, Branch, Facility, Title);
|
---|
187 | await _service.AddWorker(healthcareWorker);
|
---|
188 | }
|
---|
189 | else
|
---|
190 | {
|
---|
191 | HealthFacility Facility = new HealthFacility(
|
---|
192 | Convert.ToString(FacilityName),
|
---|
193 | "",
|
---|
194 | "",
|
---|
195 | Convert.ToString(Branch),
|
---|
196 | "",
|
---|
197 | ""
|
---|
198 | );
|
---|
199 | HealthcareWorker healthcareWorker = new HealthcareWorker(Name, Branch, Facility, Title);
|
---|
200 | await _service.AddWorker(healthcareWorker);
|
---|
201 | }
|
---|
202 |
|
---|
203 |
|
---|
204 | }
|
---|
205 | }
|
---|
206 | catch (Exception e)
|
---|
207 | {
|
---|
208 | _logger.LogInformation(e.Message);
|
---|
209 | }
|
---|
210 | }
|
---|
211 | //Medicines
|
---|
212 | public async void GetProcessedMedicinesFromJSON()
|
---|
213 | {
|
---|
214 | try
|
---|
215 | {
|
---|
216 | var client = new WebClient();
|
---|
217 | var jsonM = client.DownloadString(@"http://www.otvorenipodatoci.gov.mk/datastore/dump/ecff2aef-9c8e-4efd-a557-96df4fff9adb?format=json");
|
---|
218 |
|
---|
219 | var jsonResponseM = JObject.Parse(jsonM);
|
---|
220 | var recordsM = JArray.Parse(jsonResponseM.GetValue("records").ToString());
|
---|
221 |
|
---|
222 | foreach (var rec in recordsM)
|
---|
223 | {
|
---|
224 | dynamic obj = JsonConvert.DeserializeObject(rec.ToString());
|
---|
225 | var Name = obj[1];
|
---|
226 | var Strength = obj[7];
|
---|
227 | var Form = obj[6];
|
---|
228 | var WayOfIssuing = obj[9];
|
---|
229 | var Manufacturer = obj[11];
|
---|
230 | var Price = float.Parse(Convert.ToString(obj[17]));
|
---|
231 | var Packaging = obj[8];
|
---|
232 | string price = Convert.ToString(Price);
|
---|
233 | Medicine medicine = new Medicine(Convert.ToString(Name), Convert.ToString(Strength), Convert.ToString(Form), Convert.ToString(WayOfIssuing), Convert.ToString(Manufacturer), Price, Convert.ToString(Packaging));
|
---|
234 |
|
---|
235 | await _service.AddMedicines(medicine);
|
---|
236 | }
|
---|
237 | }
|
---|
238 | catch (Exception e)
|
---|
239 | {
|
---|
240 | _logger.LogInformation(e.Message);
|
---|
241 | throw new Exception("medicine");
|
---|
242 | }
|
---|
243 | }
|
---|
244 | }
|
---|
245 | }
|
---|