GetStoreListByLocation
This commit is contained in:
@@ -61,6 +61,30 @@ type StoreExt struct {
|
||||
OrderCount int `json:"orderCount"`
|
||||
}
|
||||
|
||||
type Store4User struct {
|
||||
model.Store
|
||||
FloatLng float64 `json:"lng"`
|
||||
FloatLat float64 `json:"lat"`
|
||||
CityName string `json:"cityName"`
|
||||
DistrictName string `json:"districtName"`
|
||||
|
||||
Distance int `json:"distance"`
|
||||
}
|
||||
|
||||
type Store4UserList []*Store4User
|
||||
|
||||
func (x Store4UserList) Len() int {
|
||||
return len(x)
|
||||
}
|
||||
|
||||
func (x Store4UserList) Less(i, j int) bool {
|
||||
return x[i].Distance < x[j].Distance
|
||||
}
|
||||
|
||||
func (x Store4UserList) Swap(i, j int) {
|
||||
x[i], x[j] = x[j], x[i]
|
||||
}
|
||||
|
||||
type StoresInfo struct {
|
||||
TotalCount int `json:"totalCount"`
|
||||
MapCenterLng float64 `json:"mapCenterLng"`
|
||||
@@ -2003,3 +2027,61 @@ func SyncStoresQualify(ctx *jxcontext.Context, storeIDs []int, isAsync, isContin
|
||||
}
|
||||
return hint, err
|
||||
}
|
||||
|
||||
func GetStoreListByLocation(ctx *jxcontext.Context, lng, lat float64) (storeList []*Store4User, err error) {
|
||||
const (
|
||||
maxRadius = 5000
|
||||
maxStoreCount4User = 5
|
||||
)
|
||||
|
||||
lng2, _ := jxutils.ConvertDistanceToLogLat(lng, lat, float64(maxRadius), 90)
|
||||
_, lat2 := jxutils.ConvertDistanceToLogLat(lng, lat, float64(maxRadius), 0)
|
||||
lng1 := lng - (lng2 - lng)
|
||||
lat1 := lat - (lat2 - lat)
|
||||
// globals.SugarLogger.Debugf("%f,%f,%f,%f\n", lng1, lng2, lat1, lat2)
|
||||
sql := `
|
||||
SELECT t1.*,
|
||||
city.name city_name
|
||||
FROM store t1
|
||||
JOIN place city ON city.code = t1.city_code
|
||||
WHERE t1.deleted_at = ? AND t1.status <> ? AND t1.lng > ? AND t1.lng < ? AND t1.lat > ? AND t1.lat < ?
|
||||
`
|
||||
sqlParams := []interface{}{
|
||||
utils.DefaultTimeValue,
|
||||
model.StoreStatusDisabled,
|
||||
jxutils.StandardCoordinate2Int(lng1),
|
||||
jxutils.StandardCoordinate2Int(lng2),
|
||||
jxutils.StandardCoordinate2Int(lat1),
|
||||
jxutils.StandardCoordinate2Int(lat2),
|
||||
}
|
||||
|
||||
var storeList1 []*Store4User
|
||||
if err = dao.GetRows(dao.GetDB(), &storeList1, sql, sqlParams...); err == nil {
|
||||
var storeList2 []*Store4User
|
||||
for _, v := range storeList1 {
|
||||
isStoreOK := false
|
||||
v.FloatLng = jxutils.IntCoordinate2Standard(v.Lng)
|
||||
v.FloatLat = jxutils.IntCoordinate2Standard(v.Lat)
|
||||
if v.DeliveryRangeType == model.DeliveryRangeTypeRadius {
|
||||
maxDistance := int(utils.Str2Int64WithDefault(v.DeliveryRange, 0))
|
||||
v.Distance = int(jxutils.EarthDistance(lng, lat, v.FloatLng, v.FloatLat) * 1000)
|
||||
isStoreOK = v.Distance <= maxDistance
|
||||
} else {
|
||||
points := jxutils.CoordinateStr2Points(v.DeliveryRange)
|
||||
if utils.IsPointInPolygon(lng, lat, points) {
|
||||
v.Distance = int(jxutils.EarthDistance(lng, lat, v.FloatLng, v.FloatLat) * 1000)
|
||||
isStoreOK = true
|
||||
}
|
||||
}
|
||||
if isStoreOK {
|
||||
storeList2 = append(storeList2, v)
|
||||
}
|
||||
}
|
||||
sort.Sort(Store4UserList(storeList2))
|
||||
storeList = storeList2
|
||||
if len(storeList) > maxStoreCount4User {
|
||||
storeList = storeList[:maxStoreCount4User]
|
||||
}
|
||||
}
|
||||
return storeList, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user