bizhi.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. type BiZhiCollection struct {
  16. ID int `gorm:"column:id;type:bigint(20);primary_key" json:"id"` // 壁纸id
  17. Url string `gorm:"column:url;type:varchar(255)" json:"url"` // 壁纸链接
  18. Label string `gorm:"column:label;type:varchar(255)" json:"label"` // 标签
  19. CreateTime string `gorm:"column:create_time;type:datetime" json:"create_time"` // 插入时间
  20. UpdateTime string `gorm:"column:update_time;type:datetime" json:"update_time"` // 更新时间
  21. BiZhiId int `gorm:"column:bizhi_id;type:bigint(20)" json:"biZhiId"`
  22. }
  23. func (m *BiZhi) TableName() string {
  24. return "bizhi"
  25. }
  26. //list 接口使用
  27. func (o *BiZhi) GetRoundBiZhi() ([]BiZhi, error) {
  28. var doc []BiZhi
  29. var idList []int
  30. var count int
  31. var status int
  32. err := orm.BzMysql.Table("bizhi").Count(&count).Error
  33. if err != nil {
  34. return nil, err
  35. }
  36. rand.Seed(time.Now().UnixNano())
  37. for {
  38. status = 0
  39. //fmt.Println(idList)
  40. if len(idList) > 8 {
  41. break
  42. }
  43. num := rand.Intn(count-1+1) + 1 //[n-m]
  44. for ii := 0; ii < len(idList); ii++ {
  45. if idList[ii] == num {
  46. status = 1
  47. break
  48. }
  49. }
  50. if status == 0 {
  51. idList = append(idList, num)
  52. }
  53. }
  54. table := orm.BzMysql.Table("bizhi ")
  55. table = table.Where("id in (?) ", idList)
  56. if err = table.Select("*").Order("id desc").Limit(8).Find(&doc).Error; err != nil {
  57. return nil, err
  58. }
  59. return doc, nil
  60. }
  61. func (o *BiZhi) GetBiZhiLabel(label string) (BiZhi, error) {
  62. var doc BiZhi
  63. table := orm.BzMysql.Table("bizhi ")
  64. if label == "" {
  65. table = table.Where("label = '' or label is null")
  66. } else {
  67. table = table.Where(" label like '%?%'", label)
  68. }
  69. if err := table.Select("*").Order("id desc").Limit(1).Find(&doc).Error; err != nil {
  70. return doc, err
  71. }
  72. return doc, nil
  73. }
  74. func (o *BiZhi) BiZhiLabelUpdate(label string) error {
  75. if err := orm.BzMysql.Table(o.TableName()).Model(&o).Where("id = ? ", o.ID).Updates(
  76. map[string]interface{}{
  77. "label": label,
  78. "update_time": tools.GetCurrntTimeStr()}).Error; err != nil {
  79. return err
  80. }
  81. return nil
  82. }
  83. func (o *BiZhi) BiZhiGroup(label string, openid string) ([]BiZhiCollection, error) {
  84. var doc []BiZhiCollection
  85. var data []BiZhiCollection
  86. var bizhi BiZhiCollection
  87. table := orm.BzMysql.Table("bizhi b ").Where("label = ?", label)
  88. if err := table.Select("b.*,(SELECT bizhi_id FROM collection WHERE bizhi_id = b.id and open_id = ?) as bizhi_id", openid).Order("id desc").Limit(30).Find(&doc).Error; err != nil {
  89. return doc, err
  90. }
  91. for i := 0; i < len(doc); i++ {
  92. if doc[i].ID == o.ID {
  93. bizhi.Url = doc[i].Url
  94. bizhi.ID = doc[i].ID
  95. bizhi.Label = doc[i].Label
  96. bizhi.BiZhiId = doc[i].BiZhiId
  97. }
  98. }
  99. data = append(data, bizhi)
  100. for i := 0; i < len(doc); i++ {
  101. if doc[i].ID == o.ID {
  102. continue
  103. } else {
  104. data = append(data, doc[i])
  105. }
  106. }
  107. return data, nil
  108. }
  109. func (o *BiZhi) BiZhiDes() (BiZhi, error) {
  110. var doc BiZhi
  111. err := orm.BzMysql.Table("bizhi").Where("id = ?", o.ID).First(&doc).Error
  112. if err != nil {
  113. return doc, err
  114. }
  115. return doc, nil
  116. }
  117. //list 接口使用
  118. func (o *BiZhi) GetLabelList(pageSize int, pageIndex int) ([]BiZhi, int, error) {
  119. var doc []BiZhi
  120. table := orm.BzMysql.Table("bizhi").Where("label like ?", "%"+o.Label+"%")
  121. var count int
  122. if err := table.Select("*").Order("id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
  123. return nil, 0, err
  124. }
  125. table.Count(&count)
  126. return doc, count, nil
  127. }