user.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package mysql
  2. import (
  3. orm "duoduo/database"
  4. "duoduo/tools"
  5. "errors"
  6. )
  7. type User struct {
  8. CreateTime string `gorm:"column:create_time" json:"createTime"`
  9. OpenID string `gorm:"column:open_id" json:"openId"`
  10. Phone string `gorm:"column:phone" json:"phone"`
  11. Pid string `gorm:"column:pid" json:"pid"`
  12. UpdateTime string `gorm:"column:update_time" json:"updateTime"`
  13. NickName string `gorm:"column:nick_name" json:"nickName"`
  14. AvatarUrl string `gorm:"column:avatar_url" json:"avatarUrl"`
  15. OpenIdOne string `gorm:"column:open_id_1" json:"openIdOne"`
  16. OpenIdTwo string `gorm:"column:open_id_2" json:"openIdTwo"`
  17. }
  18. type UserName struct {
  19. CreateTime string `gorm:"column:create_time" json:"createTime"`
  20. OpenID string `gorm:"column:open_id" json:"openId"`
  21. Phone string `gorm:"column:phone" json:"phone"`
  22. Pid string `gorm:"column:pid" json:"pid"`
  23. UpdateTime string `gorm:"column:update_time" json:"updateTime"`
  24. NickName string `gorm:"column:nick_name" json:"nickName"`
  25. AvatarUrl string `gorm:"column:avatar_url" json:"avatarUrl"`
  26. OpenIdOne string `gorm:"column:open_id_1" json:"openIdOne"`
  27. OpenIdTwo string `gorm:"column:open_id_2" json:"openIdTwo"`
  28. Id int `gorm:"column:id" json:"id"`
  29. }
  30. // TableName sets the insert table name for this struct type
  31. func (u *User) TableName() string {
  32. return "user"
  33. }
  34. func (u *User) GetNum() int {
  35. var count int
  36. tableCount := orm.Eloquent.Table(u.TableName()).Where("open_id = ? ", u.OpenID)
  37. tableCount.Count(&count)
  38. return count
  39. }
  40. func (u *User) Get() (User, error) {
  41. var doc User
  42. err := orm.Eloquent.Select("pid,nick_name,avatar_url,open_id_1,open_id_2").Where("open_id = ? ", u.OpenID).Table(u.TableName()).First(&doc).Error
  43. if err != nil {
  44. return doc, err
  45. }
  46. return doc, nil
  47. }
  48. func (u *User) Create() (User, error) {
  49. var doc User
  50. result := orm.Eloquent.Table(u.TableName()).Create(&u)
  51. if result.Error != nil {
  52. err := result.Error
  53. return doc, err
  54. }
  55. doc = *u
  56. return doc, nil
  57. }
  58. func (u *User) Update() (update User, err error) {
  59. if err := orm.Eloquent.Table(u.TableName()).Model(&update).Where("open_id = ? ", u.OpenID).Updates(
  60. map[string]interface{}{
  61. "pid": u.Pid,
  62. "update_time": tools.GetCurrntTimeStr()}).Error; err != nil {
  63. return update, err
  64. }
  65. return update, nil
  66. }
  67. func (u *User) UpdateName() (update User, err error) {
  68. if err := orm.Eloquent.Table(u.TableName()).Model(&update).Where("open_id = ? ", u.OpenID).Updates(
  69. map[string]interface{}{
  70. "nick_name": u.NickName,
  71. "avatar_url": u.AvatarUrl,
  72. "update_time": tools.GetCurrntTimeStr()}).Error; err != nil {
  73. return update, err
  74. }
  75. return update, nil
  76. }
  77. //list 接口使用
  78. func (u *User) GetOrderOpenIdList(pageSize int, pageIndex int, status int) ([]User, int, error) {
  79. var doc []User
  80. table := orm.Eloquent.Table(u.TableName())
  81. if status == 1 {
  82. table = table.Where("open_id_1 = ? ", u.OpenID)
  83. } else if status == 2 {
  84. table = table.Where("open_id_2 = ? ", u.OpenID)
  85. } else {
  86. return nil, 0, errors.New("status err")
  87. }
  88. //table = table.Where("open_id = ? ", o.OpenId)
  89. var count int
  90. if err := table.Select("id,nick_name,avatar_url").Order("id desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
  91. return nil, 0, err
  92. }
  93. table.Count(&count)
  94. return doc, count, nil
  95. }