123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- package common
- import (
- qutil "app.yhyue.com/moapp/jybase/common"
- "encoding/json"
- "fmt"
- "leadGeneration/public"
- "leadGeneration/util"
- "log"
- "regexp"
- "strings"
- "time"
- )
- func AdvancedProject(userid, keyWords string) (map[string]interface{}, error) {
- mae := new(MarketAnalysisEntity)
- if key := KeyWordFormat(userid, keyWords); key != "" {
- if err := json.Unmarshal([]byte(key), &mae.FormatParam.KeysItems); err != nil {
- log.Println("关键词格式化失败")
- return nil, err
- }
- }
- mae.Types = 2
- mae.FormatParam.SubType = []string{"拟建", "采购意向"}
- mae.Size = 2
- mae.FormatParam.STime = time.Now().AddDate(0, -3, 0).Unix()
- finalSql := fmt.Sprintf(mae.GetCommonQuerySqlWithAggs(), projectsNumber, mae.Size, projectsSort)
- log.Println("超前项目es查询:", finalSql)
- res, _, data := util.GetAggs("bidding", "bidding", finalSql)
- if res == nil || len(res) == 0 || data == nil || len(data) == 0 {
- return nil, nil
- }
- var thisRow SuperProjects
- for name, object := range res {
- bArr, err := object.MarshalJSON()
- if len(bArr) == 0 || err != nil {
- continue
- }
- if name == "projects_number" {
- if json.Unmarshal(bArr, &thisRow.ProjectsNumber) != nil {
- continue
- }
- }
- }
- redisData := make(map[string]interface{})
- for _, v := range thisRow.ProjectsNumber.Buckets {
- if v.DocCount == 0 {
- continue
- }
- redisData[v.Key] = v.DocCount
- }
- if len(redisData) == 0 {
- return nil, nil
- }
- resData := make([]map[string]interface{}, len(data))
- for i, v := range data {
- if json.Unmarshal(*v.Source, &resData[i]) != nil {
- continue
- }
- }
- log.Println("AdvancedProject redis data :", resData)
- return map[string]interface{}{
- "projectTop2": resData,
- "projectCount": resData,
- }, nil
- }
- func KeyWordFormat(userid, keyWords string) string {
- var (
- arryMap []map[string]interface{}
- key string
- )
- if keyWords != "" {
- var aItems []map[string]interface{}
- for _, v := range strings.Split(keyWords, ",") {
- arryMap = append(arryMap, map[string]interface{}{"from": 1, "appendkey": nil, "key": processKeyword(v), "notkey": nil, "updatetime": time.Now().Unix()})
- }
- item := map[string]interface{}{
- "a_key": arryMap,
- "s_item": "未分类",
- "updatetime": time.Now().Unix(),
- }
- aItems = append(aItems, item)
- dataType, _ := json.Marshal(aItems)
- key = string(dataType)
- } else {
- data, ok := public.MQFW.FindById("user", userid, `{"o_jy":1,"o_vipjy":1,"o_member_jy":1,"i_vip_status":1,"i_member_status":1,"s_phone":1,"s_m_phone":1}`)
- if ok && data != nil && len(*data) > 0 {
- oJy, _ := (*data)["o_jy"].(map[string]interface{}) //免费用户关键词
- vipJy, _ := (*data)["o_vipjy"].(map[string]interface{}) //超级订阅关键词
- memberJy, _ := (*data)["o_member_jy"].(map[string]interface{}) //大会员关键词
- var aItems []interface{}
- if qutil.IntAll((*data)["i_member_status"]) > 0 {
- aItems, _ = memberJy["a_items"].([]interface{})
- } else if qutil.IntAll((*data)["i_vip_status"]) > 0 {
- aItems, _ = vipJy["a_items"].([]interface{})
- } else {
- if oJy["a_key"] != nil && len(oJy["a_key"].([]interface{})) > 0 {
- a_key, _ := oJy["a_key"].([]interface{})
- for _, v := range a_key {
- v1, _ := v.(map[string]interface{})
- arryMap = append(arryMap, map[string]interface{}{"from": 1, "appendkey": nil, "key": processKeyword(qutil.InterfaceToStr(v1["key"])), "notkey": nil, "updatetime": time.Now().Unix()})
- }
- item := map[string]interface{}{
- "a_key": arryMap,
- "s_item": "未分类",
- "updatetime": time.Now().Unix(),
- }
- aItems = append(aItems, item)
- }
- }
- if len(aItems) > 0 {
- dataType, _ := json.Marshal(aItems)
- key = string(dataType)
- }
- }
- }
- return key
- }
- // 处理订阅的关键词
- func processKeyword(keyword string) []string {
- keywordReg := regexp.MustCompile("([\\s\u3000\u2003\u00a0+,,])+")
- spaceReg := regexp.MustCompile("\\s+")
- keyword = keywordReg.ReplaceAllString(keyword, " ")
- keyword = spaceReg.ReplaceAllString(keyword, " ")
- keyword = strings.Trim(keyword, " ")
- if keyword == "" {
- return nil
- }
- return strings.Split(keyword, " ")
- }
|