204 lines
7.4 KiB
Go
204 lines
7.4 KiB
Go
package jdshop
|
||
|
||
import (
|
||
"bytes"
|
||
"encoding/base64"
|
||
"encoding/hex"
|
||
"fmt"
|
||
"math"
|
||
"strings"
|
||
"time"
|
||
|
||
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
|
||
|
||
"git.rosy.net.cn/baseapi/platformapi/dingdingapi"
|
||
"git.rosy.net.cn/baseapi/platformapi/jcqapi"
|
||
"git.rosy.net.cn/baseapi/platformapi/jdshopapi"
|
||
"git.rosy.net.cn/baseapi/utils"
|
||
"git.rosy.net.cn/jx-callback/business/jxstore/common"
|
||
"git.rosy.net.cn/jx-callback/business/jxutils"
|
||
"git.rosy.net.cn/jx-callback/business/jxutils/ddmsg"
|
||
"git.rosy.net.cn/jx-callback/business/model"
|
||
"git.rosy.net.cn/jx-callback/business/model/dao"
|
||
"git.rosy.net.cn/jx-callback/business/partner"
|
||
"git.rosy.net.cn/jx-callback/globals"
|
||
"git.rosy.net.cn/jx-callback/globals/api"
|
||
)
|
||
|
||
func OnCallbackMsg(msg *jdshopapi.CallBackResult) (err error) {
|
||
msgType := msg.MsgType
|
||
switch msgType {
|
||
case jcqapi.TopicOrderPay:
|
||
utils.CallFuncAsync(func() {
|
||
SaveJdsOrders(msg)
|
||
})
|
||
case jcqapi.TopicOrderCancel:
|
||
utils.CallFuncAsync(func() {
|
||
order := getRealOrderID(msg.OrderID)
|
||
if order != nil {
|
||
if order.Status != model.OrderStatusCanceled {
|
||
CurPurchaseHandler.CancelOrder(jxcontext.AdminCtx, order, "系统取消")
|
||
}
|
||
}
|
||
})
|
||
default:
|
||
return fmt.Errorf("暂不支持的topic类型!topic: %v", msgType)
|
||
}
|
||
return err
|
||
}
|
||
|
||
func SaveJdsOrders(msg *jdshopapi.CallBackResult) (err error) {
|
||
order, err := result2Orders(msg)
|
||
if err != nil || order == nil {
|
||
return err
|
||
}
|
||
order.StoreID = 102919
|
||
order.JxStoreID = 102919
|
||
order.StoreName = "商城模板(成都发货)"
|
||
order.VendorStoreID = model.JdShopMainVendorStoreID
|
||
partner.CurOrderManager.OnOrderNew(order, model.Order2Status(order))
|
||
noticeMsg := fmt.Sprintf("京东商城新订单,订单号:[%v] ,将要发到的门店id:[%v] , 门店名:[%v]", order.VendorOrderID, order.StoreID, order.StoreName)
|
||
ddmsg.SendUserMessage(dingdingapi.MsgTyeText, "DDC5657B43EE11E9A9FF525400E86DC0", "京东商城来新订单了!", noticeMsg)
|
||
ddmsg.SendUserMessage(dingdingapi.MsgTyeText, "1439B3E07D3911EA881A525400E86DC0", "京东商城来新订单了!", noticeMsg)
|
||
return err
|
||
}
|
||
|
||
func result2Orders(msg *jdshopapi.CallBackResult) (order *model.GoodsOrder, err error) {
|
||
//有可能是库里已经有这个订单了
|
||
orderE, err := partner.CurOrderManager.LoadOrder(msg.OrderID+"000001", model.VendorIDJDShop)
|
||
if orderE != nil {
|
||
return order, fmt.Errorf("已经存在此订单!")
|
||
}
|
||
order = &model.GoodsOrder{
|
||
VendorOrderID2: msg.OrderID,
|
||
VendorOrderID: msg.OrderID + "000001",
|
||
VendorID: model.VendorIDJDShop,
|
||
BaseFreightMoney: jxutils.StandardPrice2Int(utils.Str2Float64(msg.FreightPrice)),
|
||
VendorStatus: msg.OrderState,
|
||
VendorUserID: msg.Pin,
|
||
BuyerComment: msg.OrderRemark,
|
||
PickDeadline: utils.DefaultTimeValue,
|
||
OriginalData: string(utils.MustMarshal(msg)),
|
||
OrderCreatedAt: utils.Str2Time(msg.OrderStartTime),
|
||
ConsigneeAddress: Decrypt(msg.ConsigneeInfo.FullAddress),
|
||
ConsigneeName: Decrypt(msg.ConsigneeInfo.Fullname),
|
||
ConsigneeMobile: Decrypt(msg.ConsigneeInfo.Mobile),
|
||
ConsigneeMobile2: Decrypt(msg.ConsigneeInfo.Telephone),
|
||
ActualPayPrice: jxutils.StandardPrice2Int(utils.Str2Float64(msg.OrderPayment)),
|
||
Status: model.OrderStatusNew,
|
||
TotalShopMoney: utils.Float64TwoInt64(math.Round(float64(jxutils.StandardPrice2Int(utils.Str2Float64(msg.OrderPayment))) * jdshopapi.JdsPayPercentage)),
|
||
DeliveryFlag: model.OrderDeliveryFlagMaskScheduleDisabled,
|
||
DeliveryType: model.OrderDeliveryTypeStoreSelf,
|
||
StatusTime: utils.Str2Time(msg.OrderStartTime),
|
||
OrderSeq: 0,
|
||
}
|
||
if order.TotalShopMoney < 100 {
|
||
order.TotalShopMoney = 100
|
||
}
|
||
if order.ConsigneeAddress != "" {
|
||
lng, lat, _ := api.AutonaviAPI.GetCoordinateFromAddress(order.ConsigneeAddress, "")
|
||
order.ConsigneeLng = jxutils.StandardCoordinate2Int(lng)
|
||
order.ConsigneeLat = jxutils.StandardCoordinate2Int(lat)
|
||
}
|
||
storeList, err := common.GetStoreListByLocation(jxcontext.AdminCtx, jxutils.IntCoordinate2Standard(order.ConsigneeLng), jxutils.IntCoordinate2Standard(order.ConsigneeLat), 5000, false, true)
|
||
if err != nil {
|
||
globals.SugarLogger.Debugf("jds GetStoreListByLocation error: %v", err.Error())
|
||
return order, err
|
||
}
|
||
order.StoreID = storeList[0].ID
|
||
order.StoreName = storeList[0].Name
|
||
globals.SugarLogger.Debugf("jds GetStoreListByLocation, orderID: %v storeID :%v", order.VendorOrderID, order.StoreID)
|
||
storeMaps, _ := dao.GetStoresMapList(dao.GetDB(), []int{model.VendorIDJDShop}, []int{order.StoreID}, nil, model.StoreStatusAll, model.StoreIsSyncAll, "", "")
|
||
if len(storeMaps) > 0 {
|
||
order.VendorStoreID = storeMaps[0].VendorStoreID
|
||
}
|
||
// 如果是暂停,表示是预订单
|
||
if msg.OrderState == jdshopapi.OrderStatusPause || msg.OrderState == jdshopapi.OrderStatusPopPause {
|
||
order.BusinessType = model.BusinessTypeDingshida
|
||
// order.ExpectedDeliveredTime = utils.Str2Time(orderDetail.ExpectedDeliveredTime)
|
||
order.PickDeadline = order.ExpectedDeliveredTime.Add(-time.Hour)
|
||
globals.SugarLogger.Warnf("来暂停的订单了!%v", msg.OrderID)
|
||
return nil, err
|
||
} else if msg.OrderState == jdshopapi.OrderStatusWait {
|
||
order.ExpectedDeliveredTime = order.OrderCreatedAt.Add(time.Hour)
|
||
order.BusinessType = model.BusinessTypeImmediate
|
||
} else {
|
||
globals.SugarLogger.Debugf("暂不支持的京东商城订单类型!type: %v", msg.OrderState)
|
||
return nil, err
|
||
}
|
||
|
||
//结算类型
|
||
storeDetail, _ := dao.GetStoreDetail(dao.GetDB(), order.StoreID, model.VendorIDJDShop)
|
||
if storeDetail != nil {
|
||
if storeDetail.PayPercentage < 50 {
|
||
order.EarningType = model.EarningTypePoints
|
||
} else {
|
||
order.EarningType = model.EarningTypeQuote
|
||
}
|
||
}
|
||
setJdsOrderSeq(order)
|
||
for _, v := range msg.ItemInfoList {
|
||
sku := &model.OrderSku{
|
||
VendorID: model.VendorIDJDShop,
|
||
VendorOrderID: order.VendorOrderID,
|
||
Count: utils.Str2Int(v.ItemTotal),
|
||
VendorSkuID: v.SkuID,
|
||
SkuName: v.SkuName,
|
||
VendorPrice: jxutils.StandardPrice2Int(utils.Str2Float64(v.JdPrice)),
|
||
SalePrice: jxutils.StandardPrice2Int(utils.Str2Float64(v.JdPrice)),
|
||
SkuID: utils.Str2Int(v.OuterSkuID),
|
||
}
|
||
_, _, _, specUnit, _, specQuality := jxutils.SplitSkuName(v.SkuName)
|
||
sku.Weight = jxutils.FormatSkuWeight(specQuality, specUnit)
|
||
order.Skus = append(order.Skus, sku)
|
||
}
|
||
return order, err
|
||
}
|
||
|
||
func setJdsOrderSeq(order *model.GoodsOrder) (err error) {
|
||
type tCount struct {
|
||
Count int `json:"count"`
|
||
}
|
||
var count = &tCount{}
|
||
sql := `
|
||
SELECT count(*) count FROM goods_order WHERE store_id = ? AND order_create_at >= ? AND order_create_at <= ? AND vendor_id = ?
|
||
`
|
||
sqlParams := []interface{}{
|
||
order.StoreID, utils.Time2Date(time.Now()), utils.Time2Date(time.Now().AddDate(0, 0, 1)), order.VendorID,
|
||
}
|
||
err = dao.GetRow(dao.GetDB(), &count, sql, sqlParams)
|
||
order.OrderSeq = count.Count + 1
|
||
return err
|
||
}
|
||
|
||
func Decrypt(p string) (result string) {
|
||
if p == "" {
|
||
return ""
|
||
}
|
||
data, _ := base64.StdEncoding.DecodeString(strings.ReplaceAll(p, " ", "+"))
|
||
key := GetKey(hex.EncodeToString(data)[4:36])
|
||
data2, _ := base64.StdEncoding.DecodeString(key)
|
||
b := bytes.NewBuffer(data)
|
||
b.Next(18)
|
||
iv := make([]byte, 16)
|
||
b.Read(iv)
|
||
main := make([]byte, len(data)-18-16)
|
||
b.Read(main)
|
||
decryptedData, _ := utils.AESCBCDecpryt(main, data2[:16], iv)
|
||
return string(decryptedData)
|
||
}
|
||
|
||
func getRealOrderID(orderID string) (order *model.GoodsOrder) {
|
||
var (
|
||
db = dao.GetDB()
|
||
)
|
||
sql := `
|
||
SELECT * FROM goods_order WHERE vendor_order_id2 = ? ORDER BY vendor_order_id DESC LIMIT 1
|
||
`
|
||
sqlParams := []interface{}{
|
||
orderID,
|
||
}
|
||
dao.GetRow(db, &order, sql, sqlParams)
|
||
return order
|
||
}
|