@@ -60,8 +60,13 @@ export class ChargingLocationService {
60
60
chargingFacilityIds ?: number [ ] ,
61
61
plugIds ?: number [ ] ) : Promise < ChargingLocation [ ] | LocationCluster [ ] > {
62
62
63
-
63
+ const epsilon = this . geoService . getEpsilonByZoom ( zoom ) ;
64
64
let evseWhere : any ;
65
+ let evseChargingFacilityJoin : string = '' ;
66
+ let evsePlugJoin : string = '' ;
67
+ let evseWhereStr : string = '' ;
68
+ const replacements : any = { longitude1, longitude2, latitude1, latitude2, epsilon} ;
69
+
65
70
const evseInclude : IIncludeOptions [ ] = [
66
71
{
67
72
model : db . model ( Status ) ,
@@ -71,10 +76,14 @@ export class ChargingLocationService {
71
76
// required: true
72
77
}
73
78
] ;
79
+
74
80
if ( isOpen24Hours !== void 0 ) {
75
81
evseWhere = { } ;
76
82
evseWhere . isOpen24Hours = isOpen24Hours ;
83
+ evseWhereStr += `AND e.isOpen24Hours = :isOpen24Hours` ;
84
+ replacements . isOpen24Hours = isOpen24Hours ;
77
85
}
86
+
78
87
const chargingLocationInclude = [ {
79
88
model : db . model ( EVSE ) ,
80
89
attributes : [ 'id' ] ,
@@ -83,6 +92,7 @@ export class ChargingLocationService {
83
92
include : evseInclude ,
84
93
where : evseWhere
85
94
} ] ;
95
+
86
96
const where = < any > {
87
97
longitude : {
88
98
$gte : longitude1 ,
@@ -101,7 +111,12 @@ export class ChargingLocationService {
101
111
as : 'chargingFacilities' ,
102
112
through : { attributes : [ ] } , // removes EVSEChargingFacility property from status,
103
113
where : { id : { $in : chargingFacilityIds } }
104
- } )
114
+ } ) ;
115
+ evseChargingFacilityJoin = `
116
+ INNER JOIN EVSEChargingFacility ecf ON ecf.evseId = e.id
117
+ AND ecf.chargingFacilityId IN (:chargingFacilityIds)
118
+ ` ;
119
+ replacements . chargingFacilityIds = chargingFacilityIds ;
105
120
}
106
121
107
122
if ( plugIds ) {
@@ -110,7 +125,12 @@ export class ChargingLocationService {
110
125
as : 'plugs' ,
111
126
through : { attributes : [ ] } , // removes EVSEPlug property from status
112
127
where : { id : { $in : plugIds } }
113
- } )
128
+ } ) ;
129
+ evsePlugJoin = `
130
+ INNER JOIN EVSEPlug ep ON ep.evseId = e.id
131
+ AND ep.plugId IN (:plugIds)
132
+ ` ;
133
+ replacements . plugIds = plugIds ;
114
134
}
115
135
116
136
if ( zoom >= 12 ) {
@@ -123,22 +143,50 @@ export class ChargingLocationService {
123
143
;
124
144
}
125
145
126
- where . epsilon = this . geoService . getEpsilonByZoom ( zoom ) ;
146
+ if ( zoom > 9 ) {
147
+
148
+ where . epsilon = epsilon ;
149
+
150
+ return db . model ( LocationCluster )
151
+ . findAll < LocationCluster > ( {
152
+ attributes : [ 'latitude' , 'longitude' ] ,
153
+ include : [
154
+ {
155
+ model : db . model ( ChargingLocation ) ,
156
+ attributes : [ 'id' ] ,
157
+ as : 'chargingLocations' ,
158
+ include : chargingLocationInclude
159
+ }
160
+ ] ,
161
+ where
162
+ } )
163
+ ;
164
+ }
127
165
128
- return db . model ( LocationCluster )
129
- . findAll < LocationCluster > ( {
130
- attributes : [ 'latitude' , 'longitude' ] ,
131
- include : [
132
- {
133
- model : db . model ( ChargingLocation ) ,
134
- attributes : [ 'id' ] ,
135
- as : 'chargingLocations' ,
136
- include : chargingLocationInclude
137
- }
138
- ] ,
139
- where
140
- } )
141
- ;
166
+ return db . sequelize
167
+ . query ( `
168
+ SELECT
169
+ lc.id,
170
+ lc.latitude,
171
+ lc.longitude,
172
+ COUNT(cl.id) AS groupCount
173
+ FROM LocationCluster lc
174
+ INNER JOIN LocationClusterChargingLocation lccl ON lccl.locationClusterId = lc.id
175
+ INNER JOIN ChargingLocation cl ON cl.id = lccl.chargingLocationId
176
+ INNER JOIN EVSE e ON e.chargingLocationId = cl.id ${ evseWhereStr }
177
+ ${ evseChargingFacilityJoin }
178
+ ${ evsePlugJoin }
179
+ WHERE
180
+ lc.longitude >= :longitude1
181
+ AND lc.longitude <= :longitude2
182
+ AND lc.latitude >= :latitude1
183
+ AND lc.latitude <= :latitude2
184
+ AND lc.epsilon = :epsilon
185
+ GROUP BY lc.id
186
+ HAVING groupCount > 1
187
+ ` , { replacements} )
188
+ . then ( res => res && res . length ? res [ 0 ] : [ ] )
189
+ ;
142
190
}
143
191
144
192
}
0 commit comments