初始化项目
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
||||
"cid/dao"
|
||||
"cid/model/dto"
|
||||
"cid/model/entity"
|
||||
"cid/model/types"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
@@ -63,7 +62,7 @@ func (s *cid) getMatchingStrategy(ctx context.Context, tenantLevel string) (*AdM
|
||||
}
|
||||
|
||||
return &AdMatchingStrategy{
|
||||
TenantLevel: strategyEntity.TenantLevel,
|
||||
TenantLevel: tenantLevel, // 使用传入的tenantLevel参数
|
||||
MinConversion: strategyEntity.MinConversion,
|
||||
MaxConversion: strategyEntity.MaxConversion,
|
||||
SourceWeight: sourceWeights,
|
||||
@@ -86,7 +85,14 @@ func (s *cid) GenerateCID(ctx context.Context, req *dto.GenerateCIDReq) (res *dt
|
||||
}
|
||||
|
||||
// 检查租户请求次数限制
|
||||
allowed, err := RateLimit.CheckTenantRequestLimit(ctx, tenant.Id, nil)
|
||||
// 将租户ID转换为int64用于限流检查
|
||||
tenantIdInt := int64(0)
|
||||
if tenant.Id != "" && tenant.Id != "default" {
|
||||
tryInt, _ := strconv.ParseInt(tenant.Id, 10, 64)
|
||||
tenantIdInt = tryInt
|
||||
}
|
||||
|
||||
allowed, err := RateLimit.CheckTenantRequestLimit(ctx, tenantIdInt, nil)
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "检查租户请求限制失败")
|
||||
}
|
||||
@@ -112,54 +118,71 @@ func (s *cid) GenerateCID(ctx context.Context, req *dto.GenerateCIDReq) (res *dt
|
||||
// 生成唯一CID
|
||||
cid := s.generateUniqueCID()
|
||||
|
||||
// 转换租户ID为int64(兼容性处理)
|
||||
// 这里直接使用之前已经声明的tenantIdInt变量,不需要重新声明
|
||||
if tenant.Id != "" && tenant.Id != "default" {
|
||||
// 这里简化处理,实际可能需要更复杂的转换逻辑
|
||||
tryInt, parseErr := strconv.ParseInt(tenant.Id, 10, 64)
|
||||
if parseErr == nil {
|
||||
tenantIdInt = tryInt
|
||||
}
|
||||
}
|
||||
|
||||
return &dto.GenerateCIDRes{
|
||||
CID: cid,
|
||||
Ads: ads,
|
||||
TotalAds: len(ads),
|
||||
TenantId: tenant.Id,
|
||||
TenantId: tenantIdInt,
|
||||
TenantName: tenant.Name,
|
||||
GeneratedAt: time.Now().Format("2006-01-02 15:04:05"),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// getTenantByUser 根据用户获取租户信息
|
||||
func (s *cid) getTenantByUser(ctx context.Context, userId int64) (*types.Tenant, error) {
|
||||
func (s *cid) getTenantByUser(ctx context.Context, userId int64) (*dto.TenantInfo, error) {
|
||||
// 通过common模块获取用户信息,包含租户ID
|
||||
userInfo, err := utils.GetUserInfo(ctx)
|
||||
if err != nil {
|
||||
return nil, gerror.Wrap(err, "获取用户信息失败")
|
||||
}
|
||||
|
||||
// 租户ID从用户信息中获取
|
||||
tenantId := gconv.Int64(userInfo.TenantId)
|
||||
if tenantId == 0 {
|
||||
tenantId = 1 // 默认租户ID
|
||||
// 租户ID直接从用户信息中获取
|
||||
tenantId := ""
|
||||
if userInfo.TenantId != nil {
|
||||
if tenantIdStr, ok := userInfo.TenantId.(string); ok && tenantIdStr != "" {
|
||||
tenantId = tenantIdStr
|
||||
}
|
||||
} else {
|
||||
tenantId = "default" // 默认租户ID
|
||||
tenantId = "default" // 默认租户ID
|
||||
}
|
||||
|
||||
// 租户级别和名称可以根据租户ID通过其他方式获取或配置
|
||||
// 这里使用映射配置,实际项目中可能需要调用其他服务
|
||||
tenantName := "默认租户"
|
||||
tenantLevel := "basic"
|
||||
tenantStatus := "active"
|
||||
|
||||
// 根据租户ID设置不同的级别(示例逻辑)
|
||||
switch tenantId {
|
||||
case 1:
|
||||
case "default":
|
||||
tenantName = "基础租户"
|
||||
tenantLevel = "basic"
|
||||
case 2:
|
||||
case "standard":
|
||||
tenantName = "标准租户"
|
||||
tenantLevel = "standard"
|
||||
case 3:
|
||||
case "premium":
|
||||
tenantName = "高级租户"
|
||||
tenantLevel = "premium"
|
||||
default:
|
||||
// 如果租户ID不是预设值,使用租户ID作为名称
|
||||
tenantName = tenantId + "租户"
|
||||
tenantLevel = "basic"
|
||||
}
|
||||
|
||||
return &types.Tenant{
|
||||
Id: tenantId,
|
||||
Name: tenantName,
|
||||
Level: tenantLevel,
|
||||
Status: tenantStatus,
|
||||
return &dto.TenantInfo{
|
||||
Id: tenantId,
|
||||
Name: tenantName,
|
||||
Level: tenantLevel,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -283,7 +306,7 @@ func (s *cid) generateUniqueCID() string {
|
||||
}
|
||||
|
||||
// recordCIDRequest 记录CID请求
|
||||
func (s *cid) recordCIDRequest(ctx context.Context, req *dto.GenerateCIDReq, tenant *types.Tenant, ads []*dto.AdInfo) {
|
||||
func (s *cid) recordCIDRequest(ctx context.Context, req *dto.GenerateCIDReq, tenant *dto.TenantInfo, ads []*dto.AdInfo) {
|
||||
// 转换dto.AdInfo到entity.Ad
|
||||
var entityAds []entity.Ad
|
||||
for _, ad := range ads {
|
||||
@@ -301,19 +324,21 @@ func (s *cid) recordCIDRequest(ctx context.Context, req *dto.GenerateCIDReq, ten
|
||||
request := &entity.CidRequest{
|
||||
RequestID: fmt.Sprintf("REQ_%d_%d", time.Now().Unix(), rand.Intn(10000)),
|
||||
UserID: fmt.Sprintf("%d", req.UserId),
|
||||
TenantID: fmt.Sprintf("%d", tenant.Id),
|
||||
Response: &entity.CidResponse{
|
||||
Ads: entityAds,
|
||||
},
|
||||
ProcessingTime: int64(rand.Intn(401) + 100), // 模拟处理时间
|
||||
}
|
||||
|
||||
dao.CIDRequest.Create(ctx, request)
|
||||
_, err := dao.CIDRequest.Create(ctx, request)
|
||||
if err != nil {
|
||||
g.Log().Errorf(ctx, "记录CID请求失败: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// GetCIDHistory 获取CID请求历史
|
||||
func (s *cid) GetCIDHistory(ctx context.Context, userId int64, page, size int) (res *dto.GetCIDHistoryRes, err error) {
|
||||
history, total, err := dao.CIDRequest.GetHistory(ctx, userId, page, size)
|
||||
history, total, err := dao.CIDRequest.GetHistory(ctx, strconv.FormatInt(userId, 10), page, size)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -322,8 +347,8 @@ func (s *cid) GetCIDHistory(ctx context.Context, userId int64, page, size int) (
|
||||
for _, record := range history {
|
||||
// 解析TenantID
|
||||
tenantId := int64(0)
|
||||
if record.TenantID != "" {
|
||||
tenantId, _ = strconv.ParseInt(record.TenantID, 10, 64)
|
||||
if tenantIdStr, ok := record.TenantId.(string); ok && tenantIdStr != "" {
|
||||
tenantId, _ = strconv.ParseInt(tenantIdStr, 10, 64)
|
||||
}
|
||||
|
||||
// 解析UserID
|
||||
|
||||
Reference in New Issue
Block a user