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 9.0 KiB

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