|
@@ -4,13 +4,29 @@ package analysis
|
|
|
import (
|
|
|
. "api"
|
|
|
"db"
|
|
|
+ "encoding/json"
|
|
|
+ "fmt"
|
|
|
"log"
|
|
|
qutil "qfw/util"
|
|
|
+ "qfw/util/elastic"
|
|
|
"qfw/util/jy"
|
|
|
+ "strconv"
|
|
|
"strings"
|
|
|
+ "time"
|
|
|
"util"
|
|
|
)
|
|
|
|
|
|
+const (
|
|
|
+ INDEX = "forecast"
|
|
|
+ TYPE = "forecast"
|
|
|
+ bidSearch_sort = `{"publishtime":-1}`
|
|
|
+ findfields = `"title"`
|
|
|
+
|
|
|
+ view_maxPageNum = 20
|
|
|
+ view_pageSize = 50
|
|
|
+ bidSearch_field = ``
|
|
|
+)
|
|
|
+
|
|
|
//预测详情页
|
|
|
func (this *Analysis) ForPContent() {
|
|
|
defer qutil.Catch()
|
|
@@ -208,47 +224,235 @@ func keysfilter(result []map[string]interface{}) (res []map[string]interface{})
|
|
|
}
|
|
|
|
|
|
//
|
|
|
-func getNewForecast(userId string, cpage int, o_member_jy map[string]interface{}) ([]map[string]interface{}, int) {
|
|
|
- if cpage == 0 {
|
|
|
- cpage = 1
|
|
|
+func getNewForecast(userId string, pageNum int, o_member_jy map[string]interface{}) (keys []interface{}, list *[]map[string]interface{}, count int64) {
|
|
|
+ if userId == "" {
|
|
|
+ return
|
|
|
}
|
|
|
- var count int
|
|
|
- log.Println(o_member_jy, count)
|
|
|
- return nil, 0
|
|
|
+ sql := getSqlObjFromId(o_member_jy)
|
|
|
+ if sql == nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ for _, v := range sql.Keyword {
|
|
|
+ var keys_one []string
|
|
|
+ for _, k := range v.Keyword {
|
|
|
+ keys_one = append(keys_one, k)
|
|
|
+ }
|
|
|
+ for _, k := range v.Appended {
|
|
|
+ keys_one = append(keys_one, k)
|
|
|
+ }
|
|
|
+ keys = append(keys, strings.Join(keys_one, "++"))
|
|
|
+ }
|
|
|
+ if len(keys) == 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ qstr := GetMemberForecastSql(sql)
|
|
|
+ count = elastic.Count(INDEX, TYPE, qstr)
|
|
|
+ list = elastic.GetAllByNgram(INDEX, TYPE, qstr, findfields, bidSearch_sort, bidSearch_field, (pageNum-1)*FP_Limit, FP_Limit, 0, false)
|
|
|
+ if list != nil {
|
|
|
+ for _, v := range *list {
|
|
|
+ v["_id"] = util.EncodeId(qutil.ObjToString(v["_id"]))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+//获取大会员forecast 检索库查询语句
|
|
|
+func GetMemberForecastSql(scd *util.ViewCondition) string {
|
|
|
+ query := `{"query":{"bool":{"must":[%s],"should":[%s],"minimum_should_match": %d}}}`
|
|
|
+ query_bool_should := `{"bool":{"should":[%s],"minimum_should_match": 1}}`
|
|
|
+ multi_match := `{"multi_match": {"query": %s,"type": "phrase", "fields": [%s]}}`
|
|
|
+ query_bool_must_and := `{"bool":{"must":[%s]%s}}`
|
|
|
+ query_must_exists := `{"bool":{"should":{"filtered":{"filter":{"bool":{"must":[{"exists":{"field":"yuceendtime"}},{"range":{"yuceendtime":{"lte":%d}}}]}}}}}}`
|
|
|
+
|
|
|
+ bools := []string{}
|
|
|
+ musts := []string{}
|
|
|
+ //预测时间最新三个月
|
|
|
+ musts = append(musts, fmt.Sprintf(`{"range":{"yucetime":{"gte":%d}}}`, time.Now().AddDate(0, -3, 0).Unix()), fmt.Sprintf(query_must_exists, time.Now().Unix()))
|
|
|
+ //省份
|
|
|
+ areaCity := []string{}
|
|
|
+ if len(scd.Area) > 0 {
|
|
|
+ areaquery := `{"terms":{"area":[`
|
|
|
+ for k, v := range scd.Area {
|
|
|
+ if k > 0 {
|
|
|
+ areaquery += `,`
|
|
|
+ }
|
|
|
+ areaquery += `"` + v + `"`
|
|
|
+ }
|
|
|
+ areaquery += `]}}`
|
|
|
+ areaCity = append(areaCity, areaquery)
|
|
|
+ }
|
|
|
+
|
|
|
+ //城市
|
|
|
+ if len(scd.City) > 0 {
|
|
|
+ areaquery := `{"terms":{"city":[`
|
|
|
+ for k, v := range scd.City {
|
|
|
+ if k > 0 {
|
|
|
+ areaquery += `,`
|
|
|
+ }
|
|
|
+ areaquery += `"` + v + `"`
|
|
|
+ }
|
|
|
+ areaquery += `]}}`
|
|
|
+ areaCity = append(areaCity, areaquery)
|
|
|
+ }
|
|
|
+ if len(areaCity) > 0 {
|
|
|
+ musts = append(musts, fmt.Sprintf(query_bool_should, strings.Join(areaCity, ",")))
|
|
|
+ }
|
|
|
+ if len(scd.Subtype) > 0 {
|
|
|
+ subquery := `{"terms":{"subtype":[`
|
|
|
+ for k, v := range scd.Subtype {
|
|
|
+ if k > 0 {
|
|
|
+ subquery += `,`
|
|
|
+ }
|
|
|
+ subquery += `"` + v + `"`
|
|
|
+ }
|
|
|
+ subquery += `]}}`
|
|
|
+ musts = append(musts, subquery)
|
|
|
+ }
|
|
|
+ if len(scd.Buyerclass) > 0 {
|
|
|
+ Buyerclass := `{"terms":{"buyerclass":[`
|
|
|
+ for k, v := range scd.Buyerclass {
|
|
|
+ if k > 0 {
|
|
|
+ Buyerclass += `,`
|
|
|
+ }
|
|
|
+ Buyerclass += `"` + v + `"`
|
|
|
+ }
|
|
|
+ Buyerclass += `]}}`
|
|
|
+ musts = append(musts, Buyerclass)
|
|
|
+ }
|
|
|
+ boolsNum := 0 //should
|
|
|
+ if len(scd.Keyword) > 0 {
|
|
|
+ boolsNum = 1
|
|
|
+ multi_match = fmt.Sprintf(multi_match, "%s", `"results.purchasing.mypurchasing"`)
|
|
|
+
|
|
|
+ for _, v := range scd.Keyword {
|
|
|
+ shoulds := []string{}
|
|
|
+ must_not := []string{}
|
|
|
+ //附加词
|
|
|
+ for _, vv := range v.Keyword {
|
|
|
+ shoulds = append(shoulds, fmt.Sprintf(multi_match, "\""+vv+"\""))
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, vv := range v.Appended {
|
|
|
+ shoulds = append(shoulds, fmt.Sprintf(multi_match, "\""+vv+"\""))
|
|
|
+ }
|
|
|
+
|
|
|
+ //排除词
|
|
|
+ for _, vv := range v.Exclude {
|
|
|
+ must_not = append(must_not, fmt.Sprintf(multi_match, "\""+vv+"\""))
|
|
|
+ }
|
|
|
+
|
|
|
+ //添加
|
|
|
+ if len(shoulds) > 0 {
|
|
|
+ notStr := ""
|
|
|
+ if len(must_not) > 0 {
|
|
|
+ notStr = fmt.Sprintf(`,"must_not":[%s]`, strings.Join(must_not, ","))
|
|
|
+ }
|
|
|
+ bools = append(bools, fmt.Sprintf(query_bool_must_and, strings.Join(shoulds, ","), notStr))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ qstr := fmt.Sprintf(query, strings.Join(musts, ","), strings.Join(bools, ","), boolsNum)
|
|
|
+ log.Println("------qstr", qstr)
|
|
|
+ return qstr
|
|
|
+}
|
|
|
+
|
|
|
+//
|
|
|
+
|
|
|
+//member_jy
|
|
|
+func getSqlObjFromId(o_member_jy map[string]interface{}) *util.ViewCondition {
|
|
|
+ if o_member_jy["a_items"] == nil {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ a_items := []interface{}{}
|
|
|
+ if o_member_jy["a_items"] != nil {
|
|
|
+ a_items = o_member_jy["a_items"].([]interface{})
|
|
|
+ }
|
|
|
+ a_buyerclass := []interface{}{}
|
|
|
+ if o_member_jy["a_buyerclass"] != nil {
|
|
|
+ a_buyerclass = o_member_jy["a_buyerclass"].([]interface{})
|
|
|
+ }
|
|
|
+ if len(a_buyerclass) > 0 && qutil.IntAllDef(o_member_jy["i_matchbuyerclass_other"], 1) == 1 && len(a_items) > 0 {
|
|
|
+ a_buyerclass = append(a_buyerclass, "其它")
|
|
|
+ }
|
|
|
+ a_infotype := []interface{}{}
|
|
|
+ if o_member_jy["a_infotype"] != nil {
|
|
|
+ a_infotype = o_member_jy["a_infotype"].([]interface{})
|
|
|
+ }
|
|
|
+ o_area := map[string]interface{}{}
|
|
|
+ if o_member_jy["o_area"] != nil {
|
|
|
+ o_area = o_member_jy["o_area"].(map[string]interface{})
|
|
|
+ }
|
|
|
+ return &util.ViewCondition{
|
|
|
+ Keyword: getKeyWordArr(a_items, "", -1),
|
|
|
+ Buyerclass: qutil.ObjArrToStringArr(a_buyerclass),
|
|
|
+ Subtype: qutil.ObjArrToStringArr(a_infotype),
|
|
|
+ Area: getStringArrFromDbResult(o_area, 1),
|
|
|
+ City: getStringArrFromDbResult(o_area, 2),
|
|
|
+ SelectType: strconv.Itoa(qutil.IntAll(o_member_jy["i_matchway"])),
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+//关键词 附加词 排除词
|
|
|
+func getKeyWordArr(a_items []interface{}, item string, index int) (arr []util.ViewKeyWord) {
|
|
|
+ if a_items == nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ for _, v := range a_items {
|
|
|
+ vm, _ := v.(map[string]interface{})
|
|
|
+ if item != "" && index >= 0 && item != qutil.ObjToString(vm["s_item"]) {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ kwsArr := vm["a_key"]
|
|
|
+ for i, k := range kwsArr.([]interface{}) {
|
|
|
+ if item != "" && index >= 0 && i != index {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ kw := util.ViewKeyWord{}
|
|
|
+ b, e := json.Marshal(k)
|
|
|
+ if e != nil {
|
|
|
+ log.Println(e.Error())
|
|
|
+ }
|
|
|
+ json.Unmarshal(b, &kw)
|
|
|
+ if kw.MatchWay == 1 {
|
|
|
+ for _, kk := range kw.Keyword {
|
|
|
+ arr = append(arr, util.ViewKeyWord{
|
|
|
+ Keyword: []string{kk},
|
|
|
+ Exclude: kw.Exclude,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ for _, kk := range kw.Appended {
|
|
|
+ arr = append(arr, util.ViewKeyWord{
|
|
|
+ Keyword: []string{kk},
|
|
|
+ Exclude: kw.Exclude,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ arr = append(arr, kw)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return
|
|
|
}
|
|
|
|
|
|
//
|
|
|
-func SubViewDatas(userId, allquery string, pageNum, pageSize int) (keys []interface{}, list *[]map[string]interface{}, hasNextPage bool, count int64) {
|
|
|
- // if userId == "" {
|
|
|
- // return
|
|
|
- // }
|
|
|
- // sql := GetSqlObjFromId(userId, "", -1)
|
|
|
- // if sql == nil {
|
|
|
- // return
|
|
|
- // }
|
|
|
- // //PC端
|
|
|
- // if pageSize == 0 || pageSize > 100 {
|
|
|
- // pageSize = view_pageSize
|
|
|
- // }
|
|
|
- // for _, v := range sql.Keyword {
|
|
|
- // var keys_one []string
|
|
|
- // for _, k := range v.Keyword {
|
|
|
- // keys_one = append(keys_one, k)
|
|
|
- // }
|
|
|
- // //dev3.5 附加词也要高亮
|
|
|
- // for _, k := range v.Appended {
|
|
|
- // keys_one = append(keys_one, k)
|
|
|
- // }
|
|
|
- // keys = append(keys, strings.Join(keys_one, "++"))
|
|
|
- // }
|
|
|
- // qstr := GetMemberViewSql(sql)
|
|
|
- // count = elastic.Count(INDEX, TYPE, qstr)
|
|
|
- // list = elastic.GetAllByNgram(INDEX, TYPE, qstr, findfields, bidSearch_sort, bidSearch_field, (pageNum-1)*pageSize, pageSize, 0, false)
|
|
|
- // if list != nil {
|
|
|
- // for _, v := range *list {
|
|
|
- // v["_id"] = EncodeId(qutil.ObjToString(v["_id"]))
|
|
|
- // }
|
|
|
- // }
|
|
|
- // hasNextPage = list != nil && len(*list) == pageSize && pageNum < view_maxPageNum
|
|
|
+func getStringArrFromDbResult(area map[string]interface{}, i int) (arr []string) {
|
|
|
+ if area == nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var eareArr []string
|
|
|
+ var cityArr []string
|
|
|
+ for k, v := range area {
|
|
|
+ if len(v.([]interface{})) > 0 {
|
|
|
+ cityArr = append(cityArr, qutil.ObjArrToStringArr(v.([]interface{}))...)
|
|
|
+ } else {
|
|
|
+ eareArr = append(eareArr, k)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if i == 1 {
|
|
|
+ arr = eareArr
|
|
|
+ } else {
|
|
|
+ arr = cityArr
|
|
|
+ }
|
|
|
return
|
|
|
}
|