keywordClassStruct.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. "github.com/gogf/gf/v2/util/grand"
  9. "jyseo/internal/consts"
  10. "jyseo/utility"
  11. )
  12. var (
  13. JySeoIndustryKeyWordRoot *industryKeywordRoot = &industryKeywordRoot{}
  14. )
  15. type (
  16. industryKeywordRoot struct {
  17. allKeywords []*industryKeywords
  18. allIndustry []*industryStruct
  19. treeIndustry map[string]*industryStruct
  20. keyWordIdMapping map[int64]*industryKeywords //包含已被过滤的词
  21. }
  22. industryStruct struct {
  23. Class string //一级分类
  24. ClassId int64 //一级分类id
  25. //Url string
  26. List []*industryKeywords
  27. }
  28. industryKeywords struct {
  29. TopClass string //一级分类
  30. SubClass string //二级分类
  31. ClassId int64 //一级分类id
  32. KeyWordId int64 //关键词id
  33. KeyWord string //关键词
  34. Url string //地址
  35. State int64 //是否有效
  36. }
  37. )
  38. func init() {
  39. JySeoIndustryKeyWordRoot.initIndustry(gctx.New())
  40. }
  41. // GetIndustry 获取行业和行业下标的物
  42. func (iR *industryKeywordRoot) initIndustry(ctx context.Context) {
  43. data, err := g.DB().Query(ctx, `select a.id,a.keyword,b.id as class_id,b.name,a.class1,a.class2,a.state from seo_industry a inner join seo_industry_class b on a.class1=b.name order by a.class1`)
  44. if err != nil {
  45. g.Log().Errorf(ctx, "初始化异常行业 %v", err)
  46. return
  47. }
  48. treeIndustry, keyWordIdMapping := map[string]*industryStruct{}, map[int64]*industryKeywords{}
  49. var allIndustryKeywords []*industryKeywords
  50. var allIndustry []*industryStruct
  51. if data.Len() > 0 && data.List() != nil {
  52. for _, v := range data.List() {
  53. kw := &industryKeywords{
  54. TopClass: gconv.String(v["class1"]),
  55. SubClass: gconv.String(v["class2"]),
  56. ClassId: gconv.Int64(v["class_id"]),
  57. KeyWordId: gconv.Int64(v["id"]),
  58. KeyWord: gconv.String(v["keyword"]),
  59. State: gconv.Int64(v["state"]),
  60. }
  61. kw.Url = fmt.Sprintf("/tags/industry/%d_all_all_%d.html", kw.ClassId, kw.KeyWordId)
  62. if _, ok := treeIndustry[kw.TopClass]; !ok {
  63. newIndustry := &industryStruct{
  64. Class: kw.TopClass,
  65. ClassId: kw.ClassId,
  66. }
  67. allIndustry = append(allIndustry, newIndustry)
  68. treeIndustry[kw.TopClass] = newIndustry
  69. }
  70. keyWordIdMapping[kw.KeyWordId] = kw
  71. if kw.SubClass != "药品" && kw.State == 1 {
  72. treeIndustry[kw.TopClass].List = append(treeIndustry[kw.TopClass].List, kw)
  73. }
  74. allIndustryKeywords = append(allIndustryKeywords, kw)
  75. }
  76. }
  77. iR.treeIndustry = treeIndustry
  78. iR.allIndustry = allIndustry
  79. iR.allKeywords = allIndustryKeywords
  80. iR.keyWordIdMapping = keyWordIdMapping
  81. }
  82. // GetEachIndustryRandomKeyWords 每个行业取num个少于等于5个字的标的物
  83. func (iR *industryKeywordRoot) GetEachIndustryRandomKeyWords(num int) []*industryStruct {
  84. var rData []*industryStruct
  85. for _, obj := range iR.allIndustry {
  86. var tmp []*industryKeywords
  87. selectEd, arrLen := map[int]bool{}, len(obj.List)
  88. if num >= arrLen {
  89. tmp = obj.List
  90. } else {
  91. for len(tmp) < num {
  92. val := grand.Intn(arrLen)
  93. if selectEd[val] {
  94. continue
  95. }
  96. if len([]rune(obj.List[val].KeyWord)) > 5 {
  97. continue
  98. }
  99. selectEd[val] = true
  100. tmp = append(tmp, obj.List[val])
  101. }
  102. }
  103. rData = append(rData, &industryStruct{
  104. Class: obj.Class,
  105. ClassId: obj.ClassId,
  106. List: tmp,
  107. })
  108. }
  109. return rData
  110. }
  111. func (iR *industryKeywordRoot) GetData(ctx context.Context, maxTotal int, query *SeoBiddingQuery) []map[string]interface{} {
  112. var sql string
  113. var values []interface{}
  114. if query.district != "" {
  115. sql += " AND b.district=? "
  116. values = append(values, query.district)
  117. }
  118. if query.city != "" {
  119. sql += " AND b.city=? "
  120. values = append(values, query.city)
  121. }
  122. if query.area != "" {
  123. sql += " AND b.area=? "
  124. values = append(values, query.area)
  125. }
  126. if query.topType != "" {
  127. if val, _ := consts.TopTypeMap[query.topType]; val != "" {
  128. sql += " AND b.toptype=? "
  129. values = append(values, val)
  130. } else {
  131. sql += " AND b.toptype=? "
  132. values = append(values, query.topType)
  133. }
  134. } else if query.topType == "" {
  135. sql += " AND (b.toptype !='拟建' AND b.toptype !='采购意向') "
  136. }
  137. if query.keys != "" {
  138. sql += " AND b.keyword=? "
  139. values = append(values, query.keys)
  140. }
  141. if query.topClass != "" {
  142. sql += " AND b.class1=? "
  143. values = append(values, query.topClass)
  144. }
  145. if query.subClass != "" {
  146. sql += " AND b.class2=? "
  147. values = append(values, query.subClass)
  148. }
  149. values = append(values, maxTotal+50)
  150. queryRes, err := g.DB().Query(ctx, fmt.Sprintf(`SELECT b.bid_id, b.keyword,b.desc
  151. FROM new_classKeywords b
  152. WHERE 1=1 %s
  153. ORDER BY b.publish_time DESC
  154. LIMIT 0,?`, sql), values...)
  155. if err != nil || queryRes.IsEmpty() {
  156. return nil
  157. }
  158. return FillingBiddingBaseFields(ctx, queryRes.List())
  159. }
  160. func (iR *industryKeywordRoot) GetEsData(ctx context.Context, maxTotal int, query *SeoBiddingQuery) []map[string]interface{} {
  161. list := utility.OtherElastic.Get(INDEX, TYPE, GetEsQuery(maxTotal, query))
  162. if list == nil || len(*list) == 0 {
  163. return nil
  164. }
  165. return FillingEsBiddingBaseFields(*list, query.keys)
  166. }
  167. func (iR *industryKeywordRoot) GetIndustry() []*industryStruct {
  168. return iR.allIndustry
  169. }
  170. func (iR *industryKeywordRoot) GetKeyWordsById(id int64) *industryKeywords {
  171. return iR.keyWordIdMapping[id]
  172. }