diff --git a/platformapi/jdapi/jdapi.go b/platformapi/jdapi/jdapi.go index 20a8679a..e3a341f1 100644 --- a/platformapi/jdapi/jdapi.go +++ b/platformapi/jdapi/jdapi.go @@ -6,6 +6,7 @@ import ( "encoding/json" "fmt" "net/http" + "reflect" "sort" "strings" @@ -411,3 +412,25 @@ func getErrMsgFromData(data map[string]interface{}) string { } return msg } + +func JavaDateHook(fromType reflect.Type, toType reflect.Type, data interface{}) (interface{}, error) { + if (fromType.Kind() >= reflect.Int && fromType.Kind() <= reflect.Uint64) || + fromType.Kind() == reflect.String { + if toType.Kind() == reflect.Ptr { + toType = toType.Elem() + } + if toType.Kind() == reflect.Struct && + toType.Name() == "JavaDate" { + if sec := utils.Interface2Int64WithDefault(data, 0); sec > 0 { + data = utils.NewJavaDateFromTime(utils.Timestamp2Time(sec)) + } else if fromType.Kind() == reflect.String { + data = utils.NewJavaDateFromTime(utils.Str2Time(data.(string))) + } + } + } + return data, nil +} + +func JdMap2StructByJson(inObj interface{}, outObjAddr interface{}, weaklyTypedInput bool) (err error) { + return utils.Map2Struct(inObj, outObjAddr, true, "", JavaDateHook) +} diff --git a/platformapi/jdapi/jdapi_test.go b/platformapi/jdapi/jdapi_test.go index 957260c2..f3ff6efe 100644 --- a/platformapi/jdapi/jdapi_test.go +++ b/platformapi/jdapi/jdapi_test.go @@ -101,3 +101,98 @@ func TestGenerateURL(t *testing.T) { } defer response.Body.Close() } + +func TestJdMap2StructByJson(t *testing.T) { + testStr := ` +{ + "stationNo": "11053496", + "stationName": "京西菜市-三平台测试门店4", + "outSystemId": "2", + "mobile": "18109051396", + "phone": "13684045765", + "lat": 34.224367, + "lng": 108.726791, + "city": 2376, + "county": 50231, + "stationAddress": "西安市长安区长安区", + "serviceTimeEnd1": 47, + "serviceTimeStart1": 1, + "closeStatus": 1, + "allowRangeOptimized": 0, + "cacheKey4StoreList": "_sn_11053496vi_82029p_2376c_2376co_50231oi_2wt_2sne_京西菜市-三平台测试门店4sa_西安市长安区长安区yn_0pe_2", + "carrierNo": 9966, + "cityName": "西安市", + "coordinate": "108.726791,34.224367", + "coordinateAddress": "韩南村", + "countyName": "长安区", + "createPin": "jd_jxcs8285", + "createTime": { + "day": 4, + "hours": 13, + "minutes": 33, + "seconds": 22, + "time": 1470893602321, + "timezoneOffset": -480, + "date": 11, + "month": 7, + "year": 116 + }, + "id": 123108, + "industryTag": 3, + "innerNoStatus": 1, + "isAutoOrder": 0, + "isMembership": 0, + "isNoPaper": 2, + "onlineTime": 1470893602123, + "orderAging": 60, + "orderNoticeType": 1, + "preWarehouse": 2, + "province": 2376, + "provinceName": "西安市", + "qualifyStatus": 2, + "regularFlag": 3, + "stationDeliveryStatus": 2, + "supportInvoice": 0, + "supportOfflinePurchase": 0, + "testMark": 0, + "timeAmType": 1, + "timePmType": 0, + "ts": { + "day": 2, + "hours": 16, + "minutes": 22, + "seconds": 13, + "time": 1571127733000, + "timezoneOffset": -480, + "date": 15, + "month": 9, + "year": 119 + }, + "updatePin": "jxadmin-zhouyang", + "updateTime": 1571127733, + "venderId": "82029", + "venderName": "测试京西菜市", + "wareType": 2, + "whiteDelivery": false, + "yn": 0 +} +` + var rawData interface{} + err := utils.UnmarshalUseNumber([]byte(testStr), &rawData) + if err != nil { + t.Fatal(err) + } + + var store *StoreDetail + err = JdMap2StructByJson(rawData, &store, true) + if err != nil { + t.Fatal(err) + } + if store.CreateTime.GoTime().Unix() != 1470893602 || + store.OnlineTime.GoTime().Unix() != 1470893602 || + store.UpdateTime.GoTime().Unix() != 1571127733 { + t.Fatalf("%d,%d,%d", store.CreateTime.GoTime().Unix(), store.OnlineTime.GoTime().Unix(), store.UpdateTime.GoTime().Unix()) + } + t.Log(utils.Format4Output(store, false)) + t.Log(store.CreateTime) +} diff --git a/platformapi/jdapi/order.go b/platformapi/jdapi/order.go index ee7fa960..e10ec6bf 100644 --- a/platformapi/jdapi/order.go +++ b/platformapi/jdapi/order.go @@ -399,7 +399,7 @@ func (a *API) QuerySingleOrder(orderId string) (map[string]interface{}, error) { func (a *API) OrderQuery2(queryParam *OrderQueryParam) (retVal []*OrderInfo, totalCount int, err error) { orderList, totalCount, err := a.OrderQuery(utils.Struct2MapByJson(queryParam)) if err == nil { - err = utils.Map2StructByJson(orderList, &retVal, true) + err = JdMap2StructByJson(orderList, &retVal, true) } return retVal, totalCount, err } @@ -506,7 +506,7 @@ func (a *API) OrderShoudSettlementService(orderId string) (map[string]interface{ func (a *API) OrderShoudSettlementService2(orderId string) (orderSettlement *OrderSettlementInfo, err error) { result, err := a.OrderShoudSettlementService(orderId) if err == nil { - err = utils.Map2StructByJson(result, &orderSettlement, false) + err = JdMap2StructByJson(result, &orderSettlement, false) } return orderSettlement, err } @@ -527,7 +527,7 @@ func (a *API) GetAfsService(orderId string) (map[string]interface{}, error) { func (a *API) GetAfsService2(afsOrderID string) (afsOrderInfo *AfsServiceResponse, err error) { result, err := a.GetAfsService(afsOrderID) if err == nil { - if err = utils.Map2StructByJson(result, &afsOrderInfo, false); err != nil { + if err = JdMap2StructByJson(result, &afsOrderInfo, false); err != nil { baseapi.SugarLogger.Warnf("GetAfsService2, result:%s failed with error:%v", utils.Format4Output(result, true), err) } } @@ -703,7 +703,7 @@ func (a *API) GetByOrderNoForOaos(orderNo string) (orderTrackList []*OrderTrack, } result, err := a.AccessAPINoPage("orderTrace/getByOrderNoForOaos", jdParams, nil, nil, genNoPageResultParser("code", "msg", "orderTrackList", "0")) if err == nil { - err = utils.Map2StructByJson(result, &orderTrackList, false) + err = JdMap2StructByJson(result, &orderTrackList, false) } return orderTrackList, err } @@ -716,7 +716,7 @@ func (a *API) GetByOrderNoForOaosNew(orderID string) (orderTrackList []*OrderTra } result, err := a.AccessAPINoPage("orderTrace/getByOrderNoForOaosNew", jdParams, nil, nil, genNoPageResultParser("code", "detail", "result", "0")) if err == nil { - err = utils.Map2StructByJson(result.(map[string]interface{})["orderTrackList"], &orderTrackList, false) + err = JdMap2StructByJson(result.(map[string]interface{})["orderTrackList"], &orderTrackList, false) } return orderTrackList, err } diff --git a/platformapi/jdapi/promotion_audit.go b/platformapi/jdapi/promotion_audit.go index a0c9d2a2..d04ac291 100644 --- a/platformapi/jdapi/promotion_audit.go +++ b/platformapi/jdapi/promotion_audit.go @@ -89,7 +89,7 @@ func (a *API) OrderDiscountQueryActivityInfoById(activityID int64, promotionType } result, err := a.AccessAPINoPage("orderdiscount/queryActivityInfoById", params, nil, nil, genNoPageResultParser("errorCode", "errorInfos", "data", "10000")) if err == nil { - err = utils.Map2StructByJson(result, &response, false) + err = JdMap2StructByJson(result, &response, false) } return response, err } @@ -102,7 +102,7 @@ func (a *API) QueryPromotionInfo(promotionInfoId int64) (promotionInfo *Promotio } result, err := a.AccessAPINoPage("singlePromote/queryPromotionInfo", jdParams, nil, nil, genNoPageResultParser("errorCode", "errorInfos", "data", "0")) if err == nil { - err = utils.Map2StructByJson(result, &promotionInfo, false) + err = JdMap2StructByJson(result, &promotionInfo, false) } return promotionInfo, err } @@ -117,7 +117,7 @@ func (a *API) QueryPromotionSku(promotionType int, skuID int64, promotionState i } result, _, err := a.AccessAPIHavePage("singlePromote/queryPromotionSku", jdParams, nil, nil, genNormalHavePageResultParser("data")) if err != nil { - err = utils.Map2StructByJson(result, &skuResultList, false) + err = JdMap2StructByJson(result, &skuResultList, false) } return skuResultList, err } diff --git a/platformapi/jdapi/promotion_order.go b/platformapi/jdapi/promotion_order.go index 4491d12c..12d1b0f2 100644 --- a/platformapi/jdapi/promotion_order.go +++ b/platformapi/jdapi/promotion_order.go @@ -125,7 +125,7 @@ func (a *API) OrderDiscountQuerySubmitActivityResult(activityID int64) (response "traceId": utils.GetUUID(), }, nil, nil, genNoPageResultParser("code", "detail", "result", "0")) if err == nil { - err = utils.Map2StructByJson(result.([]interface{})[0], &response, true) // todo,只取第一个? + err = JdMap2StructByJson(result.([]interface{})[0], &response, true) // todo,只取第一个? } return response, err } @@ -141,7 +141,7 @@ func (a *API) OrderDiscountQueryActivityInfo(activityID int64) (response *Activi "version": "", }, nil, nil, genNoPageResultParser("code", "detail", "result", "0")) if err == nil { - err = utils.Map2StructByJson(result, &response, true) + err = JdMap2StructByJson(result, &response, true) } return response, err } diff --git a/platformapi/jdapi/promotion_sku.go b/platformapi/jdapi/promotion_sku.go index a8ba1bff..bb60dd4a 100644 --- a/platformapi/jdapi/promotion_sku.go +++ b/platformapi/jdapi/promotion_sku.go @@ -191,7 +191,7 @@ func (a *API) createPromotionSku(promotionType int, infoId int64, outInfoId stri // todo 当前是在出错时,把data中的数据当成错误信息处理的 result, err := a.AccessAPINoPage(getPromotionCmd("createPromotionSku", promotionType), jdParams, nil, nil, genNoPageResultParser("errorCode", "data", "", "0")) if err == nil && result != nil { - err = utils.Map2StructByJson(result, &skusResult, false) + err = JdMap2StructByJson(result, &skusResult, false) } return skusResult, err } @@ -227,7 +227,7 @@ func (a *API) adjustPromotionSku(promotionType int, infoId int64, outInfoId stri jdParams["skus"] = skus result, err := a.AccessAPINoPage(getPromotionCmd("adjustPromotionSku", promotionType), jdParams, nil, nil, genNoPageResultParser("errorCode", "data", "", "0")) if err == nil && result != nil { - err = utils.Map2StructByJson(result, &skusResult, false) + err = JdMap2StructByJson(result, &skusResult, false) } return skusResult, err } diff --git a/platformapi/jdapi/sku.go b/platformapi/jdapi/sku.go index abb122b4..5de8be91 100644 --- a/platformapi/jdapi/sku.go +++ b/platformapi/jdapi/sku.go @@ -383,7 +383,7 @@ func (a *API) QuerySkuInfos(queryParam *QuerySkuParam) (skuList []*SkuMain, tota } result, totalCount, err := a.AccessAPIHavePage("pms/querySkuInfos", utils.Struct2MapByJson(queryParam), nil, nil, nil) if err == nil { - err = utils.Map2StructByJson(result, &skuList, false) + err = JdMap2StructByJson(result, &skuList, false) } return skuList, totalCount, err } @@ -393,7 +393,7 @@ func (a *API) QuerySkuInfos(queryParam *QuerySkuParam) (skuList []*SkuMain, tota func (a *API) QueryListBySkuIds(queryParam *QueryListBySkuIdsParam) (imgList []*ImgHandleQueryResult, err error) { result, err := a.AccessAPINoPage("order/queryListBySkuIds", utils.Struct2MapByJson(queryParam), nil, nil, nil) if err == nil { - err = utils.Map2StructByJson(result, &imgList, false) + err = JdMap2StructByJson(result, &imgList, false) } return imgList, err } diff --git a/platformapi/jdapi/store.go b/platformapi/jdapi/store.go index 6dc12d5d..06aff9d7 100644 --- a/platformapi/jdapi/store.go +++ b/platformapi/jdapi/store.go @@ -259,7 +259,7 @@ type UpdateStoreFreightParam struct { func (a *API) GetAllCities() (cities []*CityInfo, err error) { result, err := a.AccessAPINoPage("address/allcities", nil, nil, nil, genNoPageResultParser("code", "msg", "result", "0")) if err == nil { - err = utils.Map2StructByJson(result, &cities, false) + err = JdMap2StructByJson(result, &cities, false) } return cities, err } @@ -311,7 +311,7 @@ func (a *API) CreateStore(createParams *OpStoreParams) (*CreateShopResult, error func (a *API) GetStoreInfoByStationNo2(storeNo string) (storeDetail *StoreDetail, err error) { result, err := a.AccessAPINoPage("storeapi/getStoreInfoByStationNo", utils.Params2Map("StoreNo", storeNo), nil, nil, nil) if err == nil { - err = utils.Map2StructByJson(result, &storeDetail, false) + err = JdMap2StructByJson(result, &storeDetail, false) } return storeDetail, err } @@ -357,7 +357,7 @@ func (a *API) GetCommentByOrderId2(orderId int64) (orderComment *OrderCommentInf } result, err := a.AccessAPINoPage("commentOutApi/getCommentByOrderId", jdParams, nil, nil, genNoPageResultParser("code", "msg", "result", "200")) if err == nil { - err = utils.Map2StructByJson(result, &orderComment, false) + err = JdMap2StructByJson(result, &orderComment, false) } return orderComment, err } @@ -413,7 +413,7 @@ func (a *API) GetDeliveryRangeByStationNo2(stationNo string) (deliveryRange *Sto } result, err := a.AccessAPINoPage("store/getDeliveryRangeByStationNo", jdParams, nil, nil, nil) if err == nil { - err = utils.Map2StructByJson(result, &deliveryRange, false) + err = JdMap2StructByJson(result, &deliveryRange, false) } return deliveryRange, err } diff --git a/platformapi/jdapi/store_page.go b/platformapi/jdapi/store_page.go index c2d91655..27f5a35a 100644 --- a/platformapi/jdapi/store_page.go +++ b/platformapi/jdapi/store_page.go @@ -389,7 +389,7 @@ func (a *API) GetSkuPageInfo(skuId int64) (skuPageInfo *PageSku, err error) { result, err := a.AccessStorePage("https://daojia.jd.com/client", params, false) if err == nil { - err = utils.Map2StructByJson(result, &skuPageInfo, false) + err = JdMap2StructByJson(result, &skuPageInfo, false) } return skuPageInfo, err } @@ -419,7 +419,7 @@ func (a *API) GetStoreInfo(storeId string) (storeInfo map[string]interface{}, er func (a *API) GetStoreInfo2(storeID string) (storeInfo *PageShopInfo, err error) { retVal, err := a.GetStoreInfo(storeID) if err == nil { - err = utils.Map2StructByJson(retVal, &storeInfo, false) + err = JdMap2StructByJson(retVal, &storeInfo, false) } return storeInfo, err } @@ -461,7 +461,7 @@ func (a *API) GetCorporationInfo(stationNo, qualifyNumber string) (corporatonInf "qualifyNumber": qualifyNumber, }, true) if err == nil { - err = utils.Map2StructByJson(result, &corporatonInfo, false) + err = JdMap2StructByJson(result, &corporatonInfo, false) } return corporatonInfo, err } diff --git a/platformapi/jdapi/store_sku.go b/platformapi/jdapi/store_sku.go index 2784e4a2..86b09b7c 100644 --- a/platformapi/jdapi/store_sku.go +++ b/platformapi/jdapi/store_sku.go @@ -144,14 +144,14 @@ func (a *API) GetStationInfoList(stationNo string, skuIds []int64) (priceInfo [] } result, err := a.AccessAPINoPage("price/getStationInfoList", jdParams, nil, nil, genNoPageResultParser("code", "detail", "result", "0")) if err == nil && result != nil { - err = utils.Map2StructByJson(result, &priceInfo, false) + err = JdMap2StructByJson(result, &priceInfo, false) } return priceInfo, err } func (a *API) handleBatchOpResult(outStationNo, stationNo string, batchCount int, inErr error, result interface{}, tagName string) (responseList []*StoreSkuBatchUpdateResponse, err error) { if result != nil { - if err = utils.Map2Struct(result, &responseList, true, tagName); err == nil { + if err = utils.Map2Struct(result, &responseList, true, tagName, JavaDateHook); err == nil { var failedList []*StoreSkuBatchUpdateResponse for _, v := range responseList { if v.Code != 0 { @@ -287,7 +287,7 @@ func (a *API) QueryOpenUseable(listBaseStockCenterRequest []*BaseStockCenterRequ } result, err := a.AccessAPINoPage("stock/queryOpenUseable", jdParams, nil, nil, genNoPageResultParser("retCode", "retMsg", "data", "0")) if err == nil && result != nil { - err = utils.Map2StructByJson(result, &stockResponse, false) + err = JdMap2StructByJson(result, &stockResponse, false) } return stockResponse, err } @@ -302,7 +302,7 @@ func (a *API) QueryStockCenter(outStationNo string, skuIds []*SkuIdEntity, userP } result, err := a.AccessAPINoPage("stock/queryStockCenter", jdParams, nil, nil, genNoPageResultParser("retCode", "retMsg", "data", "0")) if err == nil && result != nil { - err = utils.Map2StructByJson(result, &vendibilityResponse, false) + err = JdMap2StructByJson(result, &vendibilityResponse, false) } return vendibilityResponse, err } diff --git a/utils/java_date.go b/utils/java_date.go index 696f65ab..90c5d954 100644 --- a/utils/java_date.go +++ b/utils/java_date.go @@ -45,3 +45,7 @@ func (j *JavaDate) GoTime() time.Time { } return Timestamp2Time(j.Time / 1000) } + +func (j *JavaDate) String() string { + return Timestamp2Str(j.Time) +} diff --git a/utils/typeconv.go b/utils/typeconv.go index bca6b810..d24f4a77 100644 --- a/utils/typeconv.go +++ b/utils/typeconv.go @@ -17,6 +17,8 @@ import ( "github.com/gazeboxu/structs" ) +const MaxTimeSecond = 9573800254 // 正常最大的秒数 + var ( DefaultTimeValue = Str2Time("1970-01-01 00:00:00") ZeroTimeValue = time.Time{} @@ -277,13 +279,14 @@ func Timestamp2Str(timestamp int64) string { return Time2Str(Timestamp2Time(timestamp)) } +// timestamp为秒或毫秒 func Timestamp2Time(timestamp int64) time.Time { - const normalTimestamp = 1533709322 - if timestamp > normalTimestamp*100 { // 传成毫秒了 - baseapi.SugarLogger.Errorf("Timestamp2Time wrong timestamp:%d", timestamp) - timestamp = timestamp / 1000 + nsec := int64(0) + if timestamp > MaxTimeSecond { // 传成毫秒了 + nsec = (timestamp % 1000) * 1000000 + timestamp /= 1000 } - return time.Unix(timestamp, 0) + return time.Unix(timestamp, nsec) } func Time2Str(t time.Time) string { @@ -472,20 +475,24 @@ func Struct2FlatMap(obj interface{}) map[string]interface{} { } // !!! 此函数好像不支持struct是内嵌结构的 -func Map2Struct(inObj interface{}, outObjAddr interface{}, weaklyTypedInput bool, tagName string) (err error) { +func Map2Struct(inObj interface{}, outObjAddr interface{}, weaklyTypedInput bool, tagName string, decodeHook interface{}) (err error) { if tagName == "" { tagName = "json" } + if !weaklyTypedInput { + weaklyTypedInput = decodeHook != nil + } decoder, _ := mapstructure.NewDecoder(&mapstructure.DecoderConfig{ TagName: tagName, Result: outObjAddr, WeaklyTypedInput: weaklyTypedInput, + DecodeHook: decodeHook, }) return decoder.Decode(inObj) } func Map2StructByJson(inObj interface{}, outObjAddr interface{}, weaklyTypedInput bool) (err error) { - return Map2Struct(inObj, outObjAddr, weaklyTypedInput, "") + return Map2Struct(inObj, outObjAddr, weaklyTypedInput, "", nil) } func Int64Slice2String(intList []int64) (outList []string) {