visit.go 1.19 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
package cp_m

import (
	"git.hilo.cn/hilo-common/domain"
	"git.hilo.cn/hilo-common/resource/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/clause"
	"time"
)

type CpVisitor struct {
	CpId        mysql.ID
	UserId1     mysql.ID
	UserId2     mysql.ID
	Visitor     mysql.ID
	Times       mysql.Num
	CreatedTime time.Time `gorm:"->"`
	UpdatedTime time.Time `gorm:"->"`
}

// 添加cp空间访问量
func AddCpSpaceVisitor(model *domain.Model, cpId, userId1, userId2, visitor mysql.ID) error {
	vis := &CpVisitor{
		CpId:    cpId,
		UserId1: userId1,
		UserId2: userId2,
		Visitor: visitor,
		Times:   1,
	}
	if err := model.DB().Model(CpVisitor{}).Clauses(clause.OnConflict{Columns: []clause.Column{{Name: "cp_id"}, {Name: "visitor"}},
		DoUpdates: clause.Assignments(map[string]interface{}{
			"times": gorm.Expr("times + ?", 1)})}).Create(vis).Error; err != nil {
		model.Log.Errorf("AddCpSpaceVisitor fail:%v", err)
		return err
	}
	return nil
}

// 获取cp空间访问人数
func CountCpSpaceVisitors(model *domain.Model, cpId mysql.ID) int64 {
	var cnt int64
	if err := model.DB().Model(CpVisitor{}).Where("cp_id = ?", cpId).Count(&cnt).Error; err != nil {
		model.Log.Errorf("CountCpSpaceVisitors fail:%v", err)
	}
	return cnt
}