面包店
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 

439 Zeilen
13 KiB

  1. package db
  2. import (
  3. "applet/app/db/model"
  4. "applet/app/md"
  5. "applet/app/utils/logx"
  6. "errors"
  7. "xorm.io/xorm"
  8. )
  9. //UserProfileFindByArkID is get userprofile by arkid
  10. func UserProfileFindByArkID(Db *xorm.Engine, id interface{}) (*model.UserProfile, error) {
  11. var m model.UserProfile
  12. if has, err := Db.Where("arkid_uid = ?", id).Get(&m); err != nil || has == false {
  13. return nil, logx.Warn(err)
  14. }
  15. return &m, nil
  16. }
  17. //UserProfileFindByInviteCode is get userprofile by InviteCode
  18. func UserProfileFindByInviteCode(Db *xorm.Engine, code string) (*model.UserProfile, error) {
  19. var m model.UserProfile
  20. if has, err := Db.Where("invite_code = ?", code).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 UserProfileFindByCustomInviteCode(Db *xorm.Engine, code string) (*model.UserProfile, error) {
  27. var m model.UserProfile
  28. if has, err := Db.Where("custom_invite_code = ?", code).Get(&m); err != nil || has == false {
  29. return nil, logx.Warn(err)
  30. }
  31. return &m, nil
  32. }
  33. //UserProfileFindByInviteCodes is get userprofile by InviteCode
  34. func UserProfileFindByInviteCodes(Db *xorm.Engine, codes ...string) (*[]model.UserProfile, error) {
  35. var m []model.UserProfile
  36. if err := Db.In("invite_code", codes).Find(&m); err != nil {
  37. return nil, logx.Warn(err)
  38. }
  39. return &m, nil
  40. }
  41. //UserProfileFindByCustomInviteCodes is get userprofile by CustomInviteCode
  42. func UserProfileFindByCustomInviteCodes(Db *xorm.Engine, codes ...string) (*[]model.UserProfile, error) {
  43. var m []model.UserProfile
  44. if err := Db.In("custom_invite_code", codes).Find(&m); err != nil {
  45. return nil, logx.Warn(err)
  46. }
  47. return &m, nil
  48. }
  49. // UserProfileFindByID search user_profile by userid
  50. func UserProfileFindByID(Db *xorm.Engine, id interface{}) (*model.UserProfile, error) {
  51. var m model.UserProfile
  52. if has, err := Db.Where("uid = ?", id).Get(&m); err != nil || has == false {
  53. return nil, logx.Warn(err)
  54. }
  55. return &m, nil
  56. }
  57. // UserProfileOrderByNew 找最新的记录
  58. func UserProfileOrderByNew(Db *xorm.Engine) (*model.UserProfile, error) {
  59. var m model.UserProfile
  60. if has, err := Db.Where("invite_code != ''").OrderBy("uid desc").Get(&m); err != nil || has == false {
  61. return nil, logx.Warn(err)
  62. }
  63. return &m, nil
  64. }
  65. // UserProfileFindByTaobaoOpenID search user_profile ByTaobaoOpenID
  66. func UserProfileFindByTaobaoOpenID(Db *xorm.Engine, openid interface{}) (*model.UserProfile, error) {
  67. var m model.UserProfile
  68. if has, err := Db.Where("third_party_taobao_oid = ?", openid).Get(&m); err != nil || has == false {
  69. return nil, logx.Warn(err)
  70. }
  71. return &m, nil
  72. }
  73. // UserProfileFindByQQOpenID search user_profile ByTaobaoOpenID
  74. func UserProfileFindByQQOpenID(Db *xorm.Engine, openid interface{}) (*model.UserProfile, error) {
  75. var m model.UserProfile
  76. if has, err := Db.Where("third_party_qq_openid = ?", openid).Get(&m); err != nil || has == false {
  77. return nil, logx.Warn(err)
  78. }
  79. return &m, nil
  80. }
  81. // UserProfileFindByAppleToken search user_profile AppleToken
  82. func UserProfileFindByAppleToken(Db *xorm.Engine, token interface{}) (*model.UserProfile, error) {
  83. var m model.UserProfile
  84. if has, err := Db.Where("third_party_apple_token = ?", token).Get(&m); err != nil || has == false {
  85. return nil, logx.Warn(err)
  86. }
  87. return &m, nil
  88. }
  89. // UserProfileFindByWeChatOpenID search user_profile By 微信openid
  90. func UserProfileFindByWeChatOpenID(Db *xorm.Engine, openid interface{}) (*model.UserProfile, error) {
  91. var m model.UserProfile
  92. if has, err := Db.Where("third_party_wechat_openid = ?", openid).Get(&m); err != nil || has == false {
  93. return nil, logx.Warn(err)
  94. }
  95. return &m, nil
  96. }
  97. // UserProfileFindByWeChatMiniOpenID search user_profile By 小程序openid
  98. func UserProfileFindByWeChatMiniOpenID(Db *xorm.Engine, openid interface{}) (*model.UserProfile, error) {
  99. var m model.UserProfile
  100. if has, err := Db.Where("third_party_wechat_mini_openid = ?", openid).Get(&m); err != nil || has == false {
  101. return nil, logx.Warn(err)
  102. }
  103. return &m, nil
  104. }
  105. // UserProfileFindByWeChatUnionID search user_profile By 微信唯一id
  106. func UserProfileFindByWeChatUnionID(Db *xorm.Engine, openid interface{}) (*model.UserProfile, error) {
  107. var m model.UserProfile
  108. if has, err := Db.Where("third_party_wechat_unionid = ?", openid).Get(&m); err != nil || has == false {
  109. return nil, logx.Warn(err)
  110. }
  111. return &m, nil
  112. }
  113. // UserProfileisExistByTaobaoOpenID is exist by Taobao
  114. func UserProfileisExistByTaobaoOpenID(Db *xorm.Engine, openid string) (bool, error) {
  115. has, err := Db.Where("third_party_taobao_oid = ?", openid).Exist(&model.UserProfile{})
  116. if err != nil {
  117. return false, err
  118. }
  119. return has, nil
  120. }
  121. // UserProfileisExistByQQOpenID is exist by QQ openid
  122. func UserProfileisExistByQQOpenID(Db *xorm.Engine, openid string) (bool, error) {
  123. has, err := Db.Where("third_party_qq_openid = ?", openid).Exist(&model.UserProfile{})
  124. if err != nil {
  125. return false, err
  126. }
  127. return has, nil
  128. }
  129. // UserProfileisExistByAppleToken is exist by apple token
  130. func UserProfileisExistByAppleToken(Db *xorm.Engine, token string) (bool, error) {
  131. has, err := Db.Where("third_party_apple_token = ?", token).Exist(&model.UserProfile{})
  132. if err != nil {
  133. return false, err
  134. }
  135. return has, nil
  136. }
  137. // UserProfileisExistByWeChatOpenID is exist by Wecaht openid
  138. func UserProfileisExistByWeChatOpenID(Db *xorm.Engine, openid string) (bool, error) {
  139. has, err := Db.Where("third_party_wechat_openid = ?", openid).Exist(&model.UserProfile{})
  140. if err != nil {
  141. return false, err
  142. }
  143. return has, nil
  144. }
  145. // UserProfileisExistByWeChatMiniOpenID is exist by Wecaht openid
  146. func UserProfileisExistByWeChatMiniOpenID(Db *xorm.Engine, openid string) (bool, error) {
  147. has, err := Db.Where("third_party_wechat_mini_openid = ?", openid).Exist(&model.UserProfile{})
  148. if err != nil {
  149. return false, err
  150. }
  151. return has, nil
  152. }
  153. // UserProfileisExistByWeChatUnionID is exist by Wecaht openid
  154. func UserProfileisExistByWeChatUnionID(Db *xorm.Engine, openid string) (bool, error) {
  155. has, err := Db.Where("third_party_wechat_unionid = ?", openid).Exist(&model.UserProfile{})
  156. if err != nil {
  157. return false, err
  158. }
  159. return has, nil
  160. }
  161. // UserProfileisExistByRelationIDAndSpecialID is exist by RelationIdAndSpecialId
  162. func UserProfileisExistByRelationIDAndSpecialID(Db *xorm.Engine, SpecialID, RelationID int64) (bool, error) {
  163. has, err := Db.Where("acc_taobao_self_id = ? AND acc_taobao_share_id = ?", SpecialID, RelationID).Exist(&model.UserProfile{})
  164. if err != nil {
  165. return false, err
  166. }
  167. return has, nil
  168. }
  169. // UserProfileisExistBySpecialID is exist by SpecialId
  170. func UserProfileisExistBySpecialID(Db *xorm.Engine, SpecialID string) (bool, error) {
  171. has, err := Db.Where("acc_taobao_self_id = ? ", SpecialID).Exist(&model.UserProfile{})
  172. if err != nil {
  173. return false, err
  174. }
  175. return has, nil
  176. }
  177. // UserProfileCountByRelationID 统计relationID数量
  178. func UserProfileCountByRelationID(Db *xorm.Engine) (total int64, err error) {
  179. relate := new(model.UserProfile)
  180. total, err = Db.Where("acc_taobao_share_id > 0").Count(relate)
  181. return
  182. }
  183. // UserProfileCountByPUID 统计直推下级数量
  184. func UserProfileCountByPUID(Db *xorm.Engine, puid int) (total int64, err error) {
  185. relate := new(model.UserProfile)
  186. total, err = Db.Where("parent_uid = ?", puid).Count(relate)
  187. return
  188. }
  189. // UserProfileisExistByRelationID is exist by RelationID
  190. func UserProfileisExistByRelationID(Db *xorm.Engine, RelationID string) (bool, error) {
  191. has, err := Db.Where("acc_taobao_share_id = ? ", RelationID).Exist(&model.UserProfile{})
  192. if err != nil {
  193. return false, err
  194. }
  195. return has, nil
  196. }
  197. // UserProfileFindByIDs is in sql by ids
  198. func UserProfileFindByIDs(Db *xorm.Engine, uids ...int) (*[]model.UserProfile, error) {
  199. var m []model.UserProfile
  200. if err := Db.In("uid", uids).Find(&m); err != nil {
  201. return nil, logx.Warn(err)
  202. }
  203. return &m, nil
  204. }
  205. // UserProfileByPuid search user_profile by parent_uid
  206. func UserProfileByPuid(Db *xorm.Engine, puid interface{}) (*[]model.UserProfile, error) {
  207. var m []model.UserProfile
  208. if err := Db.Where("parent_uid = ?", puid).Find(&m); err != nil {
  209. return nil, logx.Warn(err)
  210. }
  211. return &m, nil
  212. }
  213. //UsersProfileInByIds is profiles by ids
  214. func UsersProfileInByIds(Db *xorm.Engine, ids []int, limit, start int) (*[]model.UserProfile, error) {
  215. var m []model.UserProfile
  216. if limit == 0 && start == 0 {
  217. if err := Db.In("uid", ids).Find(&m); err != nil {
  218. return nil, logx.Warn(err)
  219. }
  220. return &m, nil
  221. }
  222. if err := Db.In("uid", ids).Limit(limit, start).Find(&m); err != nil {
  223. return nil, logx.Warn(err)
  224. }
  225. return &m, nil
  226. }
  227. // UsersProfileInByUIDByisVerify is In查询 以及是否是有效用户
  228. func UsersProfileInByUIDByisVerify(Db *xorm.Engine, ids []int, isVerify interface{}) (*[]model.UserProfile, error) {
  229. var m []model.UserProfile
  230. if err := Db.In("uid", ids).Where("is_verify = ?", isVerify).
  231. Find(&m); err != nil {
  232. return nil, logx.Warn(err)
  233. }
  234. return &m, nil
  235. }
  236. // UsersProfileInByIdsByDesc is 根据某列 降序
  237. func UsersProfileInByIdsByDesc(Db *xorm.Engine, ids []int, limit, start int, c string) (*[]model.UserProfile, error) {
  238. var m []model.UserProfile
  239. if limit == 0 && start == 0 {
  240. if err := Db.In("uid", ids).Desc(c).Find(&m); err != nil {
  241. return nil, logx.Warn(err)
  242. }
  243. return &m, nil
  244. }
  245. if err := Db.In("uid", ids).Desc(c).Limit(limit, start).Find(&m); err != nil {
  246. return nil, logx.Warn(err)
  247. }
  248. return &m, nil
  249. }
  250. // UsersProfileInByIdsByAsc is 根据某列 升序
  251. func UsersProfileInByIdsByAsc(Db *xorm.Engine, ids []int, limit, start int, c string) (*[]model.UserProfile, error) {
  252. var m []model.UserProfile
  253. if limit == 0 && start == 0 {
  254. if err := Db.In("uid", ids).Asc(c).Find(&m); err != nil {
  255. return nil, logx.Warn(err)
  256. }
  257. return &m, nil
  258. }
  259. if err := Db.In("uid", ids).Asc(c).Limit(limit, start).Find(&m); err != nil {
  260. return nil, logx.Warn(err)
  261. }
  262. return &m, nil
  263. }
  264. // UsersProfileByAll is 查询所有分享id大于0的数据
  265. func UsersProfileByTaobaoShateIdNotNull(Db *xorm.Engine, limit, start int) (*[]model.UserProfile, error) {
  266. var m []model.UserProfile
  267. if err := Db.Where("acc_taobao_share_id > 0").Limit(limit, start).Find(&m); err != nil {
  268. return nil, logx.Warn(err)
  269. }
  270. return &m, nil
  271. }
  272. // UserProfileIsExistByUserID is mobile exist
  273. func UserProfileIsExistByUserID(Db *xorm.Engine, id int) (bool, error) {
  274. has, err := Db.Where("uid = ?", id).Exist(&model.UserProfile{})
  275. if err != nil {
  276. return false, err
  277. }
  278. return has, nil
  279. }
  280. // UserProfileIsExistByInviteCode is exist ?
  281. func UserProfileIsExistByInviteCode(Db *xorm.Engine, code string) (bool, error) {
  282. has, err := Db.Where("invite_code = ?", code).Exist(&model.UserProfile{})
  283. if err != nil {
  284. return false, err
  285. }
  286. return has, nil
  287. }
  288. // UserProfileIsExistByCustomInviteCode is exist ?
  289. func UserProfileIsExistByCustomInviteCode(Db *xorm.Engine, code string) (bool, error) {
  290. has, err := Db.Where("custom_invite_code = ?", code).Exist(&model.UserProfile{})
  291. if err != nil {
  292. return false, err
  293. }
  294. return has, nil
  295. }
  296. //UserProfileInsert is insert user
  297. func UserProfileInsert(Db *xorm.Engine, userProfile *model.UserProfile) (int64, error) {
  298. affected, err := Db.Insert(userProfile)
  299. if err != nil {
  300. return 0, err
  301. }
  302. return affected, nil
  303. }
  304. //UserProfileUpdate is update userprofile
  305. func UserProfileUpdate(Db *xorm.Engine, uid interface{}, userProfile *model.UserProfile, forceCols ...string) (int64, error) {
  306. var (
  307. affected int64
  308. err error
  309. )
  310. if forceCols != nil {
  311. affected, err = Db.Where("uid=?", uid).Cols(forceCols...).Update(userProfile)
  312. } else {
  313. affected, err = Db.Where("uid=?", uid).Update(userProfile)
  314. }
  315. if err != nil {
  316. return 0, logx.Warn(err)
  317. }
  318. return affected, nil
  319. }
  320. //UserProfileUpdateByArkID is update userprofile
  321. func UserProfileUpdateByArkID(Db *xorm.Engine, arkid interface{}, userProfile *model.UserProfile, forceCols ...string) (int64, error) {
  322. var (
  323. affected int64
  324. err error
  325. )
  326. if forceCols != nil {
  327. affected, err = Db.Where("arkid_uid=?", arkid).Cols(forceCols...).Update(userProfile)
  328. } else {
  329. affected, err = Db.Where("arkid_uid=?", arkid).Update(userProfile)
  330. }
  331. if err != nil {
  332. return 0, logx.Warn(err)
  333. }
  334. return affected, nil
  335. }
  336. // UserProfileDelete is delete user profile
  337. func UserProfileDelete(Db *xorm.Engine, uid interface{}) (int64, error) {
  338. return Db.Where("uid = ?", uid).Delete(model.UserProfile{})
  339. }
  340. func UserProfileFindByIdWithSession(session *xorm.Session, uid int) (*model.UserProfile, error) {
  341. var m model.UserProfile
  342. if has, err := session.Where("uid = ?", uid).Get(&m); err != nil || has == false {
  343. return nil, logx.Warn(err)
  344. }
  345. return &m, nil
  346. }
  347. // 在事务中更新用户信息
  348. func UserProfileUpdateWithSession(session *xorm.Session, uid interface{}, userProfile *model.UserProfile, forceCols ...string) (int64, error) {
  349. var (
  350. affected int64
  351. err error
  352. )
  353. if forceCols != nil {
  354. affected, err = session.Where("uid=?", uid).Cols(forceCols...).Update(userProfile)
  355. } else {
  356. affected, err = session.Where("uid=?", uid).Update(userProfile)
  357. }
  358. if err != nil {
  359. return 0, logx.Warn(err)
  360. }
  361. return affected, nil
  362. }
  363. // 根据uid获取md.user
  364. func UserAllInfoByUid(Db *xorm.Engine, uid interface{}) (*md.User, error) {
  365. u, err := UserFindByID(Db, uid)
  366. if err != nil {
  367. return nil, err
  368. }
  369. if u == nil {
  370. return nil, errors.New("user is nil")
  371. }
  372. up, err := UserProfileFindByID(Db, uid)
  373. if err != nil {
  374. return nil, err
  375. }
  376. // 获取user 等级
  377. ul, err := UserLevelByID(Db, u.Level)
  378. if err != nil {
  379. return nil, err
  380. }
  381. user := &md.User{
  382. Info: u,
  383. Profile: up,
  384. Level: ul,
  385. }
  386. return user, nil
  387. }