附近小店
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

db_user.go 11 KiB

2 mesi fa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. func UsersFindByInviteCodeMust(Db *xorm.Engine, nickname string) (*[]model.UserProfile, error) {
  115. var m []model.UserProfile
  116. if err := Db.Where("invite_code = ? or custom_invite_code = ?", strings.ToLower(nickname), strings.ToLower(nickname)).
  117. Find(&m); err != nil {
  118. return nil, logx.Warn(err)
  119. }
  120. return &m, nil
  121. }
  122. // UsersInByIds is 根据ids 查找users
  123. func UsersInByIds(Db *xorm.Engine, ids []int, limit, start int) (*[]model.User, error) {
  124. var m []model.User
  125. if limit == 0 && start == 0 {
  126. if err := Db.In("uid", ids).
  127. Find(&m); err != nil {
  128. return nil, logx.Warn(err)
  129. }
  130. return &m, nil
  131. }
  132. if err := Db.In("uid", ids).Limit(limit, start).
  133. Find(&m); err != nil {
  134. return nil, logx.Warn(err)
  135. }
  136. return &m, nil
  137. }
  138. // UsersInByIdsWhereLv is 根据ids和 lv会员等级 查找users
  139. func UsersInByIdsWhereLv(Db *xorm.Engine, ids []int, lv interface{}, limit, start int) (*[]model.User, error) {
  140. var m []model.User
  141. if limit == 0 && start == 0 {
  142. if err := Db.Where("level = ?", lv).In("uid", ids).
  143. Find(&m); err != nil {
  144. return nil, logx.Warn(err)
  145. }
  146. return &m, nil
  147. }
  148. if err := Db.Where("level = ?", lv).In("uid", ids).Limit(limit, start).
  149. Find(&m); err != nil {
  150. return nil, logx.Warn(err)
  151. }
  152. return &m, nil
  153. }
  154. // UsersInByIdsByAscWhereLv is 根据ids和 lv会员等级 查找users 升排序
  155. func UsersInByIdsByAscWhereLv(Db *xorm.Engine, ids []int, lv interface{}, limit, start int, c string) (*[]model.User, error) {
  156. var m []model.User
  157. if limit == 0 && start == 0 {
  158. if err := Db.Where("level = ?", lv).In("uid", ids).Asc(c).
  159. Find(&m); err != nil {
  160. return nil, logx.Warn(err)
  161. }
  162. return &m, nil
  163. }
  164. if err := Db.Where("level = ?", lv).In("uid", ids).Asc(c).Limit(limit, start).
  165. Find(&m); err != nil {
  166. return nil, logx.Warn(err)
  167. }
  168. return &m, nil
  169. }
  170. // UsersInByIdsByDescWhereLv is 根据ids和 lv会员等级 查找users 降排序
  171. func UsersInByIdsByDescWhereLv(Db *xorm.Engine, ids []int, lv interface{}, limit, start int, c string) (*[]model.User, error) {
  172. var m []model.User
  173. if limit == 0 && start == 0 {
  174. if err := Db.Where("level = ?", lv).In("uid", ids).Desc(c).
  175. Find(&m); err != nil {
  176. return nil, logx.Warn(err)
  177. }
  178. return &m, nil
  179. }
  180. if err := Db.Where("level = ?", lv).In("uid", ids).Desc(c).Limit(limit, start).
  181. Find(&m); err != nil {
  182. return nil, logx.Warn(err)
  183. }
  184. return &m, nil
  185. }
  186. func UserFindByPhoneOrUsername(Db *xorm.Engine, mobile string) (*model.User, error) {
  187. var m model.User
  188. if has, err := Db.Where("(phone = ? or username=?) AND delete_at = 0", mobile, mobile).
  189. Get(&m); err != nil || has == false {
  190. return nil, err
  191. }
  192. return &m, nil
  193. }
  194. // UserFindByArkidUserName search user by mobile
  195. func UserFindByArkidUserName(Db *xorm.Engine, name string) (*model.User, error) {
  196. var m model.User
  197. if has, err := Db.Where("username = ?", name).
  198. Get(&m); err != nil || has == false {
  199. return nil, logx.Warn(err)
  200. }
  201. return &m, nil
  202. }
  203. // UserFindByID is find user byid
  204. func UserFindByID(Db *xorm.Engine, id interface{}) (*model.User, error) {
  205. var m model.User
  206. if has, err := Db.Where("uid = ?", id).
  207. Get(&m); err != nil || has == false {
  208. return nil, logx.Warn(err)
  209. }
  210. return &m, nil
  211. }
  212. func UserFindByIDWithSession(sess *xorm.Session, id interface{}) (*model.User, error) {
  213. var m model.User
  214. if has, err := sess.Where("uid = ?", id).
  215. Get(&m); err != nil || has == false {
  216. return nil, logx.Warn(err)
  217. }
  218. return &m, nil
  219. }
  220. func UserFindByIDs(Db *xorm.Engine, uids []int) (*[]model.User, error) {
  221. var m []model.User
  222. if err := Db.In("uid", uids).Find(&m); err != nil {
  223. return nil, logx.Warn(err)
  224. }
  225. return &m, nil
  226. }
  227. func UserFindByall(Db *xorm.Engine) (*[]model.User, error) {
  228. var m []model.User
  229. if err := Db.Where("uid>0").Find(&m); err != nil {
  230. return nil, logx.Warn(err)
  231. }
  232. return &m, nil
  233. }
  234. func UserFindByIDsToStr(Db *xorm.Engine, uids []string) (*[]model.User, error) {
  235. var m []model.User
  236. if err := Db.In("uid", uids).Find(&m); err != nil {
  237. return nil, logx.Warn(err)
  238. }
  239. return &m, nil
  240. }
  241. func UserFindByIsSet(Db *xorm.Engine, limit, start int) ([]model.UserProfile, error) {
  242. var m []model.UserProfile
  243. if err := Db.Where("is_set=? and parent_uid=0 and uid>0", 0).Limit(limit, start).Find(&m); err != nil {
  244. return nil, logx.Warn(err)
  245. }
  246. return m, nil
  247. }
  248. func UserFindByParentUid(Db *xorm.Engine, parentUid int) ([]model.UserProfile, error) {
  249. var m []model.UserProfile
  250. if err := Db.Where("parent_uid=?", parentUid).Find(&m); err != nil {
  251. return nil, logx.Warn(err)
  252. }
  253. return m, nil
  254. }
  255. // UsersInByIdsByDesc is 根据某列 降序
  256. func UsersInByIdsByDesc(Db *xorm.Engine, ids []int, limit, start int, c string) (*[]model.User, error) {
  257. var m []model.User
  258. if limit == 0 && start == 0 {
  259. if err := Db.In("uid", ids).Desc(c).
  260. Find(&m); err != nil {
  261. return nil, logx.Warn(err)
  262. }
  263. return &m, nil
  264. }
  265. if err := Db.In("uid", ids).Desc(c).Limit(limit, start).
  266. Find(&m); err != nil {
  267. return nil, logx.Warn(err)
  268. }
  269. return &m, nil
  270. }
  271. // UsersInByIdsByAsc is 根据某列 升序
  272. func UsersInByIdsByAsc(Db *xorm.Engine, ids []int, limit, start int, c string) (*[]model.User, error) {
  273. var m []model.User
  274. if limit == 0 && start == 0 {
  275. if err := Db.In("uid", ids).Asc(c).
  276. Find(&m); err != nil {
  277. return nil, logx.Warn(err)
  278. }
  279. return &m, nil
  280. }
  281. if err := Db.In("uid", ids).Asc(c).Limit(limit, start).
  282. Find(&m); err != nil {
  283. return nil, logx.Warn(err)
  284. }
  285. return &m, nil
  286. }
  287. // UserInsert is insert user
  288. func UserInsert(Db *xorm.Engine, user *model.User) (int64, error) {
  289. affected, err := Db.Insert(user)
  290. if err != nil {
  291. return 0, err
  292. }
  293. return affected, nil
  294. }
  295. // UserIsExistByMobile is mobile exist
  296. func UserIsExistByMobile(Db *xorm.Engine, mobile string) (bool, error) {
  297. //fmt.Println(mobile)
  298. has, err := Db.Where("phone = ? OR uid = ?", mobile, mobile).Exist(&model.User{})
  299. fmt.Println(has, mobile)
  300. if err != nil {
  301. return false, err
  302. }
  303. return has, nil
  304. }
  305. // UserIsExistByID is mobile exist by id
  306. func UserIsExistByID(Db *xorm.Engine, id string) (bool, error) {
  307. has, err := Db.Where("uid = ?", id).Exist(&model.User{})
  308. if err != nil {
  309. return false, err
  310. }
  311. return has, nil
  312. }
  313. // UserUpdate is update user
  314. func UserUpdate(Db *xorm.Engine, uid interface{}, user *model.User, forceColums ...string) (int64, error) {
  315. var (
  316. affected int64
  317. err error
  318. )
  319. if forceColums != nil {
  320. affected, err = Db.Where("uid=?", uid).Cols(forceColums...).Update(user)
  321. } else {
  322. affected, err = Db.Where("uid=?", uid).Update(user)
  323. }
  324. if err != nil {
  325. return 0, err
  326. }
  327. return affected, nil
  328. }
  329. func UserUpdateWithSession(Db *xorm.Session, uid interface{}, user *model.User, forceColums ...string) (int64, error) {
  330. var (
  331. affected int64
  332. err error
  333. )
  334. if forceColums != nil {
  335. affected, err = Db.Where("uid=?", uid).Cols(forceColums...).Update(user)
  336. } else {
  337. affected, err = Db.Where("uid=?", uid).Update(user)
  338. }
  339. if err != nil {
  340. return 0, err
  341. }
  342. return affected, nil
  343. }
  344. func UpdateUserFinValid() {
  345. }
  346. // UserDelete is delete user
  347. func UserDelete(Db *xorm.Engine, uid interface{}) (int64, error) {
  348. return Db.Where("uid = ?", uid).Delete(model.User{})
  349. }
  350. func UserDeleteWithSess(sess *xorm.Session, uid interface{}) (int64, error) {
  351. return sess.Where("uid = ?", uid).Delete(model.User{})
  352. }
  353. func UserProfileCheckInviteCode(eg *xorm.Engine, uid int, inviteCode string) bool {
  354. var data model.UserProfile
  355. get, err := eg.Where("invite_code=? or custom_invite_code=?", inviteCode, inviteCode).Get(&data)
  356. if get == false || err != nil {
  357. return false
  358. }
  359. if uid == data.Uid {
  360. return false
  361. }
  362. return true
  363. }