附近小店
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.
 
 
 

421 regels
12 KiB

  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 UserDeleteWithSess(sess *xorm.Session, uid interface{}) (int64, error) {
  360. return sess.Where("uid = ?", uid).Delete(model.User{})
  361. }
  362. func UserProfileCheckInviteCode(eg *xorm.Engine, uid int, inviteCode string) bool {
  363. var data model.UserProfile
  364. get, err := eg.Where("invite_code=? or custom_invite_code=?", inviteCode, inviteCode).Get(&data)
  365. if get == false || err != nil {
  366. return false
  367. }
  368. if uid == data.Uid {
  369. return false
  370. }
  371. return true
  372. }
  373. func GetUserList(eg *xorm.Engine, arg md.Store, uid int) (*[]md.StoreGroup, int64) {
  374. var data []md.StoreGroup
  375. sess := eg.Table("user").Where("user.is_marketer=? and community_team_store.parent_uid=?", 1, uid)
  376. size := utils.StrToInt(arg.Size)
  377. start := (utils.StrToInt(arg.P) - 1) * size
  378. if arg.Name != "" {
  379. sess.And("community_team_store.name like ?", "%"+arg.Name+"%")
  380. }
  381. sess.Join("LEFT", "community_team_store", "user.uid = community_team_store.uid")
  382. count, err := sess.Limit(size, start).Desc("user.create_at").FindAndCount(&data)
  383. if err != nil {
  384. return nil, count
  385. }
  386. return &data, count
  387. }