user.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "go-common/app/job/main/passport-user-compare/model"
  7. xsql "go-common/library/database/sql"
  8. "go-common/library/log"
  9. )
  10. var (
  11. insertUserBaseSQL = "INSERT INTO user_base (mid,userid,pwd,salt,status,deleted,mtime) VALUES(?,?,?,?,?,?,?)"
  12. selectUserBaseSQL = "SELECT mid, userid, pwd, salt, status FROM user_base WHERE mid = ?"
  13. updateUserBaseSQL = "UPDATE user_base SET userid = ?, pwd = ? ,salt = ? ,status = ? WHERE mid = ?"
  14. insertUserTelSQL = "INSERT INTO user_tel (mid,tel,cid,tel_bind_time,mtime) VALUES(?,?,?,?,?)"
  15. selectUserTelSQL = "SELECT mid, tel, cid, tel_bind_time FROM user_tel WHERE mid = ?"
  16. updateUserTelSQL = "UPDATE user_tel SET tel = ? ,cid = ? WHERE mid = ?"
  17. insertUserMailSQL = "INSERT INTO user_email (mid,email,verified,email_bind_time,mtime) VALUES(?,?,?,?,?)"
  18. selectUserMailSQL = "SELECT mid,email,email_bind_time FROM user_email WHERE mid = ?"
  19. updateUserMailSQL = "UPDATE user_email SET email = ? WHERE mid = ?"
  20. updateUserMailVerifiedSQL = "UPDATE user_email SET verified = ? WHERE mid = ?"
  21. insertUserSafeQuestionSQL = "INSERT INTO user_safe_question%02d (mid,safe_question,safe_answer,safe_bind_time) VALUES(?,?,?,?)"
  22. selectUserSafeQuestionSQL = "SELECT mid, safe_question, safe_answer FROM user_safe_question%02d WHERE mid = ?"
  23. updateUserSafeQuestionSQL = "UPDATE user_safe_question%02d SET safe_question = ? ,safe_answer = ? WHERE mid = ? "
  24. insertThirdBindSQL = "INSERT INTO user_third_bind (mid,openid,platform,token,expires) VALUES(?,?,?,?,?)"
  25. selectThirdBindSQL = "SELECT mid, openid, platform, token FROM user_third_bind WHERE mid = ? AND platform = ?"
  26. updateThirdBindSQL = "UPDATE user_third_bind SET openid = ? ,token = ? WHERE mid = ? AND platform = ? "
  27. queryCountryCodeSQL = "SELECT id,code FROM country_code"
  28. queryMidByTelSQL = "SELECT mid FROM user_tel WHERE tel = ? and cid = ?"
  29. queryMidByEmailSQL = "SELECT mid FROM user_email WHERE email = ?"
  30. getUnverifiedEmail = "SELECT mid FROM user_email where mid > ? and verified = 0 limit 20000"
  31. _getUserRegOriginByMidSQL = "SELECT mid,join_ip,join_time,origin,reg_type,appid from user_reg_origin%02d where mid = ?"
  32. _insertUpdateUserRegOriginTypeSQL = "INSERT INTO user_reg_origin%02d (mid,join_ip,join_time,origin,reg_type,appid,ctime,mtime) VALUES (?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE join_ip=?,join_time=?,origin=?,reg_type=?,appid=?,ctime=?,mtime=?"
  33. )
  34. // QueryUserBase query user basic info by mid
  35. func (d *Dao) QueryUserBase(c context.Context, mid int64) (res *model.UserBase, err error) {
  36. row := d.userDB.QueryRow(c, selectUserBaseSQL, mid)
  37. res = new(model.UserBase)
  38. if err = row.Scan(&res.Mid, &res.UserID, &res.Pwd, &res.Salt, &res.Status); err != nil {
  39. if err == xsql.ErrNoRows {
  40. res = nil
  41. err = nil
  42. } else {
  43. log.Error("row.Scan() error(%v)", err)
  44. }
  45. return
  46. }
  47. return
  48. }
  49. // UpdateUserBase update user base
  50. func (d *Dao) UpdateUserBase(c context.Context, a *model.UserBase) (affected int64, err error) {
  51. var res sql.Result
  52. if res, err = d.userDB.Exec(c, updateUserBaseSQL, a.UserID, a.Pwd, a.Salt, a.Status, a.Mid); err != nil {
  53. log.Error("failed to update user base, dao.userDB.Exec() error(%v)", err)
  54. return
  55. }
  56. return res.RowsAffected()
  57. }
  58. // InsertUserBase add user base.
  59. func (d *Dao) InsertUserBase(c context.Context, a *model.UserBase) (affected int64, err error) {
  60. var res sql.Result
  61. if res, err = d.userDB.Exec(c, insertUserBaseSQL, a.Mid, a.UserID, a.Pwd, a.Salt, a.Status, a.Deleted, a.MTime); err != nil {
  62. log.Error("fail to add user base, userBase(%+v) dao.userDB.Exec() error(%+v)", a, err)
  63. return
  64. }
  65. return res.RowsAffected()
  66. }
  67. // QueryUserTel query user tel info by mid
  68. func (d *Dao) QueryUserTel(c context.Context, mid int64) (res *model.UserTel, err error) {
  69. row := d.userDB.QueryRow(c, selectUserTelSQL, mid)
  70. res = new(model.UserTel)
  71. var cidPtr *string
  72. if err = row.Scan(&res.Mid, &res.Tel, &cidPtr, &res.TelBindTime); err != nil {
  73. if err == xsql.ErrNoRows {
  74. res = nil
  75. err = nil
  76. } else {
  77. log.Error("row.Scan() error(%v)", err)
  78. }
  79. return
  80. }
  81. if cidPtr != nil {
  82. res.Cid = *cidPtr
  83. }
  84. return
  85. }
  86. // UpdateUserTel update user tel
  87. func (d *Dao) UpdateUserTel(c context.Context, a *model.UserTel) (affected int64, err error) {
  88. var (
  89. res sql.Result
  90. telPtr *[]byte
  91. cidPtr string
  92. )
  93. if len(a.Tel) != 0 {
  94. telPtr = &a.Tel
  95. cidPtr = a.Cid
  96. }
  97. if res, err = d.userDB.Exec(c, updateUserTelSQL, telPtr, cidPtr, a.Mid); err != nil {
  98. log.Error("failed to update user tel, dao.userDB.Exec() error(%v)", err)
  99. return
  100. }
  101. return res.RowsAffected()
  102. }
  103. // InsertUserTel insert user tel
  104. func (d *Dao) InsertUserTel(c context.Context, a *model.UserTel) (affected int64, err error) {
  105. var (
  106. res sql.Result
  107. telPtr *[]byte
  108. cidPtr string
  109. )
  110. if len(a.Tel) != 0 {
  111. telPtr = &a.Tel
  112. cidPtr = a.Cid
  113. }
  114. if res, err = d.userDB.Exec(c, insertUserTelSQL, a.Mid, telPtr, cidPtr, a.TelBindTime, a.MTime); err != nil {
  115. log.Error("fail to add user tel, userTel(%+v) dao.userDB.Exec() error(%+v)", a, err)
  116. return
  117. }
  118. return res.RowsAffected()
  119. }
  120. // QueryUserMail query user mail info by mid
  121. func (d *Dao) QueryUserMail(c context.Context, mid int64) (res *model.UserEmail, err error) {
  122. row := d.userDB.QueryRow(c, selectUserMailSQL, mid)
  123. res = new(model.UserEmail)
  124. if err = row.Scan(&res.Mid, &res.Email, &res.EmailBindTime); err != nil {
  125. if err == xsql.ErrNoRows {
  126. res = nil
  127. err = nil
  128. } else {
  129. log.Error("row.Scan() error(%v)", err)
  130. }
  131. return
  132. }
  133. return
  134. }
  135. // UpdateUserMail update user tel
  136. func (d *Dao) UpdateUserMail(c context.Context, a *model.UserEmail) (affected int64, err error) {
  137. var res sql.Result
  138. if res, err = d.userDB.Exec(c, updateUserMailSQL, a.Email, a.Mid); err != nil {
  139. log.Error("failed to update user mail, dao.userDB.Exec() error(%v)", err)
  140. return
  141. }
  142. return res.RowsAffected()
  143. }
  144. // UpdateUserMailVerified update user email verified
  145. func (d *Dao) UpdateUserMailVerified(c context.Context, a *model.UserEmail) (affected int64, err error) {
  146. var res sql.Result
  147. if res, err = d.userDB.Exec(c, updateUserMailVerifiedSQL, a.Verified, a.Mid); err != nil {
  148. log.Error("failed to update user mail verified, dao.userDB.Exec() error(%v)", err)
  149. return
  150. }
  151. return res.RowsAffected()
  152. }
  153. // InsertUserEmail add user email.
  154. func (d *Dao) InsertUserEmail(c context.Context, a *model.UserEmail) (affected int64, err error) {
  155. var (
  156. res sql.Result
  157. emailPtr *[]byte
  158. )
  159. if len(a.Email) != 0 {
  160. emailPtr = &a.Email
  161. }
  162. if res, err = d.userDB.Exec(c, insertUserMailSQL, a.Mid, emailPtr, a.Verified, a.EmailBindTime, a.MTime); err != nil {
  163. log.Error("fail to add user email, userEmail(%+v) dao.userDB.Exec() error(%+v)", a, err)
  164. return
  165. }
  166. return res.RowsAffected()
  167. }
  168. // QueryUserSafeQuestion query user safe question by mid
  169. func (d *Dao) QueryUserSafeQuestion(c context.Context, mid int64) (res *model.UserSafeQuestion, err error) {
  170. row := d.userDB.QueryRow(c, fmt.Sprintf(selectUserSafeQuestionSQL, mid%safeQuestionSegment), mid)
  171. res = new(model.UserSafeQuestion)
  172. if err = row.Scan(&res.Mid, &res.SafeQuestion, &res.SafeAnswer); err != nil {
  173. if err == xsql.ErrNoRows {
  174. res = nil
  175. err = nil
  176. } else {
  177. log.Error("row.Scan() error(%v)", err)
  178. }
  179. return
  180. }
  181. return
  182. }
  183. // UpdateUserSafeQuestion update user tel
  184. func (d *Dao) UpdateUserSafeQuestion(c context.Context, a *model.UserSafeQuestion) (affected int64, err error) {
  185. var res sql.Result
  186. if res, err = d.userDB.Exec(c, fmt.Sprintf(updateUserSafeQuestionSQL, a.Mid%safeQuestionSegment), a.SafeQuestion, a.SafeAnswer, a.Mid); err != nil {
  187. log.Error("failed to update user safe question, dao.userDB.Exec() error(%v)", err)
  188. return
  189. }
  190. return res.RowsAffected()
  191. }
  192. // InsertUserSafeQuestion insert user safe question
  193. func (d *Dao) InsertUserSafeQuestion(c context.Context, a *model.UserSafeQuestion) (affected int64, err error) {
  194. var res sql.Result
  195. if res, err = d.userDB.Exec(c, fmt.Sprintf(insertUserSafeQuestionSQL, a.Mid%safeQuestionSegment), a.Mid, a.SafeQuestion, a.SafeAnswer, a.SafeBindTime); err != nil {
  196. log.Error("fail to add user safe question, userSafeQuestion(%+v) dao.userDB.Exec() error(%+v)", a, err)
  197. return
  198. }
  199. return res.RowsAffected()
  200. }
  201. // QueryUserThirdBind query user third bind by mid and platform
  202. func (d *Dao) QueryUserThirdBind(c context.Context, mid, platform int64) (res *model.UserThirdBind, err error) {
  203. row := d.userDB.QueryRow(c, selectThirdBindSQL, mid, platform)
  204. res = new(model.UserThirdBind)
  205. if err = row.Scan(&res.Mid, &res.OpenID, &res.PlatForm, &res.Token); err != nil {
  206. if err == xsql.ErrNoRows {
  207. res = nil
  208. err = nil
  209. } else {
  210. log.Error("row.Scan() error(%v)", err)
  211. }
  212. return
  213. }
  214. return
  215. }
  216. // UpdateUserThirdBind update user third bind
  217. func (d *Dao) UpdateUserThirdBind(c context.Context, a *model.UserThirdBind) (affected int64, err error) {
  218. var res sql.Result
  219. if res, err = d.userDB.Exec(c, updateThirdBindSQL, a.OpenID, a.Token, a.Mid, a.PlatForm); err != nil {
  220. log.Error("failed to update user third bind sql, dao.userDB.Exec() error(%v)", err)
  221. return
  222. }
  223. return res.RowsAffected()
  224. }
  225. // InsertUserThirdBind insert user third bind.
  226. func (d *Dao) InsertUserThirdBind(c context.Context, a *model.UserThirdBind) (affected int64, err error) {
  227. var res sql.Result
  228. if res, err = d.userDB.Exec(c, insertThirdBindSQL, a.Mid, a.OpenID, a.PlatForm, a.Token, a.Expires); err != nil {
  229. log.Error("fail to add user third bind, userThirdBind(%+v) dao.userDB.Exec() error(%+v)", a, err)
  230. return
  231. }
  232. return res.RowsAffected()
  233. }
  234. // QueryCountryCode query country code
  235. func (d *Dao) QueryCountryCode(c context.Context) (res map[int64]string, err error) {
  236. var rows *xsql.Rows
  237. if rows, err = d.userDB.Query(c, queryCountryCodeSQL); err != nil {
  238. log.Error("fail to get CountryCodeMap, dao.originDB.Query(%s) error(%v)", queryCountryCodeSQL, err)
  239. return
  240. }
  241. defer rows.Close()
  242. res = make(map[int64]string)
  243. for rows.Next() {
  244. var (
  245. id int64
  246. code string
  247. )
  248. if err = rows.Scan(&id, &code); err != nil {
  249. log.Error("row.Scan() error(%v)", err)
  250. res = nil
  251. return
  252. }
  253. res[id] = code
  254. }
  255. return
  256. }
  257. // GetMidByTel get mid by tel.
  258. func (d *Dao) GetMidByTel(c context.Context, a *model.UserTel) (mid int64, err error) {
  259. if err = d.userDB.QueryRow(c, queryMidByTelSQL, a.Tel, a.Cid).Scan(&mid); err != nil {
  260. log.Error("fail to get mid by tel, dao.userDB.QueryRow(%s) error(%+v)", queryMidByTelSQL, err)
  261. return
  262. }
  263. return
  264. }
  265. // GetMidByEmail get mid by email.
  266. func (d *Dao) GetMidByEmail(c context.Context, a *model.UserEmail) (mid int64, err error) {
  267. if err = d.userDB.QueryRow(c, queryMidByEmailSQL, a.Email).Scan(&mid); err != nil {
  268. log.Error("fail to get mid by email, dao.userDB.QueryRow(%s) error(%+v)", queryMidByEmailSQL, err)
  269. return
  270. }
  271. return
  272. }
  273. // GetUnverifiedEmail get unverified email.
  274. func (d *Dao) GetUnverifiedEmail(c context.Context, start int64) (res []*model.UserEmail, err error) {
  275. var rows *xsql.Rows
  276. if rows, err = d.userDB.Query(c, getUnverifiedEmail, start); err != nil {
  277. log.Error("fail to get UnverifiedEmail, dao.userDB.Query(%s) error(%v)", getUnverifiedEmail, err)
  278. return
  279. }
  280. defer rows.Close()
  281. for rows.Next() {
  282. r := new(model.UserEmail)
  283. if err = rows.Scan(&r.Mid); err != nil {
  284. log.Error("row.Scan() error(%v)", err)
  285. res = nil
  286. return
  287. }
  288. res = append(res, r)
  289. }
  290. return
  291. }
  292. // GetUserRegOriginByMid get user reg origin by mid.
  293. func (d *Dao) GetUserRegOriginByMid(c context.Context, mid int64) (res *model.UserRegOrigin, err error) {
  294. res = &model.UserRegOrigin{}
  295. if err = d.userDB.QueryRow(c, fmt.Sprintf(_getUserRegOriginByMidSQL, tableIndex(mid)), mid).Scan(&res.Mid, &res.JoinIP, &res.JoinTime, &res.Origin, &res.RegType, &res.AppID); err != nil {
  296. if err == xsql.ErrNoRows {
  297. err = nil
  298. res = nil
  299. } else {
  300. log.Error("fail to get UserRegOrigin by mid(%d), dao.userDB.QueryRow(%s) error(%+v)", mid, _getUserRegOriginByMidSQL, err)
  301. }
  302. return
  303. }
  304. return
  305. }
  306. // InsertUpdateUserRegOriginType insert update user reg origin type.
  307. func (d *Dao) InsertUpdateUserRegOriginType(c context.Context, a *model.UserRegOrigin) (affected int64, err error) {
  308. var res sql.Result
  309. if res, err = d.userDB.Exec(c, fmt.Sprintf(_insertUpdateUserRegOriginTypeSQL, tableIndex(a.Mid)), a.Mid, a.JoinIP, a.JoinTime, a.Origin, a.RegType, a.AppID, a.CTime, a.MTime,
  310. a.JoinIP, a.JoinTime, a.Origin, a.RegType, a.AppID, a.CTime, a.MTime); err != nil {
  311. log.Error("fail to insert update user reg origin type, userRegOrigin(%+v) dao.userDB.Exec() error(%+v)", a, err)
  312. return
  313. }
  314. return res.RowsAffected()
  315. }
  316. func tableIndex(mid int64) int64 {
  317. return mid % 100
  318. }