123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- package user
- import (
- "eta/eta_mini_ht_api/models"
- "gorm.io/gorm"
- "time"
- )
- const (
- NotDeleted = 0
- Deleted = 1
- )
- type AccountStatus string
- const (
- Open AccountStatus = "open"
- Opening AccountStatus = "opening"
- UnOpen AccountStatus = "unOpen"
- )
- type TemplateUser struct {
- Id int `gorm:"column:id;primaryKey;autoIncrement:'id'"`
- Username string `gorm:"column:username;type:varchar(20);comment:用户名"`
- AreaCode string `gorm:"column:area_code;type varchar(10);comment:区号"`
- Mobile string `gorm:"column:mobile;type:varchar(15);comment:手机号"`
- OpenId string `gorm:"column:open_id;type:varchar(50);comment:open_id"`
- GzhOpenId string `gorm:"column:gzh_open_id;type:varchar(255);comment:gzh_open_id"`
- UnionId string `gorm:"column:union_id;type:varchar(50);comment:union_id"`
- unt int `gorm:"column:read_count;type:int(11);comment:阅读次数"`
- FollowingGzh int `gorm:"column:following_gzh;type:int(1);comment:是否关注公众号"`
- LastReadTime time.Time `gorm:"column:last_read_time;type:timestamps;comment:最后阅读时间"`
- LastLoginTime time.Time `gorm:"column:last_login_time;type:timestamps;comment:最后登录时间"`
- LastLogoutTime time.Time `gorm:"column:last_logout_time;type:timestamps;comment:最后登出时间"`
- LoginStatus LogType `gorm:"column:login_status;type:enum('login','logout');comment:登录"`
- AccountStatus AccountStatus `gorm:"column:account_status;type:enum('normal','risk');comment:账号状态"`
- RiskLevel string `gorm:"column:risk_level;type:varchar(255);comment:风险等级"`
- RiskValidEndDate string `gorm:"column:risk_valid_end_date;type:varchar(20);comment:风险等级有效期"`
- IsDeleted int `gorm:"column:is_deleted;type:int(11);comment:是否删除"`
- CreatedTime time.Time `gorm:"column:created_time;type:timestamps;comment:创建时间"`
- UpdatedTime time.Time `gorm:"column:updated_time;type:timestamps;comment:更新时间"`
- }
- func UpdateRiskLevelInfo(user TemplateUser) (err error) {
- db := models.Main()
- return db.Save(user).Error
- }
- func (t *TemplateUser) BeforeCreate(tx *gorm.DB) (err error) {
- t.CreatedTime = time.Now()
- t.LoginStatus = Logout
- t.IsDeleted = NotDeleted
- t.AccountStatus = UnOpen
- return
- }
- func GetUserByMobile(mobile string) (user TemplateUser, err error) {
- return queryByColumn("mobile", mobile)
- }
- func GetUserByIdAndCountReadTimes(userId int) (err error) {
- return models.Main().Model(&TemplateUser{}).Where("id = ?", userId).Update("read_count", gorm.Expr("read_count + ?", 1)).Error
- }
- func GetUserByOpenId(openId string) (user TemplateUser, err error) {
- return queryByColumn("open_id", openId)
- }
- func GetUserById(userId int) (user TemplateUser, err error) {
- return queryByColumn("id", userId)
- }
- func GetUserByUnionId(unionId string) (user TemplateUser, err error) {
- return queryByColumn("union_id", unionId)
- }
- func queryByColumn(column string, value interface{}) (user TemplateUser, err error) {
- err = models.Main().Unscoped().Where(column, value).First(&user).Error
- return
- }
- func RegisterTemplateUser(user *TemplateUser) (err error) {
- err = models.Main().Create(&user).Error
- return
- }
- func BindMobile(userId int, mobile string, areaCode string) (err error) {
- err = models.Main().Table("template_users").Where("id = ?", userId).Updates(map[string]interface{}{"area_code": areaCode, "mobile": mobile, "updated_time": time.Now()}).Error
- return
- }
- func BindXcxOpenId(userId int, xcxOpenId string) (err error) {
- err = models.Main().Table("template_users").Where("id = ?", userId).Updates(map[string]interface{}{"open_id": xcxOpenId, "updated_time": time.Now()}).Error
- return
- }
- func BindGzhOpenId(userId int, gzhOpenId string, followingGzh int) (err error) {
- 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
- return
- }
- func GetUserByGzhOpenId(gzhOpenId string) (user TemplateUser, err error) {
- return queryByColumn("gzh_open_id", gzhOpenId)
- }
- func UserLogin(userId int) (err error) {
- 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
- return
- }
- func UserLogout(userId int) (err error) {
- 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
- return
- }
|