associate_open.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package dao
  2. import (
  3. "context"
  4. "go-common/app/service/main/vip/model"
  5. "go-common/library/database/sql"
  6. "github.com/pkg/errors"
  7. )
  8. const (
  9. _openInfoOpenIDSQL = "SELECT mid,open_id,app_id FROM oauth_open_id WHERE open_id = ? AND app_id = ?;"
  10. _openInfoByMidSQL = "SELECT mid,open_id,app_id FROM oauth_open_id WHERE mid = ? AND app_id = ?;"
  11. _bindByOutOpenIDSQL = "SELECT id,mid,app_id,out_open_id,state,ver FROM oauth_user_bind WHERE out_open_id = ? AND app_id =?;"
  12. _bindByMidSQL = "SELECT id,mid,app_id,out_open_id,state,ver FROM oauth_user_bind WHERE mid = ? AND app_id =?;"
  13. _addBindSQL = "INSERT IGNORE INTO oauth_user_bind (mid,app_id,out_open_id)VALUES(?,?,?);"
  14. _updateBindByOutOpenID = "UPDATE oauth_user_bind SET mid = ?,app_id = ?,out_open_id = ?,ver = ver + 1 WHERE out_open_id = ? AND app_id = ? AND ver = ?;"
  15. _updateBindStateSQL = "UPDATE oauth_user_bind SET state = ?,ver = ver + 1 WHERE mid = ? AND app_id = ? AND ver = ?;"
  16. _addOpenInfoSQL = "INSERT IGNORE INTO oauth_open_id (mid,open_id,app_id)VALUES(?,?,?);"
  17. _bindInfoByOutOpenIDAndMidSQL = "SELECT mid,app_id,out_open_id,state,ver FROM oauth_user_bind WHERE mid = ? AND out_open_id = ? AND app_id =?;"
  18. _deleteBindInfoSQL = "DELETE FROM oauth_user_bind WHERE id = ?;"
  19. )
  20. //RawOpenInfoByOpenID get info by open id.
  21. func (d *Dao) RawOpenInfoByOpenID(c context.Context, openID string, appID int64) (res *model.OpenInfo, err error) {
  22. res = new(model.OpenInfo)
  23. if err = d.db.QueryRow(c, _openInfoOpenIDSQL, openID, appID).
  24. Scan(&res.Mid, &res.OpenID, &res.AppID); err != nil {
  25. if err == sql.ErrNoRows {
  26. res = nil
  27. err = nil
  28. return
  29. }
  30. err = errors.Wrapf(err, "dao associate openinfo openid(%s,%d)", openID, appID)
  31. }
  32. return
  33. }
  34. // OpenInfoByMid get open info by mid.
  35. func (d *Dao) OpenInfoByMid(c context.Context, mid int64, appID int64) (res *model.OpenInfo, err error) {
  36. res = new(model.OpenInfo)
  37. if err = d.db.QueryRow(c, _openInfoByMidSQL, mid, appID).
  38. Scan(&res.Mid, &res.OpenID, &res.AppID); err != nil {
  39. if err == sql.ErrNoRows {
  40. res = nil
  41. err = nil
  42. return
  43. }
  44. err = errors.Wrapf(err, "dao associate openinfo bymid(%d,%d)", mid, appID)
  45. }
  46. return
  47. }
  48. //ByOutOpenID get open bind info by out_open_id.
  49. func (d *Dao) ByOutOpenID(c context.Context, outOpenID string, appID int64) (res *model.OpenBindInfo, err error) {
  50. res = new(model.OpenBindInfo)
  51. if err = d.db.QueryRow(c, _bindByOutOpenIDSQL, outOpenID, appID).
  52. Scan(&res.ID, &res.Mid, &res.AppID, &res.OutOpenID, &res.State, &res.Ver); err != nil {
  53. if err == sql.ErrNoRows {
  54. res = nil
  55. err = nil
  56. return
  57. }
  58. err = errors.Wrapf(err, "dao associate by out_open_id (%s,%d)", outOpenID, appID)
  59. }
  60. return
  61. }
  62. //RawBindInfoByMid get open bind info by mid.
  63. func (d *Dao) RawBindInfoByMid(c context.Context, mid int64, appID int64) (res *model.OpenBindInfo, err error) {
  64. res = new(model.OpenBindInfo)
  65. if err = d.db.QueryRow(c, _bindByMidSQL, mid, appID).
  66. Scan(&res.ID, &res.Mid, &res.AppID, &res.OutOpenID, &res.State, &res.Ver); err != nil {
  67. if err == sql.ErrNoRows {
  68. res = nil
  69. err = nil
  70. return
  71. }
  72. err = errors.Wrapf(err, "dao associate by mid (%d,%d)", mid, appID)
  73. }
  74. return
  75. }
  76. // TxAddBind add bind.
  77. func (d *Dao) TxAddBind(tx *sql.Tx, arg *model.OpenBindInfo) (err error) {
  78. if _, err = tx.Exec(_addBindSQL, arg.Mid, arg.AppID, arg.OutOpenID); err != nil {
  79. err = errors.Wrapf(err, "dao add bind(%+v)", arg)
  80. }
  81. return
  82. }
  83. // TxUpdateBindByOutOpenID update bind by out_open_id.
  84. func (d *Dao) TxUpdateBindByOutOpenID(tx *sql.Tx, arg *model.OpenBindInfo) (err error) {
  85. if _, err = tx.Exec(_updateBindByOutOpenID, arg.Mid, arg.AppID, arg.OutOpenID, arg.OutOpenID, arg.AppID, arg.Ver); err != nil {
  86. err = errors.Wrapf(err, "dao update bind by out_open_id(%+v)", arg)
  87. }
  88. return
  89. }
  90. // UpdateBindState update bind state.
  91. func (d *Dao) UpdateBindState(c context.Context, arg *model.OpenBindInfo) (err error) {
  92. if _, err = d.db.Exec(c, _updateBindStateSQL, arg.State, arg.Mid, arg.AppID, arg.Ver); err != nil {
  93. err = errors.Wrapf(err, "dao update bind state(%+v)", arg)
  94. }
  95. return
  96. }
  97. // AddOpenInfo add open info.
  98. func (d *Dao) AddOpenInfo(c context.Context, a *model.OpenInfo) (err error) {
  99. if _, err = d.db.Exec(c, _addOpenInfoSQL, a.Mid, a.OpenID, a.AppID); err != nil {
  100. err = errors.Wrapf(err, "dao insert open infp state(%+v)", a)
  101. }
  102. return
  103. }
  104. //BindInfoByOutOpenIDAndMid get bind info by out_open_id AND mid.
  105. func (d *Dao) BindInfoByOutOpenIDAndMid(c context.Context, mid int64, outOpenID string, appID int64) (res *model.OpenBindInfo, err error) {
  106. res = new(model.OpenBindInfo)
  107. if err = d.db.QueryRow(c, _bindInfoByOutOpenIDAndMidSQL, mid, outOpenID, appID).
  108. Scan(&res.Mid, &res.AppID, &res.OutOpenID, &res.State, &res.Ver); err != nil {
  109. if err == sql.ErrNoRows {
  110. res = nil
  111. err = nil
  112. return
  113. }
  114. err = errors.Wrapf(err, "dao associate by out_open_id (%s,%d)", outOpenID, appID)
  115. }
  116. return
  117. }
  118. // TxDeleteBindInfo bind info.
  119. func (d *Dao) TxDeleteBindInfo(tx *sql.Tx, id int64) (err error) {
  120. if _, err = tx.Exec(_deleteBindInfoSQL, id); err != nil {
  121. err = errors.Wrapf(err, "dao delete bind(%d)", id)
  122. }
  123. return
  124. }