- new jd order handle ok, access jd api synchronously.
This commit is contained in:
17
business/jd/controller/controller.go
Normal file
17
business/jd/controller/controller.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"git.rosy.net.cn/baseapi/platform/jdapi"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var (
|
||||
gJdapi *jdapi.JDAPI
|
||||
sugarLogger *zap.SugaredLogger
|
||||
)
|
||||
|
||||
func init() {
|
||||
logger, _ := zap.NewDevelopment()
|
||||
sugarLogger = logger.Sugar()
|
||||
gJdapi = jdapi.NewJDAPI("91633f2a-c5f5-4982-a925-a220d19095c3", "1dba76d40cac446ca500c0391a0b6c9d", "a88d031a1e7b462cb1579f12e97fe7f4", logger)
|
||||
}
|
||||
71
business/jd/controller/order.go
Normal file
71
business/jd/controller/order.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
|
||||
"git.rosy.net.cn/jx-callback/business/jd/models"
|
||||
"git.rosy.net.cn/jx-callback/compat/corm"
|
||||
"github.com/astaxie/beego/orm"
|
||||
_ "github.com/go-sql-driver/mysql" // import your used driver
|
||||
)
|
||||
|
||||
var errChecker corm.DBErrorChecker
|
||||
|
||||
func init() {
|
||||
errChecker = new(corm.MysqlErrorChecker)
|
||||
|
||||
// set default database
|
||||
orm.RegisterDataBase("default", "mysql", "root:WebServer@1@tcp(127.0.0.1:3306)/jx-callback?charset=utf8&loc=Local", 30)
|
||||
|
||||
// register model
|
||||
orm.RegisterModel(new(models.Jdorder))
|
||||
|
||||
// create table
|
||||
orm.RunSyncdb("default", false, true)
|
||||
}
|
||||
|
||||
type OrderControler struct {
|
||||
}
|
||||
|
||||
func (c *OrderControler) NewOrder(order *models.NewOrderMsg) *models.OrderMsgResponse {
|
||||
db := orm.NewOrm()
|
||||
jdorderid, _ := strconv.ParseInt(order.BillId, 10, 64)
|
||||
status, _ := strconv.Atoi(order.StatusId)
|
||||
rec := &models.Jdorder{
|
||||
JdOrderId: jdorderid,
|
||||
OrderStatus: status,
|
||||
}
|
||||
|
||||
if created, _, err := db.ReadOrCreate(rec, "Jdorderid"); err == nil {
|
||||
if created {
|
||||
c.AcceptOrder(order)
|
||||
result, err := gJdapi.LegacyQuerySingleOrder(order.BillId)
|
||||
if err != nil {
|
||||
sugarLogger.Warnf("error when query jd order:%s, error:%v", order.BillId, err)
|
||||
} else {
|
||||
rec.Code = result.Code
|
||||
rec.Msg = result.Msg
|
||||
rec.Success = 1
|
||||
rec.CityName = "all"
|
||||
rec.OrderStatus = result.OrderStatus
|
||||
rec.OrderStatusTime = result.OrderStatusTime
|
||||
result.Msg = "成功"
|
||||
resultByteArr, _ := json.Marshal(result)
|
||||
rec.Data = string(resultByteArr)
|
||||
db.Update(rec, "Data", "Code", "Msg", "Success", "CityName", "OrderStatus", "OrderStatusTime")
|
||||
}
|
||||
|
||||
} else {
|
||||
sugarLogger.Warnf("duplicated jd orderid:%s", order.BillId)
|
||||
sugarLogger.Debug(rec)
|
||||
}
|
||||
} else {
|
||||
sugarLogger.Errorf("error when calling ReadOrCreate:%v", err)
|
||||
}
|
||||
|
||||
return &models.OrderMsgResponse{"0", "success", ""}
|
||||
}
|
||||
|
||||
func (c *OrderControler) AcceptOrder(order *models.NewOrderMsg) {
|
||||
}
|
||||
33
business/jd/models/order.go
Normal file
33
business/jd/models/order.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package models
|
||||
|
||||
// type CommonCallbackMsgFields struct {
|
||||
// Id int
|
||||
// Status int `orm:default(0)`
|
||||
// Count int `orm:default(1)`
|
||||
// CreatedAt time.Time `orm:"auto_now_add;type(datetime)"`
|
||||
// UpdatedAt time.Time `orm:"auto_now;type(datetime)"`
|
||||
// }
|
||||
|
||||
type OrderMsgResponse struct {
|
||||
Code string
|
||||
Msg string
|
||||
Data string
|
||||
}
|
||||
|
||||
type NewOrderMsg struct {
|
||||
BillId string
|
||||
StatusId string
|
||||
Timestamp string
|
||||
}
|
||||
|
||||
type Jdorder struct {
|
||||
Id int
|
||||
Code string `orm:"size(2)"`
|
||||
Msg string `orm:"size(100)"`
|
||||
Data string `orm:"type(text)"`
|
||||
Success int8
|
||||
JdOrderId int64 `orm:"unique;column(jdorderid)"`
|
||||
CityName string `orm:"size(20);column(cityname)"`
|
||||
OrderStatus int `orm:"column(orderstatus)"`
|
||||
OrderStatusTime string `orm:"size(50);column(orderstatustime)"`
|
||||
}
|
||||
Reference in New Issue
Block a user