Files
jx-callback/business/partner/purchase/tiktok_store/mtwm.go
邹宗楠 7df10d105c 1
2022-10-08 14:16:59 +08:00

243 lines
6.7 KiB
Go

package tiktok_store
import (
"fmt"
"git.rosy.net.cn/baseapi/platformapi/tiktok_shop/tiktok_api"
"strings"
"sync"
"git.rosy.net.cn/baseapi/platformapi/mtwmapi"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/jxutils"
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/business/partner"
"git.rosy.net.cn/jx-callback/business/partner/putils"
"git.rosy.net.cn/jx-callback/globals"
"git.rosy.net.cn/jx-callback/globals/api"
)
var (
CurPurchaseHandler *PurchaseHandler
)
type PurchaseHandler struct {
partner.BasePurchasePlatform
putils.DefSingleStorePlatform
storeIDs []string
locker sync.RWMutex
}
func init() {
if api.TiktokStore != nil {
CurPurchaseHandler = New()
partner.RegisterPurchasePlatform(CurPurchaseHandler)
}
}
func New() (obj *PurchaseHandler) {
obj = new(PurchaseHandler)
obj.ISingleStoreStoreSkuHandler = obj
return obj
}
func (c *PurchaseHandler) GetVendorID() int {
return model.VendorIDDD
}
func (p *PurchaseHandler) GetVendorCategories(ctx *jxcontext.Context) (vendorCats []*model.SkuVendorCategory, err error) {
cats, err := getAPI("53802960", 0, "").GetShopCategory(0)
if err != nil {
return nil, err
}
vendorCats = skuCategory(cats)
return vendorCats, nil
}
func skuCategory(param1 []*tiktok_api.RetailCategoryInfo) (vendorCats []*model.SkuVendorCategory) {
size := make([]*model.SkuVendorCategory, 0)
for _, v := range param1 {
param := &model.SkuVendorCategory{
VendorCategoryID: utils.Int64ToStr(v.Id),
VendorID: model.VendorIDDD,
Name: v.Name,
Level: int(v.Level),
ParentID: utils.Int64ToStr(v.ParentId),
}
if v.Enable == false {
param.IsLeaf = 0
} else {
param.IsLeaf = 1
}
if param.Name != "" {
size = append(size, param)
}
if len(v.Children) != 0 {
kk := skuCategory(v.Children)
vendorCats = append(vendorCats, kk...)
}
}
vendorCats = append(vendorCats, size...)
return
}
func rangeMtwm2JX(areaStr string) string {
var area []interface{}
if err := utils.UnmarshalUseNumber([]byte(areaStr), &area); err == nil {
if len(area) > 0 {
coordList := make([]string, len(area))
for k, v := range area {
vv := v.(map[string]interface{})
coordList[k] = fmt.Sprintf("%.6f,%.6f", jxutils.IntCoordinate2Standard(int(utils.ForceInterface2Int64(vv["x"]))), jxutils.IntCoordinate2Standard(int(utils.ForceInterface2Int64(vv["y"]))))
}
return strings.Join(coordList, ";")
}
}
return ""
}
func rangeJX2Mtwm(coords string) string {
pairs := strings.Split(strings.Trim(coords, ";"), ";")
if len(pairs) > 0 {
coordList := make([]map[string]interface{}, len(pairs))
for k, v := range pairs {
pair := strings.Split(v, ",")
coordList[k] = map[string]interface{}{
"x": jxutils.StandardCoordinate2Int(utils.Str2Float64(pair[0])),
"y": jxutils.StandardCoordinate2Int(utils.Str2Float64(pair[1])),
}
}
return string(utils.MustMarshal(coordList))
}
return ""
}
func openTimeMtwm2JX(vendorOpenTime string) (opTimeList []int16) {
timePairs := strings.Split(vendorOpenTime, ",")
for _, v := range timePairs {
times := strings.Split(v, "-")
if len(times) >= 2 {
opTimeList = append(opTimeList, jxutils.StrTime2JxOperationTime(times[0]+":00", 700), jxutils.StrTime2JxOperationTime(times[1]+":00", 2000))
}
}
return opTimeList
}
func openTimeJX2Mtwm(opTimeList []int16) string {
timesLen := len(opTimeList) / 2 * 2
var strPairs []string
for i := 0; i < timesLen; i += 2 {
if opTimeList[i] != 0 {
strPairs = append(strPairs, jxutils.JxOperationTime2StrTime(opTimeList[i])+"-"+jxutils.JxOperationTime2StrTime(opTimeList[i+1]))
} else {
break
}
}
return strings.Join(strPairs, ",")
}
func bizStatusTiktok2JX(status int64) int {
switch status {
case tiktok_api.StoreStateNormalBusiness:
return model.StoreStatusOpened
case tiktok_api.StoreStateSuspendBusiness:
return model.StoreStatusClosed
}
return model.StoreStatusDisabled
}
func bizStatusJX2Mtwm(status int) (openLevel, online int) {
if status == model.StoreStatusDisabled {
return mtwmapi.PoiOpenLevelHaveRest, mtwmapi.PoiStatusOnline //mtwmapi.PoiStatusOffline
} else if status == model.StoreStatusHaveRest || status == model.StoreStatusClosed {
return mtwmapi.PoiOpenLevelHaveRest, mtwmapi.PoiStatusOnline
}
return mtwmapi.PoiOpenLevelNormal, mtwmapi.PoiStatusOnline
}
func skuStatusJX2Tiktok(status int) int {
if status == model.SkuStatusNormal {
return mtwmapi.SellStatusOnline
}
return mtwmapi.SellStatusOffline
}
func (p *PurchaseHandler) UploadImg(ctx *jxcontext.Context, vendorOrgCode, imgURL string, imgData []byte, imgName string, imgType int) (imgHint string, err error) {
globals.SugarLogger.Debugf("mtwm UploadImg imgURL:%s, imgName:%s", imgURL, imgName)
poiCode4UploadImg := p.getUploadImgPoiCode()
if poiCode4UploadImg == "" {
return "", fmt.Errorf("找不到一个美团门店来上传图片")
}
if globals.EnableMtwmStoreWrite {
if imgType > model.ImgTypeLocal {
if imgData != nil {
imgHint, err = api.MtwmAPI.ImageUpload(poiCode4UploadImg, imgName, imgData)
} else {
imgHint, err = api.MtwmAPI.ImageUploadByURL(poiCode4UploadImg, imgName, imgURL)
}
}
} else {
imgHint = utils.GetUpperUUID()
}
return imgHint, err
}
func getStoreIDFromList(storeIDs []string) (poiCode string) {
if len(storeIDs) > 0 {
poiCode = storeIDs[0]
}
return poiCode
}
func (p *PurchaseHandler) getUploadImgPoiCode() (poiCode string) {
var storeIDs []string
p.locker.RLock()
storeIDs = p.storeIDs
p.locker.RUnlock()
if len(storeIDs) > 0 {
return getStoreIDFromList(storeIDs)
}
p.locker.Lock()
storeIDs = p.storeIDs
if len(storeIDs) > 0 {
p.locker.Unlock()
return getStoreIDFromList(storeIDs)
}
defer p.locker.Unlock()
storeIDs, err := api.MtwmAPI.PoiGetIDs()
if err == nil {
if len(storeIDs) > 0 {
p.storeIDs = storeIDs
poiCode = getStoreIDFromList(storeIDs)
} else {
// p.storeIDs = []string{""}
}
}
return poiCode
}
func getAPI(appOrgCode string, storeID int, vendorStoreID string) (apiObj *tiktok_api.API) {
apiObj = partner.CurAPIManager.GetAPI(model.VendorIDDD, appOrgCode).(*tiktok_api.API)
//if appOrgCode == globals.TiktokShopCode {
// var storeDetail *dao.StoreDetail
// if storeID != 0 {
// storeDetail, _ = dao.GetStoreDetail(dao.GetDB(), storeID, model.VendorIDDD, appOrgCode)
// } else if vendorStoreID != "" {
// storeDetail, _ = dao.GetStoreDetailByVendorStoreID(dao.GetDB(), vendorStoreID, model.VendorIDDD, appOrgCode)
// }
// if storeDetail != nil {
// apiObj.SetToken(storeDetail.MtwmToken)
// }
//}
return apiObj
}
func getAPIWithoutToken(appOrgCode string) (apiObj *tiktok_api.API) {
return partner.CurAPIManager.GetAPI(model.VendorIDDD, appOrgCode).(*tiktok_api.API)
}