附近小店
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

227 рядки
5.7 KiB

  1. package db
  2. import (
  3. "applet/app/db/model"
  4. "applet/app/e"
  5. "applet/app/utils/logx"
  6. "github.com/gin-gonic/gin"
  7. "xorm.io/xorm"
  8. )
  9. //UserLevelByID is 根据用户id 获取对应的等级信息
  10. func UserLevelByID(Db *xorm.Engine, id interface{}) (*model.UserLevel, error) {
  11. m := new(model.UserLevel)
  12. has, err := Db.Where("id = ?", id).Get(m)
  13. if err != nil {
  14. return nil, logx.Warn(err)
  15. }
  16. if !has {
  17. return nil, logx.Error("Not found")
  18. }
  19. return m, nil
  20. }
  21. func UserLevelByIDWithSession(sess *xorm.Session, id interface{}) (*model.UserLevel, error) {
  22. m := new(model.UserLevel)
  23. has, err := sess.Where("id = ?", id).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. //UserLevelTop is 查询最高的等级
  33. func UserLevelTop(Db *xorm.Engine) (*model.UserLevel, error) {
  34. m := new(model.UserLevel)
  35. has, err := Db.OrderBy("level_weight DESC").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. //UserLevelNext is 查询下一等级
  45. func UserLevelNext(Db *xorm.Engine, curLevelWeight int) (*model.UserLevel, error) {
  46. m := new(model.UserLevel)
  47. has, err := Db.Where("level_weight > ? and before_hide=?", curLevelWeight, 0).OrderBy("level_weight ASC").Get(m)
  48. if err != nil {
  49. return nil, logx.Warn(err)
  50. }
  51. if !has {
  52. return nil, logx.Error("Not found")
  53. }
  54. return m, nil
  55. }
  56. // UserLevelByWeight is 根据权重获取对应的等级
  57. func UserLevelByWeight(Db *xorm.Engine, w interface{}) (*model.UserLevel, error) {
  58. m := new(model.UserLevel)
  59. has, err := Db.Where("level_weight = ?", w).Get(m)
  60. if err != nil {
  61. return nil, logx.Warn(err)
  62. }
  63. if !has {
  64. return nil, logx.Warn("Not found")
  65. }
  66. return m, nil
  67. }
  68. //UserLevelInIDescByWeight is In 查询获取 权重最低 对应等级
  69. func UserLevelInIDescByWeightLow(Db *xorm.Engine) ([]*model.UserLevel, error) {
  70. var ms []*model.UserLevel
  71. if err := Db.Asc("level_weight").Limit(1).Find(&ms); err != nil {
  72. return nil, err
  73. }
  74. return ms, nil
  75. }
  76. func UserLevelInIDescByWeightLowWithOne(Db *xorm.Engine) (*model.UserLevel, error) {
  77. var ms model.UserLevel
  78. has, err := Db.Asc("level_weight").Get(&ms)
  79. if err != nil {
  80. return nil, err
  81. }
  82. if has == false {
  83. return nil, e.NewErr(400, "等级不存在")
  84. }
  85. return &ms, nil
  86. }
  87. func UserLevelInIDescByWeightDescWithOne(Db *xorm.Engine) (*model.UserLevel, error) {
  88. var ms model.UserLevel
  89. has, err := Db.Desc("level_weight").Get(&ms)
  90. if err != nil {
  91. return nil, err
  92. }
  93. if has == false {
  94. return nil, e.NewErr(400, "等级不存在")
  95. }
  96. return &ms, nil
  97. }
  98. func UserLevelByWeightNext(Db *xorm.Engine, levelWeight int) (*model.UserLevel, error) {
  99. var ms model.UserLevel
  100. 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 {
  101. return nil, err
  102. }
  103. return &ms, nil
  104. }
  105. func UserLevelByWeightMax(Db *xorm.Engine) (*model.UserLevel, error) {
  106. var ms model.UserLevel
  107. if has, err := Db.Where("is_use=? and before_hide=?", 1, 0).Desc("level_weight").Get(&ms); err != nil || has == false {
  108. return nil, err
  109. }
  110. return &ms, nil
  111. }
  112. //UserLevelInIDescByWeight is In 查询获取对应等级 根据权重排序
  113. func UserLevelInIDescByWeight(Db *xorm.Engine, ids []int) ([]*model.UserLevel, error) {
  114. var ms []*model.UserLevel
  115. if err := Db.In("id", ids).Desc("level_weight").Find(&ms); err != nil {
  116. return nil, err
  117. }
  118. return ms, nil
  119. }
  120. func UserLevelIDescByWeight(Db *xorm.Engine, id int) (*model.UserLevel, error) {
  121. var ms model.UserLevel
  122. if has, err := Db.Where("id=?", id).Get(&ms); err != nil || has == false {
  123. return nil, err
  124. }
  125. return &ms, nil
  126. }
  127. // UserLevlAll is 获取所有开启等级并且升序返回
  128. func UserLevlAll(c *gin.Context, Db *xorm.Engine) ([]*model.UserLevel, error) {
  129. var m []*model.UserLevel
  130. err := Db.Where("is_use = ?", 1).Asc("level_weight").Find(&m)
  131. if err != nil {
  132. return nil, logx.Warn(err)
  133. }
  134. mm, err := sysModFormat(c, m)
  135. if err != nil {
  136. return nil, err
  137. }
  138. return mm.([]*model.UserLevel), nil
  139. }
  140. func UserLevlAllNew(c *gin.Context, Db *xorm.Engine) ([]*model.UserLevel, error) {
  141. var m []*model.UserLevel
  142. err := Db.Where("is_use = ? and before_hide=?", 1, 0).Asc("level_weight").Find(&m)
  143. if err != nil {
  144. return nil, logx.Warn(err)
  145. }
  146. mm, err := sysModFormat(c, m)
  147. if err != nil {
  148. return nil, err
  149. }
  150. return mm.([]*model.UserLevel), nil
  151. }
  152. // UserLevlEgAll is 获取所有开启等级并且升序返回
  153. func UserLevlEgAll(Db *xorm.Engine) ([]*model.UserLevel, error) {
  154. var m []*model.UserLevel
  155. err := Db.Where("is_use = ?", 1).Asc("level_weight").Find(&m)
  156. if err != nil {
  157. return nil, logx.Warn(err)
  158. }
  159. return m, nil
  160. }
  161. func UserFindByLevel(eg *xorm.Engine, level int) []model.User {
  162. var data []model.User
  163. eg.Where("level=?", level).Find(&data)
  164. return data
  165. }
  166. // UserLevlAllByWeight is 获取所有等级并且权重升序返回
  167. func UserLevlAllByWeight(c *gin.Context, Db *xorm.Engine) ([]*model.UserLevel, error) {
  168. var m []*model.UserLevel
  169. err := Db.Asc("level_weight").Find(&m)
  170. if err != nil {
  171. return nil, logx.Warn(err)
  172. }
  173. mm, err := sysModFormat(c, m)
  174. if err != nil {
  175. return nil, err
  176. }
  177. return mm.([]*model.UserLevel), nil
  178. }
  179. func UserLevelByAllMap(Db *xorm.Engine) map[int]*model.UserLevel {
  180. var maps = make(map[int]*model.UserLevel, 0)
  181. var m []*model.UserLevel
  182. err := Db.Where("is_use = ?", 1).Asc("level_weight").Find(&m)
  183. if err != nil {
  184. return maps
  185. }
  186. for _, v := range m {
  187. maps[v.Id] = v
  188. }
  189. return maps
  190. }
  191. func UserLevelByNotHideAllMap(Db *xorm.Engine) map[int]*model.UserLevel {
  192. var maps = make(map[int]*model.UserLevel, 0)
  193. var m []*model.UserLevel
  194. err := Db.Where("is_use = ? and before_hide=?", 1, 0).Asc("level_weight").Find(&m)
  195. if err != nil {
  196. return maps
  197. }
  198. for _, v := range m {
  199. maps[v.Id] = v
  200. }
  201. return maps
  202. }