133 lines
3.1 KiB
Go
133 lines
3.1 KiB
Go
package controller
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/astaxie/beego/orm"
|
|
|
|
"git.rosy.net.cn/jx-callback/business/model"
|
|
|
|
"git.rosy.net.cn/baseapi/utils"
|
|
"git.rosy.net.cn/baseapi/utils/routinepool"
|
|
"git.rosy.net.cn/jx-callback/globals"
|
|
)
|
|
|
|
const (
|
|
VenderIDUnknown = -1
|
|
VendorIDPurchaseBegin = 0
|
|
VendorIDJD = 0
|
|
VendorIDMTWM = 1
|
|
VendorIDELM = 2
|
|
VendorIDPurchaseEnd = 2
|
|
|
|
VendorIDDeliveryBegin = 101
|
|
VendorIDDada = 101
|
|
VendorIDMTPS = 102
|
|
VendorIDDeliveryEnd = 102
|
|
)
|
|
|
|
const (
|
|
OrderTypeOrder = 1
|
|
OrderTypeWaybill = 2
|
|
)
|
|
|
|
const (
|
|
CoordinateTypeMars = 0 // 火星坐标
|
|
CoordinateTypeReal = 1 // 真实坐标
|
|
)
|
|
|
|
const (
|
|
DefaultOrderCacheTimeout = 24 * time.Hour
|
|
)
|
|
|
|
var (
|
|
DefaultTimeValue = utils.Str2Time("1970-01-01 00:00:00")
|
|
OrderManager *OrderController
|
|
WaybillManager *WaybillController
|
|
|
|
routinePool *routinepool.Pool
|
|
)
|
|
|
|
type SyncMapWithTimeout struct {
|
|
sync.Map
|
|
}
|
|
|
|
func init() {
|
|
routinePool = routinepool.New(1000, 1000)
|
|
OrderManager = NewOrderManager()
|
|
WaybillManager = NewWaybillManager()
|
|
}
|
|
|
|
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 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 = VendorIDJD
|
|
} else if orderIDLen == len("3022716176275221584") {
|
|
vendorID = VendorIDELM
|
|
} else {
|
|
globals.SugarLogger.Errorf("unkown order type:%v", universalOrderID)
|
|
vendorID = VenderIDUnknown
|
|
}
|
|
orderID = universalOrderID
|
|
}
|
|
return orderID, vendorID
|
|
}
|
|
|
|
func ComposeUniversalOrderID(orderID string, vendorID int) string {
|
|
return fmt.Sprintf("%s|%d", orderID, vendorID)
|
|
}
|
|
|
|
func StandardCoordinate2Int(value float64) int {
|
|
return int(value * 1000000)
|
|
}
|
|
|
|
func IntCoordinate2Standard(value int) float64 {
|
|
return float64(value / 1000000)
|
|
}
|
|
|
|
func addOrderOrWaybillStatus(status *model.OrderStatus, db orm.Ormer) (isDuplicated bool, err error) {
|
|
status.ID = 0
|
|
created, _, err := db.ReadOrCreate(status, "VendorOrderID", "VendorID", "OrderType", "VendorStatus", "StatusTime")
|
|
if err == nil {
|
|
if !created {
|
|
isDuplicated = true
|
|
status.DuplicatedCount++
|
|
_, err = db.Update(status, "DuplicatedCount")
|
|
globals.SugarLogger.Infof("duplicated event:%v", status)
|
|
}
|
|
}
|
|
if err != nil {
|
|
// todo 这里居然会有主键重复错误,逻辑上是不应该的
|
|
globals.SugarLogger.Warnf("access db error:%v", err)
|
|
}
|
|
return isDuplicated, err
|
|
}
|
|
|
|
func CallMsgHandler(handler func(), primaryID string) {
|
|
handler()
|
|
// controller.routinePool.CallFun(func() {
|
|
// handler()
|
|
// }, primaryID)
|
|
}
|