109 lines
2.8 KiB
Go
109 lines
2.8 KiB
Go
package yl
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"github.com/bytedance/sonic"
|
|
"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
|
|
extendParam types.KeySendExtendParam
|
|
)
|
|
err = sonic.Unmarshal([]byte(req.ExtendParams), &extendParam)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("extendParam 格式错误")
|
|
}
|
|
|
|
reqData = transfer.MarketKeySendReq{
|
|
AppId: extendParam.AppId,
|
|
ReqCode: "voucher.create",
|
|
MemId: fmt.Sprintf("%d", req.SupplierId),
|
|
ReqSerialNo: req.DeliverOrderNo,
|
|
Timestamp: time.Unix(req.CreateTime, 0).Format("20060102150405"),
|
|
PosId: extendParam.PosId,
|
|
VoucherId: strconv.Itoa(int(req.SupplierSkuId)),
|
|
VoucherNum: extendParam.Num,
|
|
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())
|
|
}
|
|
|
|
var status = 1
|
|
if result.ErrCode != vo.RES_SUCCESS {
|
|
return nil, fmt.Errorf("请求失败:%v", result.Msg)
|
|
}
|
|
|
|
if result.Data == nil {
|
|
return nil, fmt.Errorf("请求失败:%v", result.Msg)
|
|
}
|
|
return &types.AsyncResp{
|
|
Msg: "",
|
|
Status: status,
|
|
SupplierOrderNo: result.Data.VoucherCode,
|
|
}, nil
|
|
|
|
}
|
|
|
|
func (l *YlAsyncLogic) getExchangeUrl(voucherCode string) string {
|
|
if strings.Contains(voucherCode, "http") {
|
|
return voucherCode
|
|
}
|
|
return fmt.Sprintf("%s%s", l.svcCtx.Config.Sys.Url, voucherCode)
|
|
}
|
|
|
|
// 往营销系统下单,返回数据,通知下游系统
|
|
func (l *YlAsyncLogic) asyncSendMarket(req *types.NotifyReq) (result *types.NotifyResp, err error) {
|
|
targetUrl := l.svcCtx.Config.YouleHost + "/supplier/order/sendResultNotify"
|
|
var header = map[string]string{"Content-Type": "application/json;charset=UTF-8"}
|
|
|
|
body, err := sonic.Marshal(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("请求失败:%v", err.Error())
|
|
}
|
|
resp, err := httpclient.FastHttpPost(targetUrl, header, body, 0)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("请求失败:%v", err.Error())
|
|
}
|
|
|
|
result = &types.NotifyResp{}
|
|
// todo 解析返回数据
|
|
err = sonic.Unmarshal(resp, result)
|
|
|
|
return
|
|
}
|