transfer_middleware/cmd/rpc/internal/logic/po/rs/coupon.go

100 lines
2.3 KiB
Go
Raw Normal View History

2024-06-18 16:34:14 +08:00
package rs
import (
"context"
"encoding/json"
"fmt"
2024-07-09 17:07:22 +08:00
"github.com/jinzhu/copier"
"time"
2024-06-18 16:34:14 +08:00
"trasfer_middleware/cmd/rpc/etc"
"trasfer_middleware/cmd/rpc/internal/logic/po"
2024-07-09 17:07:22 +08:00
"trasfer_middleware/cmd/rpc/internal/logic/po/rs/types"
2024-06-18 16:34:14 +08:00
"trasfer_middleware/cmd/rpc/internal/logic/vo"
"trasfer_middleware/cmd/rpc/pb/transfer"
"trasfer_middleware/cmd/rpc/pkg/mq"
2024-07-09 17:07:22 +08:00
"trasfer_middleware/genModel"
"trasfer_middleware/until/common"
2024-06-18 16:34:14 +08:00
"trasfer_middleware/until/request"
"trasfer_middleware/until/sysLog"
)
type RS struct {
Conf *types.RSConf
}
type RsRequest struct {
ctx context.Context
*RS
*po.RequestStruct
2024-07-09 17:07:22 +08:00
Order *genModel.ServerOrderRs
}
type RequestData struct {
ctx context.Context
*RS
*po.RequestStruct
2024-06-18 16:34:14 +08:00
}
func NewRs(conf types.RSConf) *RS {
return &RS{
Conf: &conf,
}
}
2024-07-09 17:07:22 +08:00
func (r *RS) SetData(c context.Context, in *transfer.RsCouponGrantReq, config *etc.RockerMqConfig) *RsRequest {
var reqData types.RsReq
_ = copier.Copy(reqData, &in)
reqDataMap := common.StructToMap(reqData)
requestBody := make(map[string]string, len(reqDataMap))
for key, value := range reqDataMap {
2024-06-18 16:34:14 +08:00
requestBody[key] = fmt.Sprintf("%v", value)
}
return &RsRequest{
ctx: c,
RS: r,
RequestStruct: &po.RequestStruct{
Config: *config,
RequestBody: requestBody,
},
2024-07-09 17:07:22 +08:00
Order: &genModel.ServerOrderRs{
OutBizNo: in.OutBizNo,
VoucherNum: in.VendorNo,
Num: int64(in.Num),
ReqTime: time.Now(),
},
2024-06-18 16:34:14 +08:00
}
}
func (r *RsRequest) request(url string) (*request.Response, error) {
reqUrl := fmt.Sprintf("%s%s", r.Conf.Host, url)
req := request.Request{
Method: "POST",
Url: reqUrl,
Data: r.RequestBody,
}
resp, _ := req.Send()
//异步存入请求记录
sendMq := mq.AliyunRocketMq{
AccessKey: r.Config.AccessKey,
SecretKey: r.Config.SecretKey,
SecurityToken: r.Config.SecurityToken,
ServerAddress: r.Config.Host,
}
2024-07-09 17:07:22 +08:00
err := sendMq.Produce(r.ctx, r.Config.TopicPrefix+r.Config.Topic.RS.Name, po.SetMqSendDataRs(r.RequestStruct, r.Order, &resp, url))
2024-06-18 16:34:14 +08:00
if err != nil {
sysLog.LogSendMq(r.ctx, err)
}
return &resp, err
}
func (r *RsRequest) CouponGrant() (*transfer.RsCouponGrantRes, error) {
var res transfer.RsCouponGrantRes
req, err := r.request(vo.RS_COUPON_GRANT)
if err != nil {
return nil, err
}
_ = json.Unmarshal([]byte(req.Text), &res)
return &res, nil
}