keyWordPinyinHotStruct.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/gogf/gf/v2/frame/g"
  6. "github.com/gogf/gf/v2/os/gctx"
  7. "github.com/gogf/gf/v2/util/gconv"
  8. )
  9. var (
  10. JySeoKeyWordPinyinRoot *keywordPinyinRoot = &keywordPinyinRoot{}
  11. )
  12. type (
  13. keywordPinyinRoot struct {
  14. allKeywordsMap map[string]*KeyWordHotNode
  15. industryMap map[string][]*KeyWordHotNode
  16. }
  17. KeyWordHotNode struct {
  18. Code string `json:"code" doc:"代码"`
  19. Name string `json:"name" doc:"名称"`
  20. Industry string `json:"industry" doc:"行业"`
  21. Url string `json:"url" doc:"地址"`
  22. }
  23. )
  24. func init() {
  25. JySeoKeyWordPinyinRoot.LoadKeyWordFull(gctx.New())
  26. }
  27. func (sRoot *keywordPinyinRoot) LoadKeyWordFull(ctx context.Context) {
  28. hotSubjectKeywords := g.Config("global").MustGet(ctx, "pinyinKeywords").Maps()
  29. hotKeywordsMap, industryMap := map[string]*KeyWordHotNode{}, map[string][]*KeyWordHotNode{}
  30. for _, keyword := range hotSubjectKeywords {
  31. code := gconv.String(keyword["code"])
  32. name := gconv.String(keyword["name"])
  33. industry := gconv.String(keyword["industry"])
  34. node := &KeyWordHotNode{
  35. Code: code,
  36. Name: name,
  37. Industry: industry,
  38. Url: fmt.Sprintf("/list/keywords/%s.html", code),
  39. }
  40. hotKeywordsMap[code] = node
  41. industryMap[industry] = append(industryMap[industry], node)
  42. }
  43. sRoot.allKeywordsMap = hotKeywordsMap
  44. sRoot.industryMap = industryMap
  45. }
  46. // GetData 查询数据
  47. func (sRoot *keywordPinyinRoot) GetData(ctx context.Context, maxTotal int, query *SeoBiddingQuery) []map[string]interface{} {
  48. var sql string
  49. var values []interface{}
  50. if query.keys != "" {
  51. sql += " AND b.keyword=? "
  52. values = append(values, query.keys)
  53. }
  54. if query.topClass != "" {
  55. sql += " AND b.industry=? "
  56. values = append(values, query.topClass)
  57. }
  58. values = append(values, maxTotal+50)
  59. queryRes, err := g.DB().Query(ctx, fmt.Sprintf(`SELECT bid_id
  60. FROM new_pinyinKeywords b WHERE 1=1 %s
  61. ORDER BY b.publish_time DESC
  62. LIMIT 0,?`, sql), values...)
  63. if err != nil || queryRes.IsEmpty() {
  64. return nil
  65. }
  66. return FillingBiddingBaseFields(ctx, queryRes.List(), true)
  67. //res, err := g.DB().Query(ctx, "SELECT * FROM new_bidList WHERE bid_id IN ( SELECT bid_id FROM new_pinyinKeywords b WHERE 1=1 "+sql+" order by publish_time desc limit 0,? ) ORDER BY publish_time desc LIMIT 0, ?", values...)
  68. //if err != nil {
  69. // return nil
  70. //}
  71. //return res.List()
  72. }
  73. // GetPinyinNode 获取随机标的物
  74. func (sRoot *keywordPinyinRoot) GetPinyinNode(key string) *KeyWordHotNode {
  75. return sRoot.allKeywordsMap[key]
  76. }
  77. // GetPinyinNodeByIndustry 根据行业获取词
  78. func (sRoot *keywordPinyinRoot) GetPinyinNodeByIndustry() map[string][]*KeyWordHotNode {
  79. return sRoot.industryMap
  80. }
  81. func (sRoot *keywordPinyinRoot) GetPinyinIndustryShowSort() []string {
  82. return g.Config("global").MustGet(context.Background(), "pinyinKeywordsSort").Strings()
  83. }
  84. // GetRandomHotKeywords 获取随机拼音关键词
  85. func (sRoot *keywordPinyinRoot) GetRandomHotKeywords(num int) []*KeyWordHotNode {
  86. rData := make([]*KeyWordHotNode, 0, num)
  87. for _, node := range sRoot.allKeywordsMap {
  88. if len(rData) == num {
  89. return rData
  90. }
  91. rData = append(rData, node)
  92. }
  93. return rData
  94. }