优化二维码池筛选和日期中文化

This commit is contained in:
yml2213
2026-06-19 19:16:52 +08:00
parent f4343ed8a5
commit fcf0230e04
5 changed files with 408 additions and 35 deletions
+104 -18
View File
@@ -3,6 +3,8 @@ package chat
import (
"context"
"errors"
"strconv"
"strings"
"time"
"gorm.io/gorm"
@@ -60,9 +62,15 @@ type UpdateQrCodeRequest struct {
// QrCodeListRequest 列表查询请求
type QrCodeListRequest struct {
Status string `form:"status"`
Page int `form:"page"`
Limit int `form:"limit"`
Status string `form:"status"`
Keyword string `form:"keyword"`
WecomRenamed string `form:"wecom_renamed"`
Bound string `form:"bound"`
ExpireStatus string `form:"expire_status"`
CreatedStart string `form:"created_start"`
CreatedEnd string `form:"created_end"`
Page int `form:"page"`
Limit int `form:"limit"`
}
// QrCodeStats 二维码统计
@@ -167,32 +175,28 @@ func (r *Repository) ListQrCodes(ctx context.Context, req QrCodeListRequest) ([]
req.Limit = 20
}
query := r.db.WithContext(ctx).Model(&model.ChatQrCode{})
// 状态过滤
if req.Status != "" {
query = query.Where("status = ?", req.Status)
now := time.Now()
baseQuery := func() *gorm.DB {
return applyQrCodeListFilters(
r.db.WithContext(ctx).
Table("chat_qrcode_pool AS q").
Joins("LEFT JOIN chat_conversations AS c ON c.id = q.conversation_id"),
req,
now,
)
}
// 统计总数
var total int64
if err := query.Count(&total).Error; err != nil {
if err := baseQuery().Count(&total).Error; err != nil {
return nil, 0, err
}
// 查询列表
var qrcodes []QrCodeListItem
offset := (req.Page - 1) * req.Limit
if err := r.db.WithContext(ctx).
Table("chat_qrcode_pool AS q").
if err := baseQuery().
Select("q.*, COALESCE(c.title, '') AS bound_conversation_title").
Joins("LEFT JOIN chat_conversations AS c ON c.id = q.conversation_id").
Scopes(func(db *gorm.DB) *gorm.DB {
if req.Status != "" {
return db.Where("q.status = ?", req.Status)
}
return db
}).
Order("q.id DESC").
Offset(offset).
Limit(req.Limit).
@@ -203,6 +207,88 @@ func (r *Repository) ListQrCodes(ctx context.Context, req QrCodeListRequest) ([]
return qrcodes, total, nil
}
func applyQrCodeListFilters(query *gorm.DB, req QrCodeListRequest, now time.Time) *gorm.DB {
if req.Status != "" {
query = query.Where("q.status = ?", req.Status)
}
if keyword := strings.TrimSpace(req.Keyword); keyword != "" {
like := "%" + keyword + "%"
conditions := "(q.group_name LIKE ? OR q.note LIKE ? OR COALESCE(c.title, '') LIKE ?"
args := []interface{}{like, like, like}
if id, err := strconv.ParseUint(keyword, 10, 64); err == nil {
conditions += " OR q.id = ? OR q.conversation_id = ?"
args = append(args, id, id)
}
conditions += ")"
query = query.Where(conditions, args...)
}
if value, ok := parseQrCodeBoolFilter(req.WecomRenamed); ok {
query = query.Where("q.wecom_renamed = ?", value)
}
if value, ok := parseQrCodeBoolFilter(req.Bound); ok {
if value {
query = query.Where("q.conversation_id IS NOT NULL")
} else {
query = query.Where("q.conversation_id IS NULL")
}
}
switch req.ExpireStatus {
case "valid":
query = query.Where("(q.expires_at IS NULL OR q.expires_at > ?)", now)
case "expired":
query = query.Where("q.expires_at IS NOT NULL AND q.expires_at <= ?", now)
case "permanent":
query = query.Where("q.expires_at IS NULL")
case "expiring_soon":
query = query.Where("q.expires_at > ? AND q.expires_at <= ?", now, now.Add(72*time.Hour))
}
if start, ok := parseQrCodeFilterTime(req.CreatedStart); ok {
query = query.Where("q.created_at >= ?", start)
}
if end, ok := parseQrCodeFilterTime(req.CreatedEnd); ok {
query = query.Where("q.created_at <= ?", end)
}
return query
}
func parseQrCodeBoolFilter(value string) (bool, bool) {
switch strings.ToLower(strings.TrimSpace(value)) {
case "true", "1", "yes":
return true, true
case "false", "0", "no":
return false, true
default:
return false, false
}
}
func parseQrCodeFilterTime(value string) (time.Time, bool) {
value = strings.TrimSpace(value)
if value == "" {
return time.Time{}, false
}
layouts := []string{
time.RFC3339,
"2006-01-02T15:04:05",
"2006-01-02T15:04",
"2006-01-02 15:04:05",
"2006-01-02 15:04",
"2006-01-02",
}
for _, layout := range layouts {
if parsed, err := time.Parse(layout, value); err == nil {
return parsed, true
}
}
return time.Time{}, false
}
// GetQrCodeStats 获取二维码统计信息
func (r *Repository) GetQrCodeStats(ctx context.Context) (*QrCodeStats, error) {
stats := &QrCodeStats{}