o2o公共业务代码
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.

410 lines
12 KiB

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