You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
162 lines
4.5 KiB
162 lines
4.5 KiB
package user
|
|
|
|
import (
|
|
"yunyan/comm"
|
|
"yunyan/lego/sys/mysql"
|
|
"yunyan/pb"
|
|
"time"
|
|
)
|
|
|
|
// @Summary 使用资源请求
|
|
// @Description 使用资源请求
|
|
// @Tags User
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Security BearerAuth
|
|
// @Param user body pb.UserUsagesReq true "使用资源请求"
|
|
// @Success 200 {object} comm.HttpResult{data=pb.UserUsagesResp} "成功返回"
|
|
// @Router /api/home/user_usages [post]
|
|
func (this *apiComp) Usages(session comm.IUserSession, req *pb.UserUsagesReq) (resp *pb.UserUsagesResp, errdata *pb.ErrorData) {
|
|
var (
|
|
user *pb.DBUser
|
|
statistics *pb.DBUserStatistics
|
|
usages map[int32]int64
|
|
userlog *pb.DBUserUseLog
|
|
err error
|
|
)
|
|
user, err = this.module.model.findforid(session.GetUserId())
|
|
if err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
statistics, err = this.module.model.getStatistics(session.GetUserId())
|
|
if err != nil && err != mysql.ErrNoDocuments {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
userlog = &pb.DBUserUseLog{
|
|
Uid: session.GetUserId(),
|
|
Logtype: pb.UserLogType_UserConsume,
|
|
Ts: time.Now().Unix(),
|
|
}
|
|
usages = make(map[int32]int64)
|
|
switch req.Usagetype {
|
|
case pb.UsageType_Trade:
|
|
var totalCost int64
|
|
for _, v := range req.Usages {
|
|
totalCost += v
|
|
}
|
|
if user.Tradeintegral < totalCost {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_TradeIntegralNotEnough,
|
|
Message: "翻译积分不足",
|
|
}
|
|
return
|
|
}
|
|
if v, ok := req.Usages["TRANSLATION_MODE_simultaneous"]; ok {
|
|
user.Tradeintegral -= v
|
|
statistics.Trademodel1Num += 1
|
|
statistics.Trademodel1Time += v
|
|
userlog.Addagentintegral += -1 * v
|
|
}
|
|
if v, ok := req.Usages["TRANSLATION_MODE_faceToFace"]; ok {
|
|
user.Tradeintegral -= v
|
|
statistics.Trademodel2Num += 1
|
|
statistics.Trademodel2Time += v
|
|
userlog.Addagentintegral += -1 * v
|
|
}
|
|
if v, ok := req.Usages["TRANSLATION_MODE_call"]; ok {
|
|
user.Tradeintegral -= v
|
|
statistics.Trademodel3Num += 1
|
|
statistics.Trademodel3Time += v
|
|
userlog.Addagentintegral += -1 * v
|
|
}
|
|
if v, ok := req.Usages["TRANSLATION_MODE_audioVideo"]; ok {
|
|
user.Tradeintegral -= v
|
|
statistics.Trademodel4Num += 1
|
|
statistics.Trademodel4Time += v
|
|
userlog.Addagentintegral += -1 * v
|
|
}
|
|
usages[int32(req.Usagetype)] = user.Tradeintegral
|
|
case pb.UsageType_Meet:
|
|
if v, ok := req.Usages["MEETING_ASR_AUDIO_SECONDS"]; ok {
|
|
if user.Meetintegral < v {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_MeetIntegralNotEnough,
|
|
Message: "会议积分不足",
|
|
}
|
|
return
|
|
}
|
|
user.Meetintegral -= v
|
|
statistics.Meetnum += 1
|
|
statistics.Meettime += v
|
|
userlog.Addmeetsecond += -1 * v
|
|
}
|
|
usages[int32(req.Usagetype)] = user.Meetintegral
|
|
case pb.UsageType_AI:
|
|
if user.Aichatintegral < 1 {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_AIIntegralNotEnough,
|
|
Message: "AI积分不足",
|
|
}
|
|
return
|
|
}
|
|
user.Aichatintegral -= 1
|
|
usages[int32(req.Usagetype)] = user.Aichatintegral
|
|
statistics.Aichatnum += 1
|
|
usages[int32(req.Usagetype)] = user.Aichatintegral
|
|
userlog.Addagentintegral += -1
|
|
}
|
|
|
|
err = this.module.model.save(user)
|
|
if err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
err = this.module.model.updateStatistics(statistics)
|
|
if err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
|
|
if err = this.module.model.addIntegralLog(userlog); err != nil {
|
|
errdata = &pb.ErrorData{
|
|
Code: pb.ErrorCode_DBError,
|
|
Message: err.Error(),
|
|
}
|
|
return
|
|
}
|
|
// 资源消耗埋点
|
|
if this.module.analyze != nil {
|
|
switch req.Usagetype {
|
|
case pb.UsageType_Trade:
|
|
var sec int64
|
|
for _, v := range req.Usages {
|
|
sec += v
|
|
}
|
|
this.module.analyze.Report(&comm.StatEvent{Type: comm.StatEventTranslate, ProductId: user.Lastbindproductid, ChannelId: user.Lastbindchannelid, Uid: session.GetUserId(), Second: sec})
|
|
case pb.UsageType_Meet:
|
|
if v, ok := req.Usages["MEETING_ASR_AUDIO_SECONDS"]; ok {
|
|
this.module.analyze.Report(&comm.StatEvent{Type: comm.StatEventMeeting, ProductId: user.Lastbindproductid, ChannelId: user.Lastbindchannelid, Uid: session.GetUserId(), Second: v})
|
|
}
|
|
case pb.UsageType_AI:
|
|
this.module.analyze.Report(&comm.StatEvent{Type: comm.StatEventAiChat, ProductId: user.Lastbindproductid, ChannelId: user.Lastbindchannelid, Uid: session.GetUserId()})
|
|
}
|
|
}
|
|
resp = &pb.UserUsagesResp{
|
|
Usages: usages,
|
|
}
|
|
return
|
|
}
|
|
|