面包店
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 

124 行
3.0 KiB

  1. package db
  2. import (
  3. "applet/app/db/model"
  4. "applet/app/utils/logx"
  5. "github.com/gin-gonic/gin"
  6. "xorm.io/xorm"
  7. )
  8. //UserLevelByID is 根据用户id 获取对应的等级信息
  9. func UserLevelByID(Db *xorm.Engine, id interface{}) (*model.UserLevel, error) {
  10. m := new(model.UserLevel)
  11. has, err := Db.Where("id = ?", id).Get(m)
  12. if err != nil {
  13. return nil, logx.Warn(err)
  14. }
  15. if !has {
  16. return nil, logx.Error("Not found")
  17. }
  18. return m, nil
  19. }
  20. //UserLevelTop is 查询最高的等级
  21. func UserLevelTop(Db *xorm.Engine) (*model.UserLevel, error) {
  22. m := new(model.UserLevel)
  23. has, err := Db.OrderBy("level_weight DESC").Get(m)
  24. if err != nil {
  25. return nil, logx.Warn(err)
  26. }
  27. if !has {
  28. return nil, logx.Error("Not found")
  29. }
  30. return m, nil
  31. }
  32. //UserLevelNext is 查询下一等级
  33. func UserLevelNext(Db *xorm.Engine, curLevelWeight int) (*model.UserLevel, error) {
  34. m := new(model.UserLevel)
  35. has, err := Db.Where("level_weight > ?", curLevelWeight).OrderBy("level_weight ASC").Get(m)
  36. if err != nil {
  37. return nil, logx.Warn(err)
  38. }
  39. if !has {
  40. return nil, logx.Error("Not found")
  41. }
  42. return m, nil
  43. }
  44. // UserLevelByWeight is 根据权重获取对应的等级
  45. func UserLevelByWeight(Db *xorm.Engine, w interface{}) (*model.UserLevel, error) {
  46. m := new(model.UserLevel)
  47. has, err := Db.Where("level_weight = ?", w).Get(m)
  48. if err != nil {
  49. return nil, logx.Warn(err)
  50. }
  51. if !has {
  52. return nil, logx.Warn("Not found")
  53. }
  54. return m, nil
  55. }
  56. //UserLevelInIDescByWeight is In 查询获取 权重最低 对应等级
  57. func UserLevelInIDescByWeightLow(Db *xorm.Engine) ([]*model.UserLevel, error) {
  58. var ms []*model.UserLevel
  59. if err := Db.Asc("level_weight").Limit(1).Find(&ms); err != nil {
  60. return nil, err
  61. }
  62. return ms, nil
  63. }
  64. //UserLevelInIDescByWeight is In 查询获取对应等级 根据权重排序
  65. func UserLevelInIDescByWeight(Db *xorm.Engine, ids []int) ([]*model.UserLevel, error) {
  66. var ms []*model.UserLevel
  67. if err := Db.In("id", ids).Desc("level_weight").Find(&ms); err != nil {
  68. return nil, err
  69. }
  70. return ms, nil
  71. }
  72. // UserLevlAll is 获取所有开启等级并且升序返回
  73. func UserLevlAll(c *gin.Context, Db *xorm.Engine) ([]*model.UserLevel, error) {
  74. var m []*model.UserLevel
  75. err := Db.Where("is_use = ?", 1).Asc("level_weight").Find(&m)
  76. if err != nil {
  77. return nil, logx.Warn(err)
  78. }
  79. mm, err := sysModFormat(c, m)
  80. if err != nil {
  81. return nil, err
  82. }
  83. return mm.([]*model.UserLevel), nil
  84. }
  85. // UserLevlEgAll is 获取所有开启等级并且升序返回
  86. func UserLevlEgAll(Db *xorm.Engine) ([]*model.UserLevel, error) {
  87. var m []*model.UserLevel
  88. err := Db.Where("is_use = ?", 1).Asc("level_weight").Find(&m)
  89. if err != nil {
  90. return nil, logx.Warn(err)
  91. }
  92. return m, nil
  93. }
  94. // UserLevlAllByWeight is 获取所有等级并且权重升序返回
  95. func UserLevlAllByWeight(c *gin.Context, Db *xorm.Engine) ([]*model.UserLevel, error) {
  96. var m []*model.UserLevel
  97. err := Db.Asc("level_weight").Find(&m)
  98. if err != nil {
  99. return nil, logx.Warn(err)
  100. }
  101. mm, err := sysModFormat(c, m)
  102. if err != nil {
  103. return nil, err
  104. }
  105. return mm.([]*model.UserLevel), nil
  106. }