Files
jx-callback/business/jxutils/jxutils.go
gazebo a56cb64f8e - delivery fee added.
- fixed scheduler bug
2018-07-19 18:05:40 +08:00

104 lines
2.6 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 (
"fmt"
"math"
"math/rand"
"strings"
"sync"
"time"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/model"
)
const (
DefaultOrderCacheTimeout = 24 * time.Hour
)
type SyncMapWithTimeout struct {
sync.Map
}
func init() {
rand.Seed(time.Now().Unix())
}
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)
panic(fmt.Sprintf("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
}
func EarthDistance(lat1, lng1, lat2, lng2 float64) float64 {
radius := 6378.137
rad := math.Pi / 180.0
lat1 = lat1 * rad
lng1 = lng1 * rad
lat2 = lat2 * rad
lng2 = lng2 * rad
theta := lng2 - lng1
dist := math.Acos(math.Sin(lat1)*math.Sin(lat2) + math.Cos(lat1)*math.Cos(lat2)*math.Cos(theta))
return dist * radius
}