package mysqldao import ( "context" "git.lzcwy.cn/zhengchiliu/zt-closet-manage-backend/global" "git.lzcwy.cn/zhengchiliu/zt-closet-manage-backend/internal/mysqlmodel" "gorm.io/gorm" ) // QuotaGrantDao 额度凭证数据访问(有有效期赠送额度)。 type QuotaGrantDao struct { db *gorm.DB } // NewQuotaGrantDao 创建额度凭证 DAO。 func NewQuotaGrantDao() *QuotaGrantDao { return &QuotaGrantDao{db: global.MysqlEngine} } // QuotaGrantCreate 创建额度凭证(ref_id+quota_type 唯一索引兜底幂等,冲突返回 1062)。 func (d *QuotaGrantDao) QuotaGrantCreate(ctx context.Context, grant *mysqlmodel.QuotaGrant) error { ctx, span := global.Tracer.Start(ctx, "mysqldao.QuotaGrantCreate") defer span.End() if err := d.db.WithContext(ctx).Create(grant).Error; err != nil { span.RecordError(err) return err } return nil } // QuotaGrantListByUserID 查询用户全部凭证(未删除),按 id 倒序。 func (d *QuotaGrantDao) QuotaGrantListByUserID(ctx context.Context, userID int64) ([]mysqlmodel.QuotaGrant, error) { ctx, span := global.Tracer.Start(ctx, "mysqldao.QuotaGrantListByUserID") defer span.End() var list []mysqlmodel.QuotaGrant if err := d.db.WithContext(ctx).Where("user_id = ?", userID).Order("id DESC").Find(&list).Error; err != nil { span.RecordError(err) return nil, err } return list, nil } // QuotaGrantGetByID 按主键查询凭证(未删除)。 func (d *QuotaGrantDao) QuotaGrantGetByID(ctx context.Context, id int64) (*mysqlmodel.QuotaGrant, error) { ctx, span := global.Tracer.Start(ctx, "mysqldao.QuotaGrantGetByID") defer span.End() var grant mysqlmodel.QuotaGrant if err := d.db.WithContext(ctx).First(&grant, id).Error; err != nil { span.RecordError(err) return nil, err } return &grant, nil } // QuotaGrantSoftDelete 软删除凭证(按 id + user_id 归属校验)。 func (d *QuotaGrantDao) QuotaGrantSoftDelete(ctx context.Context, id, userID int64) error { ctx, span := global.Tracer.Start(ctx, "mysqldao.QuotaGrantSoftDelete") defer span.End() if err := d.db.WithContext(ctx). Where("id = ? AND user_id = ?", id, userID). Delete(&mysqlmodel.QuotaGrant{}).Error; err != nil { span.RecordError(err) return err } return nil } // QuotaGetByUserIDAndType 查询用户某类配额(Cos 永久额度的已用/底值)。 func (d *QuotaGrantDao) QuotaGetByUserIDAndType(ctx context.Context, userID int64, quotaType int8) (*mysqlmodel.Quota, error) { ctx, span := global.Tracer.Start(ctx, "mysqldao.QuotaGetByUserIDAndType") defer span.End() var quota mysqlmodel.Quota if err := d.db.WithContext(ctx). Where("user_id = ? AND quota_type = ?", userID, quotaType). First("a).Error; err != nil { span.RecordError(err) return nil, err } return "a, nil } // UserDailyUsageGetByUserIDAndDate 查询用户某日使用计数(当日额度已用,Redis 异常时 MySQL 兜底)。 func (d *QuotaGrantDao) UserDailyUsageGetByUserIDAndDate(ctx context.Context, userID, statDate int64) (*mysqlmodel.UserDailyUsage, error) { ctx, span := global.Tracer.Start(ctx, "mysqldao.UserDailyUsageGetByUserIDAndDate") defer span.End() var usage mysqlmodel.UserDailyUsage if err := d.db.WithContext(ctx). Where("user_id = ? AND stat_date = ?", userID, statDate). First(&usage).Error; err != nil { span.RecordError(err) return nil, err } return &usage, nil }