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

80 lines
1.8 KiB
Go

package rs
import (
"context"
"encoding/json"
"fmt"
"trasfer_middleware/cmd/rpc/etc"
"trasfer_middleware/cmd/rpc/internal/logic/po"
"trasfer_middleware/cmd/rpc/internal/logic/po/zltx/types"
"trasfer_middleware/cmd/rpc/internal/logic/vo"
"trasfer_middleware/cmd/rpc/pb/transfer"
"trasfer_middleware/cmd/rpc/pkg/mq"
"trasfer_middleware/until/request"
"trasfer_middleware/until/sysLog"
)
type RS struct {
Conf *types.RSConf
}
type RsRequest struct {
ctx context.Context
*RS
*po.RequestStruct
}
func NewRs(conf types.RSConf) *RS {
return &RS{
Conf: &conf,
}
}
func (r *RS) SetData(c context.Context, data map[string]interface{}, config *etc.RockerMqConfig) *RsRequest {
requestBody := make(map[string]string, len(data))
for key, value := range data {
requestBody[key] = fmt.Sprintf("%v", value)
}
return &RsRequest{
ctx: c,
RS: r,
RequestStruct: &po.RequestStruct{
Config: *config,
RequestBody: requestBody,
},
}
}
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,
}
err := sendMq.Produce(r.ctx, r.Config.TopicPrefix+r.Config.Topic.RS.Name, po.SetMqSendData(r.RequestStruct, &resp, reqUrl))
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
}