You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.6 KiB

package user
import (
"github.com/jinzhu/gorm"
"recook/internal/v2/lib/db"
)
type RecookUserTreeModel struct {
db.BaseModel
Id uint `gorm:"column:id;primary_key" json:"id"`
UserId uint `json:"userId"`
RootId uint `json:"rootId"`
Depth uint `json:"depth"`
}
// TableName sets the insert table name for this struct type
func (r *RecookUserTreeModel) TableName() string {
return "recook_user_tree"
}
// @Style 添加
func (r *RecookUserTreeModel) Create(data *RecookUserTreeModel) {
r.GetDb().Create(data)
}
// @Style sql
func (r *RecookUserTreeModel) GetByUserIdSql(userId uint, selectStr string, args ...interface{}) (result *gorm.SqlExpr) {
return r.GetDb().Model(&RecookUserTreeModel{}).Select(selectStr, args...).Where("user_id = ?", userId).SubQuery()
}
// @Style 封装sql
func (r *RecookUserTreeModel) GetByRootIdAndDepthSql(rootId uint, depth ...int) *gorm.SqlExpr {
return r.GetDb().Model(&RecookUserTreeModel{}).Select("user_id").Where("root_id = ? and depth in (?)", rootId, depth).SubQuery()
}
func (r *RecookUserTreeModel) FindFather(userId uint) (result RecookUserTreeModel) {
r.GetDb().First(&result, "user_id = ? and depth = ?", userId, 1)
return
}
// @Style 会员所有下级
func (r *RecookUserTreeModel) UserCount(userId uint) (count int) {
r.GetDb().Model(&RecookUserTreeModel{}).Where("root_id = ?", userId).Count(&count)
return
}
// @Style 会员所有下级
func (r *RecookUserTreeModel) UserCountWhere(query interface{}, args ...interface{}) (count int) {
r.GetDb().Model(&RecookUserTreeModel{}).Where(query, args...).Count(&count)
return
}