|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- package db
-
- import (
- "applet/app/db/model"
- "applet/app/utils/logx"
- "github.com/gin-gonic/gin"
- "xorm.io/xorm"
- )
-
- //UserLevelByID is 根据用户id 获取对应的等级信息
- func UserLevelByID(Db *xorm.Engine, id interface{}) (*model.UserLevel, error) {
- m := new(model.UserLevel)
- has, err := Db.Where("id = ?", id).Get(m)
- if err != nil {
- return nil, logx.Warn(err)
- }
- if !has {
- return nil, logx.Error("Not found")
- }
-
- return m, nil
- }
-
- //UserLevelTop is 查询最高的等级
- func UserLevelTop(Db *xorm.Engine) (*model.UserLevel, error) {
- m := new(model.UserLevel)
- has, err := Db.OrderBy("level_weight DESC").Get(m)
- if err != nil {
- return nil, logx.Warn(err)
- }
- if !has {
- return nil, logx.Error("Not found")
- }
-
- return m, nil
- }
-
- //UserLevelNext is 查询下一等级
- func UserLevelNext(Db *xorm.Engine, curLevelWeight int) (*model.UserLevel, error) {
- m := new(model.UserLevel)
- has, err := Db.Where("level_weight > ?", curLevelWeight).OrderBy("level_weight ASC").Get(m)
- if err != nil {
- return nil, logx.Warn(err)
- }
- if !has {
- return nil, logx.Error("Not found")
- }
-
- return m, nil
- }
-
- // UserLevelByWeight is 根据权重获取对应的等级
- func UserLevelByWeight(Db *xorm.Engine, w interface{}) (*model.UserLevel, error) {
- m := new(model.UserLevel)
- has, err := Db.Where("level_weight = ?", w).Get(m)
- if err != nil {
- return nil, logx.Warn(err)
- }
- if !has {
- return nil, logx.Warn("Not found")
- }
- return m, nil
- }
-
- //UserLevelInIDescByWeight is In 查询获取 权重最低 对应等级
- func UserLevelInIDescByWeightLow(Db *xorm.Engine) ([]*model.UserLevel, error) {
- var ms []*model.UserLevel
- if err := Db.Asc("level_weight").Limit(1).Find(&ms); err != nil {
- return nil, err
- }
- return ms, nil
-
- }
-
- //UserLevelInIDescByWeight is In 查询获取对应等级 根据权重排序
- func UserLevelInIDescByWeight(Db *xorm.Engine, ids []int) ([]*model.UserLevel, error) {
- var ms []*model.UserLevel
- if err := Db.In("id", ids).Desc("level_weight").Find(&ms); err != nil {
- return nil, err
- }
- return ms, nil
-
- }
-
- // UserLevlAll is 获取所有开启等级并且升序返回
- func UserLevlAll(c *gin.Context, Db *xorm.Engine) ([]*model.UserLevel, error) {
- var m []*model.UserLevel
- err := Db.Where("is_use = ?", 1).Asc("level_weight").Find(&m)
- if err != nil {
- return nil, logx.Warn(err)
- }
-
- mm, err := sysModFormat(c, m)
- if err != nil {
- return nil, err
- }
- return mm.([]*model.UserLevel), nil
- }
-
- // UserLevlEgAll is 获取所有开启等级并且升序返回
- func UserLevlEgAll(Db *xorm.Engine) ([]*model.UserLevel, error) {
- var m []*model.UserLevel
- err := Db.Where("is_use = ?", 1).Asc("level_weight").Find(&m)
- if err != nil {
- return nil, logx.Warn(err)
- }
- return m, nil
- }
-
- // UserLevlAllByWeight is 获取所有等级并且权重升序返回
- func UserLevlAllByWeight(c *gin.Context, Db *xorm.Engine) ([]*model.UserLevel, error) {
- var m []*model.UserLevel
- err := Db.Asc("level_weight").Find(&m)
- if err != nil {
- return nil, logx.Warn(err)
- }
-
- mm, err := sysModFormat(c, m)
- if err != nil {
- return nil, err
- }
- return mm.([]*model.UserLevel), nil
- }
|