|
@@ -9,7 +9,6 @@ import (
|
|
|
"github.com/suyuan32/simple-admin-core/rpc/types/core"
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
"io"
|
|
|
- "math"
|
|
|
"net/http"
|
|
|
"strconv"
|
|
|
"strings"
|
|
@@ -18,7 +17,7 @@ import (
|
|
|
"wechat-api/ent/creditbalance"
|
|
|
"wechat-api/ent/custom_types"
|
|
|
"wechat-api/ent/employee"
|
|
|
- "wechat-api/ent/usagetotal"
|
|
|
+ "wechat-api/hook/credit"
|
|
|
"wechat-api/hook/dify"
|
|
|
"wechat-api/internal/svc"
|
|
|
"wechat-api/internal/types"
|
|
@@ -199,110 +198,31 @@ func (l *GptsSubmitApiChatLogic) GptsSubmitApiChat(tokenStr string, req *types.G
|
|
|
if finish {
|
|
|
if switcher {
|
|
|
tx, err := l.svcCtx.DB.Tx(context.Background())
|
|
|
+ agentId := strconv.Itoa(int(*req.AgentId))
|
|
|
+
|
|
|
// 构造 original_data
|
|
|
originalData := custom_types.OriginalData{}
|
|
|
originalData.Request = chatReq
|
|
|
originalData.Response = chatData
|
|
|
|
|
|
- agentId := strconv.Itoa(int(*req.AgentId))
|
|
|
- // 记录Token使用信息
|
|
|
- usageDetailItem, err := tx.UsageDetail.Create().
|
|
|
- SetType(3). //1-微信 2-名片 3-智能体
|
|
|
- SetBotID(agentId). //智能体ID
|
|
|
- SetReceiverID(userId). //接收者userID
|
|
|
- SetApp(8). //8-智能体
|
|
|
- SetSessionID(0).
|
|
|
- SetRequest(*req.Content).
|
|
|
- SetResponse(answer).
|
|
|
- SetOriginalData(originalData).
|
|
|
- SetTotalTokens(chatData.Metadata.Usage.TotalTokens).
|
|
|
- SetPromptTokens(chatData.Metadata.Usage.PromptTokens).
|
|
|
- SetCompletionTokens(chatData.Metadata.Usage.CompletionTokens).
|
|
|
- Save(l.ctx)
|
|
|
-
|
|
|
- if err != nil {
|
|
|
- _ = tx.Rollback()
|
|
|
- l.Logger.Errorf("save data to usage_detail error:%v\n", err)
|
|
|
- }
|
|
|
-
|
|
|
- // 记录Token使用总量
|
|
|
- usageTotal, err := tx.UsageTotal.Query().Where(usagetotal.OrganizationID(*userInfo.DepartmentId)).First(l.ctx)
|
|
|
if err != nil {
|
|
|
- if ent.IsNotFound(err) {
|
|
|
- usageTotal, err = tx.UsageTotal.Create().
|
|
|
- SetOrganizationID(*userInfo.DepartmentId).
|
|
|
- SetTotalTokens(0).
|
|
|
- Save(l.ctx)
|
|
|
- } else {
|
|
|
+ l.Logger.Errorf("start transaction error:%v\n", err)
|
|
|
+ } else {
|
|
|
+ err = credit.AddCreditUsage(tx, l.ctx,
|
|
|
+ agentId, userId, *userInfo.DepartmentId,
|
|
|
+ req.Content, &answer,
|
|
|
+ &originalData, &chatData,
|
|
|
+ )
|
|
|
+ if err != nil {
|
|
|
_ = tx.Rollback()
|
|
|
- l.Logger.Errorf("create usage_total failed:departmentId:%v err:%v\n", *userInfo.DepartmentId, err)
|
|
|
- }
|
|
|
- }
|
|
|
- _, err = tx.UsageTotal.Update().
|
|
|
- Where(usagetotal.OrganizationID(*userInfo.DepartmentId)).
|
|
|
- SetTotalTokens(usageTotal.TotalTokens + chatData.Metadata.Usage.TotalTokens).
|
|
|
- SetEndIndex(usageDetailItem.ID).
|
|
|
- Save(l.ctx)
|
|
|
- if err != nil {
|
|
|
- _ = tx.Rollback()
|
|
|
- l.Logger.Errorf("update usage_total failed:departmentId:%v err:%v\n", *userInfo.DepartmentId, err)
|
|
|
- }
|
|
|
-
|
|
|
- // 积分明细表记录使用量
|
|
|
- // 根据1积分=10000token 根据Token换算积分使用量
|
|
|
- var rate float64 = 10000
|
|
|
- change := float64(chatData.Metadata.Usage.TotalTokens) / rate
|
|
|
- number := math.Round(change*rate) / rate
|
|
|
-
|
|
|
- creditBalanceItem, err := tx.CreditBalance.Query().Where(creditbalance.OrganizationID(*userInfo.DepartmentId)).First(l.ctx)
|
|
|
- if err != nil {
|
|
|
- if ent.IsNotFound(err) {
|
|
|
- creditBalanceItem, err = tx.CreditBalance.Create().SetOrganizationID(*userInfo.DepartmentId).SetBalance(0).Save(l.ctx)
|
|
|
- if err != nil {
|
|
|
- _ = tx.Rollback()
|
|
|
- l.Logger.Errorf("create credit_balance failed. organization:%v error:%v\n", *userInfo.DepartmentId, err)
|
|
|
- }
|
|
|
+ l.Logger.Errorf("save credits info failed:%v\n", err)
|
|
|
} else {
|
|
|
- _ = tx.Rollback()
|
|
|
- l.Logger.Errorf("query credit_balance failed. organization:%v error:%v\n", *userInfo.DepartmentId, err)
|
|
|
+ _ = tx.Commit()
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- beforeNumber := creditBalanceItem.Balance
|
|
|
- afterNumber := l.subtraction(beforeNumber, number)
|
|
|
- _, err = tx.CreditUsage.Create().
|
|
|
- SetUserID(userId).
|
|
|
- SetNumber(number).
|
|
|
- SetBeforeNumber(beforeNumber).
|
|
|
- SetAfterNumber(afterNumber).
|
|
|
- SetNtype(1).
|
|
|
- SetNid(usageDetailItem.ID).
|
|
|
- SetTable("usage_detail").
|
|
|
- SetOrganizationID(*userInfo.DepartmentId).
|
|
|
- Save(l.ctx)
|
|
|
- if err != nil {
|
|
|
- _ = tx.Rollback()
|
|
|
- l.Logger.Errorf("save data to credit_usage error:%v\n", err)
|
|
|
- }
|
|
|
-
|
|
|
- // 减积分
|
|
|
- _, err = tx.CreditBalance.Update().Where(creditbalance.OrganizationID(*userInfo.DepartmentId)).SetBalance(creditBalance.Balance - number).Save(l.ctx)
|
|
|
- if err != nil {
|
|
|
- _ = tx.Rollback()
|
|
|
- l.Logger.Errorf("update organization:%v balance error:%v\n", *userInfo.DepartmentId, err)
|
|
|
- }
|
|
|
- _ = tx.Commit()
|
|
|
}
|
|
|
break
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-// subtraction() 保留小数点后4位的精确减法
|
|
|
-func (l *GptsSubmitApiChatLogic) subtraction(number1, number2 float64) float64 {
|
|
|
- d1 := number1 * 10000
|
|
|
- d2 := number2 * 10000
|
|
|
- res := math.Floor(d1-d2) / 10000
|
|
|
- return res
|
|
|
-}
|