template_user.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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');comment:登录"`
  24. RiskLevel string `gorm:"column:risk_level;type:varchar(255);comment:风险等级"`
  25. IsDeleted int `gorm:"column:is_deleted;type:int(11);comment:是否删除"`
  26. CreatedTime time.Time `gorm:"column:created_time;type:timestamps;comment:创建时间"`
  27. UpdatedTime time.Time `gorm:"column:updated_time;type:timestamps;comment:更新时间"`
  28. }
  29. func (t *TemplateUser) BeforeCreate(tx *gorm.DB) (err error) {
  30. t.CreatedTime = time.Now()
  31. t.LoginStatus = Logout
  32. t.IsDeleted = NotDeleted
  33. return
  34. }
  35. func GetUserByMobile(mobile string) (user TemplateUser, err error) {
  36. return queryByColumn("mobile", mobile)
  37. }
  38. func GetUserByIdAndCountReadTimes(userId int) (err error) {
  39. return models.Main().Model(&TemplateUser{}).Where("id = ?", userId).Update("read_count", gorm.Expr("read_count + ?", 1)).Error
  40. }
  41. func GetUserByOpenId(openId string) (user TemplateUser, err error) {
  42. return queryByColumn("open_id", openId)
  43. }
  44. func GetUserByUnionId(unionId string) (user TemplateUser, err error) {
  45. return queryByColumn("union_id", unionId)
  46. }
  47. func queryByColumn(column string, value string) (user TemplateUser, err error) {
  48. err = models.Main().Unscoped().Where(column, value).First(&user).Error
  49. return
  50. }
  51. func RegisterTemplateUser(user *TemplateUser) (err error) {
  52. err = models.Main().Create(&user).Error
  53. return
  54. }
  55. func BindMobile(userId int, mobile string) (err error) {
  56. err = models.Main().Table("template_users").Where("id = ?", userId).Updates(map[string]interface{}{"mobile": mobile, "updated_time": time.Now()}).Error
  57. return
  58. }
  59. func BindXcxOpenId(userId int, xcxOpenId string) (err error) {
  60. err = models.Main().Table("template_users").Where("id = ?", userId).Updates(map[string]interface{}{"open_id": xcxOpenId, "updated_time": time.Now()}).Error
  61. return
  62. }
  63. func BindGzhOpenId(userId int, gzhOpenId string, followingGzh int) (err error) {
  64. 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
  65. return
  66. }
  67. func GetUserByGzhOpenId(gzhOpenId string) (user TemplateUser, err error) {
  68. return queryByColumn("gzh_open_id", gzhOpenId)
  69. }
  70. func UserLogin(userId int) (err error) {
  71. 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
  72. return
  73. }
  74. func UserLogout(userId int) (err error) {
  75. 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
  76. return
  77. }