template_user.go 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package user
  2. import (
  3. "eta/eta_mini_ht_api/models"
  4. "gorm.io/gorm"
  5. "time"
  6. )
  7. const (
  8. NotDeleted = 0
  9. Deleted = 1
  10. )
  11. type TemplateUser struct {
  12. Id int `gorm:"column:id;primaryKey;autoIncrement:'id'"`
  13. Username string `gorm:"column:username;type:varchar(20);comment:用户名"`
  14. Mobile string `gorm:"column:mobile;type:varchar(15);comment:手机号"`
  15. OpenId string `gorm:"column:open_id;type:varchar(50);comment:open_id"`
  16. GzhOpenId string `gorm:"column:gzh_open_id;type:varchar(255);comment:gzh_open_id"`
  17. UnionId string `gorm:"column:union_id;type:varchar(50);comment:union_id"`
  18. ReadCount int `gorm:"column:read_count;type:int(11);comment:阅读次数"`
  19. FollowingGzh int `gorm:"column:following_gzh;type:int(1);comment:是否关注公众号"`
  20. LastReadTime time.Time `gorm:"column:last_read_time;type:timestamps;comment:最后阅读时间"`
  21. LastLoginTime time.Time `gorm:"column:last_login_time;type:timestamps;comment:最后登录时间"`
  22. LastLogoutTime time.Time `gorm:"column:last_logout_time;type:timestamps;comment:最后登出时间"`
  23. LoginStatus LogType `gorm:"column:login_status;type:enum('login','logout');default:null;comment:登录`
  24. IsDeleted int `gorm:"column:is_deleted;type:int(11);comment:是否删除"`
  25. CreatedTime time.Time `gorm:"column:created_time;type:timestamps;comment:创建时间"`
  26. UpdatedTime time.Time `gorm:"column:updated_time;type:timestamps;comment:更新时间"`
  27. }
  28. func (t *TemplateUser) BeforeCreate(tx *gorm.DB) (err error) {
  29. t.CreatedTime = time.Now()
  30. t.IsDeleted = NotDeleted
  31. return
  32. }
  33. func GetUserByMobile(mobile string) (user TemplateUser, err error) {
  34. return queryByColumn("mobile", mobile)
  35. }
  36. func GetUserByIdAndCountReadTimes(userId int) (err error) {
  37. return models.Main().Model(&TemplateUser{}).Where("id = ?", userId).Update("read_count", gorm.Expr("read_count + ?", 1)).Error
  38. }
  39. func GetUserByOpenId(openId string) (user TemplateUser, err error) {
  40. return queryByColumn("open_id", openId)
  41. }
  42. func GetUserByUnionId(unionId string) (user TemplateUser, err error) {
  43. return queryByColumn("union_id", unionId)
  44. }
  45. func queryByColumn(column string, value string) (user TemplateUser, err error) {
  46. err = models.Main().Unscoped().Where(column, value).First(&user).Error
  47. return
  48. }
  49. func RegisterTemplateUser(user *TemplateUser) (err error) {
  50. err = models.Main().Create(&user).Error
  51. return
  52. }
  53. func BindMobile(userId int, mobile string) (err error) {
  54. err = models.Main().Table("template_users").Where("id = ?", userId).Updates(map[string]interface{}{"mobile": mobile, "updated_time": time.Now()}).Error
  55. return
  56. }
  57. func BindXcxOpenId(userId int, xcxOpenId string) (err error) {
  58. err = models.Main().Table("template_users").Where("id = ?", userId).Updates(map[string]interface{}{"open_id": xcxOpenId, "updated_time": time.Now()}).Error
  59. return
  60. }
  61. func BindGzhOpenId(userId int, gzhOpenId string, followingGzh int) (err error) {
  62. err = models.Main().Table("template_users").Where("id = ?", userId).Updates(map[string]interface{}{"gzh_open_id": gzhOpenId, "following_gzh": followingGzh, "updated_time": time.Now()}).Error
  63. return
  64. }
  65. func GetUserByGzhOpenId(gzhOpenId string) (user TemplateUser, err error) {
  66. return queryByColumn("gzh_open_id", gzhOpenId)
  67. }
  68. func UserLogin(userId int) (err error) {
  69. err = models.Main().Table("template_users").Where("id = ?", userId).Updates(map[string]interface{}{"login_status": Login, "last_login_time": time.Now(), "updated_time": time.Now()}).Error
  70. return
  71. }
  72. func UserLogout(userId int) (err error) {
  73. err = models.Main().Table("template_users").Where("id = ?", userId).Updates(map[string]interface{}{"login_status": Logout, "last_logout_time": time.Now(), "updated_time": time.Now()}).Error
  74. return
  75. }