修复二维码库存与过期时间编辑
This commit is contained in:
@@ -36,9 +36,11 @@ type BatchCreateQrCodeRequest struct {
|
||||
|
||||
// UpdateQrCodeRequest 更新二维码请求
|
||||
type UpdateQrCodeRequest struct {
|
||||
Note *string `json:"note"`
|
||||
Status *string `json:"status"`
|
||||
ExpiresAt *time.Time `json:"expires_at"`
|
||||
ImageURL *string `json:"image_url"`
|
||||
Note *string `json:"note"`
|
||||
Status *string `json:"status"`
|
||||
ExpiresAt *time.Time `json:"expires_at"`
|
||||
ClearExpiresAt bool `json:"clear_expires_at"`
|
||||
}
|
||||
|
||||
// QrCodeListRequest 列表查询请求
|
||||
@@ -140,32 +142,29 @@ func (r *Repository) ListQrCodes(ctx context.Context, req QrCodeListRequest) ([]
|
||||
// GetQrCodeStats 获取二维码统计信息
|
||||
func (r *Repository) GetQrCodeStats(ctx context.Context) (*QrCodeStats, error) {
|
||||
stats := &QrCodeStats{}
|
||||
now := time.Now()
|
||||
|
||||
// 统计各状态数量
|
||||
type CountResult struct {
|
||||
Status string
|
||||
Count int64
|
||||
}
|
||||
var results []CountResult
|
||||
|
||||
if err := r.db.WithContext(ctx).
|
||||
Model(&model.ChatQrCode{}).
|
||||
Select("status, COUNT(*) as count").
|
||||
Group("status").
|
||||
Find(&results).Error; err != nil {
|
||||
if err := r.db.WithContext(ctx).Model(&model.ChatQrCode{}).Count(&stats.TotalCount).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, r := range results {
|
||||
stats.TotalCount += r.Count
|
||||
switch r.Status {
|
||||
case QrCodeStatusUnused:
|
||||
stats.UnusedCount = r.Count
|
||||
case QrCodeStatusUsed:
|
||||
stats.UsedCount = r.Count
|
||||
case QrCodeStatusDisabled:
|
||||
stats.DisabledCount = r.Count
|
||||
}
|
||||
if err := r.db.WithContext(ctx).
|
||||
Model(&model.ChatQrCode{}).
|
||||
Where("status = ?", QrCodeStatusUsed).
|
||||
Count(&stats.UsedCount).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := r.db.WithContext(ctx).
|
||||
Model(&model.ChatQrCode{}).
|
||||
Where("status = ?", QrCodeStatusDisabled).
|
||||
Count(&stats.DisabledCount).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := r.db.WithContext(ctx).
|
||||
Model(&model.ChatQrCode{}).
|
||||
Where("status = ?", QrCodeStatusUnused).
|
||||
Where("expires_at IS NULL OR expires_at > ?", now).
|
||||
Count(&stats.UnusedCount).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return stats, nil
|
||||
@@ -175,6 +174,9 @@ func (r *Repository) GetQrCodeStats(ctx context.Context) (*QrCodeStats, error) {
|
||||
func (r *Repository) UpdateQrCode(ctx context.Context, id uint64, req UpdateQrCodeRequest) error {
|
||||
updates := make(map[string]interface{})
|
||||
|
||||
if req.ImageURL != nil {
|
||||
updates["image_url"] = *req.ImageURL
|
||||
}
|
||||
if req.Note != nil {
|
||||
updates["note"] = *req.Note
|
||||
}
|
||||
@@ -187,6 +189,8 @@ func (r *Repository) UpdateQrCode(ctx context.Context, id uint64, req UpdateQrCo
|
||||
}
|
||||
if req.ExpiresAt != nil {
|
||||
updates["expires_at"] = req.ExpiresAt
|
||||
} else if req.ClearExpiresAt {
|
||||
updates["expires_at"] = nil
|
||||
}
|
||||
|
||||
if len(updates) == 0 {
|
||||
|
||||
Reference in New Issue
Block a user