card.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package card
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "time"
  7. "go-common/app/interface/main/app-card/model/card/operate"
  8. "go-common/app/interface/main/app-channel/conf"
  9. "go-common/app/interface/main/app-channel/model/card"
  10. "go-common/library/cache/memcache"
  11. "go-common/library/database/sql"
  12. )
  13. const (
  14. _cardSQL = `SELECT c.id,c.title,c.tag_id,c.card_type,c.card_value,c.recommand_reason,c.recommand_state,c.priority FROM channel_card AS c
  15. WHERE c.stime<? AND c.etime>? AND c.check=2 AND c.is_delete=0 ORDER BY c.priority DESC`
  16. _cardPlatSQL = `SELECT card_id,plat,conditions,build FROM channel_card_plat WHERE is_delete=0`
  17. _followSQL = "SELECT `id`,`type`,`long_title`,`content` FROM `card_follow` WHERE `deleted`=0"
  18. _cardSetSQL = `SELECT c.id,c.type,c.value,c.title,c.long_title,c.content FROM card_set AS c WHERE c.deleted=0`
  19. )
  20. // Dao is card dao.
  21. type Dao struct {
  22. db *sql.DB
  23. // memcache
  24. expire int32
  25. mc *memcache.Pool
  26. }
  27. // New is card dao new.
  28. func New(c *conf.Config) *Dao {
  29. d := &Dao{
  30. db: sql.NewMySQL(c.MySQL.Show),
  31. // memcache
  32. expire: int32(time.Duration(c.Memcache.Channels.Expire) / time.Second),
  33. mc: memcache.NewPool(c.Memcache.Channels.Config),
  34. }
  35. return d
  36. }
  37. // Card channel card
  38. func (d *Dao) Card(ctx context.Context, now time.Time) (res map[int64][]*card.Card, err error) {
  39. res = map[int64][]*card.Card{}
  40. rows, err := d.db.Query(ctx, _cardSQL, now, now)
  41. if err != nil {
  42. return
  43. }
  44. defer rows.Close()
  45. for rows.Next() {
  46. c := &card.Card{}
  47. if err = rows.Scan(&c.ID, &c.Title, &c.ChannelID, &c.Type, &c.Value, &c.Reason, &c.ReasonType, &c.Pos); err != nil {
  48. return
  49. }
  50. res[c.ChannelID] = append(res[c.ChannelID], c)
  51. }
  52. return
  53. }
  54. // CardPlat channel card plat
  55. func (d *Dao) CardPlat(ctx context.Context) (res map[string][]*card.CardPlat, err error) {
  56. res = map[string][]*card.CardPlat{}
  57. var (
  58. _initCardPlatKey = "card_platkey_%d_%d"
  59. )
  60. rows, err := d.db.Query(ctx, _cardPlatSQL)
  61. if err != nil {
  62. return
  63. }
  64. defer rows.Close()
  65. for rows.Next() {
  66. c := &card.CardPlat{}
  67. if err = rows.Scan(&c.CardID, &c.Plat, &c.Condition, &c.Build); err != nil {
  68. return
  69. }
  70. key := fmt.Sprintf(_initCardPlatKey, c.Plat, c.CardID)
  71. res[key] = append(res[key], c)
  72. }
  73. return
  74. }
  75. // UpCard upper
  76. func (d *Dao) UpCard(ctx context.Context) (res map[int64]*operate.Follow, err error) {
  77. var rows *sql.Rows
  78. if rows, err = d.db.Query(ctx, _followSQL); err != nil {
  79. return
  80. }
  81. defer rows.Close()
  82. res = make(map[int64]*operate.Follow)
  83. for rows.Next() {
  84. c := &operate.Follow{}
  85. if err = rows.Scan(&c.ID, &c.Type, &c.Title, &c.Content); err != nil {
  86. return
  87. }
  88. c.Change()
  89. res[c.ID] = c
  90. }
  91. return
  92. }
  93. // CardSet card set
  94. func (d *Dao) CardSet(ctx context.Context) (res map[int64]*operate.CardSet, err error) {
  95. var rows *sql.Rows
  96. if rows, err = d.db.Query(ctx, _cardSetSQL); err != nil {
  97. return
  98. }
  99. defer rows.Close()
  100. res = make(map[int64]*operate.CardSet)
  101. for rows.Next() {
  102. var (
  103. c = &operate.CardSet{}
  104. value string
  105. )
  106. if err = rows.Scan(&c.ID, &c.Type, &value, &c.Title, &c.LongTitle, &c.Content); err != nil {
  107. return
  108. }
  109. c.Value, _ = strconv.ParseInt(value, 10, 64)
  110. res[c.ID] = c
  111. }
  112. return
  113. }
  114. // PingDB ping db
  115. func (d *Dao) PingDB(c context.Context) (err error) {
  116. return d.db.Ping(c)
  117. }