mysql.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "strings"
  7. "time"
  8. "go-common/app/job/main/passport-game-data/model"
  9. xsql "go-common/library/database/sql"
  10. "go-common/library/log"
  11. )
  12. const (
  13. _getAsoAccountRangeCloudSQL = "SELECT mid,userid,uname,pwd,salt,email,tel,country_id,mobile_verified,isleak,ctime,mtime FROM aso_account WHERE mtime>=? AND mtime<?"
  14. _getAsoAccountsCloudSQL = "SELECT mid,userid,uname,pwd,salt,email,tel,country_id,mobile_verified,isleak,ctime,mtime FROM aso_account WHERE mid in(%s)"
  15. _getRangeAsoAccountsLocalSQL = "SELECT mid,userid,uname,pwd,salt,email,tel,country_id,mobile_verified,isleak,modify_time FROM aso_account WHERE modify_time>=? AND modify_time<?"
  16. _getAsoAccountsLocalSQL = "SELECT mid,userid,uname,pwd,salt,email,tel,country_id,mobile_verified,isleak,modify_time FROM aso_account WHERE mid in(%s)"
  17. _updateAsoAccountCloudSQL = "UPDATE aso_account SET userid=?,uname=?,pwd=?,salt=?,email=?,tel=?,country_id=?,mobile_verified=?,isleak=? WHERE mid=? AND mtime=?"
  18. _addIgnoreAsoAccountCloudSQL = "INSERT IGNORE INTO aso_account (mid,userid,uname,pwd,salt,email,tel,country_id,mobile_verified,isleak) VALUES(?,?,?,?,?,?,?,?,?,?)"
  19. _addIgnoreAsoAccountsCloudSQL = "INSERT IGNORE INTO aso_account (mid,userid,uname,pwd,salt,email,tel,country_id,mobile_verified,isleak) VALUES %s"
  20. )
  21. // AddAsoAccountsCloud batch add aso account to cloud.
  22. func (d *Dao) AddAsoAccountsCloud(c context.Context, vs []*model.AsoAccount) (err error) {
  23. if len(vs) == 0 {
  24. return
  25. }
  26. var args = make([]string, 0)
  27. for _, v := range vs {
  28. args = append(args, getValues(v))
  29. }
  30. s := fmt.Sprintf(_addIgnoreAsoAccountsCloudSQL, strings.Join(args, ","))
  31. if _, err = d.cloudDB.Exec(c, s); err != nil {
  32. log.Error("d.cloudDB.Exec(%s) error(%v)", s, err)
  33. }
  34. return
  35. }
  36. func getValues(a *model.AsoAccount) string {
  37. email := "NULL"
  38. tel := "NULL"
  39. if len(a.Email) > 0 {
  40. email = "'" + a.Email + "'"
  41. }
  42. if len(a.Tel) > 0 {
  43. tel = "'" + a.Tel + "'"
  44. }
  45. return fmt.Sprintf(`(%d,'%s','%s','%s','%s',%s,%s,%d,%d,%d)`, a.Mid, a.UserID, a.Uname, a.Pwd, a.Salt, email, tel, a.CountryID, a.MobileVerified, a.Isleak)
  46. }
  47. // AsoAccountRangeCloud get aso account from cloud.
  48. func (d *Dao) AsoAccountRangeCloud(c context.Context, st, ed time.Time) (res []*model.AsoAccount, err error) {
  49. var rows *xsql.Rows
  50. if rows, err = d.cloudDB.Query(c, _getAsoAccountRangeCloudSQL, st, ed); err != nil {
  51. log.Error("get aso account range cloud, dao.cloudDB.Query(%s) error(%v)", _getAsoAccountRangeCloudSQL, err)
  52. return
  53. }
  54. for rows.Next() {
  55. a := new(model.AsoAccount)
  56. var telPtr, emailPtr *string
  57. if err = rows.Scan(&a.Mid, &a.UserID, &a.Uname, &a.Pwd, &a.Salt, &emailPtr, &telPtr, &a.CountryID, &a.MobileVerified, &a.Isleak, &a.Ctime, &a.Mtime); err != nil {
  58. if err == xsql.ErrNoRows {
  59. err = nil
  60. res = nil
  61. return
  62. }
  63. log.Error("row.Scan() error(%v)", err)
  64. return
  65. }
  66. if telPtr != nil {
  67. a.Tel = *telPtr
  68. }
  69. if emailPtr != nil {
  70. a.Email = *emailPtr
  71. }
  72. res = append(res, a)
  73. }
  74. err = rows.Err()
  75. return
  76. }
  77. // AsoAccountsCloud get aso accounts from cloud.
  78. func (d *Dao) AsoAccountsCloud(c context.Context, vs []int64) (res []*model.AsoAccount, err error) {
  79. if len(vs) == 0 {
  80. return
  81. }
  82. var args = make([]string, 0, len(vs))
  83. for _, v := range vs {
  84. args = append(args, fmt.Sprintf(`'%d'`, v))
  85. }
  86. if len(args) == 0 {
  87. return
  88. }
  89. s := fmt.Sprintf(_getAsoAccountsCloudSQL, strings.Join(args, ","))
  90. var rows *xsql.Rows
  91. if rows, err = d.cloudDB.Query(c, s); err != nil {
  92. log.Error("get aso accounts cloud, dao.cloudDB.Query(%s) error(%v)", s, err)
  93. return
  94. }
  95. for rows.Next() {
  96. a := new(model.AsoAccount)
  97. var telPtr, emailPtr *string
  98. if err = rows.Scan(&a.Mid, &a.UserID, &a.Uname, &a.Pwd, &a.Salt, &emailPtr, &telPtr, &a.CountryID, &a.MobileVerified, &a.Isleak, &a.Ctime, &a.Mtime); err != nil {
  99. if err == xsql.ErrNoRows {
  100. err = nil
  101. res = nil
  102. return
  103. }
  104. log.Error("row.Scan() error(%v)", err)
  105. return
  106. }
  107. if telPtr != nil {
  108. a.Tel = *telPtr
  109. }
  110. if emailPtr != nil {
  111. a.Email = *emailPtr
  112. }
  113. res = append(res, a)
  114. }
  115. err = rows.Err()
  116. return
  117. }
  118. // AsoAccountRangeLocal get aso account from local range start and end time.
  119. func (d *Dao) AsoAccountRangeLocal(c context.Context, st, ed time.Time) (res []*model.OriginAsoAccount, err error) {
  120. var rows *xsql.Rows
  121. if rows, err = d.localDB.Query(c, _getRangeAsoAccountsLocalSQL, st, ed); err != nil {
  122. log.Error("get aso account range local, dao.localDB.Query(%s) error(%v)", _getRangeAsoAccountsLocalSQL, err)
  123. return
  124. }
  125. for rows.Next() {
  126. a := new(model.OriginAsoAccount)
  127. var telPtr, emailPtr *string
  128. if err = rows.Scan(&a.Mid, &a.UserID, &a.Uname, &a.Pwd, &a.Salt, &emailPtr, &telPtr, &a.CountryID, &a.MobileVerified, &a.Isleak, &a.Mtime); err != nil {
  129. if err == xsql.ErrNoRows {
  130. err = nil
  131. res = nil
  132. return
  133. }
  134. log.Error("row.Scan() error(%v)", err)
  135. return
  136. }
  137. if telPtr != nil {
  138. a.Tel = *telPtr
  139. }
  140. if emailPtr != nil {
  141. a.Email = *emailPtr
  142. }
  143. res = append(res, a)
  144. }
  145. err = rows.Err()
  146. return
  147. }
  148. // AsoAccountsLocal get aso accounts from origin.
  149. func (d *Dao) AsoAccountsLocal(c context.Context, vs []int64) (res []*model.OriginAsoAccount, err error) {
  150. if len(vs) == 0 {
  151. return
  152. }
  153. var args = make([]string, 0, len(vs))
  154. for _, v := range vs {
  155. args = append(args, fmt.Sprintf(`'%d'`, v))
  156. }
  157. if len(args) == 0 {
  158. return
  159. }
  160. s := fmt.Sprintf(_getAsoAccountsLocalSQL, strings.Join(args, ","))
  161. var rows *xsql.Rows
  162. if rows, err = d.localDB.Query(c, s); err != nil {
  163. log.Error("get aso accounts local, dao.localDB.Query(%s) error(%v)", s, err)
  164. return
  165. }
  166. for rows.Next() {
  167. a := new(model.OriginAsoAccount)
  168. var telPtr, emailPtr *string
  169. if err = rows.Scan(&a.Mid, &a.UserID, &a.Uname, &a.Pwd, &a.Salt, &emailPtr, &telPtr, &a.CountryID, &a.MobileVerified, &a.Isleak, &a.Mtime); err != nil {
  170. if err == xsql.ErrNoRows {
  171. err = nil
  172. res = nil
  173. return
  174. }
  175. log.Error("row.Scan() error(%v)", err)
  176. return
  177. }
  178. if telPtr != nil {
  179. a.Tel = *telPtr
  180. }
  181. if emailPtr != nil {
  182. a.Email = *emailPtr
  183. }
  184. res = append(res, a)
  185. }
  186. err = rows.Err()
  187. return
  188. }
  189. // UpdateAsoAccountCloud update aso account.
  190. func (d *Dao) UpdateAsoAccountCloud(c context.Context, a *model.AsoAccount, mt time.Time) (affected int64, err error) {
  191. var telPtr, emailPtr *string
  192. if a.Tel != "" {
  193. telPtr = &a.Tel
  194. }
  195. if a.Email != "" {
  196. emailPtr = &a.Email
  197. }
  198. var res sql.Result
  199. if res, err = d.cloudDB.Exec(c, _updateAsoAccountCloudSQL, a.UserID, a.Uname, a.Pwd, a.Salt, emailPtr, telPtr, a.CountryID, a.MobileVerified, a.Isleak, a.Mid, mt); err != nil {
  200. log.Error("failed to update aso account, dao.cloudDB.Exec(%s) email(%s) tel(%s) error(%v)", _updateAsoAccountCloudSQL, emailPtr, telPtr, err)
  201. return
  202. }
  203. return res.RowsAffected()
  204. }
  205. // AddIgnoreAsoAccount add ignore aso account.
  206. func (d *Dao) AddIgnoreAsoAccount(c context.Context, a *model.AsoAccount) (affected int64, err error) {
  207. var res sql.Result
  208. var telPtr, emailPtr *string
  209. if a.Tel != "" {
  210. telPtr = &a.Tel
  211. }
  212. if a.Email != "" {
  213. emailPtr = &a.Email
  214. }
  215. if res, err = d.cloudDB.Exec(c, _addIgnoreAsoAccountCloudSQL, a.Mid, a.UserID, a.Uname, a.Pwd, a.Salt, emailPtr, telPtr, a.CountryID, a.MobileVerified, a.Isleak); err != nil {
  216. log.Error("failed to add ignore aso account, dao.cloudDB.Exec(%s) email(%s) tel(%s) error(%s)", _addIgnoreAsoAccountCloudSQL, a.Email, a.Tel, err)
  217. return
  218. }
  219. return res.RowsAffected()
  220. }