43 lines
1.4 KiB
Go
43 lines
1.4 KiB
Go
// Package cardrealname 提供卡当前实名时间与实名时间历史的只读查询。
|
|
package cardrealname
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/break/junhong_cmp_fiber/internal/model"
|
|
"github.com/break/junhong_cmp_fiber/pkg/errors"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// Current 是卡当前实名时间及来源。
|
|
type Current struct {
|
|
RealnameAt *time.Time
|
|
Source string
|
|
}
|
|
|
|
// LoadCurrent 批量读取卡当前实名记录;没有记录的卡不在返回结果中。
|
|
func LoadCurrent(ctx context.Context, db *gorm.DB, cardIDs []uint) (map[uint]Current, error) {
|
|
result := make(map[uint]Current, len(cardIDs))
|
|
if db == nil || len(cardIDs) == 0 {
|
|
return result, nil
|
|
}
|
|
var rows []model.IotCardRealname
|
|
if err := db.WithContext(ctx).Select("card_id", "realname_at", "realname_at_source").
|
|
Where("card_id IN ?", cardIDs).Find(&rows).Error; err != nil {
|
|
return nil, errors.Wrap(errors.CodeDatabaseError, err, "查询卡当前实名时间失败")
|
|
}
|
|
for _, row := range rows {
|
|
result[row.CardID] = Current{RealnameAt: row.RealnameAt, Source: row.RealnameAtSource}
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
// ResolveRealnameAt 返回卡的实名时间:有当前实名记录用记录(可能为空,如等待二次实名),没有记录回退 first_realname_at。
|
|
func ResolveRealnameAt(current map[uint]Current, cardID uint, fallback *time.Time) (*time.Time, string) {
|
|
if value, ok := current[cardID]; ok {
|
|
return value.RealnameAt, value.Source
|
|
}
|
|
return fallback, ""
|
|
}
|