package dao import ( "gorm.io/gorm" "hilo/app/service/user/internal/model" "time" ) func (d *Dao) GetUser(userId int64) (res *model.User, err error) { res = &model.User{} err = d.DB.Model(&model.User{}).Where("id = ?", userId).Take(res).Error return } func (d *Dao) CreateUser(user *model.User) (*model.User, error) { now := time.Now() user.CreatedTime = now user.UpdatedTime = now err := d.DB.Model(&model.User{}).Create(user).Error return user, err } func (d *Dao) UpdateUser(userId int64, user *model.User) (res *model.User, err error) { now := time.Now() user.UpdatedTime = now err = d.DB.Model(&model.User{}).Where("id = ?", userId).Updates(user).Error return user, err } func (d *Dao) UpdateUserOauth(id int64, uo *model.UserOauth) (res *model.UserOauth, err error) { err = d.DB.Model(&model.UserOauth{}).Where("id = ?", id).Updates(uo).Error return uo, err } func (d *Dao) GetUserOauth(thirdPartyId string, thirdPartyType int) (res *model.UserOauth, err error) { res = &model.UserOauth{} err = d.DB.Model(&model.UserOauth{}). Where("third_party_id = ? and third_party_type = ?", thirdPartyId, thirdPartyType). Take(res). Error if err != nil && err == gorm.ErrRecordNotFound { return nil, nil } return } func (d *Dao) CountImei(imei string) (int64, error) { var c int64 = 0 if err := d.DB.Model(&model.UserImei{}).Where("imei = ?", imei). Count(&c).Error; err != nil { return 0, err } return c, nil } func (d *Dao) CountImeiThird(imei string, thirdPartyType int) (int64, error) { var c int64 = 0 if err := d.DB.Table((&model.UserImei{}).TableName()). Joins("left join user_oauth o on o.user_id = user_imei.user_id"). Where("user_imei.imei = ?", imei). Where("o.third_party_type = ?", thirdPartyType). Count(&c).Error; err != nil { return 0, err } return c, nil } func (d *Dao) CountIp(ip string, beginTime time.Time) (int64, error) { var c int64 = 0 if err := d.DB.Model(&model.UserIp{}).Where("ip = ?", ip). Where("created_time >= ?", beginTime).Count(&c).Error; err != nil { return 0, err } return c, nil } func (d *Dao) AddUserIp(userId int64, ip string) (err error) { res := &model.UserIp{} err = d.DB.Where(&model.UserIp{UserId: userId}).Take(res).Error if err != nil && err != gorm.ErrRecordNotFound { return err } now := time.Now() if err == gorm.ErrRecordNotFound { err = d.DB.Model(&model.UserIp{}).Create(&model.UserIp{ Ip: ip, CreatedTime: now, UpdatedTime: now, UserId: userId, }).Error } else { err = d.DB.Model(&model.UserIp{}).Where("user_id = ?", userId).Updates(&model.UserIp{ Ip: ip, UpdatedTime: now, }).Error } return } func (d *Dao) GetVip(userId int64) (*model.UserVip, error) { rows := make([]*model.UserVip, 0) err := d.DB.Where("user_id = ? AND expire_at >= NOW()", userId).Find(&rows).Error if err != nil { return nil, err } if len(rows) > 0 { return rows[0], nil } return nil, nil } func (d *Dao) GetUserMedal(userId int64) ([]int, error) { rows := make([]*model.UserMedal, 0) if err := d.DB.Model(&model.UserMedal{}).Where("user_id = ? AND (end_time >= NOW() or end_time is null)", userId).Find(&rows).Error; err != nil { return nil, err } result := make([]int, 0) for _, i := range rows { result = append(result, i.MedalId) } return result, nil } func (d *Dao) BatchGetPropertyMapByUserIds(userIds []int64) (map[int64]int64, error) { rows := make([]*model.UserProperty, 0) if err := d.DB.Model(&model.UserProperty{}). Where("end_time > NOW() AND user_id IN ?", userIds).Order("id DESC").Find(&rows).Error; err != nil { return nil, err } result := make(map[int64]int64, 0) tmp := make(map[int64]int64, 0) for _, i := range rows { if _, ok := result[i.UserId]; !ok { if i.Using == model.UserPropertyYesUsing { // using = true且id最大,就确定是当前使用的 result[i.UserId] = i.PropertyId } else if _, ok := tmp[i.UserId]; !ok { // using = false且id最大,先记下,因为不知道还有没有using=true的 tmp[i.UserId] = i.PropertyId } } } for k, v := range tmp { // result中没有的,就采用tmp保存的 if _, ok := result[k]; !ok { result[k] = v } } return result, nil } func (d *Dao) GetUserProperty(userId int64, propertyId int64) (*model.UserProperty, error) { userProperty := &model.UserProperty{} if err := d.DB.Model(&model.UserProperty{}).Where(&model.UserProperty{ UserId: userId, PropertyId: propertyId, }).First(&userProperty).Error; err != nil { if err == gorm.ErrRecordNotFound { return &model.UserProperty{ UserId: userId, PropertyId: propertyId, EndTime: time.Now(), }, nil } else { return nil, err } } return userProperty, nil } // ListNoble 查询用户未过期的贵族 func (d *Dao) ListNoble(userId int64) ([]*model.UserNoble, error) { rows := make([]*model.UserNoble, 0) if err := d.DB.Where("end_time>=NOW() and user_id = ?", userId).Order("level DESC").Find(&rows).Error; err != nil { return nil, err } return rows, nil } func (d *Dao) GetDiamond(userId int64) (*model.CvDiamond, error) { var diamondAccount = &model.DiamondAccount{} err := d.DB.Model(&model.DiamondAccount{}).Where(&model.DiamondAccount{ UserId: userId, }).First(&diamondAccount).Error if err != nil { return nil, err } return &model.CvDiamond{DiamondNum: diamondAccount.DiamondNum}, nil }