relation.go 3.08 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
package user_cv

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/mysql"
	"gorm.io/gorm"
	"hilo-group/_const/enum/user_e"
	"hilo-group/myerr"
	"time"
)

type Relation struct {
	// 永恒之心值
	HeartValue uint32 `json:"heartValue"`
	// 永恒之心的最大值(0代表没有永恒之心,即没有相互关注)
	HeartValueMax uint32 `json:"heartValueMax"`
	// 成长关系建立的时间(天数)
	MeetDays uint `json:"meetDays"`
}

//用户成长关系
type UserRelation struct {
	mysql.Entity
	*domain.Model `gorm:"-"`
	UserId_1      mysql.ID
	UserId_2      mysql.ID
	HeartValue    uint32
}

// 批量查询成长关系
func BatchGetRelations(userId mysql.ID, others []mysql.ID) (map[mysql.ID]Relation, error) {
	smaller := make([]mysql.ID, 0)
	greater := make([]mysql.ID, 0)
	for _, i := range others {
		if i < userId {
			smaller = append(smaller, i)
		} else if i > userId {
			greater = append(greater, i)
		}
	}
	rows := make([]UserRelation, 0)
	result := make(map[mysql.ID]Relation, 0)
	var err error
	if len(greater) > 0 {
		if err = mysql.Db.Model(&UserRelation{}).Where("user_id_1 = ? AND user_id_2 IN ?", userId, greater).Find(&rows).Error; err != nil {
			return result, err
		}
	}

	for _, i := range rows {
		if i.HeartValue > user_e.HEART_VALUE_MAX {
			i.HeartValue = user_e.HEART_VALUE_MAX
		}
		d := uint(time.Since(i.CreatedTime).Hours() / 24)
		if d < 0 {
			d = 0
		}
		result[i.UserId_2] = Relation{HeartValue: i.HeartValue, HeartValueMax: user_e.HEART_VALUE_MAX, MeetDays: d}
	}

	rows = make([]UserRelation, 0)
	if len(smaller) > 0 {
		if err = mysql.Db.Model(&UserRelation{}).Where("user_id_1 IN ? AND user_id_2 = ?", smaller, userId).Find(&rows).Error; err != nil {
			return result, err
		}
	}
	for _, i := range rows {
		if i.HeartValue > user_e.HEART_VALUE_MAX {
			i.HeartValue = user_e.HEART_VALUE_MAX
		}
		d := uint(time.Since(i.CreatedTime).Hours() / 24)
		if d < 0 {
			d = 0
		}
		result[i.UserId_1] = Relation{HeartValue: i.HeartValue, HeartValueMax: user_e.HEART_VALUE_MAX, MeetDays: d}
	}
	// 补上没有成长关系的人
	for _, u := range others {
		if _, exists := result[u]; !exists {
			result[u] = Relation{0, 0, 0}
		}
	}
	return result, err
}

// 查询成长关系
func GetRelation(userId1, userId2 mysql.ID) (Relation, error) {
	result := Relation{0, 0, 0}
	if userId1 == userId2 {
		return result, nil
	}
	// 保证uid小的在前面
	if userId1 > userId2 {
		userId1, userId2 = userId2, userId1
	}
	rel := UserRelation{UserId_1: userId1, UserId_2: userId2, HeartValue: 0}
	if err := mysql.Db.Model(&UserRelation{}).Where(&UserRelation{
		UserId_1: userId1,
		UserId_2: userId2,
	}).First(&rel).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			// 没有成长关系的情况
			return result, nil
		} else {
			return result, myerr.WrapErr(err)
		}
	}
	result.HeartValueMax = user_e.HEART_VALUE_MAX

	result.HeartValue = rel.HeartValue
	if result.HeartValue > user_e.HEART_VALUE_MAX {
		result.HeartValue = user_e.HEART_VALUE_MAX
	}
	d := uint(time.Since(rel.CreatedTime).Hours() / 24)
	if d < 0 {
		d = 0
	}
	result.MeetDays = d

	return result, nil
}