user.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package mysql
  2. import (
  3. orm "duoduo/database"
  4. "duoduo/tools"
  5. )
  6. type User struct {
  7. CreateTime string `gorm:"column:create_time" json:"createTime"`
  8. OpenID string `gorm:"column:open_id" json:"openId"`
  9. Phone string `gorm:"column:phone" json:"phone"`
  10. Pid string `gorm:"column:pid" json:"pid"`
  11. UpdateTime string `gorm:"column:update_time" json:"updateTime"`
  12. NickName string `gorm:"column:nick_name" json:"nickName"`
  13. AvatarUrl string `gorm:"column:avatar_url" json:"avatarUrl"`
  14. }
  15. // TableName sets the insert table name for this struct type
  16. func (u *User) TableName() string {
  17. return "user"
  18. }
  19. func (u *User) GetNum() int {
  20. var count int
  21. tableCount := orm.Eloquent.Table(u.TableName()).Where("open_id = ? ", u.OpenID)
  22. tableCount.Count(&count)
  23. return count
  24. }
  25. func (u *User) Get() (User, error) {
  26. var doc User
  27. err := orm.Eloquent.Select("pid,nick_name,avatar_url").Where("open_id = ? ", u.OpenID).Table(u.TableName()).First(&doc).Error
  28. if err != nil {
  29. return doc, err
  30. }
  31. return doc, nil
  32. }
  33. func (u *User) Create() (User, error) {
  34. var doc User
  35. result := orm.Eloquent.Table(u.TableName()).Create(&u)
  36. if result.Error != nil {
  37. err := result.Error
  38. return doc, err
  39. }
  40. doc = *u
  41. return doc, nil
  42. }
  43. func (u *User) Update() (update User, err error) {
  44. if err := orm.Eloquent.Table(u.TableName()).Model(&update).Where("open_id = ? ", u.OpenID).Updates(
  45. map[string]interface{}{
  46. "pid": u.Pid,
  47. "update_time": tools.GetCurrntTimeStr()}).Error; err != nil {
  48. return update, err
  49. }
  50. return update, nil
  51. }
  52. func (u *User) UpdateName() (update User, err error) {
  53. if err := orm.Eloquent.Table(u.TableName()).Model(&update).Where("open_id = ? ", u.OpenID).Updates(
  54. map[string]interface{}{
  55. "nick_name": u.NickName,
  56. "avatar_url": u.AvatarUrl,
  57. "update_time": tools.GetCurrntTimeStr()}).Error; err != nil {
  58. return update, err
  59. }
  60. return update, nil
  61. }