package db

import (
	"applet/app/db/model"
	"applet/app/e"
	"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
}
func UserLevelByIDWithSession(sess *xorm.Session, id interface{}) (*model.UserLevel, error) {
	m := new(model.UserLevel)
	has, err := sess.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 > ? and before_hide=?", curLevelWeight, 0).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

}
func UserLevelInIDescByWeightLowWithOne(Db *xorm.Engine) (*model.UserLevel, error) {
	var ms model.UserLevel
	has, err := Db.Asc("level_weight").Get(&ms)
	if err != nil {
		return nil, err
	}
	if has == false {
		return nil, e.NewErr(400, "等级不存在")
	}
	return &ms, nil

}
func UserLevelInIDescByWeightDescWithOne(Db *xorm.Engine) (*model.UserLevel, error) {
	var ms model.UserLevel
	has, err := Db.Desc("level_weight").Get(&ms)
	if err != nil {
		return nil, err
	}
	if has == false {
		return nil, e.NewErr(400, "等级不存在")
	}
	return &ms, nil

}
func UserLevelByWeightNext(Db *xorm.Engine, levelWeight int) (*model.UserLevel, error) {
	var ms model.UserLevel
	if has, err := Db.Where("level_weight>? and is_use=? and before_hide=?", levelWeight, 1, 0).Asc("level_weight").Get(&ms); err != nil || has == false {
		return nil, err
	}
	return &ms, nil

}
func UserLevelByWeightMax(Db *xorm.Engine) (*model.UserLevel, error) {
	var ms model.UserLevel
	if has, err := Db.Where("is_use=?  and before_hide=?", 1, 0).Desc("level_weight").Get(&ms); err != nil || has == false {
		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

}
func UserLevelIDescByWeight(Db *xorm.Engine, id int) (*model.UserLevel, error) {
	var ms model.UserLevel
	if has, err := Db.Where("id=?", id).Get(&ms); err != nil || has == false {
		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
}
func UserLevlAllNew(c *gin.Context, Db *xorm.Engine) ([]*model.UserLevel, error) {
	var m []*model.UserLevel
	err := Db.Where("is_use = ? and before_hide=?", 1, 0).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
}
func UserFindByLevel(eg *xorm.Engine, level int) []model.User {
	var data []model.User
	eg.Where("level=?", level).Find(&data)
	return data
}

// 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
}
func UserLevelByAllMap(Db *xorm.Engine) map[int]*model.UserLevel {
	var maps = make(map[int]*model.UserLevel, 0)
	var m []*model.UserLevel
	err := Db.Where("is_use = ?", 1).Asc("level_weight").Find(&m)
	if err != nil {
		return maps
	}
	for _, v := range m {
		maps[v.Id] = v
	}
	return maps
}
func UserLevelByNotHideAllMap(Db *xorm.Engine) map[int]*model.UserLevel {
	var maps = make(map[int]*model.UserLevel, 0)
	var m []*model.UserLevel
	err := Db.Where("is_use = ? and before_hide=?", 1, 0).Asc("level_weight").Find(&m)
	if err != nil {
		return maps
	}
	for _, v := range m {
		maps[v.Id] = v
	}
	return maps
}
