This commit is contained in:
2025-08-02 02:54:21 +08:00
parent 934dce2776
commit 66845d3fe0
74 changed files with 8686 additions and 212 deletions

View File

@@ -10,6 +10,7 @@ import (
"tyapi-server/internal/shared/database"
"tyapi-server/internal/shared/interfaces"
"github.com/shopspring/decimal"
"go.uber.org/zap"
"gorm.io/gorm"
)
@@ -113,13 +114,39 @@ func (r *GormSubscriptionRepository) ListSubscriptions(ctx context.Context, quer
// 应用筛选条件
if query.UserID != "" {
dbQuery = dbQuery.Where("user_id = ?", query.UserID)
dbQuery = dbQuery.Where("subscription.user_id = ?", query.UserID)
}
// 这里筛选的是关联的Product实体里的name或code字段只有当keyword匹配关联Product的name或code时才返回
// 关键词搜索(产品名称或编码)
if query.Keyword != "" {
dbQuery = dbQuery.Joins("LEFT JOIN product ON product.id = subscription.product_id").
Where("product.name LIKE ? OR product.code LIKE ?", "%"+query.Keyword+"%", "%"+query.Keyword+"%")
}
// 产品名称筛选
if query.ProductName != "" {
dbQuery = dbQuery.Joins("LEFT JOIN product ON product.id = subscription.product_id").
Where("product.name LIKE ?", "%"+query.ProductName+"%")
}
// 企业名称筛选(需要关联用户和企业信息)
if query.CompanyName != "" {
dbQuery = dbQuery.Joins("LEFT JOIN users ON users.id = subscription.user_id").
Joins("LEFT JOIN enterprise_infos ON enterprise_infos.user_id = users.id").
Where("enterprise_infos.company_name LIKE ?", "%"+query.CompanyName+"%")
}
// 时间范围筛选
if query.StartTime != "" {
if t, err := time.Parse("2006-01-02 15:04:05", query.StartTime); err == nil {
dbQuery = dbQuery.Where("subscription.created_at >= ?", t)
}
}
if query.EndTime != "" {
if t, err := time.Parse("2006-01-02 15:04:05", query.EndTime); err == nil {
dbQuery = dbQuery.Where("subscription.created_at <= ?", t)
}
}
// 获取总数
if err := dbQuery.Count(&total).Error; err != nil {
@@ -136,7 +163,7 @@ func (r *GormSubscriptionRepository) ListSubscriptions(ctx context.Context, quer
}
dbQuery = dbQuery.Order(order)
} else {
dbQuery = dbQuery.Order("created_at DESC")
dbQuery = dbQuery.Order("subscription.created_at DESC")
}
// 应用分页
@@ -173,13 +200,23 @@ func (r *GormSubscriptionRepository) CountByUser(ctx context.Context, userID str
return count, err
}
// CountByProduct 统计产品订阅数量
// CountByProduct 统计产品订阅数量
func (r *GormSubscriptionRepository) CountByProduct(ctx context.Context, productID string) (int64, error) {
var count int64
err := r.GetDB(ctx).WithContext(ctx).Model(&entities.Subscription{}).Where("product_id = ?", productID).Count(&count).Error
return count, err
}
// GetTotalRevenue 获取总收入
func (r *GormSubscriptionRepository) GetTotalRevenue(ctx context.Context) (float64, error) {
var total decimal.Decimal
err := r.GetDB(ctx).WithContext(ctx).Model(&entities.Subscription{}).Select("COALESCE(SUM(price), 0)").Scan(&total).Error
if err != nil {
return 0, err
}
return total.InexactFloat64(), nil
}
// 基础Repository接口方法
// Count 返回订阅总数