From 5492ff0608449fdf60dad41692487285b208aa5b Mon Sep 17 00:00:00 2001 From: gazebo Date: Thu, 17 Oct 2019 15:39:29 +0800 Subject: [PATCH] GetStoreListByLocation --- business/jxstore/cms/store.go | 82 +++++++++++++++++++++++++++ business/jxstore/cms/user2.go | 2 +- business/model/store.go | 6 ++ controllers/cms_store.go | 15 +++++ routers/commentsRouter_controllers.go | 9 +++ 5 files changed, 113 insertions(+), 1 deletion(-) diff --git a/business/jxstore/cms/store.go b/business/jxstore/cms/store.go index da195eb10..36ca5e8db 100644 --- a/business/jxstore/cms/store.go +++ b/business/jxstore/cms/store.go @@ -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 +} diff --git a/business/jxstore/cms/user2.go b/business/jxstore/cms/user2.go index 7cca16dd3..e97213b58 100644 --- a/business/jxstore/cms/user2.go +++ b/business/jxstore/cms/user2.go @@ -380,7 +380,7 @@ func GetMyStoreListNew(ctx *jxcontext.Context) (storesInfo interface{}, errCode } var storeList []*dao.StoreWithCityName if storeList, err = GetStoreList4User(ctx, mobileNum, userID); err == nil && len(storeList) > 0 { - // todo,应该用通过方法 + // todo,应该用通用方法 mapDataList := make([]map[string]interface{}, len(storeList)) for k, v := range storeList { mapDataList[k] = map[string]interface{}{ diff --git a/business/model/store.go b/business/model/store.go index e60e952b0..9338dd8d5 100644 --- a/business/model/store.go +++ b/business/model/store.go @@ -314,6 +314,12 @@ func (*Store) TableUnique() [][]string { } } +func (*Store) TableIndex() [][]string { + return [][]string{ + []string{"Lng", "Lat"}, + } +} + func (s *Store) IsPrinterDisabled() bool { return s.PrinterDisabled == 1 } diff --git a/controllers/cms_store.go b/controllers/cms_store.go index b21bbad7c..9b3696a3a 100644 --- a/controllers/cms_store.go +++ b/controllers/cms_store.go @@ -529,3 +529,18 @@ func (c *StoreController) SyncStoresCourierInfo() { return retVal, "", err }) } + +// @Title 根据位置得到推荐门店列表 +// @Description 根据位置得到推荐门店列表 +// @Param token header string true "认证token" +// @Param lng query float64 true "经度" +// @Param lat query float64 true "纬度" +// @Success 200 {object} controllers.CallResult +// @Failure 200 {object} controllers.CallResult +// @router /GetStoreListByLocation [get] +func (c *StoreController) GetStoreListByLocation() { + c.callGetStoreListByLocation(func(params *tStoreGetStoreListByLocationParams) (retVal interface{}, errCode string, err error) { + retVal, err = cms.GetStoreListByLocation(params.Ctx, params.Lng, params.Lat) + return retVal, "", err + }) +} diff --git a/routers/commentsRouter_controllers.go b/routers/commentsRouter_controllers.go index 2621dd933..1fe39de4e 100644 --- a/routers/commentsRouter_controllers.go +++ b/routers/commentsRouter_controllers.go @@ -1269,6 +1269,15 @@ func init() { Filters: nil, Params: nil}) + beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:StoreController"] = append(beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:StoreController"], + beego.ControllerComments{ + Method: "GetStoreListByLocation", + Router: `/GetStoreListByLocation`, + AllowHTTPMethods: []string{"get"}, + MethodParams: param.Make(), + Filters: nil, + Params: nil}) + beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:StoreController"] = append(beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:StoreController"], beego.ControllerComments{ Method: "GetStoreTotalScoreList",