附近小店
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

db_user.go 12 KiB

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