Files
jx-callback/business/jxutils/jxutils.go
2018-07-18 22:52:30 +08:00

85 lines
2.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package jxutils
import (
"strings"
"sync"
"time"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/globals"
)
const (
DefaultOrderCacheTimeout = 24 * time.Hour
)
type SyncMapWithTimeout struct {
sync.Map
}
func (m *SyncMapWithTimeout) StoreWithTimeout(key, value interface{}, timeout time.Duration) {
m.Map.Store(key, value)
time.AfterFunc(timeout, func() {
m.Delete(key)
})
}
func (m *SyncMapWithTimeout) Store(key, value interface{}) {
m.StoreWithTimeout(key, value, DefaultOrderCacheTimeout)
}
func GetJxStoreIDFromOrder(order *model.GoodsOrder) (retVal int) {
if order.JxStoreID != 0 {
return order.JxStoreID
}
return order.StoreID
}
func SplitUniversalOrderID(universalOrderID string) (orderID string, vendorID int) {
index := strings.Index(universalOrderID, "|")
if index != -1 {
orderID = universalOrderID[:index]
vendorID = int(utils.Str2Int64(universalOrderID[index:]))
} else {
// 800402581000221 jd order
// 3022716176275221584 elm order
orderIDLen := len(universalOrderID)
if orderIDLen == len("800402581000221") {
vendorID = model.VendorIDJD
} else if orderIDLen == len("3022716176275221584") {
vendorID = model.VendorIDELM
} else {
globals.SugarLogger.Errorf("unkown order type:%v", universalOrderID)
vendorID = model.VendorIDUnknown
}
orderID = universalOrderID
}
return orderID, vendorID
}
func ComposeUniversalOrderID(orderID string, vendorID int) string {
// return fmt.Sprintf("%s|%d", orderID, vendorID)
return orderID // 当前用长度就能区分先不加上vendorID
}
func GetUniversalOrderIDFromWaybill(bill *model.Waybill) string {
return ComposeUniversalOrderID(bill.VendorOrderID, bill.OrderVendorID)
}
func GetUniversalOrderIDFromOrder(order *model.GoodsOrder) string {
return ComposeUniversalOrderID(order.VendorOrderID, order.VendorID)
}
func GetUniversalOrderIDFromOrderStatus(status *model.OrderStatus) string {
return ComposeUniversalOrderID(status.VendorOrderID, status.VendorID)
}
func GetRealTimeout(beginTime time.Time, timeout time.Duration) time.Duration {
retVal := beginTime.Add(timeout).Sub(time.Now())
if retVal < 0 {
retVal = 0
}
return retVal
}