mysql.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package medal
  2. import (
  3. "context"
  4. "fmt"
  5. "go-common/app/service/main/usersuit/model"
  6. "go-common/library/database/sql"
  7. "github.com/pkg/errors"
  8. )
  9. const (
  10. _sharding = 10
  11. _insOwnerSQL = "INSERT INTO medal_owner_%s(mid,nid) VALUES (?,?)"
  12. _updateOwnerSQL = "UPDATE medal_owner_%s SET is_activated=? WHERE mid=? AND nid=?"
  13. _updateOwnerUnallSQL = "UPDATE medal_owner_%s SET is_activated=0 WHERE mid=? AND nid!=?"
  14. _selInfoAllSQL = "SELECT id,name,description,image,image_small,cond,gid,level,level_rank,sort FROM medal_info ORDER BY sort ASC,gid ASC,level ASC"
  15. _selOwnerByMidSQL = "SELECT id,mid,nid,is_activated,ctime,mtime FROM medal_owner_%s WHERE mid=? AND is_del=0 ORDER BY ctime DESC"
  16. _selInfoByNidSQL = "SELECT name FROM medal_info WHERE id=? AND is_online=1"
  17. _selInstalledOwnerBYMidSQL = "SELECT nid FROM medal_owner_%s WHERE mid=? AND is_activated=1 AND is_del=0 LIMIT 1"
  18. _countOwnerBYNidMidSQL = "SELECT COUNT(*) FROM medal_owner_%s WHERE mid=? AND nid=?"
  19. _OwnerBYNidMidSQL = "SELECT id,mid,nid,is_activated,ctime,mtime FROM medal_owner_%s WHERE mid=? AND nid=?"
  20. _selGroupAllSQL = "SELECT id,name,pid,rank FROM medal_group WHERE is_online=1 ORDER BY pid ASC,rank ASC"
  21. )
  22. func (d *Dao) hit(id int64) string {
  23. return fmt.Sprintf("%d", id%_sharding)
  24. }
  25. // AddMedalOwner insert into medal_owner.
  26. func (d *Dao) AddMedalOwner(c context.Context, mid, nid int64) (err error) {
  27. if _, err = d.db.Exec(c, fmt.Sprintf(_insOwnerSQL, d.hit(mid)), mid, nid); err != nil {
  28. err = errors.WithStack(err)
  29. }
  30. return
  31. }
  32. // InstallMedalOwner update medal_owner set is_activated=1.
  33. func (d *Dao) InstallMedalOwner(c context.Context, mid, nid int64) (err error) {
  34. if _, err = d.db.Exec(c, fmt.Sprintf(_updateOwnerSQL, d.hit(mid)), model.OwnerInstall, mid, nid); err != nil {
  35. err = errors.WithStack(err)
  36. }
  37. return
  38. }
  39. // UninstallMedalOwner update medal_owner set is_activated=0.
  40. func (d *Dao) UninstallMedalOwner(c context.Context, mid, nid int64) (err error) {
  41. if _, err = d.db.Exec(c, fmt.Sprintf(_updateOwnerSQL, d.hit(mid)), model.OwnerUninstall, mid, nid); err != nil {
  42. err = errors.WithStack(err)
  43. }
  44. return
  45. }
  46. // UninstallAllMedalOwner uninst all medal_owner set is_activated=0.
  47. func (d *Dao) UninstallAllMedalOwner(c context.Context, mid, nid int64) (err error) {
  48. if _, err = d.db.Exec(c, fmt.Sprintf(_updateOwnerUnallSQL, d.hit(mid)), mid, nid); err != nil {
  49. err = errors.WithStack(err)
  50. }
  51. return
  52. }
  53. // MedalInfoAll retun all medal_info where is_online=1.
  54. func (d *Dao) MedalInfoAll(c context.Context) (res map[int64]*model.MedalInfo, err error) {
  55. res = make(map[int64]*model.MedalInfo)
  56. rows, err := d.db.Query(c, _selInfoAllSQL)
  57. if err != nil {
  58. err = errors.WithStack(err)
  59. return
  60. }
  61. defer rows.Close()
  62. for rows.Next() {
  63. info := new(model.MedalInfo)
  64. if err = rows.Scan(&info.ID, &info.Name, &info.Description, &info.Image, &info.ImageSmall, &info.Condition, &info.GID, &info.Level, &info.LevelRank, &info.Sort); err != nil {
  65. err = errors.WithStack(err)
  66. return
  67. }
  68. info.Build()
  69. res[info.ID] = info
  70. }
  71. err = rows.Err()
  72. return
  73. }
  74. // MedalOwnerByMid return medal_owner by mid.
  75. func (d *Dao) MedalOwnerByMid(c context.Context, mid int64) (res []*model.MedalOwner, err error) {
  76. res = make([]*model.MedalOwner, 0)
  77. rows, err := d.db.Query(c, fmt.Sprintf(_selOwnerByMidSQL, d.hit(mid)), mid)
  78. if err != nil {
  79. err = errors.WithStack(err)
  80. return
  81. }
  82. defer rows.Close()
  83. for rows.Next() {
  84. r := new(model.MedalOwner)
  85. if err = rows.Scan(&r.ID, &r.MID, &r.NID, &r.IsActivated, &r.CTime, &r.MTime); err != nil {
  86. err = errors.WithStack(err)
  87. return
  88. }
  89. res = append(res, r)
  90. }
  91. err = rows.Err()
  92. return
  93. }
  94. // MedalInfoByNid return medal_info by nid.
  95. func (d *Dao) MedalInfoByNid(c context.Context, nid int64) (res *model.MedalInfo, err error) {
  96. res = &model.MedalInfo{}
  97. rows := d.db.QueryRow(c, _selInfoByNidSQL, nid)
  98. if err = rows.Scan(&res.Name); err != nil {
  99. if err != sql.ErrNoRows {
  100. err = errors.Wrap(err, "InstalledOwnerBYMid")
  101. return
  102. }
  103. err = nil
  104. }
  105. return
  106. }
  107. // ActivatedOwnerByMid retun nid of medal_owner by mid where is_activated=1.
  108. func (d *Dao) ActivatedOwnerByMid(c context.Context, mid int64) (nid int64, err error) {
  109. row := d.db.QueryRow(c, fmt.Sprintf(_selInstalledOwnerBYMidSQL, d.hit(mid)), mid)
  110. if err = row.Scan(&nid); err != nil {
  111. if err != sql.ErrNoRows {
  112. err = errors.Wrap(err, "InstalledOwnerBYMid")
  113. return
  114. }
  115. err = nil
  116. }
  117. return
  118. }
  119. // CountOwnerBYNidMid retun number of medal_owner by mid and nid.
  120. func (d *Dao) CountOwnerBYNidMid(c context.Context, mid, nid int64) (count int64, err error) {
  121. row := d.db.QueryRow(c, fmt.Sprintf(_countOwnerBYNidMidSQL, d.hit(mid)), mid, nid)
  122. if err = row.Scan(&count); err != nil {
  123. if err != sql.ErrNoRows {
  124. err = errors.Wrap(err, "CountOwnerBYNidMid")
  125. return
  126. }
  127. count = 0
  128. err = nil
  129. }
  130. return
  131. }
  132. // OwnerBYNidMid retun medal_owner by mid and nid.
  133. func (d *Dao) OwnerBYNidMid(c context.Context, mid, nid int64) (res *model.MedalOwner, err error) {
  134. res = &model.MedalOwner{}
  135. row := d.db.QueryRow(c, fmt.Sprintf(_OwnerBYNidMidSQL, d.hit(mid)), mid, nid)
  136. if err = row.Scan(&res.ID, &res.MID, &res.NID, &res.IsActivated, &res.CTime, &res.MTime); err != nil {
  137. if err != sql.ErrNoRows {
  138. err = errors.Wrap(err, "OwnerBYNidMid")
  139. return
  140. }
  141. res = nil
  142. err = nil
  143. }
  144. return
  145. }
  146. // MedalGroupAll retun all medal_group where is_online=1.
  147. func (d *Dao) MedalGroupAll(c context.Context) (res []*model.MedalGroup, err error) {
  148. rows, err := d.db.Query(c, _selGroupAllSQL)
  149. if err != nil {
  150. err = errors.WithStack(err)
  151. return
  152. }
  153. defer rows.Close()
  154. for rows.Next() {
  155. info := new(model.MedalGroup)
  156. if err = rows.Scan(&info.ID, &info.Name, &info.PID, &info.Rank); err != nil {
  157. err = errors.WithStack(err)
  158. return
  159. }
  160. res = append(res, info)
  161. }
  162. err = rows.Err()
  163. return
  164. }