123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- package util
- import (
- "encoding/json"
- "fmt"
- "log"
- qutil "qfw/util"
- "qfw/util/elastic"
- "strconv"
- "strings"
- "time"
- "go.mongodb.org/mongo-driver/bson"
- )
- /*已选条件--关键词*/
- type ViewKeyWord struct {
- Keyword []string `json:"key"` //关键词
- Appended []string `json:"appendkey"` //附加词
- Exclude []string `json:"notkey"` //排除词
- }
- /*已选条件*/
- type ViewCondition struct {
- Area []string //地区-省份
- City []string //地区-城市
- Buyerclass []string //采购行业
- Keyword []ViewKeyWord //关键词
- SelectType string //筛选(正文 or 标题)
- Subtype []string //信息类型
- }
- const (
- INDEX = "bidding"
- TYPE = "bidding"
- bidSearch_sort = `{"publishtime":-1}`
- findfields = `"title"`
- view_maxPageNum = 20
- view_pageSize = 50
- bidSearch_field = `"_id","title","publishtime","toptype","subtype","type","area","buyerclass","budget","bidamount"`
- )
- func SubViewDatas(userId, allquery string, pageNum int) (keys []interface{}, list *[]map[string]interface{}, hasNextPage bool) {
- if userId == "" {
- return
- }
- sql := GetSqlObjFromId(userId)
- 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 := GetVIPViewSql(sql)
- list = elastic.GetAllByNgram(INDEX, TYPE, qstr, findfields, bidSearch_sort, bidSearch_field, (pageNum-1)*view_pageSize, view_pageSize, 0, false)
- if list != nil {
- for _, v := range *list {
- v["_id"] = qutil.EncodeArticleId2ByCheck(qutil.ObjToString(v["_id"]))
- }
- }
- hasNextPage = list != nil && len(*list) == view_pageSize && pageNum < view_maxPageNum
- return
- }
- //获取vip订阅预览的查询语句
- func GetVIPViewSql(scd *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}}`
- bools := []string{}
- musts := []string{}
- //省份
- 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
- if scd.SelectType == "" || scd.SelectType == "2" {
- scd.SelectType = "detail\", \"title"
- } else {
- scd.SelectType = "title"
- }
- multi_match = fmt.Sprintf(multi_match, "%s", "\""+scd.SelectType+"\"")
- 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
- }
- //
- func GetSqlObjFromId(_id string) *ViewCondition {
- var (
- query *map[string]interface{}
- ok bool
- )
- if query, ok = MQFW.FindById("user", _id, `{"o_vipjy":1}`); !ok {
- return nil
- }
- o_vipjy, _ := (*query)["o_vipjy"].(map[string]interface{})
- a_items := o_vipjy["a_items"].([]interface{})
- a_buyerclass := o_vipjy["a_buyerclass"].([]interface{})
- if len(a_buyerclass) > 0 && qutil.IntAllDef(o_vipjy["i_matchbuyerclass_other"], 1) == 1 && len(a_items) > 0 {
- a_buyerclass = append(a_buyerclass, "其它")
- }
- a_infotype := o_vipjy["a_infotype"].([]interface{})
- o_area := o_vipjy["o_area"].(map[string]interface{})
- return &ViewCondition{
- Keyword: getKeyWordArrFromDbResult(a_items),
- 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_vipjy["i_matchway"])),
- }
- }
- //
- 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
- }
- //关键词 附加词 排除词
- func getKeyWordArrFromDbResult(a_items []interface{}) (arr []ViewKeyWord) {
- if a_items == nil {
- return
- }
- for _, v := range a_items {
- kwsArr := v.(map[string]interface{})["a_key"]
- for _, k := range kwsArr.([]interface{}) {
- kw := ViewKeyWord{}
- b, e := json.Marshal(k)
- if e != nil {
- log.Println(e.Error())
- }
- json.Unmarshal(b, &kw)
- arr = append(arr, kw)
- }
- }
- return
- }
- //初始化vip订阅关键词
- func MergeKws(userId string) {
- defer qutil.Catch()
- if userId == "" { //11-11 取消此操作
- return
- }
- data, ok := MQFW.FindById("user", userId, `{"o_jy":1,"o_vipjy":1}`)
- var o_vipjy map[string]interface{}
- if ok && data != nil && len(*data) > 0 {
- o_vipjy, _ = (*data)["o_vipjy"].(map[string]interface{})
- a_items, _ := o_vipjy["a_items"].([]interface{})
- if a_items == nil { //首次
- a := MQFW.UpdateById("user", userId, bson.M{
- "$set": bson.M{"o_vipjy.i_matchway": 1, "o_vipjy.i_ratemode": 1, "o_vipjy.i_wxpush": 1, "o_vipjy.i_apppush": 1, "o_vipjy.i_projectmatch": 0, "o_vipjy.a_infotype": []string{}, "o_vipjy.a_items": []string{}, "o_vipjy.l_modifydate": time.Now().Unix()},
- })
- log.Println(a)
- }
- }
- }
- //m 月 超过一年传12+n月 如14; endtime 当前周期结束时间戳 int64 ;val - 1:年 2:月
- func GetDATE(val int, m int, endtime int64) (_endtime time.Time) {
- if val == 1 {
- m = m * 12
- }
- endFormat := qutil.FormatDateByInt64(&endtime, qutil.Date_Short_Layout)
- date_y, _ := strconv.Atoi(strings.Split(endFormat, "-")[0])
- date_m, _ := strconv.Atoi(strings.Split(endFormat, "-")[1])
- date_d, _ := strconv.Atoi(strings.Split(endFormat, "-")[2])
- if date_m+m > 12 {
- date_y = date_y + (date_m+m-1)/12
- date_m = date_m + m - 12*((date_m+m-1)/12)
- } else {
- date_m = date_m + m
- }
- mstr := strconv.Itoa(date_m)
- if date_m < 10 {
- mstr = "0" + mstr
- }
- _date := strconv.Itoa(date_y) + "-" + mstr + "-01"
- p, _ := time.ParseInLocation(qutil.Date_Short_Layout, _date, time.Local)
- date := time.Unix(p.Unix(), 0).AddDate(0, 1, -1)
- thisD, _ := strconv.Atoi(strings.Split(qutil.FormatDate(&date, qutil.Date_Short_Layout), "-")[2])
- dstr := "01"
- if thisD < date_d {
- dstr = strconv.Itoa(thisD)
- if thisD < 10 {
- dstr = "0" + strconv.Itoa(thisD)
- }
- } else {
- dstr = strconv.Itoa(date_d)
- if date_d < 10 {
- dstr = "0" + strconv.Itoa(date_d)
- }
- }
- _date = strconv.Itoa(date_y) + "-" + mstr + "-" + dstr + " 23:59:59"
- lastDate, _ := time.ParseInLocation(qutil.Date_Full_Layout, _date, time.Local)
- return lastDate
- }
|