pets.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package mysqlLy
  2. import (
  3. orm "duoduo/database"
  4. "github.com/shopspring/decimal"
  5. "time"
  6. )
  7. type Pets struct {
  8. ID int64 `gorm:"column:id;type:bigint(20);primary_key;AUTO_INCREMENT" json:"id"`
  9. UserID int64 `gorm:"column:user_id;type:bigint(20)" json:"user_id"`
  10. Name string `gorm:"column:name;type:varchar(64)" json:"name"` // 昵称
  11. Pet string `gorm:"column:pet;type:varchar(64)" json:"pet"` // 品种
  12. Age string `gorm:"column:age;type:varchar(16)" json:"age"` // 年龄 '0-3个月','3-6个月','6-9个月','小于1岁','小于2岁','小于3岁','大于3岁'
  13. Sex string `gorm:"column:sex;type:varchar(4)" json:"sex"` // 性别 MM GG
  14. PetStatus int `gorm:"column:pet_status;type:tinyint(4)" json:"pet_status"` // 1-流浪 2-家养 3-收养所
  15. PetDes string `gorm:"column:pet_des;type:varchar(16)" json:"pet_des"` // 宠物详细种类
  16. IsSterilization int `gorm:"column:is_sterilization;type:tinyint(4)" json:"is_sterilization"` // 绝育状态:1-是 2-否 3-不确认
  17. IsExpellingParasite int `gorm:"column:is_expelling_parasite;type:tinyint(4)" json:"is_expelling_parasite"` // 驱虫状态:1-是 2-否 3-不确认
  18. IsImmune int `gorm:"column:is_immune;type:tinyint(4)" json:"is_immune"` // 免疫状态:1-是 2-否 3-不确认
  19. IsFee int `gorm:"column:is_fee;type:tinyint(4)" json:"is_fee"` // 1-免费 2-押金 3-有偿
  20. Deposit decimal.Decimal `gorm:"column:deposit;type:decimal(10,2)" json:"deposit"` // 押金
  21. DepositDate time.Time `gorm:"column:deposit_date;type:date;default:null" json:"deposit_date"` // 押金到期时间
  22. Amount decimal.Decimal `gorm:"column:amount;type:decimal(10,2)" json:"amount"` // 金额
  23. Province string `gorm:"column:province;type:varchar(16)" json:"province"` // 省
  24. City string `gorm:"column:city;type:varchar(16)" json:"city"` // 市
  25. Area string `gorm:"column:area;type:varchar(16)" json:"area"` // 区
  26. PetInfo string `gorm:"column:pet_info;type:text" json:"pet_info"` // 宠物信息
  27. AdoptInfo string `gorm:"column:adopt_info;type:text" json:"adopt_info"` // 领养要求
  28. Pictures string `gorm:"column:pictures;type:json" json:"pictures"` // 图片url
  29. Status int `gorm:"column:status;type:tinyint(4)" json:"status"` // 状态 1-待领养 2-已领养
  30. CreateTime time.Time `gorm:"column:create_time;type:datetime" json:"create_time"` // 创建时间
  31. UpdateTime time.Time `gorm:"column:update_time;type:datetime" json:"update_time"` // 更新时间
  32. OpenId string `gorm:"column:open_id;type:varchar(255)" json:"open_id"` //openid
  33. }
  34. func (m *Pets) TableName() string {
  35. return "pets"
  36. }
  37. func (m *Pets) Create() (Pets, error) {
  38. var doc Pets
  39. result := orm.LyMysql.Table(m.TableName()).Create(&m)
  40. if result.Error != nil {
  41. err := result.Error
  42. return doc, err
  43. }
  44. doc = *m
  45. return doc, nil
  46. }
  47. //list 接口使用
  48. func (m *Pets) GetPetsList(pageSize int, pageIndex int) ([]Pets, int, error) {
  49. var doc []Pets
  50. table := orm.LyMysql.Table(m.TableName())
  51. if m.OpenId != "" {
  52. table = table.Where("open_id = ? ", m.OpenId)
  53. }
  54. if m.Pet != "" { //品种
  55. table = table.Where("pet = ? ", m.Pet)
  56. }
  57. if m.City != "" { //城市
  58. table = table.Where("city = ?", m.City)
  59. }
  60. if m.Status != 0 { //状态
  61. table = table.Where("status = ?", m.Status)
  62. }
  63. if m.IsFee == 1 || m.IsFee == 2 { //
  64. table = table.Where("is_fee in (1,2)")
  65. } else if m.IsFee == 3 {
  66. table = table.Where("is_fee = 3")
  67. }
  68. var count int
  69. if err := table.Order("id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
  70. return nil, 0, err
  71. }
  72. table.Count(&count)
  73. return doc, count, nil
  74. }
  75. func (m *Pets) Get() (Pets, error) {
  76. var doc Pets
  77. err := orm.LyMysql.Where("id = ? ", m.ID).Table(m.TableName()).First(&doc).Error
  78. if err != nil {
  79. return doc, err
  80. }
  81. return doc, nil
  82. }