156 lines
3.9 KiB
Go
156 lines
3.9 KiB
Go
package yl
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"fmt"
|
||
"github.com/bytedance/sonic"
|
||
"rs/untils/sign"
|
||
|
||
"rs/cmd/api/internal/logic/vo"
|
||
"rs/rpc/transfer"
|
||
"rs/untils/httpclient"
|
||
"strconv"
|
||
"strings"
|
||
"time"
|
||
|
||
"rs/cmd/api/internal/svc"
|
||
"rs/cmd/api/internal/types"
|
||
|
||
"github.com/zeromicro/go-zero/core/logx"
|
||
)
|
||
|
||
type YlAsyncLogic struct {
|
||
logx.Logger
|
||
ctx context.Context
|
||
svcCtx *svc.ServiceContext
|
||
}
|
||
|
||
// 卡密异步发放接口
|
||
func NewYlAsyncLogic(ctx context.Context, svcCtx *svc.ServiceContext) *YlAsyncLogic {
|
||
return &YlAsyncLogic{
|
||
Logger: logx.WithContext(ctx),
|
||
ctx: ctx,
|
||
svcCtx: svcCtx,
|
||
}
|
||
}
|
||
|
||
func (l *YlAsyncLogic) YlAsync(req *types.AsyncReq) (resp *types.AsyncResp, err error) {
|
||
var (
|
||
reqData transfer.MarketKeySendReq
|
||
VoucherId string
|
||
)
|
||
|
||
if v, ok := l.svcCtx.Config.MarketConfig.SupplierSkuId[strconv.Itoa(int(req.SupplierSkuId))]; ok {
|
||
VoucherId = v
|
||
} else {
|
||
return nil, fmt.Errorf("供货商sku 未配置")
|
||
}
|
||
|
||
reqData = transfer.MarketKeySendReq{
|
||
AppId: l.svcCtx.Config.MarketConfig.AppId,
|
||
ReqCode: "voucher.create",
|
||
MemId: fmt.Sprintf("%d", req.SupplierId),
|
||
ReqSerialNo: req.DeliverOrderNo,
|
||
Timestamp: time.Unix(req.CreateTime, 0).Format("20060102150405"),
|
||
PosId: l.svcCtx.Config.MarketConfig.PosId,
|
||
VoucherId: VoucherId,
|
||
VoucherNum: 1,
|
||
//MobileNo: extendParam.MobileNo,
|
||
//SendMsg: extendParam.SendMsg,
|
||
}
|
||
if reqData.SendMsg == "" {
|
||
reqData.SendMsg = "2"
|
||
}
|
||
|
||
reqData.Sign = l.svcCtx.Config.Sys.PrimaryKey
|
||
result, err := l.svcCtx.TransferRpc.MarketKeySend(l.ctx, &reqData)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("rpc请求失败:%v", err.Error())
|
||
}
|
||
|
||
if result.ErrCode != vo.RES_SUCCESS {
|
||
return nil, fmt.Errorf("请求失败:%v", result.Msg)
|
||
}
|
||
|
||
if result.Data == nil {
|
||
return nil, fmt.Errorf("请求失败:%v", result.Msg)
|
||
}
|
||
|
||
supplierOrderNo := l.getExchangeCode(result.Data.VoucherCode)
|
||
if supplierOrderNo == "" {
|
||
return nil, fmt.Errorf("请求失败未取到数据:%v", result.Msg)
|
||
}
|
||
// 异步通知
|
||
go func() {
|
||
time.Sleep(2 * time.Second)
|
||
l.asyncSendMarket(supplierOrderNo, req, result.Data)
|
||
}()
|
||
|
||
return &types.AsyncResp{
|
||
Msg: "",
|
||
Status: 0,
|
||
SupplierOrderNo: supplierOrderNo,
|
||
}, nil
|
||
|
||
}
|
||
|
||
func (l *YlAsyncLogic) getExchangeCode(voucherCode string) string {
|
||
if !strings.Contains(voucherCode, "http") {
|
||
return voucherCode
|
||
}
|
||
temp := strings.Split(voucherCode, "/")
|
||
return temp[len(temp)-1]
|
||
}
|
||
|
||
// 往营销系统下单,返回数据,通知下游系统
|
||
func (l *YlAsyncLogic) asyncSendMarket(supplierOrderNo string, asyncReq *types.AsyncReq, data *transfer.MarketKeySendRes_Data) {
|
||
var (
|
||
reqDataMap map[string]interface{}
|
||
header = map[string]string{"Content-Type": "application/json;charset=UTF-8"}
|
||
targetUrl = l.svcCtx.Config.YouleHost + "/supplier/order/sendResultNotify"
|
||
req = &types.NotifyReq{
|
||
DeliverOrderNo: asyncReq.DeliverOrderNo,
|
||
SupplierOrderNo: supplierOrderNo,
|
||
SupplierSkuId: asyncReq.SupplierSkuId,
|
||
RequestTime: time.Now().Unix(),
|
||
Account: asyncReq.Account,
|
||
Status: 2,
|
||
Msg: "",
|
||
Price: asyncReq.Price,
|
||
SupplierId: asyncReq.SupplierId,
|
||
CardNo: "",
|
||
CardKey: "",
|
||
CardExpireTime: "",
|
||
CardExchangeUrl: data.ShortUrl,
|
||
Sign: "",
|
||
}
|
||
)
|
||
|
||
b, _ := json.Marshal(req)
|
||
err := json.Unmarshal(b, &reqDataMap)
|
||
if err != nil {
|
||
return
|
||
}
|
||
req.Sign, err = sign.GetSign(reqDataMap, l.svcCtx.Config.Sys.PrimaryKey)
|
||
if err != nil {
|
||
l.Logger.Errorf("签名失败:%v", err.Error())
|
||
return
|
||
}
|
||
|
||
body, err := sonic.Marshal(req)
|
||
if err != nil {
|
||
l.Logger.Errorf("解析json失败:%v", err.Error())
|
||
return
|
||
}
|
||
resp, err := httpclient.FastHttpPost(targetUrl, header, body, 0)
|
||
if err != nil {
|
||
l.Logger.Errorf("请求失败:%v", err.Error())
|
||
return
|
||
}
|
||
|
||
l.Logger.Infof("发货通知完成:url=%s,请求数据:%v 请求返回数据:%v", targetUrl, string(body), string(resp))
|
||
|
||
return
|
||
}
|