card.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package card
  2. import (
  3. "encoding/json"
  4. "strconv"
  5. "go-common/app/interface/main/app-card/model/card/ai"
  6. "go-common/app/interface/main/app-channel/model"
  7. "go-common/app/interface/main/app-channel/model/recommend"
  8. "go-common/library/log"
  9. )
  10. type Card struct {
  11. ID int64 `json:"-"`
  12. Title string `json:"-"`
  13. ChannelID int64 `json:"-"`
  14. Type string `json:"-"`
  15. Value int64 `json:"-"`
  16. Reason string `json:"-"`
  17. ReasonType int8 `json:"-"`
  18. Pos int `json:"-"`
  19. FromType string `json:"-"`
  20. }
  21. type CardPlat struct {
  22. CardID int64 `json:"-"`
  23. Plat int8 `json:"-"`
  24. Condition string `json:"-"`
  25. Build int `json:"-"`
  26. }
  27. type UpCard struct {
  28. ID int64 `json:"id,omitempty"`
  29. Type string `json:"type,omitempty"`
  30. Title string `json:"title,omitempty"`
  31. RcmdReason string `json:"rcmd_reason,omitempty"`
  32. Content json.RawMessage `json:"content,omitempty"`
  33. Data *recommend.Item `json:"data,omitempty"`
  34. }
  35. type UpContent struct {
  36. CType string `json:"ctype,omitempty"`
  37. CValue interface{} `json:"cvalue,omitempty"`
  38. }
  39. type ChannelSingle struct {
  40. AID interface{} `json:"aid,omitempty"`
  41. ChannelID interface{} `json:"channel_id,omitempty"`
  42. }
  43. func (c *UpCard) Change() {
  44. data := []*UpContent{}
  45. if err := json.Unmarshal(c.Content, &data); err != nil {
  46. log.Error("json.Unmarshal(%s) error(%v)", c.Content, err)
  47. return
  48. }
  49. upItem := make([]*recommend.Item, 0, len(data))
  50. for _, d := range data {
  51. switch d.CType {
  52. case "mid", "channel_id":
  53. var (
  54. value int64
  55. err error
  56. )
  57. switch v := d.CValue.(type) {
  58. case float64:
  59. value = int64(v)
  60. case string:
  61. if value, err = strconv.ParseInt(v, 10, 64); err != nil {
  62. continue
  63. }
  64. }
  65. item := &recommend.Item{ID: value}
  66. upItem = append(upItem, item)
  67. }
  68. }
  69. if len(upItem) < 3 {
  70. return
  71. }
  72. // TODO rcmd_reason
  73. c.Data = &recommend.Item{Goto: model.GotoSubscribe, ID: c.ID, Config: &recommend.Config{Title: c.RcmdReason}, Items: upItem}
  74. }
  75. func (c *UpCard) ChannelSingleChange() {
  76. data := &ChannelSingle{}
  77. if err := json.Unmarshal(c.Content, &data); err != nil {
  78. log.Error("json.Unmarshal(%s) error(%v)", c.Content, err)
  79. return
  80. }
  81. var (
  82. aid, channelid int64
  83. err error
  84. )
  85. switch v := data.AID.(type) {
  86. case float64:
  87. aid = int64(v)
  88. case string:
  89. if aid, err = strconv.ParseInt(v, 10, 64); err != nil {
  90. return
  91. }
  92. }
  93. switch v := data.ChannelID.(type) {
  94. case float64:
  95. channelid = int64(v)
  96. case string:
  97. if channelid, err = strconv.ParseInt(v, 10, 64); err != nil {
  98. return
  99. }
  100. }
  101. c.Data = &recommend.Item{Goto: model.GotoChannelRcmd, ID: aid, Config: &recommend.Config{Title: c.Title}, TagID: channelid}
  102. }
  103. func (c *Card) CardToAiChange() (a *ai.Item) {
  104. a = &ai.Item{
  105. Goto: c.Type,
  106. ID: c.Value,
  107. RcmdReason: c.fromRcmdReason(),
  108. }
  109. return
  110. }
  111. func (c *Card) fromRcmdReason() (a *ai.RcmdReason) {
  112. var content string
  113. switch c.ReasonType {
  114. case 0:
  115. content = ""
  116. case 1:
  117. content = "编辑精选"
  118. case 2:
  119. content = "热门推荐"
  120. case 3:
  121. content = c.Reason
  122. }
  123. if content != "" {
  124. a = &ai.RcmdReason{ID: 1, Content: content, BgColor: "yellow", IconLocation: "left_top"}
  125. }
  126. return
  127. }