File size: 9,059 Bytes
d4abe4b
 
 
c869f17
 
 
 
 
 
 
 
 
 
d4abe4b
 
c869f17
 
 
 
 
 
 
 
d4abe4b
c869f17
 
 
d4abe4b
c869f17
 
 
 
 
 
 
 
 
 
 
d4abe4b
 
c869f17
 
 
 
 
 
 
 
 
 
 
 
 
 
d4abe4b
c869f17
 
 
 
 
 
d4abe4b
c869f17
 
 
 
d4abe4b
 
 
c869f17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4abe4b
 
 
c869f17
 
d4abe4b
 
c869f17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4abe4b
c869f17
 
d4abe4b
c869f17
d4abe4b
c869f17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4abe4b
 
c869f17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4abe4b
c869f17
 
 
 
d4abe4b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import axios from 'axios';
import { logger } from '../utils/logger.js';
import type { NearestClinic, Location } from '../types/index.js';
import { 
  getSpecialtiesForCondition, 
  calculateSpecialtyMatchScore,
  type MedicalSpecialty 
} from '../utils/medical-specialty-mapper.js';

export interface HospitalCandidate extends NearestClinic {
  specialty_score?: number;
  specialties?: MedicalSpecialty[];
}

export class MapsService {
  /**
   * Find top N nearest hospitals/clinics using OpenStreetMap Overpass API
   * @param location User location coordinates
   * @param keyword Search keyword (default: 'phòng khám bệnh viện')
   * @param limit Maximum number of results (default: 3)
   * @returns Array of nearest clinics/hospitals, sorted by distance
   */
  async findNearestHospitals(
    location: Location,
    keyword: string = 'phòng khám bệnh viện',
    limit: number = 3
  ): Promise<HospitalCandidate[]> {
    try {
      logger.info('Searching for nearest clinic using OpenStreetMap...');

      // Determine search radius in meters (5km = 5000m)
      const radiusMeters = 5000;

      // Determine amenity type based on keyword
      let amenityType = 'hospital'; // Default for hospital
      if (keyword && keyword.toLowerCase().includes('phòng khám')) {
        amenityType = 'clinic';
      } else if (keyword && (keyword.toLowerCase().includes('bệnh viện') || keyword.toLowerCase().includes('hospital'))) {
        amenityType = 'hospital';
      }

      // Build Overpass QL query
      // Search for hospitals/clinics within radius
      const query = `
[out:json][timeout:25];
(
  node["amenity"="${amenityType}"](around:${radiusMeters},${location.lat},${location.lng});
  way["amenity"="${amenityType}"](around:${radiusMeters},${location.lat},${location.lng});
  relation["amenity"="${amenityType}"](around:${radiusMeters},${location.lat},${location.lng});
);
out center;
`;

      // Encode query for URL
      const encodedQuery = encodeURIComponent(query);

      // Use public Overpass API endpoint
      const overpassUrl = 'https://overpass-api.de/api/interpreter';
      
      const response = await axios.post(
        overpassUrl,
        `data=${encodedQuery}`,
        {
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          },
          timeout: 30000 // 30 seconds timeout
        }
      );

      if (!response.data || !response.data.elements || response.data.elements.length === 0) {
        logger.warn('No clinics found nearby in OpenStreetMap');
        return [];
      }

      // Process all elements and calculate distances
      const candidates: Array<{ element: any; distance: number }> = [];

      for (const element of response.data.elements) {
        let elementLat: number;
        let elementLng: number;

        // Handle different element types
        if (element.type === 'node') {
          elementLat = element.lat;
          elementLng = element.lon;
        } else if (element.type === 'way' || element.type === 'relation') {
          // For ways and relations, use center coordinates
          elementLat = element.center?.lat || element.lat;
          elementLng = element.center?.lon || element.lon;
        } else {
          continue;
        }

        // Calculate distance
        const distance = this.calculateDistance(
          location.lat,
          location.lng,
          elementLat,
          elementLng
        );

        candidates.push({ element, distance });
      }

      // Sort by distance and take top N
      candidates.sort((a, b) => a.distance - b.distance);
      const topCandidates = candidates.slice(0, limit);

      // Convert to HospitalCandidate format
      const hospitals: HospitalCandidate[] = topCandidates.map(({ element, distance }) => {
        // Extract information from tags
        const tags = element.tags || {};
        const name = tags.name || tags['name:vi'] || tags['name:en'] || 'Bệnh viện/Khám bệnh';
        
        // Build address from available tags
        const addressParts: string[] = [];
        if (tags['addr:street']) addressParts.push(tags['addr:street']);
        if (tags['addr:housenumber']) addressParts.push(tags['addr:housenumber']);
        if (tags['addr:city']) addressParts.push(tags['addr:city']);
        if (tags['addr:district']) addressParts.push(tags['addr:district']);
        const address = addressParts.length > 0 
          ? addressParts.join(', ') 
          : tags['addr:full'] || 'Địa chỉ không có sẵn';

        return {
          name,
          distance_km: Math.round(distance * 10) / 10,
          address,
          rating: undefined // OSM doesn't provide ratings
        };
      });

      logger.info(`Found ${hospitals.length} hospitals nearby`);
      return hospitals;
    } catch (error) {
      logger.error({ error }, 'OpenStreetMap Overpass API error');
      return [];
    }
  }

  /**
   * Find nearest hospital/clinic (backward compatibility)
   * @param location User location coordinates
   * @param keyword Search keyword (default: 'phòng khám bệnh viện')
   * @returns Nearest clinic/hospital information or null
   */
  async findNearestClinic(
    location: Location,
    keyword: string = 'phòng khám bệnh viện'
  ): Promise<NearestClinic | null> {
    const hospitals = await this.findNearestHospitals(location, keyword, 1);
    return hospitals.length > 0 ? hospitals[0] : null;
  }

  /**
   * Find best matching hospital based on condition and location
   * Returns the most appropriate hospital considering both specialty match and distance
   * @param location User location coordinates
   * @param condition Medical condition/disease name (optional)
   * @param keyword Search keyword (default: 'bệnh viện')
   * @returns Best matching hospital or null
   */
  async findBestMatchingHospital(
    location: Location,
    condition?: string,
    keyword: string = 'bệnh viện'
  ): Promise<HospitalCandidate | null> {
    try {
      logger.info(`Finding best matching hospital${condition ? ` for condition: ${condition}` : ''}...`);

      // Get top 3 hospitals
      const hospitals = await this.findNearestHospitals(location, keyword, 3);

      if (hospitals.length === 0) {
        logger.warn('No hospitals found nearby');
        return null;
      }

      // If no condition provided, return nearest
      if (!condition || condition.trim() === '') {
        logger.info('No condition provided, returning nearest hospital');
        return hospitals[0];
      }

      // Get specialties for condition
      const specialties = getSpecialtiesForCondition(condition);
      logger.info(`Condition "${condition}" maps to specialties: ${specialties.join(', ')}`);

      // Calculate match scores for each hospital
      const scoredHospitals = hospitals.map(hospital => {
        const specialtyScore = calculateSpecialtyMatchScore(hospital.name, specialties);
        
        // Combined score: specialty match (70%) + distance (30%)
        // Normalize distance score (closer = higher score, max 5km = 0, 0km = 1)
        const distanceScore = Math.max(0, 1 - (hospital.distance_km / 5));
        const combinedScore = (specialtyScore * 0.7) + (distanceScore * 0.3);

        return {
          ...hospital,
          specialty_score: specialtyScore,
          specialties,
          combined_score: combinedScore
        };
      });

      // Sort by combined score (highest first)
      scoredHospitals.sort((a, b) => {
        // First by specialty score (prefer specialty match)
        if (Math.abs(a.specialty_score! - b.specialty_score!) > 0.1) {
          return b.specialty_score! - a.specialty_score!;
        }
        // Then by distance (prefer closer)
        return a.distance_km - b.distance_km;
      });

      const bestMatch = scoredHospitals[0];
      logger.info(`Best match: ${bestMatch.name} (specialty score: ${bestMatch.specialty_score?.toFixed(2)}, distance: ${bestMatch.distance_km}km)`);
      
      return bestMatch;
    } catch (error) {
      logger.error({ error }, 'Error finding best matching hospital');
      // Fallback to nearest
      const hospitals = await this.findNearestHospitals(location, keyword, 1);
      return hospitals.length > 0 ? hospitals[0] : null;
    }
  }

  private calculateDistance(
    lat1: number,
    lon1: number,
    lat2: number,
    lon2: number
  ): number {
    // Haversine formula for calculating distance between two points
    const R = 6371; // Radius of the Earth in km
    const dLat = this.deg2rad(lat2 - lat1);
    const dLon = this.deg2rad(lon2 - lon1);
    
    const a =
      Math.sin(dLat / 2) * Math.sin(dLat / 2) +
      Math.cos(this.deg2rad(lat1)) *
        Math.cos(this.deg2rad(lat2)) *
        Math.sin(dLon / 2) *
        Math.sin(dLon / 2);
    
    const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    const distance = R * c;
    
    return distance;
  }

  private deg2rad(deg: number): number {
    return deg * (Math.PI / 180);
  }
}