附近小店
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

db_user_relate.go 7.4 KiB

vor 2 Monaten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. func UserRelateInsertWithSess(sess *xorm.Session, userRelate *model.UserRelate) (int64, error) {
  16. affected, err := sess.Insert(userRelate)
  17. if err != nil {
  18. return 0, err
  19. }
  20. return affected, nil
  21. }
  22. func UserRelateUpdate(Db *xorm.Engine, userRelate *model.UserRelate) (int64, error) {
  23. affected, err := Db.Where("parent_uid=? and uid=?", userRelate.ParentUid, userRelate.Uid).Cols("level,invite_time").Update(userRelate)
  24. if err != nil {
  25. return 0, err
  26. }
  27. return affected, nil
  28. }
  29. //UserRelateByPuid is 获取用户关系列表 by puid
  30. func UserRelatesByPuid(Db *xorm.Engine, puid interface{}, limit, start int) (*[]model.UserRelate, error) {
  31. var m []model.UserRelate
  32. if limit == 0 && start == 0 {
  33. if err := Db.Where("parent_uid = ?", puid).
  34. Cols(`id,parent_uid,uid,level,invite_time`).
  35. Find(&m); err != nil {
  36. return nil, logx.Warn(err)
  37. }
  38. return &m, nil
  39. }
  40. if err := Db.Where("parent_uid = ?", puid).
  41. Cols(`id,parent_uid,uid,level,invite_time`).Limit(limit, start).
  42. Find(&m); err != nil {
  43. return nil, logx.Warn(err)
  44. }
  45. return &m, nil
  46. }
  47. //UserRelatesByPuidByLv is 获取用户关系列表 by puid 和lv
  48. func UserRelatesByPuidByLv(Db *xorm.Engine, puid, lv interface{}, limit, start int) (*[]model.UserRelate, error) {
  49. var m []model.UserRelate
  50. if limit == 0 && start == 0 {
  51. if err := Db.Where("parent_uid = ? AND level = ?", puid, lv).
  52. Cols(`id,parent_uid,uid,level,invite_time`).
  53. Find(&m); err != nil {
  54. return nil, logx.Warn(err)
  55. }
  56. return &m, nil
  57. }
  58. if err := Db.Where("parent_uid = ? AND level = ?", puid, lv).
  59. Cols(`id,parent_uid,uid,level,invite_time`).Limit(limit, start).
  60. Find(&m); err != nil {
  61. return nil, logx.Warn(err)
  62. }
  63. return &m, nil
  64. }
  65. //UserRelatesByPuidByLvByTime is 获取直属 level =1用户关系列表 by puid 和lv by time
  66. func UserRelatesByPuidByLvByTime(Db *xorm.Engine, puid, lv, stime, etime interface{}, limit, start int) (*[]model.UserRelate, error) {
  67. var m []model.UserRelate
  68. if limit == 0 && start == 0 {
  69. if err := Db.Where("parent_uid = ? AND level = ? AND invite_time > ? AND invite_time < ?", puid, lv, stime, etime).
  70. Find(&m); err != nil {
  71. return nil, logx.Warn(err)
  72. }
  73. return &m, nil
  74. }
  75. if err := Db.Where("parent_uid = ? AND level = ? AND invite_time > ? AND invite_time < ?", puid, lv, stime, etime).
  76. Limit(limit, start).
  77. Find(&m); err != nil {
  78. return nil, logx.Warn(err)
  79. }
  80. return &m, nil
  81. }
  82. //UserRelatesByPuidByTime is 获取户关系列表 by puid 和lv by time
  83. func UserRelatesByPuidByTime(Db *xorm.Engine, puid, stime, etime interface{}, limit, start int) (*[]model.UserRelate, error) {
  84. var m []model.UserRelate
  85. if limit == 0 && start == 0 {
  86. if err := Db.Where("parent_uid = ? AND invite_time > ? AND invite_time < ?", puid, stime, etime).
  87. Cols(`id,parent_uid,uid,level,invite_time`).
  88. Find(&m); err != nil {
  89. return nil, logx.Warn(err)
  90. }
  91. return &m, nil
  92. }
  93. if err := Db.Where("parent_uid = ? AND invite_time > ? AND invite_time < ?", puid, stime, etime).
  94. Cols(`id,parent_uid,uid,level,invite_time`).Limit(limit, start).
  95. Find(&m); err != nil {
  96. return nil, logx.Warn(err)
  97. }
  98. return &m, nil
  99. }
  100. //UserRelatesByPuidExceptLv is 获取用户关系列表 by puid 和非 lv
  101. func UserRelatesByPuidExceptLv(Db *xorm.Engine, puid, lv interface{}, limit, start int) (*[]model.UserRelate, error) {
  102. var m []model.UserRelate
  103. if limit == 0 && start == 0 {
  104. if err := Db.Where("parent_uid = ? AND level != ?", puid, lv).
  105. Cols(`id,parent_uid,uid,level,invite_time`).
  106. Find(&m); err != nil {
  107. return nil, logx.Warn(err)
  108. }
  109. return &m, nil
  110. }
  111. if err := Db.Where("parent_uid = ? AND level != ?", puid, lv).
  112. Cols(`id,parent_uid,uid,level,invite_time`).Limit(limit, start).
  113. Find(&m); err != nil {
  114. return nil, logx.Warn(err)
  115. }
  116. return &m, nil
  117. }
  118. //UserRelateByUID is 获取用户关系表 by uid
  119. func UserRelateByUID(Db *xorm.Engine, uid interface{}) (*model.UserRelate, bool, error) {
  120. r := new(model.UserRelate)
  121. has, err := Db.Where("uid=?", uid).Get(r)
  122. if err != nil {
  123. return nil, false, err
  124. }
  125. return r, has, nil
  126. }
  127. //UserRelateByUIDByLv is 获取用户关系表 by uid
  128. func UserRelateByUIDByLv(Db *xorm.Engine, uid, lv interface{}) (*model.UserRelate, bool, error) {
  129. r := new(model.UserRelate)
  130. has, err := Db.Where("uid=? AND level=?", uid, lv).Get(r)
  131. if err != nil {
  132. return nil, false, err
  133. }
  134. return r, has, nil
  135. }
  136. //UserRelateByUIDAndPUID 根据 Puid 和uid 查找 ,用于确认关联
  137. func UserRelateByUIDAndPUID(Db *xorm.Engine, uid, puid interface{}) (*model.UserRelate, bool, error) {
  138. r := new(model.UserRelate)
  139. has, err := Db.Where("uid=? AND parent_uid=?", uid, puid).Get(r)
  140. if err != nil {
  141. return nil, false, err
  142. }
  143. return r, has, nil
  144. }
  145. //UserRelatesByPuIDAndLv is 查询用户关系表 获取指定等级和puid的关系
  146. func UserRelatesByPuIDAndLv(Db *xorm.Engine, puid, lv interface{}) (*[]model.UserRelate, error) {
  147. var m []model.UserRelate
  148. if err := Db.Where("parent_uid = ? AND level = ?", puid, lv).
  149. Cols(`id,parent_uid,uid,level,invite_time`).
  150. Find(&m); err != nil {
  151. return nil, logx.Warn(err)
  152. }
  153. return &m, nil
  154. }
  155. // UserRelateInByUID is In查询
  156. func UserRelateInByUID(Db *xorm.Engine, ids []int) (*[]model.UserRelate, error) {
  157. var m []model.UserRelate
  158. if err := Db.In("uid", ids).
  159. Find(&m); err != nil {
  160. return nil, logx.Warn(err)
  161. }
  162. return &m, nil
  163. }
  164. // UserRelatesByUIDDescLv is Where 查询 根据level 降序
  165. func UserRelatesByUIDDescLv(Db *xorm.Engine, id interface{}) (*[]model.UserRelate, error) {
  166. var m []model.UserRelate
  167. if err := Db.Where("uid = ?", id).Desc("level").
  168. Find(&m); err != nil {
  169. return nil, logx.Warn(err)
  170. }
  171. return &m, nil
  172. }
  173. func UserRelatesByInvite(Db *xorm.Engine, times interface{}) (*[]model.UserRelate, error) {
  174. var m []model.UserRelate
  175. if err := Db.Where("invite_time >= ?", times).
  176. Find(&m); err != nil {
  177. return nil, logx.Warn(err)
  178. }
  179. return &m, nil
  180. }
  181. //UserRelateCountByPUID is 根据puid 计数
  182. func UserRelateCountByPUID(Db *xorm.Engine, pid interface{}) (int64, error) {
  183. count, err := Db.Where("parent_uid = ?", pid).Count(model.UserRelate{})
  184. if err != nil {
  185. return 0, nil
  186. }
  187. return count, nil
  188. }
  189. //UserRelateDelete is 删除关联他上级的关系记录,以及删除他下级的关联记录
  190. func UserRelateDelete(Db *xorm.Engine, uid interface{}) (int64, error) {
  191. // 删除与之上级的记录
  192. _, err := Db.Where("uid = ?", uid).Delete(model.UserRelate{})
  193. if err != nil {
  194. return 0, err
  195. }
  196. // 删除与之下级的记录
  197. _, err = Db.Where("parent_uid = ?", uid).Delete(model.UserRelate{})
  198. if err != nil {
  199. return 0, err
  200. }
  201. return 1, nil
  202. }
  203. func UserRelateDeleteWithSession(sess *xorm.Session, uid interface{}) (int64, error) {
  204. // 删除与之上级的记录
  205. _, err := sess.Where("uid = ?", uid).Delete(model.UserRelate{})
  206. if err != nil {
  207. return 0, err
  208. }
  209. // 删除与之下级的记录
  210. _, err = sess.Where("parent_uid = ?", uid).Delete(model.UserRelate{})
  211. if err != nil {
  212. return 0, err
  213. }
  214. return 1, nil
  215. }
  216. //UserRelateDelete is 删除关联他上级的关系记录
  217. func UserRelateExtendDelete(Db *xorm.Engine, uid interface{}) (int64, error) {
  218. // 删除与之上级的记录
  219. _, err := Db.Where("uid = ?", uid).Delete(model.UserRelate{})
  220. if err != nil {
  221. return 0, err
  222. }
  223. return 1, nil
  224. }