user.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "go-common/app/job/main/passport-user/model"
  7. xsql "go-common/library/database/sql"
  8. "go-common/library/log"
  9. )
  10. const (
  11. _addCountryCodeSQL = "INSERT INTO country_code (id,code,cname,rank,type,ename) VALUES(?,?,?,?,?,?)"
  12. _getAesKeySQL = "SELECT `key` FROM user_secret WHERE key_type = 2"
  13. _getSaltSQL = "SELECT `key` FROM user_secret WHERE key_type = 3"
  14. _getCountryCodeMapSQL = "SELECT id,code FROM country_code"
  15. _getUserTelSQL = "SELECT mid FROM user_tel WHERE mid > ? limit ?"
  16. _getUserEmailByMidSQL = "SELECT mid,email,verified,email_bind_time,ctime,mtime FROM user_email WHERE mid = ?"
  17. _getUserTelByMidSQL = "SELECT mid,tel,cid,tel_bind_time,ctime,mtime FROM user_tel WHERE mid = ?"
  18. _addUserBaseSQL = "INSERT INTO user_base (mid,userid,pwd,salt,status,deleted,mtime) VALUES(?,?,?,?,?,?,?)"
  19. _addUserEmailSQL = "INSERT INTO user_email (mid,email,verified,email_bind_time,mtime) VALUES(?,?,?,?,?)"
  20. _addUserTelSQL = "INSERT INTO user_tel (mid,tel,cid,tel_bind_time,mtime) VALUES(?,?,?,?,?)"
  21. _addUserSafeQuestionSQL = "INSERT INTO user_safe_question%02d (mid,safe_question,safe_answer,safe_bind_time) VALUES(?,?,?,?)"
  22. _addUserThirdBindSQL = "INSERT INTO user_third_bind (mid,openid,platform,token,expires) VALUES(?,?,?,?,?)"
  23. _updateUserBaseSQL = "UPDATE user_base SET userid=?,pwd=?,salt=?,status=? WHERE mid =?"
  24. _updateUserEmailSQL = "UPDATE user_email SET email=? WHERE mid =?"
  25. _updateUserEmailAndBindTimeSQL = "UPDATE user_email SET email=?,email_bind_time=? WHERE mid =?"
  26. _updateUserTelSQL = "UPDATE user_tel SET tel=?,cid=? WHERE mid =?"
  27. _updateUserTelAndBindTimeSQL = "UPDATE user_tel SET tel=?,cid=?,tel_bind_time=? WHERE mid =?"
  28. _updateUserSafeQuestionSQL = "UPDATE user_safe_question%02d SET safe_question=?,safe_answer=? WHERE mid =?"
  29. _updateUserThirdBindSQL = "UPDATE user_third_bind SET openid=?,token=?,expires=? WHERE mid =? and platform=?"
  30. _updateUserEmailVerifiedSQL = "UPDATE user_email SET verified=? WHERE mid =?"
  31. _updateUserEmailBindTimeSQL = "UPDATE user_email SET verified=?,email_bind_time=? WHERE mid =?"
  32. _updateUserTelBindTimeSQL = "UPDATE user_tel SET tel_bind_time=? WHERE mid =?"
  33. _insertUpdateUserRegOriginSQL = "INSERT INTO user_reg_origin%02d (mid,join_ip,join_ip_v6,port,join_time) VALUES (?,?,?,?,?) ON DUPLICATE KEY UPDATE join_ip=?,join_ip_v6=?,port=?,join_time=?"
  34. _insertUpdateUserRegOriginTypeSQL = "INSERT INTO user_reg_origin%02d (mid,origin,reg_type,appid,ctime,mtime) VALUES (?,?,?,?,?,?) ON DUPLICATE KEY UPDATE origin=?,reg_type=?,appid=?,ctime=?,mtime=?"
  35. _delUserBase = "UPDATE user_base SET deleted=1 WHERE mid =?"
  36. _delUserTel = "UPDATE user_tel SET tel=null,cid=null WHERE mid =?"
  37. _delUserEmail = "UPDATE user_email SET email=null WHERE mid =?"
  38. _getMidByTelSQL = "SELECT mid FROM user_tel WHERE tel = ? and cid = ?"
  39. _getMidByEmailSQL = "SELECT mid FROM user_email WHERE email = ?"
  40. _getUserBaseByMidSQL = "SELECT mid,userid,pwd,salt,status,ctime,mtime FROM user_base WHERE mid = ?"
  41. _getUserSafeQuestionByMidSQL = "SELECT mid,safe_question,safe_answer,safe_bind_time,ctime,mtime FROM user_safe_question%02d where mid = ?"
  42. _getUserThirdBindByMidSQL = "SELECT id,mid,openid,platform,token,expires,ctime,mtime FROM user_third_bind where mid = ?"
  43. _getUserThirdBindByMidAndPlatformSQL = "SELECT id,mid,openid,platform,token,expires,ctime,mtime FROM user_third_bind where mid = ? and platform = ? limit 1"
  44. _getUserRegOriginByMidSQL = "SELECT mid,join_ip,join_ip_v6,port,join_time,origin,reg_type,appid from user_reg_origin%02d where mid = ?"
  45. _insertUpdateUserBaseSQL = "INSERT INTO user_base (mid,userid,pwd,salt,status,deleted,mtime) VALUES (?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE userid=?,pwd=?,salt=?,status=?"
  46. _insertIgnoreUserSafeQuestionSQL = "INSERT IGNORE INTO user_safe_question%02d (mid,safe_question,safe_answer,safe_bind_time) VALUES(?,?,?,?)"
  47. _insertIgnoreUserRegOriginSQL = "INSERT IGNORE INTO user_reg_origin%02d (mid,join_ip,join_ip_v6,port,join_time) VALUES (?,?,?,?,?)"
  48. _delUserThirdBindSQL = "UPDATE user_third_bind SET openid='',token='',expires=0 WHERE mid =?"
  49. )
  50. // AddCountryCode add country code.
  51. func (d *Dao) AddCountryCode(c context.Context, a *model.CountryCode) (affected int64, err error) {
  52. var res sql.Result
  53. if res, err = d.userDB.Exec(c, _addCountryCodeSQL, a.ID, a.Code, a.Cname, a.Rank, a.Type, a.Ename); err != nil {
  54. log.Error("fail to add country code, countryCode(%+v) dao.userDB.Exec() error(%+v)", a, err)
  55. return
  56. }
  57. return res.RowsAffected()
  58. }
  59. // AesKey get aes key.
  60. func (d *Dao) AesKey(c context.Context) (res string, err error) {
  61. if err = d.encryptDB.QueryRow(c, _getAesKeySQL).Scan(&res); err != nil {
  62. log.Error("fail to get AesKey, dao.encryptDB.QueryRow(%s) error(%v)", _getAesKeySQL, err)
  63. return
  64. }
  65. return
  66. }
  67. // Salt get salt.
  68. func (d *Dao) Salt(c context.Context) (res string, err error) {
  69. if err = d.encryptDB.QueryRow(c, _getSaltSQL).Scan(&res); err != nil {
  70. log.Error("fail to get Salt, dao.encryptDB.QueryRow(%s) error(%v)", _getSaltSQL, err)
  71. return
  72. }
  73. return
  74. }
  75. // UserTel get user tel.
  76. func (d *Dao) UserTel(c context.Context, start, count int64) (res []*model.UserTel, err error) {
  77. var rows *xsql.Rows
  78. if rows, err = d.userDB.Query(c, _getUserTelSQL, start, count); err != nil {
  79. log.Error("fail to get UserTel, dao.userDB.Query(%s) error(%v)", _getUserTelSQL, err)
  80. return
  81. }
  82. defer rows.Close()
  83. for rows.Next() {
  84. r := new(model.UserTel)
  85. if err = rows.Scan(&r.Mid); err != nil {
  86. log.Error("row.Scan() error(%v)", err)
  87. res = nil
  88. return
  89. }
  90. res = append(res, r)
  91. }
  92. return
  93. }
  94. // GetUserEmailByMid get user email by mid.
  95. func (d *Dao) GetUserEmailByMid(c context.Context, mid int64) (res *model.UserEmail, err error) {
  96. res = &model.UserEmail{}
  97. if err = d.userDB.QueryRow(c, _getUserEmailByMidSQL, mid).Scan(&res.Mid, &res.Email, &res.Verified, &res.EmailBindTime, &res.CTime, &res.MTime); err != nil {
  98. if err == xsql.ErrNoRows {
  99. err = nil
  100. res = nil
  101. } else {
  102. log.Error("fail to get UserEmail by mid(%d), dao.encryptDB.QueryRow(%s) error(%v)", mid, _getUserEmailByMidSQL, err)
  103. }
  104. return
  105. }
  106. return
  107. }
  108. // GetUserTelByMid get user email by mid.
  109. func (d *Dao) GetUserTelByMid(c context.Context, mid int64) (res *model.UserTel, err error) {
  110. var cidPtr *string
  111. res = &model.UserTel{}
  112. if err = d.userDB.QueryRow(c, _getUserTelByMidSQL, mid).Scan(&res.Mid, &res.Tel, &cidPtr, &res.TelBindTime, &res.CTime, &res.MTime); err != nil {
  113. if err == xsql.ErrNoRows {
  114. err = nil
  115. res = nil
  116. } else {
  117. log.Error("fail to get UserTel by mid(%d), dao.encryptDB.QueryRow(%s) error(%v)", mid, _getUserTelByMidSQL, err)
  118. }
  119. return
  120. }
  121. if cidPtr != nil {
  122. res.Cid = *cidPtr
  123. }
  124. return
  125. }
  126. // AddUserBase add user base.
  127. func (d *Dao) AddUserBase(c context.Context, a *model.UserBase) (affected int64, err error) {
  128. var res sql.Result
  129. if res, err = d.userDB.Exec(c, _addUserBaseSQL, a.Mid, a.UserID, a.Pwd, a.Salt, a.Status, a.Deleted, a.MTime); err != nil {
  130. log.Error("fail to add user base, userBase(%+v) dao.userDB.Exec() error(%+v)", a, err)
  131. return
  132. }
  133. return res.RowsAffected()
  134. }
  135. // TxAddUserBase add user base.
  136. func (d *Dao) TxAddUserBase(tx *xsql.Tx, a *model.UserBase) (affected int64, err error) {
  137. var res sql.Result
  138. if res, err = tx.Exec(_addUserBaseSQL, a.Mid, a.UserID, a.Pwd, a.Salt, a.Status, a.Deleted, a.MTime); err != nil {
  139. log.Error("fail to add user base, userBase(%+v) dao.userDB.Exec() error(%+v)", a, err)
  140. return
  141. }
  142. return res.RowsAffected()
  143. }
  144. // AddUserEmail add user email.
  145. func (d *Dao) AddUserEmail(c context.Context, a *model.UserEmail) (affected int64, err error) {
  146. var (
  147. res sql.Result
  148. emailPtr *[]byte
  149. )
  150. if len(a.Email) != 0 {
  151. emailPtr = &a.Email
  152. }
  153. if res, err = d.userDB.Exec(c, _addUserEmailSQL, a.Mid, emailPtr, a.Verified, a.EmailBindTime, a.MTime); err != nil {
  154. log.Error("fail to add user email, userEmail(%+v) dao.userDB.Exec() error(%+v)", a, err)
  155. return
  156. }
  157. return res.RowsAffected()
  158. }
  159. // TxAddUserEmail add user email.
  160. func (d *Dao) TxAddUserEmail(tx *xsql.Tx, a *model.UserEmail) (affected int64, err error) {
  161. var (
  162. res sql.Result
  163. emailPtr *[]byte
  164. )
  165. if len(a.Email) != 0 {
  166. emailPtr = &a.Email
  167. }
  168. if res, err = tx.Exec(_addUserEmailSQL, a.Mid, emailPtr, a.Verified, a.EmailBindTime, a.MTime); err != nil {
  169. log.Error("fail to add user email, userEmail(%+v) dao.userDB.Exec() error(%+v)", a, err)
  170. return
  171. }
  172. return res.RowsAffected()
  173. }
  174. // AddUserTel add user tel.
  175. func (d *Dao) AddUserTel(c context.Context, a *model.UserTel) (affected int64, err error) {
  176. var (
  177. res sql.Result
  178. telPtr *[]byte
  179. cidPtr *string
  180. )
  181. if len(a.Tel) != 0 {
  182. telPtr = &a.Tel
  183. cidPtr = &a.Cid
  184. }
  185. if res, err = d.userDB.Exec(c, _addUserTelSQL, a.Mid, telPtr, cidPtr, a.TelBindTime, a.MTime); err != nil {
  186. log.Error("fail to add user tel, userTel(%+v) dao.userDB.Exec() error(%+v)", a, err)
  187. return
  188. }
  189. return res.RowsAffected()
  190. }
  191. // TxAddUserTel add user tel.
  192. func (d *Dao) TxAddUserTel(tx *xsql.Tx, a *model.UserTel) (affected int64, err error) {
  193. var (
  194. res sql.Result
  195. telPtr *[]byte
  196. cidPtr *string
  197. )
  198. if len(a.Tel) != 0 {
  199. telPtr = &a.Tel
  200. cidPtr = &a.Cid
  201. }
  202. if res, err = tx.Exec(_addUserTelSQL, a.Mid, telPtr, cidPtr, a.TelBindTime, a.MTime); err != nil {
  203. log.Error("fail to add user tel, userTel(%+v) dao.userDB.Exec() error(%+v)", a, err)
  204. return
  205. }
  206. return res.RowsAffected()
  207. }
  208. // AddUserSafeQuestion add user safe question.
  209. func (d *Dao) AddUserSafeQuestion(c context.Context, a *model.UserSafeQuestion) (affected int64, err error) {
  210. var res sql.Result
  211. if res, err = d.userDB.Exec(c, fmt.Sprintf(_addUserSafeQuestionSQL, tableIndex(a.Mid)), a.Mid, a.SafeQuestion, a.SafeAnswer, a.SafeBindTime); err != nil {
  212. log.Error("fail to add user safe question, userSafeQuestion(%+v) dao.userDB.Exec() error(%+v)", a, err)
  213. return
  214. }
  215. return res.RowsAffected()
  216. }
  217. // TxAddUserSafeQuestion add user safe question.
  218. func (d *Dao) TxAddUserSafeQuestion(tx *xsql.Tx, a *model.UserSafeQuestion) (affected int64, err error) {
  219. var res sql.Result
  220. if res, err = tx.Exec(fmt.Sprintf(_addUserSafeQuestionSQL, tableIndex(a.Mid)), a.Mid, a.SafeQuestion, a.SafeAnswer, a.SafeBindTime); err != nil {
  221. log.Error("fail to add user safe question, userSafeQuestion(%+v) dao.userDB.Exec() error(%+v)", a, err)
  222. return
  223. }
  224. return res.RowsAffected()
  225. }
  226. // AddUserThirdBind add user third bind.
  227. func (d *Dao) AddUserThirdBind(c context.Context, a *model.UserThirdBind) (affected int64, err error) {
  228. var res sql.Result
  229. if res, err = d.userDB.Exec(c, _addUserThirdBindSQL, a.Mid, a.OpenID, a.PlatForm, a.Token, a.Expires); err != nil {
  230. log.Error("fail to add user third bind, userThirdBind(%+v) dao.userDB.Exec() error(%+v)", a, err)
  231. return
  232. }
  233. return res.RowsAffected()
  234. }
  235. // TxAddUserThirdBind add user third bind.
  236. func (d *Dao) TxAddUserThirdBind(tx *xsql.Tx, a *model.UserThirdBind) (affected int64, err error) {
  237. var res sql.Result
  238. if res, err = tx.Exec(_addUserThirdBindSQL, a.Mid, a.OpenID, a.PlatForm, a.Token, a.Expires); err != nil {
  239. log.Error("fail to add user third bind, userThirdBind(%+v) dao.userDB.Exec() error(%+v)", a, err)
  240. return
  241. }
  242. return res.RowsAffected()
  243. }
  244. // UpdateUserBase update user base.
  245. func (d *Dao) UpdateUserBase(c context.Context, a *model.UserBase) (affected int64, err error) {
  246. var res sql.Result
  247. if res, err = d.userDB.Exec(c, _updateUserBaseSQL, a.UserID, a.Pwd, a.Salt, a.Status, a.Mid); err != nil {
  248. log.Error("fail to update user base, userBase(%+v) dao.userDB.Exec() error(%+v)", a, err)
  249. return
  250. }
  251. return res.RowsAffected()
  252. }
  253. // UpdateUserEmail update user email.
  254. func (d *Dao) UpdateUserEmail(c context.Context, a *model.UserEmail) (affected int64, err error) {
  255. var (
  256. res sql.Result
  257. emailPtr *[]byte
  258. )
  259. if len(a.Email) != 0 {
  260. emailPtr = &a.Email
  261. }
  262. if res, err = d.userDB.Exec(c, _updateUserEmailSQL, emailPtr, a.Mid); err != nil {
  263. log.Error("fail to update user email, userEmail(%+v) dao.userDB.Exec() error(%+v)", a, err)
  264. return
  265. }
  266. return res.RowsAffected()
  267. }
  268. // UpdateUserEmailAndBindTime update user email and bind time.
  269. func (d *Dao) UpdateUserEmailAndBindTime(c context.Context, a *model.UserEmail) (affected int64, err error) {
  270. var (
  271. res sql.Result
  272. emailPtr *[]byte
  273. )
  274. if len(a.Email) != 0 {
  275. emailPtr = &a.Email
  276. }
  277. if res, err = d.userDB.Exec(c, _updateUserEmailAndBindTimeSQL, emailPtr, a.EmailBindTime, a.Mid); err != nil {
  278. log.Error("fail to update user email and bind time, userEmail(%+v) dao.userDB.Exec() error(%+v)", a, err)
  279. return
  280. }
  281. return res.RowsAffected()
  282. }
  283. // TxUpdateUserEmail update user email.
  284. func (d *Dao) TxUpdateUserEmail(tx *xsql.Tx, a *model.UserEmail) (affected int64, err error) {
  285. var (
  286. res sql.Result
  287. emailPtr *[]byte
  288. )
  289. if len(a.Email) != 0 {
  290. emailPtr = &a.Email
  291. }
  292. if res, err = tx.Exec(_updateUserEmailSQL, emailPtr, a.Mid); err != nil {
  293. log.Error("fail to update user email, userEmail(%+v) dao.userDB.Exec() error(%+v)", a, err)
  294. return
  295. }
  296. return res.RowsAffected()
  297. }
  298. // UpdateUserTel update user tel.
  299. func (d *Dao) UpdateUserTel(c context.Context, a *model.UserTel) (affected int64, err error) {
  300. var (
  301. res sql.Result
  302. telPtr *[]byte
  303. cidPtr *string
  304. )
  305. if len(a.Tel) != 0 {
  306. telPtr = &a.Tel
  307. cidPtr = &a.Cid
  308. }
  309. if res, err = d.userDB.Exec(c, _updateUserTelSQL, telPtr, cidPtr, a.Mid); err != nil {
  310. log.Error("fail to update user tel, userTel(%+v) dao.userDB.Exec() error(%+v)", a, err)
  311. return
  312. }
  313. return res.RowsAffected()
  314. }
  315. // UpdateUserTelAndBindTime update user tel and bind time.
  316. func (d *Dao) UpdateUserTelAndBindTime(c context.Context, a *model.UserTel) (affected int64, err error) {
  317. var (
  318. res sql.Result
  319. telPtr *[]byte
  320. cidPtr *string
  321. )
  322. if len(a.Tel) != 0 {
  323. telPtr = &a.Tel
  324. cidPtr = &a.Cid
  325. }
  326. if res, err = d.userDB.Exec(c, _updateUserTelAndBindTimeSQL, telPtr, cidPtr, a.TelBindTime, a.Mid); err != nil {
  327. log.Error("fail to update user tel and bind time, userTel(%+v) dao.userDB.Exec() error(%+v)", a, err)
  328. return
  329. }
  330. return res.RowsAffected()
  331. }
  332. // UpdateUserSafeQuesion update user safe question.
  333. func (d *Dao) UpdateUserSafeQuesion(c context.Context, a *model.UserSafeQuestion) (affected int64, err error) {
  334. var res sql.Result
  335. if res, err = d.userDB.Exec(c, fmt.Sprintf(_updateUserSafeQuestionSQL, tableIndex(a.Mid)), a.SafeQuestion, a.SafeAnswer, a.Mid); err != nil {
  336. log.Error("fail to update user safe question, userSafeQuestion(%+v) dao.userDB.Exec() error(%+v)", a, err)
  337. return
  338. }
  339. return res.RowsAffected()
  340. }
  341. // TxUpdateUserSafeQuesion update user safe question.
  342. func (d *Dao) TxUpdateUserSafeQuesion(tx *xsql.Tx, a *model.UserSafeQuestion) (affected int64, err error) {
  343. var res sql.Result
  344. if res, err = tx.Exec(fmt.Sprintf(_updateUserSafeQuestionSQL, tableIndex(a.Mid)), a.SafeQuestion, a.SafeAnswer, a.Mid); err != nil {
  345. log.Error("fail to update user safe question, userSafeQuestion(%+v) dao.userDB.Exec() error(%+v)", a, err)
  346. return
  347. }
  348. return res.RowsAffected()
  349. }
  350. // UpdateUserThirdBind update user third bind.
  351. func (d *Dao) UpdateUserThirdBind(c context.Context, a *model.UserThirdBind) (affected int64, err error) {
  352. var res sql.Result
  353. if res, err = d.userDB.Exec(c, _updateUserThirdBindSQL, a.OpenID, a.Token, a.Expires, a.Mid, a.PlatForm); err != nil {
  354. log.Error("fail to update user third bind, userThirdBind(%+v) dao.userDB.Exec() error(%+v)", a, err)
  355. return
  356. }
  357. return res.RowsAffected()
  358. }
  359. // UpdateUserEmailVerified update user email verified.
  360. func (d *Dao) UpdateUserEmailVerified(c context.Context, a *model.UserEmail) (affected int64, err error) {
  361. var res sql.Result
  362. if res, err = d.userDB.Exec(c, _updateUserEmailVerifiedSQL, a.Verified, a.Mid); err != nil {
  363. log.Error("fail to update user email verified, userEmail(%+v) dao.userDB.Exec() error(%+v)", a, err)
  364. return
  365. }
  366. return res.RowsAffected()
  367. }
  368. // TxUpdateUserEmailVerified update user email verified.
  369. func (d *Dao) TxUpdateUserEmailVerified(tx *xsql.Tx, a *model.UserEmail) (affected int64, err error) {
  370. var res sql.Result
  371. if res, err = tx.Exec(_updateUserEmailVerifiedSQL, a.Verified, a.Mid); err != nil {
  372. log.Error("fail to update user email verified, userEmail(%+v) dao.userDB.Exec() error(%+v)", a, err)
  373. return
  374. }
  375. return res.RowsAffected()
  376. }
  377. // UpdateUserEmailBindTime update user email bind time.
  378. func (d *Dao) UpdateUserEmailBindTime(c context.Context, a *model.UserEmail) (affected int64, err error) {
  379. var res sql.Result
  380. if res, err = d.userDB.Exec(c, _updateUserEmailBindTimeSQL, a.Verified, a.EmailBindTime, a.Mid); err != nil {
  381. log.Error("fail to update user email bind time, userEmail(%+v) dao.userDB.Exec() error(%+v)", a, err)
  382. return
  383. }
  384. return res.RowsAffected()
  385. }
  386. // TxUpdateUserEmailBindTime update user email bind time.
  387. func (d *Dao) TxUpdateUserEmailBindTime(tx *xsql.Tx, a *model.UserEmail) (affected int64, err error) {
  388. var res sql.Result
  389. if res, err = tx.Exec(_updateUserEmailBindTimeSQL, a.Verified, a.EmailBindTime, a.Mid); err != nil {
  390. log.Error("fail to update user email bind time, userEmail(%+v) dao.userDB.Exec() error(%+v)", a, err)
  391. return
  392. }
  393. return res.RowsAffected()
  394. }
  395. // UpdateUserTelBindTime update user tel bind time.
  396. func (d *Dao) UpdateUserTelBindTime(c context.Context, a *model.UserTel) (affected int64, err error) {
  397. var res sql.Result
  398. if res, err = d.userDB.Exec(c, _updateUserTelBindTimeSQL, a.TelBindTime, a.Mid); err != nil {
  399. log.Error("fail to update user tel bind time, userTel(%+v) dao.userDB.Exec() error(%+v)", a, err)
  400. return
  401. }
  402. return res.RowsAffected()
  403. }
  404. // TxDelUserBase update user base deleted = 1.
  405. func (d *Dao) TxDelUserBase(tx *xsql.Tx, mid int64) (affected int64, err error) {
  406. var res sql.Result
  407. if res, err = tx.Exec(_delUserBase, mid); err != nil {
  408. log.Error("fail to del user base, mid(%d) dao.userDB.Exec() error(%+v)", mid, err)
  409. return
  410. }
  411. return res.RowsAffected()
  412. }
  413. // TxDelUserTel update user tel deleted = 1.
  414. func (d *Dao) TxDelUserTel(tx *xsql.Tx, mid int64) (affected int64, err error) {
  415. var res sql.Result
  416. if res, err = tx.Exec(_delUserTel, mid); err != nil {
  417. log.Error("fail to del user tel, mid(%d) dao.userDB.Exec() error(%+v)", mid, err)
  418. return
  419. }
  420. return res.RowsAffected()
  421. }
  422. // TxDelUserEmail update user email deleted = 1.
  423. func (d *Dao) TxDelUserEmail(tx *xsql.Tx, mid int64) (affected int64, err error) {
  424. var res sql.Result
  425. if res, err = tx.Exec(_delUserEmail, mid); err != nil {
  426. log.Error("fail to del user email, mid(%d) dao.userDB.Exec() error(%+v)", mid, err)
  427. return
  428. }
  429. return res.RowsAffected()
  430. }
  431. // TxInsertUpdateUserRegOrigin insert update user reg origin.
  432. func (d *Dao) TxInsertUpdateUserRegOrigin(tx *xsql.Tx, a *model.UserRegOrigin) (affected int64, err error) {
  433. var res sql.Result
  434. if res, err = tx.Exec(fmt.Sprintf(_insertUpdateUserRegOriginSQL, tableIndex(a.Mid)), a.Mid, a.JoinIP, a.JoinIPV6, a.Port, a.JoinTime, a.JoinIP, a.JoinIPV6, a.Port, a.JoinTime); err != nil {
  435. log.Error("fail to insert update user reg origin, userRegOrigin(%+v) dao.userDB.Exec() error(%+v)", a, err)
  436. return
  437. }
  438. return res.RowsAffected()
  439. }
  440. // InsertUpdateUserRegOriginType insert update user reg origin type.
  441. func (d *Dao) InsertUpdateUserRegOriginType(c context.Context, a *model.UserRegOrigin) (affected int64, err error) {
  442. var res sql.Result
  443. if res, err = d.userDB.Exec(c, fmt.Sprintf(_insertUpdateUserRegOriginTypeSQL, tableIndex(a.Mid)), a.Mid, a.Origin, a.RegType, a.AppID, a.CTime, a.MTime,
  444. a.Origin, a.RegType, a.AppID, a.CTime, a.MTime); err != nil {
  445. log.Error("fail to insert update user reg origin type, userRegOrigin(%+v) dao.userDB.Exec() error(%+v)", a, err)
  446. return
  447. }
  448. return res.RowsAffected()
  449. }
  450. // CountryCodeMap get country code map.
  451. func (d *Dao) CountryCodeMap(c context.Context) (res map[int64]string, err error) {
  452. var rows *xsql.Rows
  453. if rows, err = d.userDB.Query(c, _getCountryCodeMapSQL); err != nil {
  454. log.Error("fail to get CountryCodeMap, dao.userDB.Query(%s) error(%+v)", _getCountryCodeMapSQL, err)
  455. return
  456. }
  457. defer rows.Close()
  458. res = make(map[int64]string)
  459. for rows.Next() {
  460. var (
  461. id int64
  462. code string
  463. )
  464. if err = rows.Scan(&id, &code); err != nil {
  465. log.Error("row.Scan() error(%v)", err)
  466. res = nil
  467. return
  468. }
  469. res[id] = code
  470. }
  471. return
  472. }
  473. // GetMidByTel get mid by tel.
  474. func (d *Dao) GetMidByTel(c context.Context, a *model.UserTel) (mid int64, err error) {
  475. if err = d.userDB.QueryRow(c, _getMidByTelSQL, a.Tel, a.Cid).Scan(&mid); err != nil {
  476. log.Error("fail to get mid by tel, dao.userDB.QueryRow(%s) error(%+v)", _getMidByTelSQL, err)
  477. return
  478. }
  479. return
  480. }
  481. // GetMidByEmail get mid by email.
  482. func (d *Dao) GetMidByEmail(c context.Context, a *model.UserEmail) (mid int64, err error) {
  483. if err = d.userDB.QueryRow(c, _getMidByEmailSQL, a.Email).Scan(&mid); err != nil {
  484. log.Error("fail to get mid by email, dao.userDB.QueryRow(%s) error(%+v)", _getMidByEmailSQL, err)
  485. return
  486. }
  487. return
  488. }
  489. // GetUserBaseByMid get user base by mid.
  490. func (d *Dao) GetUserBaseByMid(c context.Context, mid int64) (res *model.UserBase, err error) {
  491. res = &model.UserBase{}
  492. if err = d.userDB.QueryRow(c, _getUserBaseByMidSQL, mid).Scan(&res.Mid, &res.UserID, &res.Pwd, &res.Salt, &res.Status, &res.CTime, &res.MTime); err != nil {
  493. if err == xsql.ErrNoRows {
  494. err = nil
  495. res = nil
  496. } else {
  497. log.Error("fail to get UserBase by mid(%d), dao.userDB.QueryRow(%s) error(%+v)", mid, _getUserBaseByMidSQL, err)
  498. }
  499. return
  500. }
  501. return
  502. }
  503. // GetUserSafeQuestionByMid get user safe question by mid.
  504. func (d *Dao) GetUserSafeQuestionByMid(c context.Context, mid int64) (res *model.UserSafeQuestion, err error) {
  505. res = &model.UserSafeQuestion{}
  506. if err = d.userDB.QueryRow(c, fmt.Sprintf(_getUserSafeQuestionByMidSQL, tableIndex(mid)), mid).Scan(&res.Mid, &res.SafeQuestion, &res.SafeAnswer, &res.SafeBindTime, &res.CTime, &res.MTime); err != nil {
  507. if err == xsql.ErrNoRows {
  508. err = nil
  509. res = nil
  510. } else {
  511. log.Error("fail to get UserSafeQuestion by mid(%d), dao.userDB.QueryRow(%s) error(%+v)", mid, _getUserSafeQuestionByMidSQL, err)
  512. }
  513. return
  514. }
  515. return
  516. }
  517. // GetUserThirdBindByMid get user third bind by mid.
  518. func (d *Dao) GetUserThirdBindByMid(c context.Context, mid int64) (res []*model.UserThirdBind, err error) {
  519. var rows *xsql.Rows
  520. if rows, err = d.userDB.Query(c, _getUserThirdBindByMidSQL, mid); err != nil {
  521. log.Error("fail to get UserThirdBind, dao.userDB.Query(%s) error(%+v)", _getUserThirdBindByMidSQL, err)
  522. return
  523. }
  524. defer rows.Close()
  525. for rows.Next() {
  526. r := new(model.UserThirdBind)
  527. if err = rows.Scan(&r.ID, &r.Mid, &r.OpenID, &r.PlatForm, &r.Token, &r.Expires, &r.CTime, &r.MTime); err != nil {
  528. log.Error("row.Scan() error(%v)", err)
  529. res = nil
  530. return
  531. }
  532. res = append(res, r)
  533. }
  534. return
  535. }
  536. // GetUserThirdBindByMidAndPlatform get user third bind by mid and platform.
  537. func (d *Dao) GetUserThirdBindByMidAndPlatform(c context.Context, mid, platform int64) (res *model.UserThirdBind, err error) {
  538. res = &model.UserThirdBind{}
  539. if err = d.userDB.QueryRow(c, _getUserThirdBindByMidAndPlatformSQL, mid, platform).Scan(&res.ID, &res.Mid, &res.OpenID, &res.PlatForm, &res.Token, &res.Expires, &res.CTime, &res.MTime); err != nil {
  540. if err == xsql.ErrNoRows {
  541. err = nil
  542. res = nil
  543. } else {
  544. log.Error("fail to get UserSafeQuestion by mid(%d) platform(%d), dao.userDB.QueryRow(%s) error(%+v)", mid, platform, _getUserThirdBindByMidAndPlatformSQL, err)
  545. }
  546. return
  547. }
  548. return
  549. }
  550. // GetUserRegOriginByMid get user reg origin by mid.
  551. func (d *Dao) GetUserRegOriginByMid(c context.Context, mid int64) (res *model.UserRegOrigin, err error) {
  552. res = &model.UserRegOrigin{}
  553. if err = d.userDB.QueryRow(c, fmt.Sprintf(_getUserRegOriginByMidSQL, tableIndex(mid)), mid).Scan(&res.Mid, &res.JoinIP, &res.JoinIPV6, &res.Port, &res.JoinTime, &res.Origin, &res.RegType, &res.AppID); err != nil {
  554. if err == xsql.ErrNoRows {
  555. err = nil
  556. res = nil
  557. } else {
  558. log.Error("fail to get UserRegOrigin by mid(%d), dao.userDB.QueryRow(%s) error(%+v)", mid, _getUserRegOriginByMidSQL, err)
  559. }
  560. return
  561. }
  562. return
  563. }
  564. // InsertUpdateUserBase insert update user base.
  565. func (d *Dao) InsertUpdateUserBase(c context.Context, a *model.UserBase) (affected int64, err error) {
  566. var res sql.Result
  567. if res, err = d.userDB.Exec(c, _insertUpdateUserBaseSQL, a.Mid, a.UserID, a.Pwd, a.Salt, a.Status, a.Deleted, a.MTime, a.UserID, a.Pwd, a.Salt, a.Status); err != nil {
  568. log.Error("fail to insert update user base, userBase(%+v) dao.userDB.Exec() error(%+v)", a, err)
  569. return
  570. }
  571. return res.RowsAffected()
  572. }
  573. // TxInsertIgnoreUserSafeQuestion insert ignore user safe question.
  574. func (d *Dao) TxInsertIgnoreUserSafeQuestion(tx *xsql.Tx, a *model.UserSafeQuestion) (affected int64, err error) {
  575. var res sql.Result
  576. if res, err = tx.Exec(fmt.Sprintf(_insertIgnoreUserSafeQuestionSQL, tableIndex(a.Mid)), a.Mid, a.SafeQuestion, a.SafeAnswer, a.SafeBindTime); err != nil {
  577. log.Error("fail to insert ignore user safe question, userSafeQuestion(%+v) dao.userDB.Exec() error(%+v)", a, err)
  578. return
  579. }
  580. return res.RowsAffected()
  581. }
  582. // TxInsertIgnoreUserRegOrigin insert ignore user reg origin.
  583. func (d *Dao) TxInsertIgnoreUserRegOrigin(tx *xsql.Tx, a *model.UserRegOrigin) (affected int64, err error) {
  584. var res sql.Result
  585. if res, err = tx.Exec(fmt.Sprintf(_insertIgnoreUserRegOriginSQL, tableIndex(a.Mid)), a.Mid, a.JoinIP, a.JoinIPV6, a.Port, a.JoinTime); err != nil {
  586. log.Error("fail to insert ignore user reg origin, userRegOrigin(%+v) dao.userDB.Exec() error(%+v)", a, err)
  587. return
  588. }
  589. return res.RowsAffected()
  590. }
  591. // DelUserThirdBind del user third bind.
  592. func (d *Dao) DelUserThirdBind(c context.Context, mid int64) (affected int64, err error) {
  593. var res sql.Result
  594. if res, err = d.userDB.Exec(c, _delUserThirdBindSQL, mid); err != nil {
  595. log.Error("fail to del user third bind, mid(%d) dao.userDB.Exec() error(%+v)", mid, err)
  596. return
  597. }
  598. return res.RowsAffected()
  599. }
  600. func tableIndex(mid int64) int64 {
  601. return mid % 100
  602. }