diff --git a/business/jxutils/jxutils_cms.go b/business/jxutils/jxutils_cms.go index cb28b7c98..7c0ff0d73 100644 --- a/business/jxutils/jxutils_cms.go +++ b/business/jxutils/jxutils_cms.go @@ -1,5 +1,13 @@ package jxutils +import ( + "fmt" + "strings" + "time" + + "git.rosy.net.cn/baseapi/utils" +) + // 合并得到最终的门店状态 func MergeStoreStatus(status int, vendorStatus int) int { if status < vendorStatus { @@ -28,3 +36,31 @@ func SplitSlice(list []interface{}, batchCount int) (listInList [][]interface{}) } return listInList } + +func SplitStoreName(fullName, separator, defaultPrefix string) (prefix, bareName string) { + names := strings.Split(fullName, separator) + if len(names) == 2 { + prefix = names[0] + bareName = names[1] + } else { + prefix = defaultPrefix + bareName = strings.Trim(strings.Trim(fullName, defaultPrefix), separator) + + } + return utils.TrimBlanChar(defaultPrefix), utils.TrimBlanChar(bareName) +} + +func ComposeStoreName(bareName, separator, prefix string) (fullName string) { + return prefix + separator + utils.TrimBlanChar(strings.Trim(bareName, separator)) +} + +func StrTime2JxOperationTime(strTime string, defValue int16) int16 { + if timeValue, err := time.Parse("15:04:05", strTime); err == nil { + return int16(timeValue.Hour()*100 + timeValue.Minute()) + } + return defValue +} + +func JxOperationTime2StrTime(value int16) string { + return fmt.Sprintf("%02d:%02d:00", value/100, value%100) +} diff --git a/business/model/dao/place.go b/business/model/dao/place.go index 4d1abe22e..a3320487e 100644 --- a/business/model/dao/place.go +++ b/business/model/dao/place.go @@ -20,12 +20,20 @@ func GetPlaceByName(db *DaoDB, name string, level int, parentCode int) (place *m if db == nil { db = GetDB() } - place = &model.Place{ - Name: name, - Level: int8(level), - ParentCode: parentCode, + cols := []string{ + "Name", + "Level", } - if err = GetEntity(db, place, "Name", "Level", "ParentCode"); err == orm.ErrNoRows { + place = &model.Place{ + Name: name, + Level: int8(level), + } + if parentCode != 0 { + cols = append(cols, "ParentCode") + place.ParentCode = parentCode + + } + if err = GetEntity(db, place, cols...); err == orm.ErrNoRows { err = db.db.Raw("SELECT * FROM place WHERE parent_code = ? AND level = ? AND name LIKE ?", parentCode, level, "%"+name+"%").QueryRow(place) } return place, err diff --git a/business/model/place.go b/business/model/place.go index fe1b9ab5f..497d7776a 100644 --- a/business/model/place.go +++ b/business/model/place.go @@ -2,9 +2,9 @@ package model // https://github.com/videni/pcr const ( - CityLevelProvince = 1 - CityLevelCity = 2 - CityLevelDistrict = 3 + PlaceLevelProvince = 1 + PlaceLevelCity = 2 + PlaceLevelDistrict = 3 ) type Place struct { diff --git a/business/partner/purchase/ebai/ebai.go b/business/partner/purchase/ebai/ebai.go new file mode 100644 index 000000000..6aa20418b --- /dev/null +++ b/business/partner/purchase/ebai/ebai.go @@ -0,0 +1,41 @@ +package ebai + +import ( + "git.rosy.net.cn/baseapi/platformapi/ebaiapi" + "git.rosy.net.cn/jx-callback/business/jxcallback/scheduler" + "git.rosy.net.cn/jx-callback/business/model" +) + +var ( + curPurchaseHandler *PurchaseHandler +) + +type PurchaseHandler struct { + scheduler.BasePurchasePlatform +} + +func init() { + curPurchaseHandler = new(PurchaseHandler) + scheduler.CurrentScheduler.RegisterPurchasePlatform(model.VendorIDEBAI, curPurchaseHandler) +} + +func EbaiBusStatus2JxStatus(ebaiStatus int) int { + if ebaiStatus == ebaiapi.ShopBusStatusOffline || ebaiStatus == ebaiapi.ShopBusStatusSuspended { + return model.StoreStatusClosed + } + return model.StoreStatusOpened +} + +func (p *PurchaseHandler) SyncCategories(catIDs []int) (err error) { + return err +} +func (p *PurchaseHandler) ReadStoreCategories(storeID int) (cats []*model.SkuCategory, err error) { + return cats, err +} + +func (p *PurchaseHandler) SyncSkus(skuIDs []int) (err error) { + return err +} +func (p *PurchaseHandler) ReadStoreSku(storeID, skuID int) (skuNameExt *model.SkuNameExt, err error) { + return skuNameExt, err +} diff --git a/business/partner/purchase/ebai/order.go b/business/partner/purchase/ebai/order.go new file mode 100644 index 000000000..2c0107bb9 --- /dev/null +++ b/business/partner/purchase/ebai/order.go @@ -0,0 +1,39 @@ +package ebai + +import ( + "git.rosy.net.cn/jx-callback/business/model" +) + +func (p *PurchaseHandler) GetStatusFromVendorStatus(vendorStatus string) int { + return 0 +} +func (p *PurchaseHandler) GetOrder(vendorOrderID string) (order *model.GoodsOrder, err error) { + return order, err +} + +func (p *PurchaseHandler) AcceptOrRefuseOrder(order *model.GoodsOrder, isAcceptIt bool, userName string) (err error) { + return err +} +func (p *PurchaseHandler) PickupGoods(order *model.GoodsOrder, userName string) (err error) { + return err +} + +// 将订单从购物平台配送转为自送 +func (p *PurchaseHandler) Swtich2SelfDeliver(order *model.GoodsOrder, userName string) (err error) { + return err +} + +// 将订单从购物平台配送转为自送后又送达 +func (p *PurchaseHandler) Swtich2SelfDelivered(order *model.GoodsOrder, userName string) (err error) { + return err +} + +// 完全自送的门店表示开始配送 +func (p *PurchaseHandler) SelfDeliverDelievering(order *model.GoodsOrder, userName string) (err error) { + return err +} + +// 完全自送的门店表示配送完成 +func (p *PurchaseHandler) SelfDeliverDelievered(order *model.GoodsOrder, userName string) (err error) { + return err +} diff --git a/business/partner/purchase/ebai/store.go b/business/partner/purchase/ebai/store.go new file mode 100644 index 000000000..cd31ebe8f --- /dev/null +++ b/business/partner/purchase/ebai/store.go @@ -0,0 +1,102 @@ +package ebai + +import ( + "fmt" + "strings" + + "git.rosy.net.cn/baseapi/platformapi/autonavi" + "git.rosy.net.cn/baseapi/platformapi/ebaiapi" + "git.rosy.net.cn/baseapi/utils" + "git.rosy.net.cn/jx-callback/business/jxcallback/scheduler" + "git.rosy.net.cn/jx-callback/business/jxutils" + "git.rosy.net.cn/jx-callback/business/model" + "git.rosy.net.cn/jx-callback/business/model/dao" + "git.rosy.net.cn/jx-callback/business/partner" + "git.rosy.net.cn/jx-callback/globals/api" +) + +const ( + VendorStorePrefix = "好菜鲜生" +) + +func (p *PurchaseHandler) ReadStore(vendorStoreID string) (*model.Store, error) { + baiduShopID := utils.Str2Int64(vendorStoreID) + result, err := api.EbaiAPI.ShopGet("", baiduShopID) + if err == nil { + retVal := &model.Store{ + Address: utils.Interface2String(result["address"]), + Tel1: utils.Interface2String(result["phone"]), + } + _, retVal.Name = jxutils.SplitStoreName(utils.Interface2String(result["name"]), partner.StoreNameSeparator, VendorStorePrefix) + retVal.DeliveryType = EbaiDeliveryType2Jx(utils.Interface2String(result["delivery_type"])) + + businessTime := result["business_time"].([]interface{}) + btime := businessTime[0].(map[string]interface{}) + retVal.OpenTime1 = jxutils.StrTime2JxOperationTime(utils.Interface2String(btime["start"].(string)), 700) + retVal.CloseTime1 = jxutils.StrTime2JxOperationTime(utils.Interface2String(btime["end"].(string)), 2000) + if len(businessTime) > 1 { + btime = businessTime[1].(map[string]interface{}) + retVal.OpenTime2 = jxutils.StrTime2JxOperationTime(utils.Interface2String(btime["start"].(string)), 700) + retVal.CloseTime2 = jxutils.StrTime2JxOperationTime(utils.Interface2String(btime["end"].(string)), 2000) + } + + if ebaiStatus, err2 := api.EbaiAPI.ShopBusStatusGet("", baiduShopID, ebaiapi.PlatformFlagBaidu); err2 == nil { + retVal.Status = EbaiBusStatus2JxStatus(ebaiStatus) + } + + tel2 := utils.Interface2String(result["ivr_phone"]) + if tel2 != "" && tel2 != retVal.Tel1 { + retVal.Tel2 = tel2 + } + + db := dao.GetDB() + if city, err2 := dao.GetPlaceByName(db, utils.Interface2String(result["city"]), model.PlaceLevelCity, 0); err2 == nil { + retVal.CityCode = city.Code + if district, err2 := dao.GetPlaceByName(db, utils.Interface2String(result["county"]), model.PlaceLevelDistrict, city.Code); err2 == nil { + retVal.DistrictCode = district.Code + } + } + lng := utils.MustInterface2Float64(result["longitude"]) + lat := utils.MustInterface2Float64(result["latitude"]) + if utils.Interface2String(result["coord_type"]) == ebaiapi.CoordTypeBaidu { + var err2 error + if lng, lat, err2 = api.AutonaviAPI.CoordinateConvert(lng, lat, autonavi.CoordSysBaidu); err2 != nil { + return nil, err2 + } + } + retVal.Lng = jxutils.StandardCoordinate2Int(lng) + retVal.Lat = jxutils.StandardCoordinate2Int(lat) + + retVal.ID = int(utils.Str2Int64WithDefault(utils.Interface2String(result["shop_id"]), 0)) + + retVal.DeliveryRangeType = model.DeliveryRangeTypePolygon + region := result["delivery_region"].([]interface{})[0].(map[string]interface{})["region"].([]interface{})[0].([]interface{}) + coords := make([]string, len(region)) + for k, v := range region { + mapV := v.(map[string]interface{}) + coords[k] = fmt.Sprintf("%d,%d", jxutils.StandardCoordinate2Int(utils.MustInterface2Float64(mapV["longitude"])), jxutils.StandardCoordinate2Int(utils.MustInterface2Float64(mapV["latitude"]))) + } + retVal.DeliveryRange = strings.Join(coords, ";") + return retVal, nil + } + return nil, err +} + +func (p *PurchaseHandler) UpdateStore(storeID int, userName string) error { + return nil +} + +func EbaiDeliveryType2Jx(deliveryType string) int8 { + spIndex := strings.Index(deliveryType, "|") + baiduDeliveryType := utils.Str2Int64(deliveryType[spIndex+1:]) + switch baiduDeliveryType { + case ebaiapi.DeliveryTypeBaiduLogistics: + return scheduler.StoreDeliveryTypeByPlatform + case ebaiapi.DeliveryTypeBaiduDeliveryBySelf: + return scheduler.StoreDeliveryTypeByStore + case ebaiapi.DeliveryTypeBaiduCrowdSourcing: + return scheduler.StoreDeliveryTypeCrowdSourcing + default: + return scheduler.StoreDeliveryTypeCrowdSourcing + } +} diff --git a/business/partner/purchase/ebai/store_sku.go b/business/partner/purchase/ebai/store_sku.go new file mode 100644 index 000000000..a4fa1d6c8 --- /dev/null +++ b/business/partner/purchase/ebai/store_sku.go @@ -0,0 +1,5 @@ +package ebai + +func (p *PurchaseHandler) SyncStoreSku(storeID int, skuIDs []int, isForce bool, userName string) (err error) { + return err +} diff --git a/business/partner/purchase/ebai/waybill.go b/business/partner/purchase/ebai/waybill.go new file mode 100644 index 000000000..745aa2b8a --- /dev/null +++ b/business/partner/purchase/ebai/waybill.go @@ -0,0 +1,2 @@ +package ebai + diff --git a/business/partner/purchase/elm/store.go b/business/partner/purchase/elm/store.go index 12681439f..c76e2b3e2 100644 --- a/business/partner/purchase/elm/store.go +++ b/business/partner/purchase/elm/store.go @@ -1,6 +1,8 @@ package elm -import "git.rosy.net.cn/jx-callback/business/model" +import ( + "git.rosy.net.cn/jx-callback/business/model" +) func (p *PurchaseHandler) ReadStore(vendorStoreID string) (*model.Store, error) { return nil, nil diff --git a/business/partner/purchase/jd/store.go b/business/partner/purchase/jd/store.go index 56ec259a0..9aad56451 100644 --- a/business/partner/purchase/jd/store.go +++ b/business/partner/purchase/jd/store.go @@ -40,7 +40,7 @@ func (p *PurchaseHandler) ReadStore(vendorStoreID string) (*model.Store, error) Status: JdStoreStatus2JxStatus(result["yn"], result["closeStatus"]), Tel1: utils.Interface2String(result["phone"]), } - _, retVal.Name = SplitStoreName(utils.Interface2String(result["stationName"])) + _, retVal.Name = jxutils.SplitStoreName(utils.Interface2String(result["stationName"]), partner.StoreNameSeparator, VendorStorePrefix) retVal.DeliveryType = JdDeliveryType2Jx(int(utils.MustInterface2Int64(result["carrierNo"]))) tel2 := utils.Interface2String(result["mobile"]) @@ -91,7 +91,7 @@ func (p *PurchaseHandler) UpdateStore(storeID int, userName string) (err error) if err = dao.GetRow(db, &store, sql, model.VendorIDJD, storeID); err == nil { params := map[string]interface{}{ "outSystemId": utils.Int2Str(int(store.ID)), // todo 直接修改这个字段可能会有问题 - "stationName": ComposeStoreName(store.Name), + "stationName": jxutils.ComposeStoreName(store.Name, partner.StoreNameSeparator, VendorStorePrefix), "stationAddress": store.Address, "serviceTimeStart1": JxOperationTime2JdOperationTime(store.OpenTime1), "serviceTimeEnd1": JxOperationTime2JdOperationTime(store.CloseTime1), @@ -208,20 +208,3 @@ func JdDeliveryType2Jx(deliveryType int) int8 { } return scheduler.StoreDeliveryTypeByPlatform } - -func SplitStoreName(fullName string) (prefix, bareName string) { - names := strings.Split(fullName, partner.StoreNameSeparator) - if len(names) == 2 { - prefix = names[0] - bareName = names[1] - } else { - prefix = VendorStorePrefix - bareName = strings.Trim(strings.Trim(fullName, VendorStorePrefix), partner.StoreNameSeparator) - - } - return utils.TrimBlanChar(prefix), utils.TrimBlanChar(bareName) -} - -func ComposeStoreName(bareName string) (fullName string) { - return VendorStorePrefix + partner.StoreNameSeparator + utils.TrimBlanChar(strings.Trim(bareName, partner.StoreNameSeparator)) -} diff --git a/conf/app.conf b/conf/app.conf index 9b5d3188b..405cfc2c8 100644 --- a/conf/app.conf +++ b/conf/app.conf @@ -25,6 +25,9 @@ qiniuAK = "bKWilgyrJlc8aXgLEpCUmRrYcxJ8OYhoKk5mO3jJ" qiniuSK = "3MNdU_lKnu22vrNtUELDcLBv23UOUWYKzc16Pmkj" qiniuBucket = "jingxistandardlib" +ebaiSource = "63032" +ebaiSecret = "8c8b66720b69ae85" + [dev] freshFoodServerURL = "http://portal.beta.jxc4.com" diff --git a/controllers/ebai_order.go b/controllers/ebai_order.go new file mode 100644 index 000000000..182fe10cd --- /dev/null +++ b/controllers/ebai_order.go @@ -0,0 +1,10 @@ +package controllers + +import ( + _ "git.rosy.net.cn/jx-callback/business/partner/purchase/ebai" + "github.com/astaxie/beego" +) + +type EbaiController struct { + beego.Controller +} diff --git a/globals/api/api.go b/globals/api/api.go index 2075d12c6..620582f2c 100644 --- a/globals/api/api.go +++ b/globals/api/api.go @@ -3,6 +3,7 @@ package api import ( "git.rosy.net.cn/baseapi/platformapi/autonavi" "git.rosy.net.cn/baseapi/platformapi/dadaapi" + "git.rosy.net.cn/baseapi/platformapi/ebaiapi" "git.rosy.net.cn/baseapi/platformapi/elmapi" "git.rosy.net.cn/baseapi/platformapi/jdapi" "git.rosy.net.cn/baseapi/platformapi/mtpsapi" @@ -17,6 +18,7 @@ import ( var ( JdAPI *jdapi.API ElmAPI *elmapi.API + EbaiAPI *ebaiapi.API MtpsAPI *mtpsapi.API DadaAPI *dadaapi.API WeixinAPI *weixinapi.API @@ -32,6 +34,7 @@ func Init() { WeixinAPI = weixinapi.New(beego.AppConfig.String("weixinAppID"), beego.AppConfig.String("weixinSecret")) AutonaviAPI = autonavi.New(beego.AppConfig.String("autonaviKey")) QiniuAPI = qbox.NewMac(beego.AppConfig.String("qiniuAK"), beego.AppConfig.String("qiniuSK")) + EbaiAPI = ebaiapi.New(beego.AppConfig.String("ebaiSource"), beego.AppConfig.String("ebaiSecret")) } func initElm() { diff --git a/routers/router.go b/routers/router.go index bf0dbc3ed..7ab5ed621 100644 --- a/routers/router.go +++ b/routers/router.go @@ -58,6 +58,7 @@ func init() { beego.AutoRouter(&controllers.MtpsController{}) beego.AutoRouter(&controllers.ElemeController{}) beego.AutoRouter(&controllers.DadaDeliveryController{}) + beego.AutoRouter(&controllers.EbaiController{}) beego.Any("/", func(ctx *beecontext.Context) { ctx.WriteString("pong\n")