package model import "github.com/codinl/go-logger" //go:generate goqueryset -in search.go // gen:qs type SearchHistory struct { Model Uid uint32 `json:"uid" gorm:"index"` Keyword string `json:"keyword"` // 搜索内容 } func (*SearchHistory) TableName() string { return "search_history" } // gen:qs type HotSearch struct { Model Keyword string `json:"keyword"` // 搜索内容 Sort uint32 `json:"sort" gorm:"index"` // 排序 } func (*HotSearch) TableName() string { return "hot_search" } func GetSearchHistoryList(uid uint32) ([]SearchHistory, error) { var historys []SearchHistory err := NewSearchHistoryQuerySet(DB).UidEq(uid).OrderDescByCreatedAt().Limit(10).All(&historys) if err != nil && err != RecordNotFound { logger.Error("err:", err) return historys, err } return historys, nil } func SearchHistoryAdd(uid uint32, key string) error { history := SearchHistory{ Uid: uid, Keyword: key, } if key == "" { return nil } err := DB.Create(&history).Error if err != nil { logger.Error("err:", err) return err } return nil } func HotSearchList() ([]HotSearch, error) { hots := make([]HotSearch, 0) err := NewHotSearchQuerySet(DB).OrderDescBySort().Limit(10).All(&hots) if err != nil && err != RecordNotFound { logger.Error("err:", err) return hots, err } return hots, nil }