origin.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  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. selectAccountSQL = "SELECT mid, userid, uname, pwd, salt, email, tel, mobile_verified, isleak, country_id, modify_time FROM aso_account WHERE mid > ? limit ? "
  12. selectAccountByMidSQL = "SELECT mid, userid, uname, pwd, salt, email, tel, mobile_verified, isleak, country_id, modify_time FROM aso_account WHERE mid = ?"
  13. selectAccountByTelSQL = "SELECT mid, userid, uname, pwd, salt, email, tel, mobile_verified, isleak, country_id, modify_time FROM aso_account WHERE tel = ?"
  14. selectAccountByMailSQL = "SELECT mid, userid, uname, pwd, salt, email, tel, mobile_verified, isleak, country_id, modify_time FROM aso_account WHERE email = ?"
  15. selectAccountByTimeSQL = "SELECT mid, userid, uname, pwd, salt, email, tel, mobile_verified, isleak, country_id, modify_time FROM aso_account WHERE modify_time >= ? AND modify_time < ?"
  16. selectAccountInfoSQL = "SELECT id, mid, spacesta, safe_question, safe_answer, join_time, join_ip, active_time, modify_time FROM aso_account_info%d WHERE id > ? limit ?"
  17. selectAccountInfoByMidSQL = "SELECT id, mid, spacesta, safe_question, safe_answer, join_time, join_ip, active_time, modify_time FROM aso_account_info%d WHERE mid = ? "
  18. selectAccountInfoByTimeSQL = "SELECT id, mid, spacesta, safe_question, safe_answer, join_time, join_ip, active_time, modify_time FROM aso_account_info%d WHERE modify_time >= ? AND modify_time < ? "
  19. selectAccountSnsSQL = "SELECT mid, sina_uid, sina_access_token, sina_access_expires, qq_openid, qq_access_token, qq_access_expires FROM aso_account_sns WHERE mid > ? limit ?"
  20. selectAccountSnsByMidSQL = "SELECT mid, sina_uid, sina_access_token, sina_access_expires, qq_openid, qq_access_token, qq_access_expires FROM aso_account_sns WHERE mid = ? "
  21. selectOriginTelBindLogSQL = "SELECT timestamp FROM aso_telephone_bind_log WHERE mid = ? order by timestamp limit 1"
  22. selectAccountRegByMidSQL = "SELECT id, mid, origintype, regtype, appid, ctime, mtime FROM aso_account_reg_origin_%d WHERE mid = ? "
  23. selectAccountRegByTimeSQL = "SELECT id, mid, origintype, regtype, appid, ctime, mtime FROM aso_account_reg_origin_%d WHERE mtime >= ? AND mtime < ? "
  24. )
  25. // BatchQueryAccount batch query account
  26. func (d *Dao) BatchQueryAccount(c context.Context, start, limit int64) (res []*model.OriginAccount, err error) {
  27. var rows *xsql.Rows
  28. if rows, err = d.originDB.Query(c, selectAccountSQL, start, limit); err != nil {
  29. log.Error("fail to get BatchQueryAccount, dao.originDB.Query(%s) error(%v)", selectAccountSQL, err)
  30. return
  31. }
  32. defer rows.Close()
  33. for rows.Next() {
  34. var telPtr, emailPtr *string
  35. r := new(model.OriginAccount)
  36. if err = rows.Scan(&r.Mid, &r.UserID, &r.Uname, &r.Pwd, &r.Salt, &emailPtr, &telPtr, &r.MobileVerified, &r.Isleak, &r.CountryID, &r.MTime); err != nil {
  37. log.Error("BatchQueryAccount row.Scan() error(%v)", err)
  38. res = nil
  39. return
  40. }
  41. if telPtr != nil {
  42. r.Tel = *telPtr
  43. }
  44. if emailPtr != nil {
  45. r.Email = *emailPtr
  46. }
  47. res = append(res, r)
  48. }
  49. return
  50. }
  51. // QueryAccountByMid query account by mid
  52. func (d *Dao) QueryAccountByMid(c context.Context, mid int64) (res *model.OriginAccount, err error) {
  53. row := d.originDB.QueryRow(c, selectAccountByMidSQL, mid)
  54. res = new(model.OriginAccount)
  55. var telPtr, emailPtr *string
  56. if err = row.Scan(&res.Mid, &res.UserID, &res.Uname, &res.Pwd, &res.Salt, &emailPtr, &telPtr, &res.MobileVerified, &res.Isleak, &res.CountryID, &res.MTime); err != nil {
  57. if err == xsql.ErrNoRows {
  58. res = nil
  59. err = nil
  60. } else {
  61. log.Error("QueryAccountByMid row.Scan() error(%v)", err)
  62. }
  63. return
  64. }
  65. if telPtr != nil {
  66. res.Tel = *telPtr
  67. }
  68. if emailPtr != nil {
  69. res.Email = *emailPtr
  70. }
  71. return
  72. }
  73. // QueryAccountByTel query account by mid
  74. func (d *Dao) QueryAccountByTel(c context.Context, tel string) (res *model.OriginAccount, err error) {
  75. row := d.originDB.QueryRow(c, selectAccountByTelSQL, tel)
  76. res = new(model.OriginAccount)
  77. var telPtr, emailPtr *string
  78. if err = row.Scan(&res.Mid, &res.UserID, &res.Uname, &res.Pwd, &res.Salt, &emailPtr, &telPtr, &res.MobileVerified, &res.Isleak, &res.CountryID, &res.MTime); err != nil {
  79. if err == xsql.ErrNoRows {
  80. res = nil
  81. err = nil
  82. } else {
  83. log.Error("QueryAccountByTel row.Scan() error(%v)", err)
  84. }
  85. return
  86. }
  87. if telPtr != nil {
  88. res.Tel = *telPtr
  89. }
  90. if emailPtr != nil {
  91. res.Email = *emailPtr
  92. }
  93. return
  94. }
  95. // QueryAccountByMail query account by mid
  96. func (d *Dao) QueryAccountByMail(c context.Context, mail string) (res *model.OriginAccount, err error) {
  97. row := d.originDB.QueryRow(c, selectAccountByMailSQL, mail)
  98. res = new(model.OriginAccount)
  99. var telPtr, emailPtr *string
  100. if err = row.Scan(&res.Mid, &res.UserID, &res.Uname, &res.Pwd, &res.Salt, &emailPtr, &telPtr, &res.MobileVerified, &res.Isleak, &res.CountryID, &res.MTime); err != nil {
  101. if err == xsql.ErrNoRows {
  102. res = nil
  103. err = nil
  104. } else {
  105. log.Error("QueryAccountByMail row.Scan() error(%v)", err)
  106. }
  107. return
  108. }
  109. if telPtr != nil {
  110. res.Tel = *telPtr
  111. }
  112. if emailPtr != nil {
  113. res.Email = *emailPtr
  114. }
  115. return
  116. }
  117. // BatchQueryAccountByTime batch query by time
  118. func (d *Dao) BatchQueryAccountByTime(c context.Context, start, end time.Time) (res []*model.OriginAccount, err error) {
  119. var rows *xsql.Rows
  120. if rows, err = d.originDB.Query(c, selectAccountByTimeSQL, start, end); err != nil {
  121. log.Error("fail to get BatchQueryAccountByTime, dao.originDB.Query(%s) error(%v)", selectAccountSQL, err)
  122. return
  123. }
  124. defer rows.Close()
  125. for rows.Next() {
  126. var telPtr, emailPtr *string
  127. r := new(model.OriginAccount)
  128. if err = rows.Scan(&r.Mid, &r.UserID, &r.Uname, &r.Pwd, &r.Salt, &emailPtr, &telPtr, &r.MobileVerified, &r.Isleak, &r.CountryID, &r.MTime); err != nil {
  129. log.Error("BatchQueryAccountByTime row.Scan() error(%v)", err)
  130. res = nil
  131. return
  132. }
  133. if telPtr != nil {
  134. r.Tel = *telPtr
  135. }
  136. if emailPtr != nil {
  137. r.Email = *emailPtr
  138. }
  139. res = append(res, r)
  140. }
  141. return
  142. }
  143. // BatchQueryAccountInfo batch query account info
  144. func (d *Dao) BatchQueryAccountInfo(c context.Context, start, limit int64, suffix int) (res []*model.OriginAccountInfo, err error) {
  145. var rows *xsql.Rows
  146. if rows, err = d.originDB.Query(c, fmt.Sprintf(selectAccountInfoSQL, suffix), start, limit); err != nil {
  147. log.Error("fail to get BatchQueryAccountInfo, dao.originDB.Query(%s) error(%v)", selectAccountInfoSQL, err)
  148. return
  149. }
  150. defer rows.Close()
  151. for rows.Next() {
  152. r := new(model.OriginAccountInfo)
  153. if err = rows.Scan(&r.ID, &r.Mid, &r.Spacesta, &r.SafeQuestion, &r.SafeAnswer, &r.JoinTime, &r.JoinIP, &r.ActiveTime, &r.MTime); err != nil {
  154. log.Error("BatchQueryAccountInfo row.Scan() error(%v)", err)
  155. res = nil
  156. return
  157. }
  158. res = append(res, r)
  159. }
  160. return
  161. }
  162. // QueryAccountInfoByMid query account info by mid
  163. func (d *Dao) QueryAccountInfoByMid(c context.Context, mid int64) (res *model.OriginAccountInfo, err error) {
  164. row := d.originDB.QueryRow(c, fmt.Sprintf(selectAccountInfoByMidSQL, mid%30), mid)
  165. res = new(model.OriginAccountInfo)
  166. if err = row.Scan(&res.ID, &res.Mid, &res.Spacesta, &res.SafeQuestion, &res.SafeAnswer, &res.JoinTime, &res.JoinIP, &res.ActiveTime, &res.MTime); err != nil {
  167. if err == xsql.ErrNoRows {
  168. res = nil
  169. err = nil
  170. } else {
  171. log.Error("QueryAccountInfoByMid row.Scan() error(%v)", err)
  172. }
  173. return
  174. }
  175. return
  176. }
  177. // BatchQueryAccountInfoByTime batch query account info
  178. func (d *Dao) BatchQueryAccountInfoByTime(c context.Context, start, end time.Time, suffix int) (res []*model.OriginAccountInfo, err error) {
  179. var rows *xsql.Rows
  180. if rows, err = d.originDB.Query(c, fmt.Sprintf(selectAccountInfoByTimeSQL, suffix), start, end); err != nil {
  181. log.Error("fail to get BatchQueryAccountInfoByTime, dao.originDB.Query(%s) error(%v)", selectAccountInfoSQL, err)
  182. return
  183. }
  184. defer rows.Close()
  185. for rows.Next() {
  186. r := new(model.OriginAccountInfo)
  187. if err = rows.Scan(&r.ID, &r.Mid, &r.Spacesta, &r.SafeQuestion, &r.SafeAnswer, &r.JoinTime, &r.JoinIP, &r.ActiveTime, &r.MTime); err != nil {
  188. log.Error("BatchQueryAccountInfoByTime row.Scan() error(%v)", err)
  189. res = nil
  190. return
  191. }
  192. res = append(res, r)
  193. }
  194. return
  195. }
  196. // BatchQueryAccountSns batch query account sns
  197. func (d *Dao) BatchQueryAccountSns(c context.Context, start, limit int64) (res []*model.OriginAccountSns, err error) {
  198. var rows *xsql.Rows
  199. if rows, err = d.originDB.Query(c, selectAccountSnsSQL, start, limit); err != nil {
  200. log.Error("fail to get BatchQueryAccountSns, dao.originDB.Query(%s) error(%v)", selectAccountSnsSQL, err)
  201. return
  202. }
  203. defer rows.Close()
  204. for rows.Next() {
  205. r := new(model.OriginAccountSns)
  206. if err = rows.Scan(&r.Mid, &r.SinaUID, &r.SinaAccessToken, &r.SinaAccessExpires, &r.QQOpenid, &r.QQAccessToken, &r.QQAccessExpires); err != nil {
  207. log.Error("BatchQueryAccountSns row.Scan() error(%v)", err)
  208. res = nil
  209. return
  210. }
  211. res = append(res, r)
  212. }
  213. return
  214. }
  215. // QueryAccountSnsByMid query account sns by mid
  216. func (d *Dao) QueryAccountSnsByMid(c context.Context, mid int64) (res *model.OriginAccountSns, err error) {
  217. row := d.originDB.QueryRow(c, selectAccountSnsByMidSQL, mid)
  218. res = new(model.OriginAccountSns)
  219. if err = row.Scan(&res.Mid, &res.SinaUID, &res.SinaAccessToken, &res.SinaAccessExpires, &res.QQOpenid, &res.QQAccessToken, &res.QQAccessExpires); err != nil {
  220. if err == xsql.ErrNoRows {
  221. res = nil
  222. err = nil
  223. } else {
  224. log.Error("QueryAccountSnsByMid row.Scan() error(%v)", err)
  225. }
  226. return
  227. }
  228. return
  229. }
  230. // QueryTelBindLog get aso tel bind log.
  231. func (d *Dao) QueryTelBindLog(c context.Context, mid int64) (res int64, err error) {
  232. if err = d.originDB.QueryRow(c, selectOriginTelBindLogSQL, mid).Scan(&res); err != nil {
  233. if err == xsql.ErrNoRows {
  234. err = nil
  235. } else {
  236. log.Error("fail to get AsoTelBindLog, dao.originDB.QueryRow(%s) error(%v)", selectOriginTelBindLogSQL, err)
  237. }
  238. return
  239. }
  240. return
  241. }
  242. // QueryAccountRegByMid query account reg by mid
  243. func (d *Dao) QueryAccountRegByMid(c context.Context, mid int64) (res *model.OriginAccountReg, err error) {
  244. row := d.originDB.QueryRow(c, fmt.Sprintf(selectAccountRegByMidSQL, mid%20), mid)
  245. res = new(model.OriginAccountReg)
  246. if err = row.Scan(&res.ID, &res.Mid, &res.OriginType, &res.RegType, &res.AppID, &res.CTime, &res.MTime); err != nil {
  247. if err == xsql.ErrNoRows {
  248. res = nil
  249. err = nil
  250. } else {
  251. log.Error("QueryAccountRegByMid row.Scan() error(%v)", err)
  252. }
  253. return
  254. }
  255. return
  256. }
  257. // BatchQueryAccountRegByTime batch query account info
  258. func (d *Dao) BatchQueryAccountRegByTime(c context.Context, start, end time.Time, suffix int) (res []*model.OriginAccountReg, err error) {
  259. var rows *xsql.Rows
  260. if rows, err = d.originDB.Query(c, fmt.Sprintf(selectAccountRegByTimeSQL, suffix), start, end); err != nil {
  261. log.Error("fail to get BatchQueryAccountRegByTime, dao.originDB.Query(%s) error(%v)", selectAccountInfoSQL, err)
  262. return
  263. }
  264. defer rows.Close()
  265. for rows.Next() {
  266. r := new(model.OriginAccountReg)
  267. if err = rows.Scan(&r.ID, &r.Mid, &r.OriginType, &r.RegType, &r.AppID, &r.CTime, &r.MTime); err != nil {
  268. log.Error("BatchQueryAccountInfoByTime row.Scan() error(%v)", err)
  269. res = nil
  270. return
  271. }
  272. res = append(res, r)
  273. }
  274. return
  275. }