golang 的 rabbitmq 消费项目
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.

db_user.go 11 KiB

2 years ago
7 months ago
2 years ago
7 months ago
7 months ago
2 years ago
7 months ago
2 years ago
2 years ago
7 months ago
2 years ago
7 months ago
2 years ago
2 years ago
2 years ago
7 months ago
2 years ago
7 months ago
2 years ago
7 months ago
7 months ago
2 years ago
7 months ago
2 years ago
7 months ago
2 years ago
2 years ago
1 year ago
7 months ago
1 year ago
7 months ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. package db
  2. import (
  3. "applet/app/db/model"
  4. "applet/app/utils/logx"
  5. "fmt"
  6. "strings"
  7. "xorm.io/xorm"
  8. )
  9. // UserisExistByUsernameAndPassword is usernameAndPassword exist
  10. func UserisExistByUsernameAndPassword(Db *xorm.Engine, username, password, zone string) (*model.User, error) {
  11. var user model.User
  12. sess := Db.Where("(username = ? or phone=?) ", username, username)
  13. if zone != "" && zone != "86" {
  14. sess = sess.And("zone=?", zone)
  15. }
  16. has, err := sess.Get(&user)
  17. if err != nil || has == false {
  18. return nil, err
  19. }
  20. return &user, nil
  21. }
  22. // UserisExistByMobile is exist
  23. func UserisExistByMobile(Db *xorm.Engine, n string) (bool, error) {
  24. has, err := Db.Where("phone = ? and phone<>''", n).Exist(&model.User{})
  25. if err != nil {
  26. return false, err
  27. }
  28. return has, nil
  29. }
  30. func UserisFindByMobile(sess *xorm.Session, n string) *model.User {
  31. var data model.User
  32. has, err := sess.Where("phone = ? and phone<>''", n).Get(&data)
  33. if err != nil || has == false {
  34. return nil
  35. }
  36. return &data
  37. }
  38. // UserInByUIDByLevel is In查询 以及是否是有效用户
  39. func UserInByUIDByLevel(Db *xorm.Engine, ids []int, levelID interface{}) (*[]model.User, error) {
  40. var m []model.User
  41. if err := Db.In("uid", ids).Where("level = ?", levelID).
  42. Find(&m); err != nil {
  43. return nil, logx.Warn(err)
  44. }
  45. return &m, nil
  46. }
  47. // UserFindByMobile search user by mobile
  48. func UserFindByMobile(Db *xorm.Engine, mobile string) (*model.User, error) {
  49. var m model.User
  50. if has, err := Db.Where("(phone = ? OR uid = ?) AND delete_at = 0", mobile, mobile).
  51. Get(&m); err != nil || has == false {
  52. return nil, logx.Warn(err)
  53. }
  54. return &m, nil
  55. }
  56. // UserFindExistByMobile search user by mobile
  57. func UserFindExistByMobile(Db *xorm.Engine, mobile string) (*model.User, bool, error) {
  58. var m model.User
  59. has, err := Db.Where("(phone = ? OR uid = ?) AND delete_at = 0", mobile, mobile).Get(&m)
  60. if err != nil {
  61. logx.Infof("UserFindExistByMobile err")
  62. return nil, false, logx.Warn(err)
  63. }
  64. return &m, has, nil
  65. }
  66. // UserFindByMobile search user by mobile
  67. func UserFindByMobileAll(Db *xorm.Engine, mobile string) (*model.User, error) {
  68. var m model.User
  69. if has, err := Db.Where("(phone = ? OR uid = ?)", mobile, mobile).
  70. Get(&m); err != nil || has == false {
  71. return nil, logx.Warn(err)
  72. }
  73. return &m, nil
  74. }
  75. // UserGetByMobileIgnoreDelete search user by mobile ignore delete
  76. func UserGetByMobileIgnoreDelete(Db *xorm.Engine, mobile, zone string) (*model.User, bool, error) {
  77. m := new(model.User)
  78. sess := Db.Where("phone = ?", mobile)
  79. if zone != "" && zone != "86" {
  80. sess = sess.And("zone=?", zone)
  81. }
  82. has, err := sess.Get(m)
  83. if err != nil {
  84. return nil, false, logx.Warn(err)
  85. }
  86. return m, has, nil
  87. }
  88. // UsersFindByMobileLike search users by mobile
  89. func UsersFindByMobileLike(Db *xorm.Engine, mobile string) (*[]model.User, error) {
  90. var m []model.User
  91. if err := Db.Where("phone like ?", "%"+mobile+"%").
  92. Find(&m); err != nil {
  93. return nil, logx.Warn(err)
  94. }
  95. return &m, nil
  96. }
  97. // UsersFindByNickNameLike search users by nickname
  98. func UsersFindByNickNameLike(Db *xorm.Engine, nickname string) (*[]model.User, error) {
  99. var m []model.User
  100. if err := Db.Where("nickname like ?", "%"+nickname+"%").
  101. Find(&m); err != nil {
  102. return nil, logx.Warn(err)
  103. }
  104. return &m, nil
  105. }
  106. func UsersFindByInviteCode(Db *xorm.Engine, nickname string) (*[]model.UserProfile, error) {
  107. var m []model.UserProfile
  108. if err := Db.Where("invite_code like ? or custom_invite_code like ?", "%"+strings.ToLower(nickname)+"%", "%"+strings.ToLower(nickname)+"%").
  109. Find(&m); err != nil {
  110. return nil, logx.Warn(err)
  111. }
  112. return &m, nil
  113. }
  114. //UsersInByIds is 根据ids 查找users
  115. func UsersInByIds(Db *xorm.Engine, ids []int, limit, start int) (*[]model.User, error) {
  116. var m []model.User
  117. if limit == 0 && start == 0 {
  118. if err := Db.In("uid", ids).
  119. Find(&m); err != nil {
  120. return nil, logx.Warn(err)
  121. }
  122. return &m, nil
  123. }
  124. if err := Db.In("uid", ids).Limit(limit, start).
  125. Find(&m); err != nil {
  126. return nil, logx.Warn(err)
  127. }
  128. return &m, nil
  129. }
  130. //UsersInByIdsWhereLv is 根据ids和 lv会员等级 查找users
  131. func UsersInByIdsWhereLv(Db *xorm.Engine, ids []int, lv interface{}, limit, start int) (*[]model.User, error) {
  132. var m []model.User
  133. if limit == 0 && start == 0 {
  134. if err := Db.Where("level = ?", lv).In("uid", ids).
  135. Find(&m); err != nil {
  136. return nil, logx.Warn(err)
  137. }
  138. return &m, nil
  139. }
  140. if err := Db.Where("level = ?", lv).In("uid", ids).Limit(limit, start).
  141. Find(&m); err != nil {
  142. return nil, logx.Warn(err)
  143. }
  144. return &m, nil
  145. }
  146. //UsersInByIdsByAscWhereLv is 根据ids和 lv会员等级 查找users 升排序
  147. func UsersInByIdsByAscWhereLv(Db *xorm.Engine, ids []int, lv interface{}, limit, start int, c string) (*[]model.User, error) {
  148. var m []model.User
  149. if limit == 0 && start == 0 {
  150. if err := Db.Where("level = ?", lv).In("uid", ids).Asc(c).
  151. Find(&m); err != nil {
  152. return nil, logx.Warn(err)
  153. }
  154. return &m, nil
  155. }
  156. if err := Db.Where("level = ?", lv).In("uid", ids).Asc(c).Limit(limit, start).
  157. Find(&m); err != nil {
  158. return nil, logx.Warn(err)
  159. }
  160. return &m, nil
  161. }
  162. //UsersInByIdsByDescWhereLv is 根据ids和 lv会员等级 查找users 降排序
  163. func UsersInByIdsByDescWhereLv(Db *xorm.Engine, ids []int, lv interface{}, limit, start int, c string) (*[]model.User, error) {
  164. var m []model.User
  165. if limit == 0 && start == 0 {
  166. if err := Db.Where("level = ?", lv).In("uid", ids).Desc(c).
  167. Find(&m); err != nil {
  168. return nil, logx.Warn(err)
  169. }
  170. return &m, nil
  171. }
  172. if err := Db.Where("level = ?", lv).In("uid", ids).Desc(c).Limit(limit, start).
  173. Find(&m); err != nil {
  174. return nil, logx.Warn(err)
  175. }
  176. return &m, nil
  177. }
  178. func UserFindByPhoneOrUsername(Db *xorm.Engine, mobile string) (*model.User, error) {
  179. var m model.User
  180. if has, err := Db.Where("(phone = ? or username=?) AND delete_at = 0", mobile, mobile).
  181. Get(&m); err != nil || has == false {
  182. return nil, err
  183. }
  184. return &m, nil
  185. }
  186. // UserFindByArkidUserName search user by mobile
  187. func UserFindByArkidUserName(Db *xorm.Engine, name string) (*model.User, error) {
  188. var m model.User
  189. if has, err := Db.Where("username = ?", name).
  190. Get(&m); err != nil || has == false {
  191. return nil, logx.Warn(err)
  192. }
  193. return &m, nil
  194. }
  195. // UserFindByID is find user byid
  196. func UserFindByID(Db *xorm.Engine, id interface{}) (*model.User, error) {
  197. var m model.User
  198. if has, err := Db.Where("uid = ?", id).
  199. Get(&m); err != nil || has == false {
  200. return nil, logx.Warn(err)
  201. }
  202. return &m, nil
  203. }
  204. func UserFindByIDWithSession(sess *xorm.Session, id interface{}) (*model.User, error) {
  205. var m model.User
  206. if has, err := sess.Where("uid = ?", id).
  207. Get(&m); err != nil || has == false {
  208. return nil, logx.Warn(err)
  209. }
  210. return &m, nil
  211. }
  212. func UserFindByIDs(Db *xorm.Engine, uids []int) (*[]model.User, error) {
  213. var m []model.User
  214. if err := Db.In("uid", uids).Find(&m); err != nil {
  215. return nil, logx.Warn(err)
  216. }
  217. return &m, nil
  218. }
  219. func UserFindByall(Db *xorm.Engine) (*[]model.User, error) {
  220. var m []model.User
  221. if err := Db.Where("uid>0").Find(&m); err != nil {
  222. return nil, logx.Warn(err)
  223. }
  224. return &m, nil
  225. }
  226. func UserFindByIDsToStr(Db *xorm.Engine, uids []string) (*[]model.User, error) {
  227. var m []model.User
  228. if err := Db.In("uid", uids).Find(&m); err != nil {
  229. return nil, logx.Warn(err)
  230. }
  231. return &m, nil
  232. }
  233. func UserFindByIsSet(Db *xorm.Engine, limit, start int) ([]model.UserProfile, error) {
  234. var m []model.UserProfile
  235. if err := Db.Where("is_set=? and parent_uid=0 and uid>0", 0).Limit(limit, start).Find(&m); err != nil {
  236. return nil, logx.Warn(err)
  237. }
  238. return m, nil
  239. }
  240. func UserFindByParentUid(Db *xorm.Engine, parentUid int) ([]model.UserProfile, error) {
  241. var m []model.UserProfile
  242. if err := Db.Where("parent_uid=?", parentUid).Find(&m); err != nil {
  243. return nil, logx.Warn(err)
  244. }
  245. return m, nil
  246. }
  247. // UsersInByIdsByDesc is 根据某列 降序
  248. func UsersInByIdsByDesc(Db *xorm.Engine, ids []int, limit, start int, c string) (*[]model.User, error) {
  249. var m []model.User
  250. if limit == 0 && start == 0 {
  251. if err := Db.In("uid", ids).Desc(c).
  252. Find(&m); err != nil {
  253. return nil, logx.Warn(err)
  254. }
  255. return &m, nil
  256. }
  257. if err := Db.In("uid", ids).Desc(c).Limit(limit, start).
  258. Find(&m); err != nil {
  259. return nil, logx.Warn(err)
  260. }
  261. return &m, nil
  262. }
  263. // UsersInByIdsByAsc is 根据某列 升序
  264. func UsersInByIdsByAsc(Db *xorm.Engine, ids []int, limit, start int, c string) (*[]model.User, error) {
  265. var m []model.User
  266. if limit == 0 && start == 0 {
  267. if err := Db.In("uid", ids).Asc(c).
  268. Find(&m); err != nil {
  269. return nil, logx.Warn(err)
  270. }
  271. return &m, nil
  272. }
  273. if err := Db.In("uid", ids).Asc(c).Limit(limit, start).
  274. Find(&m); err != nil {
  275. return nil, logx.Warn(err)
  276. }
  277. return &m, nil
  278. }
  279. //UserInsert is insert user
  280. func UserInsert(Db *xorm.Engine, user *model.User) (int64, error) {
  281. affected, err := Db.Insert(user)
  282. if err != nil {
  283. return 0, err
  284. }
  285. return affected, nil
  286. }
  287. // UserIsExistByMobile is mobile exist
  288. func UserIsExistByMobile(Db *xorm.Engine, mobile string) (bool, error) {
  289. //fmt.Println(mobile)
  290. has, err := Db.Where("phone = ? OR uid = ?", mobile, mobile).Exist(&model.User{})
  291. fmt.Println(has, mobile)
  292. if err != nil {
  293. return false, err
  294. }
  295. return has, nil
  296. }
  297. // UserIsExistByID is mobile exist by id
  298. func UserIsExistByID(Db *xorm.Engine, id string) (bool, error) {
  299. has, err := Db.Where("uid = ?", id).Exist(&model.User{})
  300. if err != nil {
  301. return false, err
  302. }
  303. return has, nil
  304. }
  305. // UserUpdate is update user
  306. func UserUpdate(Db *xorm.Engine, uid interface{}, user *model.User, forceColums ...string) (int64, error) {
  307. var (
  308. affected int64
  309. err error
  310. )
  311. if forceColums != nil {
  312. affected, err = Db.Where("uid=?", uid).Cols(forceColums...).Update(user)
  313. } else {
  314. affected, err = Db.Where("uid=?", uid).Update(user)
  315. }
  316. if err != nil {
  317. return 0, err
  318. }
  319. return affected, nil
  320. }
  321. func UserUpdateWithSession(Db *xorm.Session, uid interface{}, user *model.User, forceColums ...string) (int64, error) {
  322. var (
  323. affected int64
  324. err error
  325. )
  326. if forceColums != nil {
  327. affected, err = Db.Where("uid=?", uid).Cols(forceColums...).Update(user)
  328. } else {
  329. affected, err = Db.Where("uid=?", uid).Update(user)
  330. }
  331. if err != nil {
  332. return 0, err
  333. }
  334. return affected, nil
  335. }
  336. func UpdateUserFinValid() {
  337. }
  338. // UserDelete is delete user
  339. func UserDelete(Db *xorm.Engine, uid interface{}) (int64, error) {
  340. return Db.Where("uid = ?", uid).Delete(model.User{})
  341. }
  342. func UserDeleteWithSess(sess *xorm.Session, uid interface{}) (int64, error) {
  343. return sess.Where("uid = ?", uid).Delete(model.User{})
  344. }
  345. func UserProfileCheckInviteCode(eg *xorm.Engine, uid int, inviteCode string) bool {
  346. var data model.UserProfile
  347. get, err := eg.Where("invite_code=? or custom_invite_code=?", inviteCode, inviteCode).Get(&data)
  348. if get == false || err != nil {
  349. return false
  350. }
  351. if uid == data.Uid {
  352. return false
  353. }
  354. return true
  355. }