123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- package service
- import (
- "context"
- "fmt"
- "github.com/gogf/gf/v2/frame/g"
- "github.com/gogf/gf/v2/os/gctx"
- "github.com/gogf/gf/v2/util/gconv"
- "github.com/gogf/gf/v2/util/grand"
- "jyseo/internal/consts"
- "jyseo/utility"
- )
- var (
- JySeoIndustryKeyWordRoot *industryKeywordRoot = &industryKeywordRoot{}
- )
- type (
- industryKeywordRoot struct {
- allKeywords []*industryKeywords
- allIndustry []*industryStruct
- treeIndustry map[string]*industryStruct
- keyWordIdMapping map[int64]*industryKeywords //包含已被过滤的词
- }
- industryStruct struct {
- Class string //一级分类
- ClassId int64 //一级分类id
- //Url string
- List []*industryKeywords
- }
- industryKeywords struct {
- TopClass string //一级分类
- SubClass string //二级分类
- ClassId int64 //一级分类id
- KeyWordId int64 //关键词id
- KeyWord string //关键词
- Url string //地址
- State int64 //是否有效
- }
- )
- func init() {
- JySeoIndustryKeyWordRoot.initIndustry(gctx.New())
- }
- // GetIndustry 获取行业和行业下标的物
- func (iR *industryKeywordRoot) initIndustry(ctx context.Context) {
- 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`)
- if err != nil {
- g.Log().Errorf(ctx, "初始化异常行业 %v", err)
- return
- }
- treeIndustry, keyWordIdMapping := map[string]*industryStruct{}, map[int64]*industryKeywords{}
- var allIndustryKeywords []*industryKeywords
- var allIndustry []*industryStruct
- if data.Len() > 0 && data.List() != nil {
- for _, v := range data.List() {
- kw := &industryKeywords{
- TopClass: gconv.String(v["class1"]),
- SubClass: gconv.String(v["class2"]),
- ClassId: gconv.Int64(v["class_id"]),
- KeyWordId: gconv.Int64(v["id"]),
- KeyWord: gconv.String(v["keyword"]),
- State: gconv.Int64(v["state"]),
- }
- kw.Url = fmt.Sprintf("/tags/industry/%d_all_all_%d.html", kw.ClassId, kw.KeyWordId)
- if _, ok := treeIndustry[kw.TopClass]; !ok {
- newIndustry := &industryStruct{
- Class: kw.TopClass,
- ClassId: kw.ClassId,
- }
- allIndustry = append(allIndustry, newIndustry)
- treeIndustry[kw.TopClass] = newIndustry
- }
- keyWordIdMapping[kw.KeyWordId] = kw
- if kw.SubClass != "药品" && kw.State == 1 {
- treeIndustry[kw.TopClass].List = append(treeIndustry[kw.TopClass].List, kw)
- }
- allIndustryKeywords = append(allIndustryKeywords, kw)
- }
- }
- iR.treeIndustry = treeIndustry
- iR.allIndustry = allIndustry
- iR.allKeywords = allIndustryKeywords
- iR.keyWordIdMapping = keyWordIdMapping
- }
- // GetEachIndustryRandomKeyWords 每个行业取num个少于等于5个字的标的物
- func (iR *industryKeywordRoot) GetEachIndustryRandomKeyWords(num int) []*industryStruct {
- var rData []*industryStruct
- for _, obj := range iR.allIndustry {
- var tmp []*industryKeywords
- selectEd, arrLen := map[int]bool{}, len(obj.List)
- if num >= arrLen {
- tmp = obj.List
- } else {
- for len(tmp) < num {
- val := grand.Intn(arrLen)
- if selectEd[val] {
- continue
- }
- if len([]rune(obj.List[val].KeyWord)) > 5 {
- continue
- }
- selectEd[val] = true
- tmp = append(tmp, obj.List[val])
- }
- }
- rData = append(rData, &industryStruct{
- Class: obj.Class,
- ClassId: obj.ClassId,
- List: tmp,
- })
- }
- return rData
- }
- func (iR *industryKeywordRoot) GetData(ctx context.Context, maxTotal int, query *SeoBiddingQuery) []map[string]interface{} {
- var sql string
- var values []interface{}
- if query.district != "" {
- sql += " AND b.district=? "
- values = append(values, query.district)
- }
- if query.city != "" {
- sql += " AND b.city=? "
- values = append(values, query.city)
- }
- if query.area != "" {
- sql += " AND b.area=? "
- values = append(values, query.area)
- }
- if query.topType != "" {
- if val, _ := consts.TopTypeMap[query.topType]; val != "" {
- sql += " AND b.toptype=? "
- values = append(values, val)
- } else {
- sql += " AND b.toptype=? "
- values = append(values, query.topType)
- }
- } else if query.topType == "" {
- sql += " AND (b.toptype !='拟建' AND b.toptype !='采购意向') "
- }
- if query.keys != "" {
- sql += " AND b.keyword=? "
- values = append(values, query.keys)
- }
- if query.topClass != "" {
- sql += " AND b.class1=? "
- values = append(values, query.topClass)
- }
- if query.subClass != "" {
- sql += " AND b.class2=? "
- values = append(values, query.subClass)
- }
- values = append(values, maxTotal+50)
- queryRes, err := g.DB().Query(ctx, fmt.Sprintf(`SELECT b.bid_id, b.keyword,b.desc
- FROM new_classKeywords b
- WHERE 1=1 %s
- ORDER BY b.publish_time DESC
- LIMIT 0,?`, sql), values...)
- if err != nil || queryRes.IsEmpty() {
- return nil
- }
- return FillingBiddingBaseFields(ctx, queryRes.List())
- }
- func (iR *industryKeywordRoot) GetEsData(ctx context.Context, maxTotal int, query *SeoBiddingQuery) []map[string]interface{} {
- list := utility.OtherElastic.Get(INDEX, TYPE, GetEsQuery(maxTotal, query))
- if list == nil || len(*list) == 0 {
- return nil
- }
- return FillingEsBiddingBaseFields(*list, query.keys)
- }
- func (iR *industryKeywordRoot) GetIndustry() []*industryStruct {
- return iR.allIndustry
- }
- func (iR *industryKeywordRoot) GetKeyWordsById(id int64) *industryKeywords {
- return iR.keyWordIdMapping[id]
- }
|