面包店
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.
 
 
 
 
 

214 line
6.4 KiB

  1. package db
  2. import (
  3. "applet/app/db/model"
  4. "applet/app/utils/logx"
  5. "xorm.io/xorm"
  6. )
  7. // UserRelateInsert is 插入一条数据到用户关系表
  8. func UserRelateInsert(Db *xorm.Engine, userRelate *model.UserRelate) (int64, error) {
  9. affected, err := Db.Insert(userRelate)
  10. if err != nil {
  11. return 0, err
  12. }
  13. return affected, nil
  14. }
  15. //UserRelateByPuid is 获取用户关系列表 by puid
  16. func UserRelatesByPuid(Db *xorm.Engine, puid interface{}, limit, start int) (*[]model.UserRelate, error) {
  17. var m []model.UserRelate
  18. if limit == 0 && start == 0 {
  19. if err := Db.Where("parent_uid = ?", puid).
  20. Cols(`id,parent_uid,uid,level,invite_time`).
  21. Find(&m); err != nil {
  22. return nil, logx.Warn(err)
  23. }
  24. return &m, nil
  25. }
  26. if err := Db.Where("parent_uid = ?", puid).
  27. Cols(`id,parent_uid,uid,level,invite_time`).Limit(limit, start).
  28. Find(&m); err != nil {
  29. return nil, logx.Warn(err)
  30. }
  31. return &m, nil
  32. }
  33. //UserRelatesByPuidByLv is 获取用户关系列表 by puid 和lv
  34. func UserRelatesByPuidByLv(Db *xorm.Engine, puid, lv interface{}, limit, start int) (*[]model.UserRelate, error) {
  35. var m []model.UserRelate
  36. if limit == 0 && start == 0 {
  37. if err := Db.Where("parent_uid = ? AND level = ?", puid, lv).
  38. Cols(`id,parent_uid,uid,level,invite_time`).
  39. Find(&m); err != nil {
  40. return nil, logx.Warn(err)
  41. }
  42. return &m, nil
  43. }
  44. if err := Db.Where("parent_uid = ? AND level = ?", puid, lv).
  45. Cols(`id,parent_uid,uid,level,invite_time`).Limit(limit, start).
  46. Find(&m); err != nil {
  47. return nil, logx.Warn(err)
  48. }
  49. return &m, nil
  50. }
  51. //UserRelatesByPuidByLvByTime is 获取直属 level =1用户关系列表 by puid 和lv by time
  52. func UserRelatesByPuidByLvByTime(Db *xorm.Engine, puid, lv, stime, etime interface{}, limit, start int) (*[]model.UserRelate, error) {
  53. var m []model.UserRelate
  54. if limit == 0 && start == 0 {
  55. if err := Db.Where("parent_uid = ? AND level = ? AND invite_time > ? AND invite_time < ?", puid, lv, stime, etime).
  56. Find(&m); err != nil {
  57. return nil, logx.Warn(err)
  58. }
  59. return &m, nil
  60. }
  61. if err := Db.Where("parent_uid = ? AND level = ? AND invite_time > ? AND invite_time < ?", puid, lv, stime, etime).
  62. Limit(limit, start).
  63. Find(&m); err != nil {
  64. return nil, logx.Warn(err)
  65. }
  66. return &m, nil
  67. }
  68. //UserRelatesByPuidByTime is 获取户关系列表 by puid 和lv by time
  69. func UserRelatesByPuidByTime(Db *xorm.Engine, puid, stime, etime interface{}, limit, start int) (*[]model.UserRelate, error) {
  70. var m []model.UserRelate
  71. if limit == 0 && start == 0 {
  72. if err := Db.Where("parent_uid = ? AND invite_time > ? AND invite_time < ?", puid, stime, etime).
  73. Cols(`id,parent_uid,uid,level,invite_time`).
  74. Find(&m); err != nil {
  75. return nil, logx.Warn(err)
  76. }
  77. return &m, nil
  78. }
  79. if err := Db.Where("parent_uid = ? AND invite_time > ? AND invite_time < ?", puid, stime, etime).
  80. Cols(`id,parent_uid,uid,level,invite_time`).Limit(limit, start).
  81. Find(&m); err != nil {
  82. return nil, logx.Warn(err)
  83. }
  84. return &m, nil
  85. }
  86. //UserRelatesByPuidExceptLv is 获取用户关系列表 by puid 和非 lv
  87. func UserRelatesByPuidExceptLv(Db *xorm.Engine, puid, lv interface{}, limit, start int) (*[]model.UserRelate, error) {
  88. var m []model.UserRelate
  89. if limit == 0 && start == 0 {
  90. if err := Db.Where("parent_uid = ? AND level != ?", puid, lv).
  91. Cols(`id,parent_uid,uid,level,invite_time`).
  92. Find(&m); err != nil {
  93. return nil, logx.Warn(err)
  94. }
  95. return &m, nil
  96. }
  97. if err := Db.Where("parent_uid = ? AND level != ?", puid, lv).
  98. Cols(`id,parent_uid,uid,level,invite_time`).Limit(limit, start).
  99. Find(&m); err != nil {
  100. return nil, logx.Warn(err)
  101. }
  102. return &m, nil
  103. }
  104. //UserRelateByUID is 获取用户关系表 by uid
  105. func UserRelateByUID(Db *xorm.Engine, uid interface{}) (*model.UserRelate, bool, error) {
  106. r := new(model.UserRelate)
  107. has, err := Db.Where("uid=?", uid).Get(r)
  108. if err != nil {
  109. return nil, false, err
  110. }
  111. return r, has, nil
  112. }
  113. //UserRelateByUIDByLv is 获取用户关系表 by uid
  114. func UserRelateByUIDByLv(Db *xorm.Engine, uid, lv interface{}) (*model.UserRelate, bool, error) {
  115. r := new(model.UserRelate)
  116. has, err := Db.Where("uid=? AND level=?", uid, lv).Get(r)
  117. if err != nil {
  118. return nil, false, err
  119. }
  120. return r, has, nil
  121. }
  122. //UserRelateByUIDAndPUID 根据 Puid 和uid 查找 ,用于确认关联
  123. func UserRelateByUIDAndPUID(Db *xorm.Engine, uid, puid interface{}) (*model.UserRelate, bool, error) {
  124. r := new(model.UserRelate)
  125. has, err := Db.Where("uid=? AND parent_uid=?", uid, puid).Get(r)
  126. if err != nil {
  127. return nil, false, err
  128. }
  129. return r, has, nil
  130. }
  131. //UserRelatesByPuIDAndLv is 查询用户关系表 获取指定等级和puid的关系
  132. func UserRelatesByPuIDAndLv(Db *xorm.Engine, puid, lv interface{}) (*[]model.UserRelate, error) {
  133. var m []model.UserRelate
  134. if err := Db.Where("parent_uid = ? AND level = ?", puid, lv).
  135. Cols(`id,parent_uid,uid,level,invite_time`).
  136. Find(&m); err != nil {
  137. return nil, logx.Warn(err)
  138. }
  139. return &m, nil
  140. }
  141. // UserRelateInByUID is In查询
  142. func UserRelateInByUID(Db *xorm.Engine, ids []int) (*[]model.UserRelate, error) {
  143. var m []model.UserRelate
  144. if err := Db.In("uid", ids).
  145. Find(&m); err != nil {
  146. return nil, logx.Warn(err)
  147. }
  148. return &m, nil
  149. }
  150. // UserRelatesByUIDDescLv is Where 查询 根据level 降序
  151. func UserRelatesByUIDDescLv(Db *xorm.Engine, id interface{}) (*[]model.UserRelate, error) {
  152. var m []model.UserRelate
  153. if err := Db.Where("uid = ?", id).Desc("level").
  154. Find(&m); err != nil {
  155. return nil, logx.Warn(err)
  156. }
  157. return &m, nil
  158. }
  159. //UserRelateCountByPUID is 根据puid 计数
  160. func UserRelateCountByPUID(Db *xorm.Engine, pid interface{}) (int64, error) {
  161. count, err := Db.Where("parent_uid = ?", pid).Count(model.UserRelate{})
  162. if err != nil {
  163. return 0, nil
  164. }
  165. return count, nil
  166. }
  167. //UserRelateDelete is 删除关联他上级的关系记录,以及删除他下级的关联记录
  168. func UserRelateDelete(Db *xorm.Engine, uid interface{}) (int64, error) {
  169. // 删除与之上级的记录
  170. _, err := Db.Where("uid = ?", uid).Delete(model.UserRelate{})
  171. if err != nil {
  172. return 0, err
  173. }
  174. // 删除与之下级的记录
  175. _, err = Db.Where("parent_uid = ?", uid).Delete(model.UserRelate{})
  176. if err != nil {
  177. return 0, err
  178. }
  179. return 1, nil
  180. }
  181. //UserRelateDelete is 删除关联他上级的关系记录
  182. func UserRelateExtendDelete(Db *xorm.Engine, uid interface{}) (int64, error) {
  183. // 删除与之上级的记录
  184. _, err := Db.Where("uid = ?", uid).Delete(model.UserRelate{})
  185. if err != nil {
  186. return 0, err
  187. }
  188. return 1, nil
  189. }