|
|
package s2s
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
"joybuywork/internal/plugins/jsonm"
|
|
|
"joybuywork/internal/repository/model"
|
|
|
"joybuywork/internal/repository/mtype"
|
|
|
"joybuywork/internal/repository/withdefault"
|
|
|
"fmt"
|
|
|
"time"
|
|
|
|
|
|
"joybuywork/proto/go_proto/workerr"
|
|
|
)
|
|
|
|
|
|
type UserCoinChange struct {
|
|
|
S2S
|
|
|
|
|
|
user *model.ProjectUser
|
|
|
log *model.ProjectUserCoinLog
|
|
|
}
|
|
|
|
|
|
func NewUserCoinChange(ctx context.Context, user *model.ProjectUser, urlId int64, opts ...Option) (*UserCoinChange, error) {
|
|
|
c := &UserCoinChange{
|
|
|
user: user,
|
|
|
}
|
|
|
|
|
|
err := c.init(ctx, user.ProjectID, urlId, opts...)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
|
|
|
return c, nil
|
|
|
}
|
|
|
|
|
|
func (s *UserCoinChange) CenterExchange(num int32, deductId, mallItemId, centerId int64, title, centerHashId, langMark string) (res *PointsChangeRespData, err error) {
|
|
|
return s.Change(num, mtype.ProjectUserCoinLogStateDeduct, mtype.ProjectUserCoinLogSourceCenterExchange, centerId, centerHashId, title, langMark, mtype.PointsLogOther{
|
|
|
MallItemId: mallItemId,
|
|
|
DeductId: deductId,
|
|
|
})
|
|
|
}
|
|
|
|
|
|
// Change 积分变更
|
|
|
func (s *UserCoinChange) Change(num int32, state, source string, sourceId int64, reqHashId, title, langMark string, other mtype.PointsLogOther) (res *PointsChangeRespData, err error) {
|
|
|
var resp, errMsg string
|
|
|
var code int32
|
|
|
defer func() {
|
|
|
s.after(code, resp, errMsg)
|
|
|
}()
|
|
|
|
|
|
req := mtype.PointsLogReq{
|
|
|
State: state,
|
|
|
Num: num,
|
|
|
UserId: s.user.UserID,
|
|
|
Source: source,
|
|
|
CampaignId: reqHashId,
|
|
|
Title: title,
|
|
|
}
|
|
|
|
|
|
switch state {
|
|
|
case mtype.ProjectUserCoinLogStateDeduct:
|
|
|
req.DeductId = fmt.Sprint(other.DeductId)
|
|
|
}
|
|
|
|
|
|
s.before(source, sourceId, req, other)
|
|
|
|
|
|
body, _ := jsonm.Marshal(req)
|
|
|
resp, code, err = s.request(s.log.ID, body, langMark, prefixToken, NewCertToken)
|
|
|
if err != nil {
|
|
|
errMsg = fmt.Sprintf("请求处理失败: %v", err)
|
|
|
err = workerr.S2SNotSuccessError
|
|
|
return
|
|
|
}
|
|
|
|
|
|
var data = &PointsChangeResp{}
|
|
|
err = jsonm.StrUnmarshal(resp, data)
|
|
|
if err != nil || data.Data == nil {
|
|
|
errMsg = fmt.Sprintf("无法解析的返回数据: %v", err)
|
|
|
err = workerr.S2SUnableToParseError
|
|
|
return
|
|
|
}
|
|
|
|
|
|
if data.Code != successCode {
|
|
|
errMsg = "code返回的非200,请确认业务是否正确对接"
|
|
|
if data.Msg != "" {
|
|
|
errMsg = fmt.Sprintf("code返回的非200, 提供的信息为: %s", data.Msg)
|
|
|
}
|
|
|
err = workerr.S2SNotSuccessError
|
|
|
return
|
|
|
}
|
|
|
|
|
|
res = data.Data
|
|
|
|
|
|
return
|
|
|
}
|
|
|
|
|
|
func (s *UserCoinChange) before(source string, sourceId int64, req mtype.PointsLogReq, other mtype.PointsLogOther) {
|
|
|
s.log = withdefault.ProjectUserCoinLog(&model.ProjectUserCoinLog{
|
|
|
ProjectID: s.user.ProjectID,
|
|
|
UserID: s.user.ID,
|
|
|
Source: source,
|
|
|
SourceID: sourceId,
|
|
|
ReqURL: s.GetUrl(),
|
|
|
Num: req.Num,
|
|
|
})
|
|
|
|
|
|
s.log.SetReq(req)
|
|
|
s.log.SetOtherData(other)
|
|
|
_ = s.GetDao().ProjectUserCoinLog.WithContext(s.Ctx).Create(s.log)
|
|
|
|
|
|
s.start = time.Now()
|
|
|
}
|
|
|
|
|
|
func (s *UserCoinChange) after(code int32, resp, errMsg string) {
|
|
|
s.log.RespCode = code
|
|
|
s.log.Resp = resp
|
|
|
s.log.ErrMsg = errMsg
|
|
|
s.log.Token = s.GetToken()
|
|
|
s.log.ReqHeader = s.GetHeader()
|
|
|
s.log.ReqTime = time.Since(s.start).Milliseconds()
|
|
|
|
|
|
_ = s.GetDao().ProjectUserCoinLog.WithContext(s.Ctx).Save(s.log)
|
|
|
}
|