matchWealth.go 1.87 KB
Newer Older
chenweijian's avatar
chenweijian 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
package user_m

import (
	"gorm.io/gorm"
	"hilo-user/_const/enum/match_e"
	"hilo-user/domain"
	"hilo-user/myerr"
	"hilo-user/resource/mysql"
)

/**
 * 用户财富分数
 **/
type MatchWealthUserScore struct {
	mysql.Entity
	*domain.Model `gorm:"-"`
	UserId        mysql.ID
	Score         mysql.Num
	Grade         mysql.Num
}

/**
 * 获取的分数同等级关系
 **/
type MatchWealthSetScoreGrade struct {
	mysql.Entity
	*domain.Model `gorm:"-"`
	MinNum        mysql.Num
	MaxNum        mysql.Num
	Grade         mysql.Num
}

type MatchWealthUserScoreDetail struct {
	mysql.Entity
	MatchWealthUserScoreId mysql.ID
	UserId                 mysql.ID
	BeforeScore            mysql.Num
	Score                  mysql.Num
	AfterScore             mysql.Num
	Type                   match_e.MatchWealthUserScoreDetailType
	OrginId                mysql.ID
}

func addMatchWealthUserScoreDetail(model *domain.Model, matchWealthUserScoreId mysql.ID, userId mysql.ID, beforeScore mysql.Num, score mysql.Num, t match_e.MatchWealthUserScoreDetailType, orginId mysql.ID) error {
	if err := model.Db.Save(&MatchWealthUserScoreDetail{
		MatchWealthUserScoreId: matchWealthUserScoreId,
		UserId:                 userId,
		BeforeScore:            beforeScore,
		Score:                  score,
		AfterScore:             beforeScore + score,
		Type:                   t,
		OrginId:                orginId,
	}).Error; err != nil {
		return myerr.WrapErr(err)
	}
	return nil
}

//获取财富等级
func GetWealthGrade(model *domain.Model, userId mysql.ID) (uint32, uint32, error) {
	var wealthUserScore MatchWealthUserScore
	if err := model.Db.Model(&MatchWealthUserScore{}).Where(&MatchWealthUserScore{UserId: userId}).First(&wealthUserScore).Error; err != nil {
		if err == gorm.ErrRecordNotFound {
			return 0, 0, nil
		} else {
			return 0, 0, myerr.WrapErr(err)
		}
	}
	return wealthUserScore.Grade, wealthUserScore.Score, nil
}