|
@@ -0,0 +1,219 @@
|
|
|
+package utility
|
|
|
+
|
|
|
+import (
|
|
|
+ "app.yhyue.com/moapp/jybase/common"
|
|
|
+ elastic "app.yhyue.com/moapp/jybase/es"
|
|
|
+ "app.yhyue.com/moapp/jybase/redis"
|
|
|
+ "encoding/json"
|
|
|
+ "errors"
|
|
|
+ "fmt"
|
|
|
+ "math/rand"
|
|
|
+ "strings"
|
|
|
+ "time"
|
|
|
+)
|
|
|
+
|
|
|
+// PortraitScreen 画像筛选
|
|
|
+type PortraitScreen struct {
|
|
|
+ Ent string //企业id
|
|
|
+ Match string //检索词 50个字符
|
|
|
+ MatchRange string //搜索范围 项目名称/项目标的物、招标代理机构、采购单位、中标单位
|
|
|
+ ExactMatch bool //匹配方式 true精确匹配、false模糊匹配
|
|
|
+ Area string //省份
|
|
|
+ ScopeClass string //行业
|
|
|
+ BuyerClass string //采购单位类型
|
|
|
+ TimeRange string //信息发布时间 以年为单位,逗号分隔、结束时间不得超过当前时间
|
|
|
+ HasPower bool //是否有相应权限
|
|
|
+ //数据导出回显
|
|
|
+ PareStartTime, PareEndTime int64 //筛选时间戳
|
|
|
+ ShowMatch string
|
|
|
+ KeyWordArr []string
|
|
|
+ UserLevel int //用户身份 3:未登录;2:免费;1:付费
|
|
|
+}
|
|
|
+
|
|
|
+// PortraitProjectScreen 动态翻页
|
|
|
+type PortraitProjectScreen struct {
|
|
|
+ Ent string //企业id
|
|
|
+ PageNum int //页码
|
|
|
+ PageSize int //每页数量
|
|
|
+}
|
|
|
+
|
|
|
+const (
|
|
|
+ NewMustSearch = `{"query":{"bool":{"must":[%s]}}%s}`
|
|
|
+ biddingIndex, biddingType = "bidding", "bidding"
|
|
|
+)
|
|
|
+
|
|
|
+// 未登录用户动态信息
|
|
|
+func (pwp *PortraitProjectScreen) FreeBuyerNews(name string, isWinner bool) ([]map[string]interface{}, int64, error) {
|
|
|
+ if pwp.Ent == "" {
|
|
|
+ return nil, 0, errors.New("参数错误")
|
|
|
+ }
|
|
|
+ pwp.PageNum = common.If(pwp.PageNum == 0, 1, pwp.PageNum).(int) //默认第一页
|
|
|
+ pwp.PageSize = common.If(pwp.PageSize == 0, 10, pwp.PageSize).(int) //默认每页10条
|
|
|
+
|
|
|
+ redisKey := fmt.Sprintf("free_portrait_list_%s", pwp.Ent)
|
|
|
+ var data []map[string]interface{}
|
|
|
+ if rBytes, err := redis.GetBytes("other", redisKey); err == nil && len(*rBytes) != 0 {
|
|
|
+ _ = json.Unmarshal(*rBytes, &data)
|
|
|
+ } else {
|
|
|
+ list := elastic.Get(biddingIndex, biddingIndex, PareSelect(name, isWinner))
|
|
|
+ if list != nil && len(*list) > 0 {
|
|
|
+ data = *list
|
|
|
+ go func() {
|
|
|
+ redis.Put("other", redisKey, data, int(redisTime()))
|
|
|
+ }()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ total := int64(len(data))
|
|
|
+ if len(data) > 0 {
|
|
|
+ start := (pwp.PageNum - 1) * pwp.PageSize
|
|
|
+ // 对数据切片进行裁剪
|
|
|
+ if start < 0 {
|
|
|
+ start = 0
|
|
|
+ }
|
|
|
+ if start >= len(data) {
|
|
|
+ data = []map[string]interface{}{}
|
|
|
+ } else {
|
|
|
+ end := start + pwp.PageSize
|
|
|
+ if end > len(data) {
|
|
|
+ end = len(data)
|
|
|
+ }
|
|
|
+ data = data[start:end]
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return data, total, nil
|
|
|
+}
|
|
|
+
|
|
|
+// PareSelect sql
|
|
|
+func PareSelect(name string, isWinner bool) string {
|
|
|
+ sYear := 2018
|
|
|
+ //返回默认时间
|
|
|
+ sTimeStamp := time.Date(sYear, 1, 1, 0, 0, 0, 0, time.Local)
|
|
|
+ var (
|
|
|
+ mustQueryArr []string
|
|
|
+ fields string
|
|
|
+ )
|
|
|
+ if isWinner {
|
|
|
+ fields = `"_id","projectname","bidamount","budget","title","publishtime","subtype","toptype","area","bidopentime","buyer"`
|
|
|
+ mustQueryArr = append(mustQueryArr, fmt.Sprintf(`{"term":{"entidlist":"%s"}}`, name))
|
|
|
+ mustQueryArr = append(mustQueryArr, fmt.Sprintf(`{"range":{"publishtime":{"gte":"%d","lte":"%d"}}}`, sTimeStamp.Unix(), time.Now().Unix()))
|
|
|
+ } else {
|
|
|
+ fields = `"bidstatus","_id","title","subtype","projectname","publishtime","area","bidamount","budget","bidopentime","s_winner","entidlist"`
|
|
|
+ mustQueryArr = append(mustQueryArr, fmt.Sprintf(`{"term":{"buyer":"%s"}}`, name))
|
|
|
+ mustQueryArr = append(mustQueryArr, fmt.Sprintf(`{"range":{"publishtime":{"gte":"%d","lte":"%d"}}}`, sTimeStamp.Unix(), time.Now().Unix()))
|
|
|
+ }
|
|
|
+ return fmt.Sprintf(NewMustSearch, strings.Join(mustQueryArr, ","), fmt.Sprintf(`,"_source":[%s],"sort":{%s},"from":0,"size":30`, fields, `"publishtime":"desc","id":"desc"`))
|
|
|
+}
|
|
|
+
|
|
|
+func redisTime() float64 {
|
|
|
+ currentTime := time.Now()
|
|
|
+
|
|
|
+ currentWeekday := currentTime.Weekday()
|
|
|
+ daysUntilMonday := time.Monday - currentWeekday
|
|
|
+ if daysUntilMonday < 0 {
|
|
|
+ daysUntilMonday += 7
|
|
|
+ }
|
|
|
+ nextMonday := currentTime.AddDate(0, 0, int(daysUntilMonday))
|
|
|
+
|
|
|
+ nextMondayMidnight := time.Date(nextMonday.Year(), nextMonday.Month(), nextMonday.Day(), 0, 0, 0, 0, nextMonday.Location())
|
|
|
+
|
|
|
+ return nextMondayMidnight.Sub(currentTime).Seconds()
|
|
|
+}
|
|
|
+
|
|
|
+func RelevanceData(name string, isWinner bool) []map[string]interface{} {
|
|
|
+ if name == "" {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ redisKey := fmt.Sprintf("portrait_relevance_%v_%s", isWinner, name)
|
|
|
+ var data []map[string]interface{}
|
|
|
+ if rBytes, err := redis.GetBytes("other", redisKey); err == nil && len(*rBytes) != 0 {
|
|
|
+ _ = json.Unmarshal(*rBytes, &data)
|
|
|
+ } else {
|
|
|
+ querySql := `{"query": {"bool": {"must": [%s]}},"_source":["buyer","s_winner","entidlist"],"size": 200}`
|
|
|
+ var mustSql string
|
|
|
+ if isWinner {
|
|
|
+ mustSql = fmt.Sprintf(`{"terms": {"entidlist": ["%s"]}},{"exists": {"field": "buyer"}}`, name)
|
|
|
+ } else {
|
|
|
+ mustSql = fmt.Sprintf(`{"match": {"buyer": "%s"}},{"exists": {"field": "s_winner"}},{"exists": {"field": "entidlist"}}`, name)
|
|
|
+ }
|
|
|
+ getData := elastic.Get(biddingIndex, biddingType, fmt.Sprintf(querySql, mustSql))
|
|
|
+ if getData != nil && len(*getData) > 0 {
|
|
|
+ if !isWinner { //采购关联企业 校验正确性
|
|
|
+ entMap := make(map[string]string)
|
|
|
+ for _, v := range *getData {
|
|
|
+ s_winner := common.InterfaceToStr(v["s_winner"])
|
|
|
+ entidlist, _ := v["entidlist"].([]interface{})
|
|
|
+ winners := strings.Split(s_winner, ",")
|
|
|
+ if s_winner == "" || entidlist == nil || len(winners) != len(entidlist) {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ //中标企业去重
|
|
|
+ for k, v1 := range winners {
|
|
|
+ entMap[v1] = EncodeId(common.InterfaceToStr(entidlist[k]))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ for entname, entid := range entMap {
|
|
|
+ data = append(data, map[string]interface{}{"s_winner": entname, "entidlist": []string{entid}})
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ data = *getData
|
|
|
+ }
|
|
|
+
|
|
|
+ go func() {
|
|
|
+ redis.Put("other", redisKey, data, 24*3600)
|
|
|
+ }()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ var start int
|
|
|
+ if len(data) > 10 { //大于10 随机取10个
|
|
|
+ rand.Seed(time.Now().UnixNano())
|
|
|
+ start = rand.Intn(len(data) - 10)
|
|
|
+ data = data[start : start+10]
|
|
|
+ }
|
|
|
+ return data
|
|
|
+}
|
|
|
+
|
|
|
+// name 名称 province 省份 isWinner 来源是否中标企业画像 false 来源企业画像
|
|
|
+func RecommendedData(name, province string, isWinner bool) []map[string]interface{} {
|
|
|
+ var data []map[string]interface{}
|
|
|
+ if name != "" && province != "" && province != "其他" {
|
|
|
+ redisKey := fmt.Sprintf("portrait_recommended_%v_%s", isWinner, province)
|
|
|
+ if rBytes, err := redis.GetBytes("other", redisKey); err == nil && len(*rBytes) != 0 {
|
|
|
+ _ = json.Unmarshal(*rBytes, &data)
|
|
|
+ } else {
|
|
|
+ if isWinner {
|
|
|
+ winnerData, _ := Mgo_Ent.Find("qyxy_std", map[string]interface{}{"company_area": province, "name": map[string]interface{}{"$ne": name}},
|
|
|
+ "", `{"company_area": 1,"_id": 1,"company_name": 1,}`, true, 0, 200)
|
|
|
+ if winnerData != nil && len(*winnerData) > 0 {
|
|
|
+ for _, v := range *winnerData {
|
|
|
+ v["id"] = EncodeId(common.InterfaceToStr(v["_id"]))
|
|
|
+ delete(v, "_id")
|
|
|
+ }
|
|
|
+ data = *winnerData
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ qsl := `{"query": {"bool": {"must": [{"term": {"province": "%s"}}],"must_not": [{"term": {"name": "%s"}}]}},"_source":["buyer_name"],"size": 200}`
|
|
|
+ buyerData := elastic.Get("buyer", "buyer", fmt.Sprintf(qsl, province, name))
|
|
|
+ if buyerData != nil {
|
|
|
+ data = *buyerData
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if len(data) > 0 {
|
|
|
+ go func() {
|
|
|
+ redis.Put("other", redisKey, data, 24*3600)
|
|
|
+ }()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ var start int
|
|
|
+ if len(data) > 10 { //大于10 随机取10个
|
|
|
+ rand.Seed(time.Now().UnixNano())
|
|
|
+ start = rand.Intn(len(data) - 10)
|
|
|
+ data = data[start : start+10]
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //TODO 无数据从运行数据从取
|
|
|
+ if len(data) == 0 {
|
|
|
+
|
|
|
+ }
|
|
|
+ return data
|
|
|
+}
|