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.

411 lines
13 KiB

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