|
@@ -1,11 +1,14 @@
|
|
|
package service
|
|
|
|
|
|
import (
|
|
|
+ elastic "app.yhyue.com/moapp/jybase/es"
|
|
|
"context"
|
|
|
"fmt"
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
"github.com/gogf/gf/v2/util/gconv"
|
|
|
+ "math/rand"
|
|
|
"strings"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
var (
|
|
@@ -16,11 +19,17 @@ type (
|
|
|
Enterprises struct{}
|
|
|
|
|
|
MapsTotalResp struct {
|
|
|
- Total int `json:"total"`
|
|
|
- List []map[string]interface{} `json:"list"`
|
|
|
+ Total int `json:"total"`
|
|
|
+ List []map[string]interface{} `json:"list"`
|
|
|
+ UpdateTime int64 `json:"updateTime"`
|
|
|
}
|
|
|
)
|
|
|
|
|
|
+const (
|
|
|
+ NewMustSearch = `{"query":{"bool":{"must":[%s]}}%s}`
|
|
|
+ biddingIndex, biddingType = "bidding", "bidding"
|
|
|
+)
|
|
|
+
|
|
|
// GetEnterprisesList 获取最新采购单位或中标企业列表
|
|
|
// identityType : 0 采购单位 1-中标单位
|
|
|
// limit: 数量
|
|
@@ -36,9 +45,9 @@ func (*Enterprises) GetEnterprisesList(ctx context.Context, identityType, pageNu
|
|
|
var sql string
|
|
|
if identityType == 0 {
|
|
|
sql = fmt.Sprintf(`
|
|
|
- SELECT c.id AS sid,c.name,c.company_id AS id,b.NAME AS buyerclass,p.area AS province,ca.city AS city
|
|
|
+ SELECT c.seo_id AS sid,c.name,c.company_id AS id,b.NAME AS buyerclass,p.area AS province,ca.city AS city
|
|
|
FROM (
|
|
|
- SELECT dfeb.id,dfeb.NAME, dfeb.company_id, dfeb.area_code, dfeb.city_code, dfeb.name_id,dfet.labelvalues
|
|
|
+ SELECT dfeb.seo_id,dfeb.NAME, dfeb.company_id, dfeb.area_code, dfeb.city_code, dfeb.name_id,dfet.labelvalues
|
|
|
FROM global_common_data.dws_f_ent_baseinfo dfeb
|
|
|
INNER JOIN global_common_data.dws_f_ent_tags dfet
|
|
|
ON (dfeb.identity_type &(1 << 0)) > 0 AND dfeb.name_id = dfet.name_id
|
|
@@ -48,12 +57,12 @@ func (*Enterprises) GetEnterprisesList(ctx context.Context, identityType, pageNu
|
|
|
LEFT JOIN global_common_data.code_area ca ON (ca. CODE = c.city_code)`, limit)
|
|
|
} else if identityType == 1 {
|
|
|
sql = fmt.Sprintf(`
|
|
|
- SELECT c.id AS sid,c.name, c.company_id AS id,c.address,cb.legal_person,cb.company_status,cb.capital,DATE_FORMAT(cb.establish_date, '%s') AS establish_date,cb.company_phone
|
|
|
+ SELECT c.seo_id AS sid,c.name, c.company_id AS id,c.address,cb.legal_person,cb.company_status,cb.capital,DATE_FORMAT(cb.establish_date, '%s') AS establish_date,cb.company_phone
|
|
|
FROM (
|
|
|
- SELECT id,name, company_id,address
|
|
|
+ SELECT seo_id,name, company_id,address
|
|
|
FROM global_common_data.dws_f_ent_baseinfo
|
|
|
- WHERE company_id!=''and (identity_type & (1 << 1)) > 0 order by latest_time desc
|
|
|
- LIMIT %d
|
|
|
+ WHERE company_id!=''and (identity_type & (1 << 1)) > 0
|
|
|
+ order by latest_time desc LIMIT %d
|
|
|
) c left join global_common_data.company_baseinfo cb on(cb.company_id=c.company_id)`, "%Y-%m-%d", limit)
|
|
|
}
|
|
|
|
|
@@ -100,74 +109,250 @@ func (*Enterprises) GetEnterprisesList(ctx context.Context, identityType, pageNu
|
|
|
return res, nil
|
|
|
}
|
|
|
|
|
|
-// GetEnterprisesListOld 获取最新采购单位或中标企业列表
|
|
|
-// identityType : 0 采购单位 1-中标单位
|
|
|
-// limit: 数量
|
|
|
-//func (*Enterprises) GetEnterprisesListOld(ctx context.Context, identityType, pageNum, pageSize, limit int) (res *MapsTotalResp, err error) {
|
|
|
-// key := fmt.Sprintf("enterpriseList_%d_%d_%d_%d", identityType, pageNum, pageSize, limit)
|
|
|
-// gVal, err := g.Redis().Get(ctx, key)
|
|
|
-// if err == nil && !gVal.IsNil() {
|
|
|
-// res = &MapsTotalResp{}
|
|
|
-// if err := gVal.Struct(res); err == nil {
|
|
|
-// return res, nil
|
|
|
-// }
|
|
|
-// }
|
|
|
-//
|
|
|
-// arr, err := func() (r []string, err error) {
|
|
|
-// res, err := g.DB("tidb").Query(context.Background(), "SELECT company_id AS end_id,name_id as buyer_id FROM global_common_data.dws_f_ent_baseinfo WHERE company_id !='' and company_id is not null AND (identity_type &(1 << ?)) > 0 order by latest_time desc limit ?", identityType, limit)
|
|
|
-// if err != nil {
|
|
|
-// return nil, err
|
|
|
-// }
|
|
|
-// for _, m := range res.List() {
|
|
|
-// if identityType == 0 {
|
|
|
-// r = append(r, gconv.String(m["buyer_id"]))
|
|
|
-// } else if identityType == 1 {
|
|
|
-// r = append(r, gconv.String(m["end_id"]))
|
|
|
-// }
|
|
|
-// }
|
|
|
-// return
|
|
|
-// }()
|
|
|
-//
|
|
|
-// if err != nil {
|
|
|
-// return nil, err
|
|
|
-// }
|
|
|
-// if len(arr) == 0 {
|
|
|
-// return nil, nil
|
|
|
-// }
|
|
|
-//
|
|
|
-// var finalArr []map[string]interface{}
|
|
|
-// switch identityType {
|
|
|
-// case 0:
|
|
|
-// if res := elastic.Get("buyer", "buyer", fmt.Sprintf(`{"query":{"bool":{"must":[{"terms":{"id":["%s"]}}]}},"size":%d,"_source":["_id","name","province","city","buyerclass"]}`, strings.Join(arr, `","`), limit)); res != nil && len(*res) > 0 {
|
|
|
-// finalArr = *res
|
|
|
-// }
|
|
|
-// case 1:
|
|
|
-// if res := elastic.Get("qyxy", "qyxy", fmt.Sprintf(`{"query":{"bool":{"must":[{"terms":{"id":["%s"]}}]}},"size":%d,"_source":["_id","company_name","company_status","legal_person","capital","establish_date","company_phone","company_address"]}`, strings.Join(arr, `","`), limit)); res != nil && len(*res) > 0 {
|
|
|
-// finalArr = *res
|
|
|
-// }
|
|
|
-// }
|
|
|
-//
|
|
|
-// if count := len(finalArr); count > 0 {
|
|
|
-// totalPage := count / pageSize
|
|
|
-// if count%pageSize != 0 {
|
|
|
-// totalPage++
|
|
|
-// }
|
|
|
-// for i := 1; i <= gconv.Int(totalPage); i++ {
|
|
|
-// start, end := (i-1)*pageSize, (i)*pageSize
|
|
|
-// if end > count {
|
|
|
-// end = count
|
|
|
-// }
|
|
|
-// pageTmp := &MapsTotalResp{
|
|
|
-// Total: totalPage,
|
|
|
-// List: finalArr[start:end],
|
|
|
-// }
|
|
|
-// if i == pageNum {
|
|
|
-// res = pageTmp
|
|
|
-// }
|
|
|
-// if err := g.Redis().SetEX(ctx, fmt.Sprintf("enterpriseList_%d_%d_%d_%d", identityType, i, pageSize, limit), pageTmp, 60*60*24); err != nil {
|
|
|
-// g.Log().Errorf(ctx, "第%d页数据 存储redis err:%v", i, err)
|
|
|
-// }
|
|
|
-// }
|
|
|
-// }
|
|
|
-// return res, nil
|
|
|
-//}
|
|
|
+// GetEntDetailMsg 查询企业详细信息
|
|
|
+func (*Enterprises) GetEntDetailMsg(ctx context.Context, seoId string, isWinner bool) map[string]interface{} {
|
|
|
+ if isWinner {
|
|
|
+ res, err := g.DB("tidb").Query(context.Background(), `
|
|
|
+ SELECT c.seo_id AS sid,c.name,cb.*,DATE_FORMAT(cb.establish_date, '%Y-%m-%d') AS establish_formatDate,ca.area as areaName
|
|
|
+ FROM (
|
|
|
+ SELECT seo_id,name, company_id,address
|
|
|
+ FROM global_common_data.dws_f_ent_baseinfo
|
|
|
+ WHERE company_id !='' and (identity_type & (1 << 1)) > 0
|
|
|
+ and seo_id = ?
|
|
|
+ ) c LEFT JOIN global_common_data.company_baseinfo cb on(cb.company_id=c.company_id)
|
|
|
+ LEFT JOIN global_common_data.code_area ca on(cb.area_code=ca.code )`, seoId)
|
|
|
+ if err != nil {
|
|
|
+ g.Log().Errorf(ctx, "GetEntDetailMsg 异常", err)
|
|
|
+ }
|
|
|
+ if res.Len() == 1 {
|
|
|
+ return res.List()[0]
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ res, err := g.DB("tidb").Query(context.Background(), `
|
|
|
+ SELECT c.*,cb.name AS buyerclass,ca.area as areaName ,ca.city as cityName FROM
|
|
|
+ (SELECT name,area_code,city_code,name_id FROM global_common_data.dws_f_ent_baseinfo WHERE seo_id = ?) c
|
|
|
+ LEFT JOIN global_common_data.dws_f_ent_tags dfet on(c.name_id=dfet.name_id )
|
|
|
+ LEFT JOIN global_common_data.code_buyerclass cb on(dfet.labelvalues=cb.code)
|
|
|
+ LEFT JOIN global_common_data.code_area ca on(IF(c.city_code,ca.code=c.city_code,ca.code=c.area_code))`, seoId)
|
|
|
+ if err != nil {
|
|
|
+ g.Log().Errorf(ctx, "GetEntDetailMsg 异常", err)
|
|
|
+ }
|
|
|
+ if res.Len() == 1 {
|
|
|
+ return res.List()[0]
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return nil
|
|
|
+}
|
|
|
+
|
|
|
+// GetDynamicNews 未登录用户动态信息
|
|
|
+func (pwp *Enterprises) GetDynamicNews(ctx context.Context, name string, isWinner bool) (res *MapsTotalResp, err error) {
|
|
|
+ if name == "" {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ res = &MapsTotalResp{}
|
|
|
+ redisKey := fmt.Sprintf("free_portrait_list_%s", name)
|
|
|
+ gVal, err := g.Redis().Get(ctx, redisKey)
|
|
|
+ if err == nil && !gVal.IsNil() {
|
|
|
+ if err := gVal.Struct(res); err == nil {
|
|
|
+ return res, nil
|
|
|
+ }
|
|
|
+ }
|
|
|
+ res.Total = gconv.Int(elastic.Count(biddingIndex, biddingType, pareSelect(name, isWinner, true)))
|
|
|
+ if res.Total == 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ list := elastic.Get(biddingIndex, biddingType, pareSelect(name, isWinner, false))
|
|
|
+ if list != nil && len(*list) > 0 {
|
|
|
+ res.List = *list
|
|
|
+ }
|
|
|
+ res.UpdateTime = time.Now().Unix()
|
|
|
+ _ = g.Redis().SetEX(ctx, redisKey, res, 60*60*24*7)
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// RelevanceData 关联单位
|
|
|
+func (pwp *Enterprises) RelevanceData(ctx context.Context, name string, isWinner bool) (randomMaps []map[string]interface{}) {
|
|
|
+ if name == "" {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ redisKey := fmt.Sprintf("portrait_relevance_%v_%s", isWinner, name)
|
|
|
+ getFullArr, fromCache := func() (data []map[string]interface{}, fromCache bool) {
|
|
|
+ gVal, err := g.Redis().Get(ctx, redisKey)
|
|
|
+ if err == nil && !gVal.IsNil() {
|
|
|
+ if err := gVal.Struct(&data); err == nil {
|
|
|
+ fromCache = true
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ data = []map[string]interface{}{}
|
|
|
+ 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 {
|
|
|
+ queryMap := make(map[string]bool)
|
|
|
+ if !isWinner { //采购关联企业 校验正确性
|
|
|
+ for _, v := range *getData {
|
|
|
+ for _, name := range gconv.Strings(v["entidlist"]) {
|
|
|
+ queryMap[name] = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ for _, v := range *getData {
|
|
|
+ for _, name := range gconv.Strings(v["buyer"]) {
|
|
|
+ queryMap[name] = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if len(queryMap) == 0 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ var queryArr []string = make([]string, 0, len(queryMap))
|
|
|
+ for name, _ := range queryMap {
|
|
|
+ queryArr = append(queryArr, name)
|
|
|
+ }
|
|
|
+ var sql string
|
|
|
+ if !isWinner {
|
|
|
+ sql = fmt.Sprintf(`SELECT name,seo_id FROM global_common_data.dws_f_ent_baseinfo WHERE company_id in ("%s")`, strings.Join(queryArr, `","`))
|
|
|
+ } else {
|
|
|
+ sql = fmt.Sprintf(`SELECT name,seo_id FROM global_common_data.dws_f_ent_baseinfo WHERE name in ("%s")`, strings.Join(queryArr, `","`))
|
|
|
+ }
|
|
|
+
|
|
|
+ //根据采购单位名字或中标企业id查询seo_id
|
|
|
+ dbRes, _ := g.DB("tidb").Query(ctx, sql)
|
|
|
+ if dbRes.IsEmpty() {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, m := range dbRes.List() {
|
|
|
+ name, seo_id := gconv.String(m["name"]), gconv.String(m["seo_id"])
|
|
|
+ if name == "" || seo_id == "" {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ var url string
|
|
|
+ if isWinner {
|
|
|
+ url = fmt.Sprintf("/dw/%s.html", seo_id)
|
|
|
+ } else {
|
|
|
+ url = fmt.Sprintf("/qy/%s.html", seo_id)
|
|
|
+ }
|
|
|
+ data = append(data, map[string]interface{}{
|
|
|
+ "name": name,
|
|
|
+ "url": url,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return
|
|
|
+ }()
|
|
|
+ if !fromCache {
|
|
|
+ _ = g.Redis().SetEX(ctx, redisKey, getFullArr, 60*60*24)
|
|
|
+ }
|
|
|
+ if len(getFullArr) > 10 { //大于10 随机取10个
|
|
|
+ rand.Seed(time.Now().UnixNano())
|
|
|
+ start := rand.Intn(len(getFullArr) - 10)
|
|
|
+ randomMaps = getFullArr[start : start+10]
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+// RecommendedData 其他推荐
|
|
|
+// name 名称 province 省份 isWinner 来源是否中标企业画像 false 来源企业画像
|
|
|
+func (pwp *Enterprises) RecommendedData(ctx context.Context, name, province string, isWinner bool) (rData []map[string]interface{}) {
|
|
|
+ if name == "" || province == "" || len(province) != 6 {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ redisKey := fmt.Sprintf("jyseo_portrait_recommended_%v_%s", isWinner, province)
|
|
|
+ getFullArr, formCache := func() (data []map[string]interface{}, formCache bool) {
|
|
|
+ data = []map[string]interface{}{}
|
|
|
+ province = province[:2] + "0000"
|
|
|
+
|
|
|
+ gVal, err := g.Redis().Get(ctx, redisKey)
|
|
|
+ if err == nil && !gVal.IsNil() {
|
|
|
+ if err := gVal.Struct(&data); err == nil {
|
|
|
+ formCache = true
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+ var sql string
|
|
|
+ if isWinner {
|
|
|
+ sql = fmt.Sprintf(`
|
|
|
+ SELECT name,seo_id FROM global_common_data.dws_f_ent_baseinfo
|
|
|
+ WHERE area_code = %s
|
|
|
+ AND (identity_type & (1 << 1)) > 0
|
|
|
+ ORDER BY latest_time desc limit 200
|
|
|
+ `, province)
|
|
|
+ } else {
|
|
|
+ sql = fmt.Sprintf(`
|
|
|
+ SELECT name,seo_id FROM global_common_data.dws_f_ent_baseinfo
|
|
|
+ WHERE area_code = %s
|
|
|
+ AND (identity_type &(1 << 0)) > 0
|
|
|
+ ORDER BY latest_time desc limit 200
|
|
|
+ `, province)
|
|
|
+ }
|
|
|
+
|
|
|
+ dbRes, _ := g.DB("tidb").Query(ctx, sql)
|
|
|
+ if dbRes.IsEmpty() {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, m := range dbRes.List() {
|
|
|
+ name, seo_id := gconv.String(m["name"]), gconv.String(m["seo_id"])
|
|
|
+ if name == "" || seo_id == "" {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+ var url string
|
|
|
+ if isWinner {
|
|
|
+ url = fmt.Sprintf("/qy/%s.html", seo_id)
|
|
|
+ } else {
|
|
|
+ url = fmt.Sprintf("/dw/%s.html", seo_id)
|
|
|
+ }
|
|
|
+ data = append(data, map[string]interface{}{
|
|
|
+ "name": name,
|
|
|
+ "url": url,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ return
|
|
|
+ }()
|
|
|
+ if !formCache {
|
|
|
+ _ = g.Redis().SetEX(ctx, redisKey, getFullArr, 60*60*24)
|
|
|
+ }
|
|
|
+ if len(getFullArr) > 0 { //大于10 随机取10个
|
|
|
+ if len(getFullArr) > 10 {
|
|
|
+ rand.Seed(time.Now().UnixNano())
|
|
|
+ start := rand.Intn(len(getFullArr) - 10)
|
|
|
+ return getFullArr[start : start+10]
|
|
|
+ }
|
|
|
+ return getFullArr
|
|
|
+ } else { //TODO 无数据从运行数据从取
|
|
|
+
|
|
|
+ }
|
|
|
+ return
|
|
|
+}
|
|
|
+
|
|
|
+func pareSelect(name string, isWinner, onlyCount 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()))
|
|
|
+ }
|
|
|
+ if onlyCount {
|
|
|
+ return fmt.Sprintf(NewMustSearch, strings.Join(mustQueryArr, ","), "")
|
|
|
+ } else {
|
|
|
+ return fmt.Sprintf(NewMustSearch, strings.Join(mustQueryArr, ","), fmt.Sprintf(`,"_source":[%s],"sort":{%s},"from":0,"size":30`, fields, `"publishtime":"desc","id":"desc"`))
|
|
|
+ }
|
|
|
+}
|