user.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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) GetTotal() (int, error) {
  43. var count int
  44. tableCount := orm.Eloquent.Table(u.TableName())
  45. tableCount.Count(&count)
  46. return count, nil
  47. }
  48. func (u *User) Get() (UserName, error) {
  49. var doc UserName
  50. err := orm.Eloquent.Select("id,pid,nick_name,avatar_url,open_id_1,open_id_2,admin").Where("open_id = ? ", u.OpenID).Table(u.TableName()).First(&doc).Error
  51. if err != nil {
  52. return doc, err
  53. }
  54. return doc, nil
  55. }
  56. func (u *User) Create() (User, error) {
  57. var doc User
  58. result := orm.Eloquent.Table(u.TableName()).Create(&u)
  59. if result.Error != nil {
  60. err := result.Error
  61. return doc, err
  62. }
  63. doc = *u
  64. return doc, nil
  65. }
  66. func (u *User) Update() (update User, err error) {
  67. if err := orm.Eloquent.Table(u.TableName()).Model(&update).Where("open_id = ? ", u.OpenID).Updates(
  68. map[string]interface{}{
  69. "pid": u.Pid,
  70. "update_time": tools.GetCurrntTimeStr()}).Error; err != nil {
  71. return update, err
  72. }
  73. return update, nil
  74. }
  75. func (u *User) UpdateName() (update User, err error) {
  76. if err := orm.Eloquent.Table(u.TableName()).Model(&update).Where("open_id = ? ", u.OpenID).Updates(
  77. map[string]interface{}{
  78. "nick_name": u.NickName,
  79. "avatar_url": u.AvatarUrl,
  80. "update_time": tools.GetCurrntTimeStr()}).Error; err != nil {
  81. return update, err
  82. }
  83. return update, nil
  84. }
  85. //list 接口使用
  86. func (u *User) GetOrderOpenIdList(pageSize int, pageIndex int, status int) ([]UserName, int, error) {
  87. var doc []UserName
  88. table := orm.Eloquent.Table(u.TableName())
  89. if status == 1 {
  90. table = table.Where("open_id_1 = ? ", u.OpenID)
  91. } else if status == 2 {
  92. table = table.Where("open_id_2 = ? ", u.OpenID)
  93. } else {
  94. return nil, 0, errors.New("status err")
  95. }
  96. //table = table.Where("open_id = ? ", o.OpenId)
  97. var count int
  98. 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 {
  99. return nil, 0, err
  100. }
  101. table.Count(&count)
  102. return doc, count, nil
  103. }