theme.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package dao
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "time"
  7. "go-common/app/interface/main/space/model"
  8. xsql "go-common/library/database/sql"
  9. "go-common/library/log"
  10. "go-common/library/xstr"
  11. )
  12. const (
  13. _themeKeyFmt = `spc_them_%d`
  14. _themeSQL = `SELECT sid,is_activated FROM dede_member_skin%d WHERE mid = ? AND expire > ?`
  15. _themeInfoSQL = `SELECT id,name,img_path,toutu,bgimg FROM dede_skin_mall WHERE is_disable = 0 AND id IN (%s)`
  16. _themeEditSQL = `UPDATE dede_member_skin%d SET is_activated = 1 WHERE mid = ? AND sid = ?`
  17. _themeUnSQL = `UPDATE dede_member_skin%d SET is_activated = 0 WHERE mid = ?`
  18. )
  19. func themeHit(mid int64) int64 {
  20. return mid % 10
  21. }
  22. func themeKey(mid int64) string {
  23. return fmt.Sprintf(_themeKeyFmt, mid)
  24. }
  25. // ThemeInfoByMid get theme info by mid.
  26. func (d *Dao) ThemeInfoByMid(c context.Context, mid int64) (res []*model.ThemeInfo, err error) {
  27. var rows *xsql.Rows
  28. if rows, err = d.db.Query(c, fmt.Sprintf(_themeSQL, themeHit(mid)), mid, time.Now()); err != nil {
  29. log.Error("ThemeInfoByMid d.db.Query(%d) error(%v)", mid, err)
  30. return
  31. }
  32. defer rows.Close()
  33. for rows.Next() {
  34. r := new(model.ThemeInfo)
  35. if err = rows.Scan(&r.SID, &r.IsActivated); err != nil {
  36. log.Error("ThemeInfoByMid row.Scan() error(%v)", err)
  37. return
  38. }
  39. res = append(res, r)
  40. }
  41. return
  42. }
  43. // ThemeDetail get theme details.
  44. func (d *Dao) ThemeDetail(c context.Context, themeIDs []int64) (res []*model.ThemeDetail, err error) {
  45. var rows *xsql.Rows
  46. if rows, err = d.db.Query(c, fmt.Sprintf(_themeInfoSQL, xstr.JoinInts(themeIDs))); err != nil {
  47. log.Error("ThemeDetail d.db.Query(%v) error(%v)", themeIDs, err)
  48. return
  49. }
  50. defer rows.Close()
  51. for rows.Next() {
  52. r := new(model.ThemeDetail)
  53. if err = rows.Scan(&r.ID, &r.Name, &r.Icon, &r.TopPhoto, &r.BgImg); err != nil {
  54. log.Error("ThemeDetail row.Scan() error(%v)", err)
  55. return
  56. }
  57. res = append(res, r)
  58. }
  59. return
  60. }
  61. // RawTheme get themes by mid.
  62. func (d *Dao) RawTheme(c context.Context, mid int64) (res *model.ThemeDetails, err error) {
  63. var (
  64. themeInfo []*model.ThemeInfo
  65. themeIDs []int64
  66. list []*model.ThemeDetail
  67. )
  68. res = new(model.ThemeDetails)
  69. if themeInfo, err = d.ThemeInfoByMid(c, mid); err != nil || len(themeInfo) == 0 {
  70. return
  71. }
  72. themeInfoMap := make(map[int64]*model.ThemeInfo, len(themeInfo))
  73. for _, v := range themeInfo {
  74. themeIDs = append(themeIDs, v.SID)
  75. themeInfoMap[v.SID] = v
  76. }
  77. if list, err = d.ThemeDetail(c, themeIDs); err != nil {
  78. return
  79. }
  80. for _, v := range list {
  81. if theme, ok := themeInfoMap[v.ID]; ok && theme != nil {
  82. v.IsActivated = theme.IsActivated
  83. }
  84. }
  85. res.List = list
  86. return
  87. }
  88. // ThemeActive active theme.
  89. func (d *Dao) ThemeActive(c context.Context, mid, themeID int64) (err error) {
  90. var (
  91. res sql.Result
  92. tx *xsql.Tx
  93. )
  94. if tx, err = d.db.Begin(c); err != nil {
  95. log.Error("ThemeActive: d.db.Begin error(%v)", err)
  96. return
  97. }
  98. if res, err = tx.Exec(fmt.Sprintf(_themeUnSQL, themeHit(mid)), mid); err != nil {
  99. tx.Rollback()
  100. log.Error("ThemeActive: db.Exec(%d) error(%v)", mid, err)
  101. return
  102. }
  103. if _, err = tx.Exec(fmt.Sprintf(_themeEditSQL, themeHit(mid)), mid, themeID); err != nil {
  104. tx.Rollback()
  105. log.Error("ThemeActive: db.Exec(%d,%d) error(%v)", mid, themeID, err)
  106. return
  107. }
  108. if err = tx.Commit(); err != nil {
  109. log.Error("ThemeActive: tx.Commit error(%v)", err)
  110. return
  111. }
  112. _, err = res.RowsAffected()
  113. return
  114. }