template_user.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 AccountStatus string
  12. const (
  13. Open AccountStatus = "open"
  14. Opening AccountStatus = "opening"
  15. UnOpen AccountStatus = "unOpen"
  16. )
  17. type TemplateUser struct {
  18. Id int `gorm:"column:id;primaryKey;autoIncrement:'id'"`
  19. Username string `gorm:"column:username;type:varchar(20);comment:用户名"`
  20. AreaCode string `gorm:"column:area_code;type varchar(10);comment:区号"`
  21. Mobile string `gorm:"column:mobile;type:varchar(15);comment:手机号"`
  22. OpenId string `gorm:"column:open_id;type:varchar(50);comment:open_id"`
  23. GzhOpenId string `gorm:"column:gzh_open_id;type:varchar(255);comment:gzh_open_id"`
  24. UnionId string `gorm:"column:union_id;type:varchar(50);comment:union_id"`
  25. unt int `gorm:"column:read_count;type:int(11);comment:阅读次数"`
  26. FollowingGzh int `gorm:"column:following_gzh;type:int(1);comment:是否关注公众号"`
  27. LastReadTime time.Time `gorm:"column:last_read_time;type:timestamps;comment:最后阅读时间"`
  28. LastLoginTime time.Time `gorm:"column:last_login_time;type:timestamps;comment:最后登录时间"`
  29. LastLogoutTime time.Time `gorm:"column:last_logout_time;type:timestamps;comment:最后登出时间"`
  30. LoginStatus LogType `gorm:"column:login_status;type:enum('login','logout');comment:登录"`
  31. AccountStatus AccountStatus `gorm:"column:account_status;type:enum('normal','risk');comment:账号状态"`
  32. RiskLevel string `gorm:"column:risk_level;type:varchar(255);comment:风险等级"`
  33. RiskValidEndDate string `gorm:"column:risk_valid_end_date;type:varchar(20);comment:风险等级有效期"`
  34. IsDeleted int `gorm:"column:is_deleted;type:int(11);comment:是否删除"`
  35. CreatedTime time.Time `gorm:"column:created_time;type:timestamps;comment:创建时间"`
  36. UpdatedTime time.Time `gorm:"column:updated_time;type:timestamps;comment:更新时间"`
  37. }
  38. func UpdateRiskLevelInfo(user TemplateUser) (err error) {
  39. db := models.Main()
  40. return db.Save(user).Error
  41. }
  42. func (t *TemplateUser) BeforeCreate(tx *gorm.DB) (err error) {
  43. t.CreatedTime = time.Now()
  44. t.LoginStatus = Logout
  45. t.IsDeleted = NotDeleted
  46. t.AccountStatus = UnOpen
  47. return
  48. }
  49. func GetUserByMobile(mobile string) (user TemplateUser, err error) {
  50. return queryByColumn("mobile", mobile)
  51. }
  52. func GetUserByIdAndCountReadTimes(userId int) (err error) {
  53. return models.Main().Model(&TemplateUser{}).Where("id = ?", userId).Update("read_count", gorm.Expr("read_count + ?", 1)).Error
  54. }
  55. func GetUserByOpenId(openId string) (user TemplateUser, err error) {
  56. return queryByColumn("open_id", openId)
  57. }
  58. func GetUserById(userId int) (user TemplateUser, err error) {
  59. return queryByColumn("id", userId)
  60. }
  61. func GetUserByUnionId(unionId string) (user TemplateUser, err error) {
  62. return queryByColumn("union_id", unionId)
  63. }
  64. func queryByColumn(column string, value interface{}) (user TemplateUser, err error) {
  65. err = models.Main().Unscoped().Where(column, value).First(&user).Error
  66. return
  67. }
  68. func RegisterTemplateUser(user *TemplateUser) (err error) {
  69. err = models.Main().Create(&user).Error
  70. return
  71. }
  72. func BindMobile(userId int, mobile string, areaCode string) (err error) {
  73. err = models.Main().Table("template_users").Where("id = ?", userId).Updates(map[string]interface{}{"area_code": areaCode, "mobile": mobile, "updated_time": time.Now()}).Error
  74. return
  75. }
  76. func BindXcxOpenId(userId int, xcxOpenId string) (err error) {
  77. err = models.Main().Table("template_users").Where("id = ?", userId).Updates(map[string]interface{}{"open_id": xcxOpenId, "updated_time": time.Now()}).Error
  78. return
  79. }
  80. func BindGzhOpenId(userId int, gzhOpenId string, followingGzh int) (err error) {
  81. 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
  82. return
  83. }
  84. func GetUserByGzhOpenId(gzhOpenId string) (user TemplateUser, err error) {
  85. return queryByColumn("gzh_open_id", gzhOpenId)
  86. }
  87. func UserLogin(userId int) (err error) {
  88. 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
  89. return
  90. }
  91. func UserLogout(userId int) (err error) {
  92. 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
  93. return
  94. }