bizhi.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package mysqlBz
  2. import (
  3. orm "duoduo/database"
  4. "math/rand"
  5. "time"
  6. )
  7. type BiZhi struct {
  8. ID int `gorm:"column:id;type:bigint(20);primary_key" json:"id"` // 壁纸id
  9. Url string `gorm:"column:url;type:varchar(255)" json:"url"` // 壁纸链接
  10. Label string `gorm:"column:label;type:varchar(255)" json:"label"` // 标签
  11. CreateTime string `gorm:"column:create_time;type:datetime" json:"create_time"` // 插入时间
  12. UpdateTime string `gorm:"column:update_time;type:datetime" json:"update_time"` // 更新时间
  13. }
  14. func (m *BiZhi) TableName() string {
  15. return "bizhi"
  16. }
  17. //list 接口使用
  18. func (o *BiZhi) GetRoundBiZhi() ([]BiZhi, error) {
  19. var doc []BiZhi
  20. var idList []int
  21. var count int
  22. err := orm.BzMysql.Table("bizhi").Count(&count).Error
  23. if err != nil {
  24. return nil, err
  25. }
  26. rand.Seed(time.Now().UnixNano())
  27. for {
  28. if len(idList) > 8 {
  29. break
  30. }
  31. num := rand.Intn(count-1+1) + 1 //[n-m]
  32. for ii := 0; ii < len(idList); ii++ {
  33. if idList[ii] == num {
  34. continue
  35. }
  36. }
  37. idList = append(idList, num)
  38. }
  39. table := orm.BzMysql.Table("bizhi ")
  40. table = table.Where("id in (?) ", idList)
  41. if err = table.Select("*").Order("id desc").Limit(8).Find(&doc).Error; err != nil {
  42. return nil, err
  43. }
  44. return doc, nil
  45. }