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

100 lines
2.3 KiB
Go

package rs
import (
"context"
"encoding/json"
"fmt"
"github.com/jinzhu/copier"
"time"
"trasfer_middleware/cmd/rpc/etc"
"trasfer_middleware/cmd/rpc/internal/logic/po"
"trasfer_middleware/cmd/rpc/internal/logic/po/rs/types"
"trasfer_middleware/cmd/rpc/internal/logic/vo"
"trasfer_middleware/cmd/rpc/pb/transfer"
"trasfer_middleware/cmd/rpc/pkg/mq"
"trasfer_middleware/genModel"
"trasfer_middleware/until/common"
"trasfer_middleware/until/request"
"trasfer_middleware/until/sysLog"
)
type RS struct {
Conf *types.RSConf
}
type RsRequest struct {
ctx context.Context
*RS
*po.RequestStruct
Order *genModel.ServerOrderRs
}
type RequestData struct {
ctx context.Context
*RS
*po.RequestStruct
}
func NewRs(conf types.RSConf) *RS {
return &RS{
Conf: &conf,
}
}
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 {
requestBody[key] = fmt.Sprintf("%v", value)
}
return &RsRequest{
ctx: c,
RS: r,
RequestStruct: &po.RequestStruct{
Config: *config,
RequestBody: requestBody,
},
Order: &genModel.ServerOrderRs{
OutBizNo: in.OutBizNo,
VoucherNum: in.VendorNo,
Num: int64(in.Num),
ReqTime: time.Now(),
},
}
}
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.SetMqSendDataRs(r.RequestStruct, r.Order, &resp, url))
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
}