bizhi.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package mysqlBz
  2. import (
  3. orm "duoduo/database"
  4. "duoduo/tools"
  5. "math/rand"
  6. "time"
  7. )
  8. type BiZhi struct {
  9. ID int `gorm:"column:id;type:bigint(20);primary_key" json:"id"` // 壁纸id
  10. Url string `gorm:"column:url;type:varchar(255)" json:"url"` // 壁纸链接
  11. Label string `gorm:"column:label;type:varchar(255)" json:"label"` // 标签
  12. CreateTime string `gorm:"column:create_time;type:datetime" json:"create_time"` // 插入时间
  13. UpdateTime string `gorm:"column:update_time;type:datetime" json:"update_time"` // 更新时间
  14. }
  15. func (m *BiZhi) TableName() string {
  16. return "bizhi"
  17. }
  18. //list 接口使用
  19. func (o *BiZhi) GetRoundBiZhi() ([]BiZhi, error) {
  20. var doc []BiZhi
  21. var idList []int
  22. var count int
  23. var status int
  24. err := orm.BzMysql.Table("bizhi").Count(&count).Error
  25. if err != nil {
  26. return nil, err
  27. }
  28. rand.Seed(time.Now().UnixNano())
  29. for {
  30. status = 0
  31. //fmt.Println(idList)
  32. if len(idList) > 8 {
  33. break
  34. }
  35. num := rand.Intn(count-1+1) + 1 //[n-m]
  36. for ii := 0; ii < len(idList); ii++ {
  37. if idList[ii] == num {
  38. status = 1
  39. break
  40. }
  41. }
  42. if status == 0 {
  43. idList = append(idList, num)
  44. }
  45. }
  46. table := orm.BzMysql.Table("bizhi ")
  47. table = table.Where("id in (?) ", idList)
  48. if err = table.Select("*").Order("id desc").Limit(8).Find(&doc).Error; err != nil {
  49. return nil, err
  50. }
  51. return doc, nil
  52. }
  53. func (o *BiZhi) GetBiZhiLabel(label string) (BiZhi, error) {
  54. var doc BiZhi
  55. table := orm.BzMysql.Table("bizhi ")
  56. if label == "" {
  57. table = table.Where("label = '' or label is null")
  58. } else {
  59. table = table.Where(" label like '%?%'", label)
  60. }
  61. if err := table.Select("*").Order("id desc").Limit(1).Find(&doc).Error; err != nil {
  62. return doc, err
  63. }
  64. return doc, nil
  65. }
  66. func (o *BiZhi) BiZhiLabelUpdate(label string) error {
  67. if err := orm.BzMysql.Table(o.TableName()).Model(&o).Where("id = ? ", o.ID).Updates(
  68. map[string]interface{}{
  69. "label": label,
  70. "update_time": tools.GetCurrntTimeStr()}).Error; err != nil {
  71. return err
  72. }
  73. return nil
  74. }