user.go 4.2 KB

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