@@ -59,70 +59,127 @@ export class ChargingLocationService {
59
59
chargingFacilityIds ?: number [ ] ,
60
60
plugIds ?: number [ ] ) {
61
61
62
- const evseWhere : any = { } ;
63
- const evseInclude : IIncludeOptions [ ] = [
64
- {
65
- model : db . model ( Status ) ,
66
- as : 'states' ,
67
- through : { attributes : [ ] } , // removes EVSEStatus property from status
68
- // required: true
69
- }
70
- ] ;
71
62
72
- if ( isOpen24Hours !== void 0 ) {
63
+ // return db.sequelize
64
+ // .query(`
65
+ // SELECT longitude, latitude
66
+ // FROM ChargingLocation
67
+ // WHERE
68
+ // longitude >= :longitude1 AND
69
+ // longitude <= :longitude2 AND
70
+ // latitude >= :latitude1 AND
71
+ // latitude <= :latitude2
72
+ // `, {
73
+ // replacements: {longitude1, latitude1, longitude2, latitude2}
74
+ // })
75
+ // .then(data => {
76
+ //
77
+ // if(data && data.length) {
78
+ // // return data[0];
79
+ // return this.geoService.getClusteredCoordinates(data[0], zoom);
80
+ // }
81
+ //
82
+ // return [];
83
+ // })
84
+ // ;
85
+
86
+ if ( zoom > 8 ) {
87
+ // return chargingLocations;
88
+
89
+
90
+ const evseWhere : any = { } ;
91
+ const evseInclude : IIncludeOptions [ ] = [
92
+ {
93
+ model : db . model ( Status ) ,
94
+ as : 'states' ,
95
+ through : { attributes : [ ] } , // removes EVSEStatus property from status
96
+ // required: true
97
+ }
98
+ ] ;
73
99
74
- evseWhere . isOpen24Hours = isOpen24Hours ;
75
- }
100
+ if ( isOpen24Hours !== void 0 ) {
76
101
77
- if ( chargingFacilityIds ) {
102
+ evseWhere . isOpen24Hours = isOpen24Hours ;
103
+ }
78
104
79
- evseInclude . push ( {
80
- model : db . model ( ChargingFacility ) ,
81
- as : 'chargingFacilities' ,
82
- through : { attributes : [ ] } , // removes EVSEChargingFacility property from status,
83
- where : { id : { $in : chargingFacilityIds } }
84
- } )
85
- }
105
+ if ( chargingFacilityIds ) {
86
106
87
- if ( plugIds ) {
88
- evseInclude . push ( {
89
- model : db . model ( Plug ) ,
90
- as : 'plugs' ,
91
- through : { attributes : [ ] } , // removes EVSEPlug property from status
92
- where : { id : { $in : plugIds } }
93
- } )
94
- }
107
+ evseInclude . push ( {
108
+ model : db . model ( ChargingFacility ) ,
109
+ as : 'chargingFacilities' ,
110
+ through : { attributes : [ ] } , // removes EVSEChargingFacility property from status,
111
+ where : { id : { $in : chargingFacilityIds } }
112
+ } )
113
+ }
95
114
96
- return db . model ( ChargingLocation )
97
- . findAll < ChargingLocation > ( {
98
- include : [
99
- {
100
- model : db . model ( EVSE ) ,
101
- attributes : [ 'id' ] ,
102
- as : 'evses' ,
103
- required : true ,
104
- include : evseInclude ,
105
- where : evseWhere
115
+ if ( plugIds ) {
116
+ evseInclude . push ( {
117
+ model : db . model ( Plug ) ,
118
+ as : 'plugs' ,
119
+ through : { attributes : [ ] } , // removes EVSEPlug property from status
120
+ where : { id : { $in : plugIds } }
121
+ } )
122
+ }
123
+
124
+
125
+ return db . model ( ChargingLocation )
126
+ . findAll < ChargingLocation > ( {
127
+ include : [
128
+ {
129
+ model : db . model ( EVSE ) ,
130
+ attributes : [ 'id' ] ,
131
+ as : 'evses' ,
132
+ required : true ,
133
+ include : evseInclude ,
134
+ where : evseWhere
135
+ }
136
+ ] ,
137
+ where : {
138
+ longitude : {
139
+ $gte : longitude1 ,
140
+ $lte : longitude2
141
+ } ,
142
+ latitude : {
143
+ $gte : latitude1 ,
144
+ $lte : latitude2
145
+ }
106
146
}
107
- ] ,
108
- where : {
109
- longitude : {
110
- $gte : longitude1 ,
111
- $lte : longitude2
112
- } ,
113
- latitude : {
114
- $gte : latitude1 ,
115
- $lte : latitude2
147
+ } )
148
+ . then ( chargingLocations => {
149
+
150
+ if ( zoom >= 12 ) {
151
+ return chargingLocations ;
116
152
}
117
- }
153
+
154
+ return this . geoService . getClusteredCoordinates ( chargingLocations , zoom ) ;
155
+ } )
156
+ ;
157
+ }
158
+
159
+ return db . sequelize
160
+ . query ( `
161
+ SELECT longitude, latitude
162
+ FROM (SELECT longitude, latitude
163
+ FROM (
164
+ SELECT ROUND(longitude, 2) AS longitude, ROUND(latitude, 2) AS latitude FROM ChargingLocation
165
+ ) AS CL GROUP BY longitude, latitude) AS CL
166
+ WHERE
167
+ longitude >= :longitude1 AND
168
+ longitude <= :longitude2 AND
169
+ latitude >= :latitude1 AND
170
+ latitude <= :latitude2
171
+ ;
172
+ ` , {
173
+ replacements : { longitude1, latitude1, longitude2, latitude2}
118
174
} )
119
- . then ( chargingLocations => {
175
+ . then ( data => {
120
176
121
- if ( zoom >= 12 ) {
122
- return chargingLocations ;
177
+ if ( data && data . length ) {
178
+ // return data[0];
179
+ return this . geoService . getClusteredCoordinates ( data [ 0 ] , zoom ) ;
123
180
}
124
181
125
- return this . geoService . getClusteredCoordinates ( chargingLocations , zoom ) ;
182
+ return [ ] ;
126
183
} )
127
184
;
128
185
}
0 commit comments