Files
jx-callback/business/partner/purchase/jdshop/store.go
2020-05-20 17:49:47 +08:00

175 lines
6.7 KiB
Go

package jdshop
import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
"git.rosy.net.cn/baseapi/platformapi/jdshopapi"
"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/jxutils/tasksch"
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/business/model/dao"
"git.rosy.net.cn/jx-callback/globals/api"
)
func (p *PurchaseHandler) ReadStore(ctx *jxcontext.Context, vendorOrgCode, vendorStoreID string) (storeDetail *dao.StoreDetail, err error) {
result, err := api.JdShopAPI.NewInfoList(utils.Str2Int64(vendorStoreID))
storeDetail.VendorStoreID = vendorStoreID
storeDetail.Status = JdsStatus2jxStatus(result.StoreStatus)
result2, err := api.JdShopAPI.QueryEntityStore(utils.Str2Int64(vendorStoreID))
storeDetail.ID = utils.Str2Int(result2.ExStoreID)
storeDetail.Name = result2.StoreName
storeDetail.Address = result2.Address
storeDetail.Tel1 = result2.Phone
storeDetail.DistrictCode = result2.AddrCode
zbs := strings.Split(result2.Coordinate, ",")
storeDetail.Lat = jxutils.StandardCoordinate2Int(utils.Str2Float64(zbs[0]))
storeDetail.Lng = jxutils.StandardCoordinate2Int(utils.Str2Float64(zbs[1]))
return storeDetail, err
}
// stoerIDs为nil表示所有
func (p *PurchaseHandler) UpdateStore(db *dao.DaoDB, storeID int, userName string) (err error) {
store, err := dao.GetStoreDetail(db, storeID, model.VendorIDJDShop)
if err != nil {
return err
}
data, _, err := jxutils.DownloadFileByURL(jdshopapi.JdsStoreImg)
timeMap := map[string]string{
"businessBeginTime": int2TimeStr(int(store.OpenTime1)),
"businessEndTime": int2TimeStr(int(store.CloseTime1)),
}
timeJSON, _ := json.Marshal(timeMap)
updateEntityStoreParam := &jdshopapi.UpdateEntityStoreParam{
StoreID: utils.Str2Int(store.VendorStoreID),
Name: store.Name,
AddCode: store.JdsCode,
AddCodeName: store.DistrictName,
AddName: store.ProvinceName + store.CityName + store.DistrictName + "@!" + store.Address,
Coordinate: utils.Float64ToStr(jxutils.IntCoordinate2Standard(store.Lat)) + "," + utils.Float64ToStr(jxutils.IntCoordinate2Standard(store.Lng)),
Phone: store.Tel1,
ExtendJSON: string(timeJSON),
ImageFile: base64.StdEncoding.EncodeToString(data),
CustomerID: utils.Int2Str(store.ID),
}
err = api.JdShopAPI.UpdateEntityStore(updateEntityStoreParam)
if err == nil {
if store.SyncStatus&(model.SyncFlagNewMask|model.SyncFlagStoreStatus) != 0 {
// mergeStatus := jxutils.MergeStoreStatus(store.Status, store.VendorStatus)
request, _ := http.NewRequest(http.MethodGet, "https://stores.shop.jd.com/stores/updateStoreStatus?storeId=24332466&storeStatus=1", nil)
c := &http.Cookie{
Name: "thor",
Value: "80FAF09E9A09B6E618A68057BDFCFCB8C86E8252DC9F7D3B34572625904FBA0AB6BF053A5325612EC0407791BB05F5301356E71E8B282C40C06D0B5DF3439DEECB102A78FAFF7AC0FC4E2D1FA8DD8BBAE1A011E50B5C74F1870AD982D7BF453F470F31F2241B73AC4C25485025C2ABEBC8A538AF7257824D2FAEE300A1435175B0B451FB5C19B78D729FC83152CA3BAF",
}
request.AddCookie(c)
client := &http.Client{}
response, _ := client.Do(request)
defer response.Body.Close()
bodyData, _ := ioutil.ReadAll(response.Body)
fmt.Println("test1", string(bodyData))
}
}
return err
}
func (p *PurchaseHandler) CreateStore2(db *dao.DaoDB, storeID int, userName string) (vendorStoreID string, err error) {
store, err := dao.GetStoreDetail(db, storeID, model.VendorIDJDShop)
if err != nil {
return vendorStoreID, err
}
data, _, err := jxutils.DownloadFileByURL(jdshopapi.JdsStoreImg)
timeMap := map[string]string{
"businessBeginTime": int2TimeStr(int(store.OpenTime1)),
"businessEndTime": int2TimeStr(int(store.CloseTime1)),
}
timeJSON, _ := json.Marshal(timeMap)
createEntityStoreParam := &jdshopapi.CreateEntityStoreParam{
Name: store.Name,
AddCode: store.JdsCode,
AddCodeName: store.DistrictName,
AddName: store.ProvinceName + store.CityName + store.DistrictName + "@!" + store.Address,
Coordinate: utils.Float64ToStr(jxutils.IntCoordinate2Standard(store.Lat)) + "," + utils.Float64ToStr(jxutils.IntCoordinate2Standard(store.Lng)),
Phone: store.Tel1,
ExtendJSON: string(timeJSON),
ImageFile: base64.StdEncoding.EncodeToString(data),
CategoryName: jdshopapi.JdsStoreCategoryName,
CustomerID: utils.Int2Str(store.ID),
}
vendorStoreID, err = api.JdShopAPI.CreateEntityStore(createEntityStoreParam)
return vendorStoreID, err
}
func (p *PurchaseHandler) DeleteStore(db *dao.DaoDB, storeID int, userName string) (err error) {
// store, err := dao.GetStoreDetail(db, storeID, model.VendorIDJDShop)
// if err != nil {
// return err
// }
// err = api.JdShopAPI.DeleteStoresByID(utils.Str2Int64(store.VendorStoreID))
return err
}
func (p *PurchaseHandler) RefreshAllStoresID(ctx *jxcontext.Context, parentTask tasksch.ITask, isAsync bool) (hint string, err error) {
return hint, err
}
func (p *PurchaseHandler) GetStoreStatus(ctx *jxcontext.Context, vendorOrgCode string, storeID int, vendorStoreID string) (storeStatus int, err error) {
return storeStatus, err
}
func (p *PurchaseHandler) EnableAutoAcceptOrder(ctx *jxcontext.Context, vendorOrgCode string, storeID int, vendorStoreID string, isSetEnable bool) (err error) {
return err
}
func (c *PurchaseHandler) UpdateStoreStatus(ctx *jxcontext.Context, vendorOrgCode string, storeID int, vendorStoreID string, status int) (err error) {
return err
}
func (c *PurchaseHandler) UpdateStoreOpTime(ctx *jxcontext.Context, vendorOrgCode string, storeID int, vendorStoreID string, opTimeList []int16) (err error) {
return err
}
func (c *PurchaseHandler) GetAllStoresVendorID(ctx *jxcontext.Context, vendorOrgCode string) (vendorStoreIDs []string, err error) {
return vendorStoreIDs, err
}
func (c *PurchaseHandler) UpdateStoreCustomID(ctx *jxcontext.Context, vendorOrgCode, vendorStoreID string, storeID int64) (err error) {
return err
}
func int2TimeStr(time int) (str string) {
str += utils.Int2Str(time / 1000)
str += utils.Int2Str(time % 1000 / 100)
str += ":"
str += utils.Int2Str(time % 100 / 10)
str += utils.Int2Str(time % 10)
return str
}
func jxStatus2JdsStatus(status int) (result int) {
if status == model.StoreStatusOpened {
result = jdshopapi.JdsStoreStatusOnline
} else if status == model.StoreStatusHaveRest || status == model.StoreStatusClosed {
result = jdshopapi.JdsStoreStatusRest
} else {
result = jdshopapi.JdsStoreStatusDisable
}
return result
}
func JdsStatus2jxStatus(status int) (result int) {
if status == jdshopapi.JdsStoreStatusOnline {
result = model.StoreStatusOpened
} else if status == jdshopapi.JdsStoreStatusRest {
result = model.StoreStatusClosed
} else {
result = model.StoreStatusDisabled
}
return result
}