package mysqlBz import ( orm "duoduo/database" "math/rand" "time" ) type BiZhi struct { ID int `gorm:"column:id;type:bigint(20);primary_key" json:"id"` // 壁纸id Url string `gorm:"column:url;type:varchar(255)" json:"url"` // 壁纸链接 Label string `gorm:"column:label;type:varchar(255)" json:"label"` // 标签 CreateTime string `gorm:"column:create_time;type:datetime" json:"create_time"` // 插入时间 UpdateTime string `gorm:"column:update_time;type:datetime" json:"update_time"` // 更新时间 } func (m *BiZhi) TableName() string { return "bizhi" } //list 接口使用 func (o *BiZhi) GetRoundBiZhi() ([]BiZhi, error) { var doc []BiZhi var idList []int var count int err := orm.BzMysql.Table("bizhi").Count(&count).Error if err != nil { return nil, err } rand.Seed(time.Now().UnixNano()) for { if len(idList) > 8 { break } num := rand.Intn(count-1+1) + 1 //[n-m] for ii := 0; ii < len(idList); ii++ { if idList[ii] == num { continue } } idList = append(idList, num) } table := orm.BzMysql.Table("bizhi ") table = table.Where("id in (?) ", idList) if err = table.Select("*").Order("id desc").Limit(8).Find(&doc).Error; err != nil { return nil, err } return doc, nil }