附近小店
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

579 строки
18 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. "errors"
  8. "xorm.io/xorm"
  9. )
  10. // UserProfileFindByArkID is get userprofile by arkid
  11. func UserProfileFindByArkID(Db *xorm.Engine, id interface{}) (*model.UserProfile, error) {
  12. var m model.UserProfile
  13. if has, err := Db.Where("arkid_uid = ?", id).Get(&m); err != nil || has == false {
  14. return nil, logx.Warn(err)
  15. }
  16. return &m, nil
  17. }
  18. func UserProfileFindByArkToken(Db *xorm.Engine, id interface{}) (*model.UserProfile, error) {
  19. var m model.UserProfile
  20. if has, err := Db.Where("arkid_token<>'' and arkid_token = ?", id).Get(&m); err != nil || has == false {
  21. return nil, logx.Warn(err)
  22. }
  23. return &m, nil
  24. }
  25. // UserProfileFindByInviteCode is get userprofile by InviteCode
  26. func UserProfileFindByInviteCode(Db *xorm.Engine, code string) (*model.UserProfile, error) {
  27. var m model.UserProfile
  28. if has, err := Db.Where("invite_code = ?", code).Get(&m); err != nil || has == false {
  29. return nil, logx.Warn(err)
  30. }
  31. return &m, nil
  32. }
  33. // UserProfileFindByInviteCode is get userprofile by InviteCode
  34. func UserProfileFindByCustomInviteCode(Db *xorm.Engine, code string) (*model.UserProfile, error) {
  35. var m model.UserProfile
  36. if has, err := Db.Where("custom_invite_code = ?", code).Get(&m); err != nil || has == false {
  37. return nil, logx.Warn(err)
  38. }
  39. return &m, nil
  40. }
  41. // UserProfileFindByInviteCodes is get userprofile by InviteCode
  42. func UserProfileFindByInviteCodes(Db *xorm.Engine, codes ...string) (*[]model.UserProfile, error) {
  43. var m []model.UserProfile
  44. if err := Db.In("invite_code", codes).Find(&m); err != nil {
  45. return nil, logx.Warn(err)
  46. }
  47. return &m, nil
  48. }
  49. func UserProfileFindByAll(Db *xorm.Engine) (*[]model.UserProfile, error) {
  50. var m []model.UserProfile
  51. if err := Db.Find(&m); err != nil {
  52. return nil, logx.Warn(err)
  53. }
  54. return &m, nil
  55. }
  56. // UserProfileFindByCustomInviteCodes is get userprofile by CustomInviteCode
  57. func UserProfileFindByCustomInviteCodes(Db *xorm.Engine, codes ...string) (*[]model.UserProfile, error) {
  58. var m []model.UserProfile
  59. if err := Db.In("custom_invite_code", codes).Find(&m); err != nil {
  60. return nil, logx.Warn(err)
  61. }
  62. return &m, nil
  63. }
  64. // UserProfileFindByID search user_profile by userid
  65. func UserProfileFindByID(Db *xorm.Engine, id interface{}) (*model.UserProfile, error) {
  66. var m model.UserProfile
  67. if has, err := Db.Where("uid = ?", id).Get(&m); err != nil || has == false {
  68. return nil, logx.Warn(err)
  69. }
  70. return &m, nil
  71. }
  72. func UserProfileFindAll(Db *xorm.Engine) (*[]model.UserProfile, error) {
  73. var m []model.UserProfile
  74. if err := Db.Where("invite_code='' and uid>=0").Asc("uid").Find(&m); err != nil {
  75. return nil, logx.Warn(err)
  76. }
  77. return &m, nil
  78. }
  79. func UserProfileFindByIDsToStr(Db *xorm.Engine, uids []string) (*[]model.UserProfile, error) {
  80. var m []model.UserProfile
  81. if err := Db.In("uid", uids).Find(&m); err != nil {
  82. return nil, logx.Warn(err)
  83. }
  84. return &m, nil
  85. }
  86. func UserProfileFindByIDSess(sess *xorm.Session, id interface{}) (*model.UserProfile, error) {
  87. var m model.UserProfile
  88. if has, err := sess.Where("uid = ?", id).Get(&m); err != nil || has == false {
  89. return nil, logx.Warn(err)
  90. }
  91. return &m, nil
  92. }
  93. func UserProfileFindByPID(Db *xorm.Engine, id interface{}) (*model.UserProfile, error) {
  94. var m model.UserProfile
  95. if has, err := Db.Where("parent_uid = ?", id).Get(&m); err != nil || has == false {
  96. return nil, logx.Warn(err)
  97. }
  98. return &m, nil
  99. }
  100. // UserProfileOrderByNew 找最新的记录
  101. func UserProfileOrderByNew(Db *xorm.Engine) (*model.UserProfile, error) {
  102. var m model.UserProfile
  103. if has, err := Db.Where("invite_code != ''").OrderBy("uid desc").Get(&m); err != nil || has == false {
  104. return nil, logx.Warn(err)
  105. }
  106. return &m, nil
  107. }
  108. // UserProfileFindByTaobaoOpenID search user_profile ByTaobaoOpenID
  109. func UserProfileFindByTaobaoOpenID(Db *xorm.Engine, openid interface{}) (*model.UserProfile, error) {
  110. var m model.UserProfile
  111. if has, err := Db.Where("third_party_taobao_oid = ?", openid).Get(&m); err != nil || has == false {
  112. return nil, logx.Warn(err)
  113. }
  114. return &m, nil
  115. }
  116. // UserProfileFindByQQOpenID search user_profile ByTaobaoOpenID
  117. func UserProfileFindByQQOpenID(Db *xorm.Engine, openid interface{}) (*model.UserProfile, error) {
  118. var m model.UserProfile
  119. if has, err := Db.Where("third_party_qq_openid = ?", openid).Get(&m); err != nil || has == false {
  120. return nil, logx.Warn(err)
  121. }
  122. return &m, nil
  123. }
  124. // UserProfileFindByAppleToken search user_profile AppleToken
  125. func UserProfileFindByAppleToken(Db *xorm.Engine, token interface{}) (*model.UserProfile, error) {
  126. var m model.UserProfile
  127. if has, err := Db.Where("third_party_apple_token = ?", token).Get(&m); err != nil || has == false {
  128. return nil, logx.Warn(err)
  129. }
  130. return &m, nil
  131. }
  132. // UserProfileFindByWeChatOpenID search user_profile By 微信openid
  133. func UserProfileFindByWeChatOpenID(Db *xorm.Engine, openid interface{}) (*model.UserProfile, error) {
  134. var m model.UserProfile
  135. if has, err := Db.Where("third_party_wechat_openid = ?", openid).Get(&m); err != nil || has == false {
  136. return nil, logx.Warn(err)
  137. }
  138. return &m, nil
  139. }
  140. // UserProfileFindByWeChatMiniOpenID search user_profile By 小程序openid
  141. func UserProfileFindByWeChatMiniOpenID(Db *xorm.Engine, openid interface{}) (*model.UserProfile, error) {
  142. var m model.UserProfile
  143. if has, err := Db.Where("third_party_wechat_mini_openid = ?", openid).Get(&m); err != nil || has == false {
  144. return nil, logx.Warn(err)
  145. }
  146. return &m, nil
  147. }
  148. // UserProfileFindByWeChatUnionID search user_profile By 微信唯一id
  149. func UserProfileFindByWeChatUnionID(Db *xorm.Engine, openid interface{}) (*model.UserProfile, error) {
  150. if openid == "" {
  151. return nil, errors.New("不存在")
  152. }
  153. var m model.UserProfile
  154. if has, err := Db.Where("third_party_wechat_unionid = ? ", openid).Get(&m); err != nil || has == false {
  155. return nil, logx.Warn(err)
  156. }
  157. return &m, nil
  158. }
  159. func UserProfileFindByAccAlipay(Db *xorm.Engine, accAlipay string, uid int) (bool, error) {
  160. if has, err := Db.Where("acc_alipay = ? and uid <>?", accAlipay, uid).Exist(&model.UserProfile{}); err != nil || has == false {
  161. return false, logx.Warn(err)
  162. }
  163. return true, nil
  164. }
  165. // UserProfileisExistByTaobaoOpenID is exist by Taobao
  166. func UserProfileisExistByTaobaoOpenID(Db *xorm.Engine, openid string) (bool, error) {
  167. has, err := Db.Where("third_party_taobao_oid = ?", openid).Exist(&model.UserProfile{})
  168. if err != nil {
  169. return false, err
  170. }
  171. return has, nil
  172. }
  173. func UserProfileisThirdPartyWechatH5Openid(Db *xorm.Engine, openid string) (*model.UserProfile, error) {
  174. var user model.UserProfile
  175. has, err := Db.Where("third_party_wechat_h5_openid = ? and third_party_wechat_h5_openid<>''", openid).Get(&user)
  176. if err != nil || has == false {
  177. return nil, err
  178. }
  179. return &user, nil
  180. }
  181. // UserProfileisExistByQQOpenID is exist by QQ openid
  182. func UserProfileisExistByQQOpenID(Db *xorm.Engine, openid string) (bool, error) {
  183. has, err := Db.Where("third_party_qq_openid = ?", openid).Exist(&model.UserProfile{})
  184. if err != nil {
  185. return false, err
  186. }
  187. return has, nil
  188. }
  189. // UserProfileisExistByAppleToken is exist by apple token
  190. func UserProfileisExistByAppleToken(Db *xorm.Engine, token string) (bool, error) {
  191. has, err := Db.Where("third_party_apple_token = ?", token).Exist(&model.UserProfile{})
  192. if err != nil {
  193. return false, err
  194. }
  195. return has, nil
  196. }
  197. // UserProfileisExistByWeChatOpenID is exist by Wecaht openid
  198. func UserProfileisExistByWeChatOpenID(Db *xorm.Engine, openid string) (bool, error) {
  199. has, err := Db.Where("third_party_wechat_openid = ?", openid).Exist(&model.UserProfile{})
  200. if err != nil {
  201. return false, err
  202. }
  203. return has, nil
  204. }
  205. // UserProfileisExistByWeChatMiniOpenID is exist by Wecaht openid
  206. func UserProfileisExistByWeChatMiniOpenID(Db *xorm.Engine, openid string) (bool, error) {
  207. has, err := Db.Where("third_party_wechat_mini_openid = ?", openid).Exist(&model.UserProfile{})
  208. if err != nil {
  209. return false, err
  210. }
  211. return has, nil
  212. }
  213. // UserProfileisExistByWeChatUnionID is exist by Wecaht openid
  214. func UserProfileisExistByWeChatUnionID(Db *xorm.Engine, openid string) (bool, error) {
  215. if openid == "" {
  216. return false, errors.New("不存在")
  217. }
  218. has, err := Db.Where("third_party_wechat_unionid = ? ", openid).Exist(&model.UserProfile{})
  219. if err != nil {
  220. return false, err
  221. }
  222. return has, nil
  223. }
  224. // UserProfileisExistByRelationIDAndSpecialID is exist by RelationIdAndSpecialId
  225. func UserProfileisExistByRelationIDAndSpecialID(Db *xorm.Engine, SpecialID, RelationID int64) (bool, error) {
  226. has, err := Db.Where("acc_taobao_self_id = ? AND acc_taobao_share_id = ?", SpecialID, RelationID).Exist(&model.UserProfile{})
  227. if err != nil {
  228. return false, err
  229. }
  230. return has, nil
  231. }
  232. // UserProfileisExistBySpecialID is exist by SpecialId
  233. func UserProfileisExistBySpecialID(Db *xorm.Engine, SpecialID string) (bool, error) {
  234. has, err := Db.Where("acc_taobao_self_id = ? ", SpecialID).Exist(&model.UserProfile{})
  235. if err != nil {
  236. return false, err
  237. }
  238. return has, nil
  239. }
  240. // UserProfileCountByRelationID 统计relationID数量
  241. func UserProfileCountByRelationID(Db *xorm.Engine) (total int64, err error) {
  242. relate := new(model.UserProfile)
  243. total, err = Db.Where("acc_taobao_share_id > 0").Count(relate)
  244. return
  245. }
  246. // UserProfileCountByPUID 统计直推下级数量
  247. func UserProfileCountByPUID(Db *xorm.Engine, puid int) (total int64, err error) {
  248. relate := new(model.UserProfile)
  249. total, err = Db.Where("parent_uid = ?", puid).Count(relate)
  250. return
  251. }
  252. // UserProfileisExistByRelationID is exist by RelationID
  253. func UserProfileisExistByRelationID(Db *xorm.Engine, RelationID string) (bool, error) {
  254. has, err := Db.Where("acc_taobao_share_id = ? ", RelationID).Exist(&model.UserProfile{})
  255. if err != nil {
  256. return false, err
  257. }
  258. return has, nil
  259. }
  260. // UserProfileFindByIDs is in sql by ids
  261. func UserProfileFindByIDs(Db *xorm.Engine, uids ...int) (*[]model.UserProfile, error) {
  262. var m []model.UserProfile
  263. if err := Db.In("uid", uids).Find(&m); err != nil {
  264. return nil, logx.Warn(err)
  265. }
  266. return &m, nil
  267. }
  268. func UserProfileFindByIDsStr(Db *xorm.Engine, uids []string) (*[]model.UserProfile, error) {
  269. var m []model.UserProfile
  270. if err := Db.In("uid", uids).Find(&m); err != nil {
  271. return nil, logx.Warn(err)
  272. }
  273. return &m, nil
  274. }
  275. // UserProfileByPuid search user_profile by parent_uid
  276. func UserProfileByPuid(Db *xorm.Engine, puid interface{}) (*[]model.UserProfile, error) {
  277. var m []model.UserProfile
  278. if err := Db.Where("parent_uid = ?", puid).Find(&m); err != nil {
  279. return nil, logx.Warn(err)
  280. }
  281. return &m, nil
  282. }
  283. func UserProfileByPuidWithSess(sess *xorm.Session, puid interface{}) (*[]model.UserProfile, error) {
  284. var m []model.UserProfile
  285. if err := sess.Where("parent_uid = ?", puid).Find(&m); err != nil {
  286. return nil, logx.Warn(err)
  287. }
  288. return &m, nil
  289. }
  290. // UsersProfileInByIds is profiles by ids
  291. func UsersProfileInByIds(Db *xorm.Engine, ids []int, limit, start int) (*[]model.UserProfile, error) {
  292. var m []model.UserProfile
  293. if limit == 0 && start == 0 {
  294. if err := Db.In("uid", ids).Find(&m); err != nil {
  295. return nil, logx.Warn(err)
  296. }
  297. return &m, nil
  298. }
  299. if err := Db.In("uid", ids).Limit(limit, start).Find(&m); err != nil {
  300. return nil, logx.Warn(err)
  301. }
  302. return &m, nil
  303. }
  304. // UsersProfileInByUIDByisVerify is In查询 以及是否是有效用户
  305. func UsersProfileInByUIDByisVerify(Db *xorm.Engine, ids []int, isVerify interface{}) (*[]model.UserProfile, error) {
  306. var m []model.UserProfile
  307. if err := Db.In("uid", ids).Where("is_verify = ?", isVerify).
  308. Find(&m); err != nil {
  309. return nil, logx.Warn(err)
  310. }
  311. return &m, nil
  312. }
  313. // UsersProfileInByIdsByDesc is 根据某列 降序
  314. func UsersProfileInByIdsByDesc(Db *xorm.Engine, ids []int, limit, start int, c string) (*[]model.UserProfile, error) {
  315. var m []model.UserProfile
  316. if limit == 0 && start == 0 {
  317. if err := Db.In("uid", ids).Desc(c).Find(&m); err != nil {
  318. return nil, logx.Warn(err)
  319. }
  320. return &m, nil
  321. }
  322. if err := Db.In("uid", ids).Desc(c).Limit(limit, start).Find(&m); err != nil {
  323. return nil, logx.Warn(err)
  324. }
  325. return &m, nil
  326. }
  327. // UsersProfileInByIdsByAsc is 根据某列 升序
  328. func UsersProfileInByIdsByAsc(Db *xorm.Engine, ids []int, limit, start int, c string) (*[]model.UserProfile, error) {
  329. var m []model.UserProfile
  330. if limit == 0 && start == 0 {
  331. if err := Db.In("uid", ids).Asc(c).Find(&m); err != nil {
  332. return nil, logx.Warn(err)
  333. }
  334. return &m, nil
  335. }
  336. if err := Db.In("uid", ids).Asc(c).Limit(limit, start).Find(&m); err != nil {
  337. return nil, logx.Warn(err)
  338. }
  339. return &m, nil
  340. }
  341. // UsersProfileByAll is 查询所有分享id大于0的数据
  342. func UsersProfileByTaobaoShateIdNotNull(Db *xorm.Engine, limit, start int) (*[]model.UserProfile, error) {
  343. var m []model.UserProfile
  344. if err := Db.Where("acc_taobao_share_id > 0").Limit(limit, start).Find(&m); err != nil {
  345. return nil, logx.Warn(err)
  346. }
  347. return &m, nil
  348. }
  349. // UserProfileIsExistByUserID is mobile exist
  350. func UserProfileIsExistByUserID(Db *xorm.Engine, id int) (bool, error) {
  351. has, err := Db.Where("uid = ?", id).Exist(&model.UserProfile{})
  352. if err != nil {
  353. return false, err
  354. }
  355. return has, nil
  356. }
  357. // UserProfileIsExistByInviteCode is exist ?
  358. func UserProfileIsExistByInviteCode(Db *xorm.Engine, code string) (bool, error) {
  359. has, err := Db.Where("invite_code = ?", code).Exist(&model.UserProfile{})
  360. if err != nil {
  361. return false, err
  362. }
  363. return has, nil
  364. }
  365. // UserProfileIsExistByCustomInviteCode is exist ?
  366. func UserProfileIsExistByCustomInviteCode(Db *xorm.Engine, code string) (bool, error) {
  367. has, err := Db.Where("custom_invite_code = ?", code).Exist(&model.UserProfile{})
  368. if err != nil {
  369. return false, err
  370. }
  371. return has, nil
  372. }
  373. // UserProfileInsert is insert user
  374. func UserProfileInsert(Db *xorm.Engine, userProfile *model.UserProfile) (int64, error) {
  375. affected, err := Db.Insert(userProfile)
  376. if err != nil {
  377. return 0, err
  378. }
  379. return affected, nil
  380. }
  381. // UserProfileUpdate is update userprofile
  382. func UserProfileUpdate(Db *xorm.Engine, uid interface{}, userProfile *model.UserProfile, forceCols ...string) (int64, error) {
  383. var (
  384. affected int64
  385. err error
  386. )
  387. if forceCols != nil {
  388. affected, err = Db.Where("uid=?", uid).Cols(forceCols...).Update(userProfile)
  389. } else {
  390. affected, err = Db.Where("uid=?", uid).AllCols().Omit("fin_valid,parent_uid").Update(userProfile)
  391. }
  392. if err != nil {
  393. return 0, logx.Warn(err)
  394. }
  395. return affected, nil
  396. }
  397. func UserProfileUpdateWithSess(sess *xorm.Session, uid interface{}, userProfile *model.UserProfile, forceCols ...string) (int64, error) {
  398. var (
  399. affected int64
  400. err error
  401. )
  402. if forceCols != nil {
  403. affected, err = sess.Where("uid=?", uid).Cols(forceCols...).Update(userProfile)
  404. } else {
  405. affected, err = sess.Where("uid=?", uid).AllCols().Omit("fin_valid").Update(userProfile)
  406. }
  407. if err != nil {
  408. return 0, logx.Warn(err)
  409. }
  410. return affected, nil
  411. }
  412. // UserProfileUpdateByArkID is update userprofile
  413. func UserProfileUpdateByArkID(Db *xorm.Engine, arkid interface{}, userProfile *model.UserProfile, forceCols ...string) (int64, error) {
  414. var (
  415. affected int64
  416. err error
  417. )
  418. if forceCols != nil {
  419. affected, err = Db.Where("arkid_uid=?", arkid).Cols(forceCols...).Update(userProfile)
  420. } else {
  421. affected, err = Db.Where("arkid_uid=?", arkid).Update(userProfile)
  422. }
  423. if err != nil {
  424. return 0, logx.Warn(err)
  425. }
  426. return affected, nil
  427. }
  428. // UserProfileDelete is delete user profile
  429. func UserProfileDelete(Db *xorm.Engine, uid interface{}) (int64, error) {
  430. return Db.Where("uid = ?", uid).Delete(model.UserProfile{})
  431. }
  432. func UserProfileDeleteWithSess(sess *xorm.Session, uid interface{}) (int64, error) {
  433. return sess.Where("uid = ?", uid).Delete(model.UserProfile{})
  434. }
  435. func UserProfileFindByIdWithSession(session *xorm.Session, uid int) (*model.UserProfile, error) {
  436. var m model.UserProfile
  437. if has, err := session.Where("uid = ?", uid).Get(&m); err != nil || has == false {
  438. return nil, logx.Warn(err)
  439. }
  440. return &m, nil
  441. }
  442. // 在事务中更新用户信息
  443. func UserProfileUpdateWithSession(session *xorm.Session, uid interface{}, userProfile *model.UserProfile, forceCols ...string) (int64, error) {
  444. var (
  445. affected int64
  446. err error
  447. )
  448. if forceCols != nil {
  449. affected, err = session.Where("uid=?", uid).Cols(forceCols...).Update(userProfile)
  450. } else {
  451. affected, err = session.Where("uid=?", uid).Omit("fin_valid").Update(userProfile)
  452. }
  453. if err != nil {
  454. return 0, logx.Warn(err)
  455. }
  456. return affected, nil
  457. }
  458. // 根据uid获取md.user
  459. func UserAllInfoByUid(Db *xorm.Engine, uid interface{}) (*md.User, error) {
  460. u, err := UserFindByID(Db, uid)
  461. if err != nil {
  462. return nil, err
  463. }
  464. if u == nil {
  465. return nil, errors.New("user is nil")
  466. }
  467. up, err := UserProfileFindByID(Db, uid)
  468. if err != nil {
  469. return nil, err
  470. }
  471. if utils.AnyToInt64(uid) == 0 {
  472. userLevel, err := UserLevelInIDescByWeightLowWithOne(Db)
  473. if err != nil {
  474. return nil, err
  475. }
  476. if userLevel != nil {
  477. u.Level = userLevel.Id
  478. }
  479. }
  480. // 获取user 等级
  481. ul, err := UserLevelByID(Db, u.Level)
  482. if u.Uid == 0 {
  483. one, err := UserLevelInIDescByWeightLowWithOne(Db)
  484. if err != nil {
  485. return nil, err
  486. }
  487. ul = one
  488. }
  489. if err != nil {
  490. return nil, err
  491. }
  492. user := &md.User{
  493. Info: u,
  494. Profile: up,
  495. Level: ul,
  496. }
  497. return user, nil
  498. }
  499. // UpdateUserProfileFinValid 更新用户余额
  500. func UpdateUserProfileFinValid(Db *xorm.Engine, uid interface{}, newAmount string) error {
  501. update, err := Db.Where("uid=?", uid).Update(&model.UserProfile{FinValid: newAmount})
  502. if err != nil {
  503. return err
  504. }
  505. if update != 1 {
  506. return errors.New("更新失败")
  507. }
  508. return nil
  509. }
  510. // UpdateUserProfileFinValidWithSess 事务更新用户余额
  511. func UpdateUserProfileFinValidWithSess(sess *xorm.Session, uid interface{}, newAmount string) error {
  512. update, err := sess.Where("uid=?", uid).Update(&model.UserProfile{FinValid: newAmount})
  513. if err != nil {
  514. return err
  515. }
  516. if update != 1 {
  517. return errors.New("更新失败")
  518. }
  519. return nil
  520. }