bizhi.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. var status int
  23. err := orm.BzMysql.Table("bizhi").Count(&count).Error
  24. if err != nil {
  25. return nil, err
  26. }
  27. rand.Seed(time.Now().UnixNano())
  28. for {
  29. status = 0
  30. //fmt.Println(idList)
  31. if len(idList) > 8 {
  32. break
  33. }
  34. num := rand.Intn(count-1+1) + 1 //[n-m]
  35. for ii := 0; ii < len(idList); ii++ {
  36. if idList[ii] == num {
  37. status = 1
  38. break
  39. }
  40. }
  41. if status == 0 {
  42. idList = append(idList, num)
  43. }
  44. }
  45. table := orm.BzMysql.Table("bizhi ")
  46. table = table.Where("id in (?) ", idList)
  47. if err = table.Select("*").Order("id desc").Limit(8).Find(&doc).Error; err != nil {
  48. return nil, err
  49. }
  50. return doc, nil
  51. }