collection.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package bizhi
  2. import orm "duoduo/database"
  3. type Collection struct {
  4. ID int `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"` // 主键
  5. OpenID string `gorm:"column:open_id;type:varchar(255)" json:"openId"`
  6. BizhiID int `gorm:"column:bizhi_id;type:bigint(20)" json:"bizhiId"` // 壁纸id
  7. Url string `gorm:"column:url;type:varchar(255)" json:"url"` // 壁纸url
  8. CreateTime string `gorm:"column:create_time;type:datetime" json:"createTime"` // 创建时间
  9. }
  10. type CollectionBiZhi struct {
  11. ID int `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"` // 主键
  12. OpenID string `gorm:"column:open_id;type:varchar(255)" json:"openId"`
  13. BizhiID int `gorm:"column:bizhi_id;type:bigint(20)" json:"bizhiId"` // 壁纸id
  14. Url string `gorm:"column:url;type:varchar(255)" json:"url"` // 壁纸url
  15. CreateTime string `gorm:"column:create_time;type:datetime" json:"createTime"` // 创建时间
  16. Label string `gorm:"column:label;type:varchar(255)" json:"label"` // 标签
  17. }
  18. func (m *Collection) TableName() string {
  19. return "collection"
  20. }
  21. func (m *Collection) Insert() error {
  22. result := orm.BzMysql.Table(m.TableName()).Create(&m)
  23. if result.Error != nil {
  24. err := result.Error
  25. return err
  26. }
  27. return nil
  28. }
  29. func (m *Collection) Del() error {
  30. result := orm.BzMysql.Table(m.TableName()).Where("bizhi_id = ? and open_id = ?", m.BizhiID, m.OpenID).Delete(nil)
  31. if result.Error != nil {
  32. err := result.Error
  33. return err
  34. }
  35. return nil
  36. }
  37. //list 接口使用
  38. func (m *Collection) GetOpenIdList(pageSize int, pageIndex int) ([]CollectionBiZhi, int, error) {
  39. var doc []CollectionBiZhi
  40. table := orm.BzMysql.Table("collection c LEFT JOIN bizhi b ON b.id = c.bizhi_id")
  41. table = table.Where("open_id = ? ", m.OpenID)
  42. //SELECT c.*,b.label FROM collection c LEFT JOIN bizhi b ON b.id = c.bizhi_id;
  43. var count int
  44. if err := table.Select("c.*,b.label").Order("id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
  45. return nil, 0, err
  46. }
  47. table.Count(&count)
  48. return doc, count, nil
  49. }
  50. //list 接口使用
  51. func (m *Collection) GetOpenIdDownList(pageSize int, pageIndex int) ([]CollectionBiZhi, int, error) {
  52. var doc []CollectionBiZhi
  53. table := orm.BzMysql.Table("download c LEFT JOIN bizhi b ON b.id = c.bizhi_id")
  54. table = table.Where("open_id = ? ", m.OpenID)
  55. //SELECT c.*,b.label FROM collection c LEFT JOIN bizhi b ON b.id = c.bizhi_id;
  56. var count int
  57. if err := table.Select("c.*,b.label").Order("id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
  58. return nil, 0, err
  59. }
  60. table.Count(&count)
  61. return doc, count, nil
  62. }