user.go 3.5 KB

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