- GetAndStoreCitiesShops
This commit is contained in:
@@ -16,6 +16,19 @@ func GetPlaceByCode(db *DaoDB, code int) (place *model.Place, err error) {
|
||||
return place, err
|
||||
}
|
||||
|
||||
func GetEnabledPlaces(db *DaoDB) (placeList []*model.Place, err error) {
|
||||
if db == nil {
|
||||
db = GetDB()
|
||||
}
|
||||
err = GetRows(db, &placeList, `
|
||||
SELECT *
|
||||
FROM place
|
||||
WHERE enabled = 1 AND level = 2
|
||||
ORDER BY code
|
||||
`)
|
||||
return placeList, err
|
||||
}
|
||||
|
||||
func GetPlaceByName(db *DaoDB, name string, level int, parentCode int) (place *model.Place, err error) {
|
||||
if db == nil {
|
||||
db = GetDB()
|
||||
|
||||
@@ -12,6 +12,11 @@ import (
|
||||
"git.rosy.net.cn/jx-callback/business/partner"
|
||||
)
|
||||
|
||||
const (
|
||||
DefRadius = 8000
|
||||
DefGridWith = 3000
|
||||
)
|
||||
|
||||
func GetCityShops(ctx *jxcontext.Context, parentTask tasksch.ITask, vendorIDs []int, cityCode, radius, gridWith int) (pageStoreList []*model.PageShop, err error) {
|
||||
coordList := ditu.GetCityCoordinateList(cityCode, radius, gridWith)
|
||||
if len(coordList) > 0 {
|
||||
@@ -29,6 +34,7 @@ func GetCityShops(ctx *jxcontext.Context, parentTask tasksch.ITask, vendorIDs []
|
||||
if err = err2; err != nil && len(list) == 0 {
|
||||
return nil, err
|
||||
}
|
||||
err = nil
|
||||
for _, v := range list {
|
||||
v2 := v.(*model.PageShop)
|
||||
v2.CityCode = cityCode
|
||||
@@ -78,9 +84,62 @@ func getStoreListByCoordinates(ctx *jxcontext.Context, parentTask tasksch.ITask,
|
||||
if err = err2; err != nil && len(shopList) == 0 {
|
||||
return nil, err
|
||||
}
|
||||
err = nil
|
||||
for _, v := range shopList {
|
||||
storeList = append(storeList, v.(*model.PageShop))
|
||||
}
|
||||
}
|
||||
return storeList, err
|
||||
}
|
||||
|
||||
func GetAndStoreCitiesShops(ctx *jxcontext.Context, vendorIDs []int, cityCodeList []int, radius, gridWith int) (hint string, err error) {
|
||||
db := dao.GetDB()
|
||||
if len(cityCodeList) == 0 {
|
||||
placeList, err2 := dao.GetEnabledPlaces(db)
|
||||
if err = err2; err != nil {
|
||||
return "", err
|
||||
}
|
||||
for _, v := range placeList {
|
||||
cityCodeList = append(cityCodeList, v.Code)
|
||||
}
|
||||
// cityCodeList = []int{510100}
|
||||
}
|
||||
if len(vendorIDs) == 0 {
|
||||
vendorIDs = []int{model.VendorIDJD, model.VendorIDEBAI}
|
||||
}
|
||||
if radius <= 0 {
|
||||
radius = DefRadius
|
||||
}
|
||||
if gridWith <= 0 {
|
||||
gridWith = DefGridWith
|
||||
}
|
||||
|
||||
task := tasksch.NewSeqTask("GetAndStoreCitiesShops", ctx,
|
||||
func(task *tasksch.SeqTask, step int, params ...interface{}) (result interface{}, err error) {
|
||||
cityCode := cityCodeList[step]
|
||||
shopList, err := GetCityShops(ctx, task, vendorIDs, cityCode, radius, gridWith)
|
||||
if err == nil {
|
||||
dao.Begin(db)
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
dao.Rollback(db)
|
||||
}
|
||||
}()
|
||||
for _, v := range shopList {
|
||||
tmpShop := *v
|
||||
dao.DeleteEntity(db, &tmpShop, model.FieldVendorStoreID, model.FieldVendorID)
|
||||
if err = dao.CreateEntity(db, v); err != nil {
|
||||
break
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
dao.Rollback(db)
|
||||
} else {
|
||||
dao.Commit(db)
|
||||
}
|
||||
}
|
||||
return nil, nil // 强制继续
|
||||
}, len(cityCodeList))
|
||||
tasksch.ManageTask(task).Run()
|
||||
return task.GetID(), err
|
||||
}
|
||||
|
||||
32
controllers/net_spider.go
Normal file
32
controllers/net_spider.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"git.rosy.net.cn/jx-callback/business/jxutils"
|
||||
"git.rosy.net.cn/jx-callback/business/netspider"
|
||||
"github.com/astaxie/beego"
|
||||
)
|
||||
|
||||
type NetSpiderController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
// @Title 同步商家商品信息
|
||||
// @Description 同步商家商品信息
|
||||
// @Param token header string true "认证token"
|
||||
// @Param vendorIDs formData string false "厂商ID列表"
|
||||
// @Param cityCodes formData string false "城市列表,缺省为后台启用了的城市"
|
||||
// @Param radius formData int false "区域半径,单位为米,缺省8000米"
|
||||
// @Param gridWith formData int false "网格大小,单位为米,缺省为3000米"
|
||||
// @Success 200 {object} controllers.CallResult
|
||||
// @Failure 200 {object} controllers.CallResult
|
||||
// @router /GetAndStoreCitiesShops [post]
|
||||
func (c *NetSpiderController) GetAndStoreCitiesShops() {
|
||||
c.callGetAndStoreCitiesShops(func(params *tNetspiderGetAndStoreCitiesShopsParams) (retVal interface{}, errCode string, err error) {
|
||||
var vendorIDs, cityCodes []int
|
||||
err = jxutils.Strings2Objs(params.VendorIDs, &vendorIDs, params.CityCodes, &cityCodes)
|
||||
if err == nil {
|
||||
retVal, err = netspider.GetAndStoreCitiesShops(params.Ctx, vendorIDs, cityCodes, params.Radius, params.GridWith)
|
||||
}
|
||||
return retVal, "", err
|
||||
})
|
||||
}
|
||||
@@ -36,7 +36,7 @@ func Init() {
|
||||
orm.RegisterModel(&model.Promotion{}, &model.PromotionStore{}, &model.PromotionSku{})
|
||||
orm.RegisterModel(&model.AuthBind{}, &model.User{})
|
||||
|
||||
// orm.RegisterModel(&model.PageShop{})
|
||||
orm.RegisterModel(&model.PageShop{})
|
||||
|
||||
// orm.RegisterModel(&model.ActivityForSku{})
|
||||
// orm.RegisterModel(&legacymodel.JxBadComments2{})
|
||||
|
||||
@@ -511,6 +511,15 @@ func init() {
|
||||
Filters: nil,
|
||||
Params: nil})
|
||||
|
||||
beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:NetSpiderController"] = append(beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:NetSpiderController"],
|
||||
beego.ControllerComments{
|
||||
Method: "GetAndStoreCitiesShops",
|
||||
Router: `/GetAndStoreCitiesShops`,
|
||||
AllowHTTPMethods: []string{"post"},
|
||||
MethodParams: param.Make(),
|
||||
Filters: nil,
|
||||
Params: nil})
|
||||
|
||||
beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:OrderController"] = append(beego.GlobalControllerRouter["git.rosy.net.cn/jx-callback/controllers:OrderController"],
|
||||
beego.ControllerComments{
|
||||
Method: "AcceptOrRefuseFailedGetOrder",
|
||||
|
||||
@@ -106,6 +106,11 @@ func init() {
|
||||
&controllers.SysController{},
|
||||
),
|
||||
),
|
||||
beego.NSNamespace("/netspider",
|
||||
beego.NSInclude(
|
||||
&controllers.NetSpiderController{},
|
||||
),
|
||||
),
|
||||
)
|
||||
beego.AddNamespace(ns)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user