diff --git a/business/jxstore/misc/misc.go b/business/jxstore/misc/misc.go index 1bce22d9f..689509ad5 100644 --- a/business/jxstore/misc/misc.go +++ b/business/jxstore/misc/misc.go @@ -2,6 +2,8 @@ package misc import ( "fmt" + "math" + "strconv" "strings" "time" @@ -13,8 +15,15 @@ import ( "git.rosy.net.cn/jx-callback/business/model/dao" "git.rosy.net.cn/jx-callback/business/partner" "git.rosy.net.cn/jx-callback/globals" + "git.rosy.net.cn/jx-callback/globals/api" ) +type CityCenter struct { + Lng float64 + Lat float64 + CityName string +} + func StartRefreshEbaiRealMobile() { if globals.ReallyCallPlatformAPI { utils.AfterFuncWithRecover(5*time.Second, func() { @@ -78,3 +87,225 @@ func RefreshRealMobile(ctx *jxcontext.Context, vendorID int, fromTime, toTime ti } return hint, err } + +func StartGetCityStoreInfo() { + cityCenters := make([]*CityCenter, 0) + guiyang := &CityCenter{ + Lng: 106.713478, + Lat: 26.578343, + CityName: "贵阳市", + } + cityCenters = append(cityCenters, guiyang) + GetCityStoreInfo(cityCenters) + // countries, err := api.AutonaviAPI.GetDistricts(2, "") + // if err == nil { + // cityCenters := make([]*CityCenter, 0) + // country := countries[0] + // districts := country.Districts + // for _, province := range districts { // 省 + // for _, city := range province.Districts { // 市 + // // globals.SugarLogger.Debug(utils.Format4Output(city.Name, false)) + // cityCenter := &CityCenter{ + // Lng: city.Lng, + // Lat: city.Lat, + // CityName: city.Name, + // } + // // globals.SugarLogger.Debug(utils.Format4Output(cityCenter, false)) + // cityCenters = append(cityCenters, cityCenter) + // } + // } + // // globals.SugarLogger.Debug(utils.Format4Output(cityCenters, false)) + // GetCityStoreInfo(cityCenters) + // } + // utils.AfterFuncWithRecover(12*time.Hour, func() { + // StartGetCityStoreInfo() + // }) +} + +func GetCityStoreInfo(cityCenters []*CityCenter) { + utils.CallFuncAsync(func() { + for i := 0; i < len(cityCenters); i++ { + cityCenter := cityCenters[i] + cityPoints := GetCityPoints(cityCenter.Lng, cityCenter.Lat, cityCenter.CityName) + for j := 0; j < len(cityPoints); j++ { + point := cityPoints[j] + SaveJdPointsStores(point) + SaveEbaiPointsStores(point) + } + } + }) +} + +func SaveJdPointsStores(point []string) { + retVal, err := api.JdAPI.GetStoreList(point[0], point[1]) + if err == nil { + retVal2 := retVal["data"].(map[string]interface{}) + storeList := retVal2["data"].([]interface{}) + for _, x := range storeList { + xMap := x.(map[string]interface{}) + floorCellData := xMap["floorCellData"].(map[string]interface{}) + storeId := floorCellData["storeId"].(string) + sql := ` + SELECT * + FROM store_info + WHERE vendor_id = ? AND vendor_store_id = ? + ` + sqlParams := []interface{}{ + model.VendorIDJD, + storeId, + } + var storeList []*model.StoreInfo + db := dao.GetDB() + if err = dao.GetRows(db, &storeList, sql, sqlParams...); err == nil && len(storeList) == 0 { + SaveJdStoreInfo(storeId, point[2]) + } + } + } +} + +func SaveJdStoreInfo(storeId, cityName string) { + retVal, err := api.JdAPI.GetStoreInfo(storeId) + if err == nil { + store := retVal["storeInfo"].(map[string]interface{}) + storeCommentVO := retVal["storeCommentVO"].(map[string]interface{}) + storeInfo := &model.StoreInfo{ + VendorID: model.VendorIDJD, + VendorStoreID: utils.Interface2String(store["storeId"]), + CityName: cityName, + StoreName: utils.Interface2String(store["storeName"]), + Address: utils.Interface2String(store["storeAddress"]), + ShopScore: utils.Interface2Float64WithDefault(storeCommentVO["scoreAvg"], 0), + Tel: utils.Interface2String(store["storeTel"]), + RecentOrderNum: utils.Interface2String(store["monthSaleNum"]), + SkuCount: utils.Interface2String(store["inSaleNum"]), + BusinessType: utils.Interface2String(store["industry"]), + } + db := dao.GetDB() + dao.CreateEntity(db, storeInfo) + } +} + +func SaveEbaiPointsStores(point []string) { + retVal, err := api.EbaiAPI.GetStoreList(point[0], point[1]) + if err == nil { + storeList := retVal["shop_list"].([]interface{}) + for _, x := range storeList { + xMap := x.(map[string]interface{}) + shopInfo := xMap["shop_info"].(map[string]interface{}) + storeId := shopInfo["wid"].(string) + sql := ` + SELECT * + FROM store_info + WHERE vendor_id = ? AND vendor_store_id = ? + ` + sqlParams := []interface{}{ + model.VendorIDEBAI, + storeId, + } + var storeList []*model.StoreInfo + db := dao.GetDB() + if err = dao.GetRows(db, &storeList, sql, sqlParams...); err == nil && len(storeList) == 0 { + SaveEbaiStoreInfo(storeId, point[2]) + } + } + } +} + +func SaveEbaiStoreInfo(storeId, cityName string) { + store, err := api.EbaiAPI.GetStoreInfo(storeId) + if err == nil { + storeInfo := &model.StoreInfo{ + VendorID: model.VendorIDEBAI, + VendorStoreID: utils.Interface2String(store["shop_id"]), + CityName: cityName, + } + if store["storeIsUnderfind"] == nil { + storeInfo.StoreName = utils.Interface2String(store["name"]) + storeInfo.Address = utils.Interface2String(store["address"]) + storeInfo.ShopScore = utils.Interface2Float64WithDefault(store["shop_score"], 0) + storeInfo.Tel = utils.Interface2String(store["phone"]) + storeInfo.RecentOrderNum = utils.Int64ToStr(utils.Interface2Int64WithDefault(store["recent_order_num"], 0)) + storeInfo.SkuCount = utils.Int64ToStr(utils.Interface2Int64WithDefault(store["sku_count"], 0)) + storeInfo.BusinessType = utils.Interface2String(store["category"]) + } + db := dao.GetDB() + dao.CreateEntity(db, storeInfo) + } +} + +// func GetCityStoreInfo(cityCenters []*CityCenter, i int) { +// cityCenter := cityCenters[i] +// cityPoints := GetCityPoints(cityCenter.lng, cityCenter.lat) +// GetJdCityPointsStores(jxcontext.AdminCtx, cityPoints, true, true) +// utils.AfterFuncWithRecover(10*time.Minute, func() { +// GetEbaiCityPointsStores(jxcontext.AdminCtx, cityPoints, true, true) +// utils.AfterFuncWithRecover(10*time.Minute, func() { +// i++ +// if i < len(cityCenters) { +// GetCityStoreInfo(cityCenters, i) +// } +// }) +// }) +// } + +// func GetJdCityPointsStores(ctx *jxcontext.Context, cityPoints [][]string, isAsync, isContinueWhenError bool) (hint string, err error) { +// if len(cityPoints) > 0 { +// task := tasksch.NewParallelTask("misc GetJdCityPointsStores", tasksch.NewParallelConfig().SetIsContinueWhenError(isContinueWhenError), ctx, +// func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) { +// // globals.SugarLogger.Debug(batchItemList) +// point := batchItemList[0].([]string) +// err2 := jd.OnSaveStoreListInfo(point[0], point[1]) +// if err = err2; err != nil { +// globals.SugarLogger.Infof("GetJdCityPointsStores point:%s,%s failed with error:%v", point[0], point[1], err) +// } +// return nil, err +// }, cityPoints) +// // globals.SugarLogger.Debug(utils.Format4Output(task, false)) +// tasksch.HandleTask(task, nil, true).Run() +// hint = task.ID +// if !isAsync { +// _, err = task.GetResult(0) +// } +// } +// return hint, err +// } + +// func GetEbaiCityPointsStores(ctx *jxcontext.Context, cityPoints [][]string, isAsync, isContinueWhenError bool) (hint string, err error) { +// if len(cityPoints) > 0 { +// task := tasksch.NewParallelTask("misc GetEbaiCityPointsStores", tasksch.NewParallelConfig().SetIsContinueWhenError(isContinueWhenError), ctx, +// func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) { +// point := batchItemList[0].([]interface{}) +// err2 := ebai.OnSaveStoreListInfo(point[0].(string), point[1].(string)) +// if err = err2; err != nil { +// globals.SugarLogger.Infof("GetEbaiCityPointsStores point:%s,%s failed with error:%v", point[0].(string), point[1].(string), err) +// } +// return nil, err +// }, cityPoints) +// tasksch.HandleTask(task, nil, true).Run() +// hint = task.ID +// if !isAsync { +// _, err = task.GetResult(0) +// } +// } +// return hint, err +// } + +func GetCityPoints(lng float64, lat float64, cityName string) (cityPoints [][]string) { + oneDu := 111319.55 + for a := 0; a <= 80; a++ { + distance := float64(a*2000 - 80000) + angle := float64(math.Pi / 2) + newLng := strconv.FormatFloat(lng+(distance*math.Sin(angle*math.Pi/180))/(oneDu*math.Cos(lat*math.Pi/180)), 'f', -1, 64) + for b := 0; b <= 80; b++ { + distance2 := float64(b*2000 - 80000) + angle2 := float64(0) + newLat := strconv.FormatFloat(lat+(distance2*math.Cos(angle2*math.Pi/180))/oneDu, 'f', -1, 64) + point := make([]string, 0) + point = append(point, newLng) + point = append(point, newLat) + point = append(point, cityName) + cityPoints = append(cityPoints, point) + } + } + return cityPoints +} diff --git a/business/model/store_page.go b/business/model/store_page.go new file mode 100644 index 000000000..6f486764e --- /dev/null +++ b/business/model/store_page.go @@ -0,0 +1,21 @@ +package model + +type StoreInfo struct { + ID int64 `orm:"column(id)" json:"id"` + VendorID int `orm:"column(vendor_id)" json:"vendorID"` // 平台 + VendorStoreID string `orm:"column(vendor_store_id);size(48)" json:"vendorStoreID"` // 店铺ID + CityName string `orm:"size(48)" json:"cityName"` // 店铺所在城市 + StoreName string `orm:"size(48)" json:"storeName"` // 店铺名称 + Address string `orm:"size(255)" json:"address"` // 店铺地址 + ShopScore float64 `json:"shopScore"` // 店铺评分 + Tel string `orm:"size(48)" json:"tel"` // 店铺电话 + RecentOrderNum string `orm:"size(48)" json:"recentOrderNum"` // 月订单量 + SkuCount string `orm:"size(48)" json:"skuCount"` // 商品总量 + BusinessType string `orm:"size(48)" json:"businessType"` // 经营范围 +} + +func (o *StoreInfo) TableUnique() [][]string { + return [][]string{ + []string{"VendorStoreID", "VendorID"}, + } +} diff --git a/globals/beegodb/beegodb.go b/globals/beegodb/beegodb.go index 87226de45..d9299e2ab 100644 --- a/globals/beegodb/beegodb.go +++ b/globals/beegodb/beegodb.go @@ -36,6 +36,8 @@ func Init() { orm.RegisterModel(&model.Promotion{}, &model.PromotionStore{}, &model.PromotionSku{}) orm.RegisterModel(&model.AuthBind{}, &model.User{}) + orm.RegisterModel(&model.StoreInfo{}) + // orm.RegisterModel(&model.ActivityForSku{}) // orm.RegisterModel(&legacymodel.JxBadComments2{}) diff --git a/main.go b/main.go index c4a566add..f89c17ca9 100644 --- a/main.go +++ b/main.go @@ -50,6 +50,7 @@ func Init() { misc.StartRefreshEbaiRealMobile() ebai.CurPurchaseHandler.StartRefreshComment() } + misc.StartGetCityStoreInfo() } // 返回true表示非运行服务