package service import ( elastic "app.yhyue.com/moapp/jybase/es" IC "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXCore/rpc/init" "fmt" "github.com/gogf/gf/v2/util/gconv" "log" "strings" ) var ( yysIndex = "bidding_customer_v1" yysType = "bidding_customer_v1" ) func GetCriteria(entId, positionId int64) map[int]interface{} { result := map[int]interface{}{} //for i := 0; i < 1; i++ { for i := 0; i < 3; i++ { result[i] = getGetCriteriaType(entId, positionId, int64(i)) } return result } func getGetCriteriaType(entId, positionId, tag int64) []map[string]interface{} { data := []map[string]interface{}{} if tag == 1 { data = append(data, map[string]interface{}{ "key": "purchaseTime", "type": "dateTimeList", "label": "预计采购时间", "defaultVal": []string{}, "props": map[string]interface{}{ "multiple": false, }, "options": []map[string]interface{}{ { "label": "全部", "value": "", }, { "label": "本月", "value": "thisMonth", }, { "label": "下月", "value": "nextMonth", }, { "label": "自定义", "value": "exact", }, }, }) } else { data = append(data, map[string]interface{}{ "key": "openingTime", "type": "dateTimeList", "label": "开标日期", "defaultVal": []string{}, "props": map[string]interface{}{ "multiple": false, }, "options": []map[string]interface{}{ { "label": "全部", "value": "", }, { "label": "本周", "value": "thisWeek", }, { "label": "下周", "value": "nextWeek", }, { "label": "本月", "value": "thisMonth", }, { "label": "自定义", "value": "exact", }, }, }) } //私有标签处理 getlalArr := getLabel(entId) if len(getlalArr) > 0 { data = append(data, getlalArr...) } //查询都显示什么数据 labelArr := IC.BiMysql.SelectBySql(`SELECT * FROM customer_data_yys_config WHERE ent_id = ? and config_type="筛选条件" and config_class=? ORDER BY id`, entId, tag) if labelArr != nil && len(*labelArr) > 0 { for _, v := range *labelArr { configName := gconv.String(v["config_name"]) switch configName { case "行业": industryArr := getConfiguration("行业", "industry", entId) if len(industryArr) > 0 { data = append(data, map[string]interface{}{ "key": "industry", "type": "selectList", "label": "行业", "defaultVal": []string{}, "props": map[string]interface{}{ "multiple": false, }, "options": industryArr, }) } case "中标人标签": winnerArr := getConfiguration("运营商中标标签", "winner_tag", entId) if len(winnerArr) > 0 { data = append(data, map[string]interface{}{ "key": "winnerTag", "type": "selectList", "label": "中标人标签", "defaultVal": []string{}, "props": map[string]interface{}{ "multiple": false, }, "options": winnerArr, }) } case "信息类型": //查看是否有二级信息 isExist := false for _, v := range *labelArr { configName1 := gconv.String(v["config_name"]) if configName1 == "信息类型二级" { isExist = true } } if isExist { data = append(data, map[string]interface{}{ "key": "subtype", "type": "selectList", "label": "信息类型", "cascader": true, "collectionMark": 1, "defaultVal": "{}", "childLabel": "信息类型二级", "props": map[string]interface{}{ "multiple": false, }, "options": []map[string]interface{}{ { "label": "全部", "value": "", "children": []map[string]interface{}{}, }, { "label": "预告", "value": "预告", "children": []map[string]interface{}{ {"label": "全部", "value": ""}, {"label": "预告", "value": "预告"}, {"label": "预审", "value": "预审"}, {"label": "预审结果", "value": "预审结果"}, {"label": "论证意见", "value": "论证意见"}, {"label": "需求公示", "value": "需求公示"}, {"label": "其它", "value": "其它"}, }, }, { "label": "招标", "value": "招标", "children": []map[string]interface{}{ {"label": "全部", "value": ""}, {"label": "变更", "value": "变更"}, {"label": "邀标", "value": "邀标"}, {"label": "询价", "value": "询价"}, {"label": "竞谈", "value": "竞谈"}, {"label": "单一", "value": "单一"}, {"label": "竞价", "value": "竞价"}, {"label": "招标", "value": "招标"}, {"label": "其它", "value": "其它"}, }, }, { "label": "结果", "value": "结果", "children": []map[string]interface{}{ {"label": "全部", "value": ""}, {"label": "废标", "value": "废标"}, {"label": "流标", "value": "流标"}, {"label": "结果变更", "value": "结果变更"}, {"label": "中标", "value": "中标"}, {"label": "成交", "value": "成交"}, {"label": "其它", "value": "其它"}, }, }, { "label": "采购意向", "value": "采购意向", "children": []map[string]interface{}{ {"label": "全部", "value": ""}, {"label": "采购意向", "value": "采购意向"}, }, }, { "label": "拟建", "value": "拟建", "children": []map[string]interface{}{ {"label": "全部", "value": ""}, {"label": "拟建", "value": "拟建"}, }, }, { "label": "其它", "value": "其它", "children": []map[string]interface{}{ {"label": "全部", "value": ""}, {"label": "合同", "value": "合同"}, {"label": "验收", "value": "验收"}, {"label": "违规", "value": "违规"}, {"label": "其它", "value": "其它"}, }, }}}) } else { data = append(data, map[string]interface{}{ "key": "subtype", "type": "selectList", "label": "信息类型", "cascader": true, "collectionMark": 1, "defaultVal": "{}", "props": map[string]interface{}{ "multiple": false, }, "options": []map[string]interface{}{ { "label": "全部", "value": "", "children": []map[string]interface{}{}, }, { "label": "预告", "value": "预告", "children": []map[string]interface{}{}, }, { "label": "招标", "value": "招标", "children": []map[string]interface{}{}, }, { "label": "结果", "value": "结果", "children": []map[string]interface{}{}, }, { "label": "采购意向", "value": "采购意向", "children": []map[string]interface{}{}, }, { "label": "拟建", "value": "拟建", "children": []map[string]interface{}{}, }, { "label": "其它", "value": "其它", "children": []map[string]interface{}{}, }}}) } case "省份": areaMap := getArea(entId, positionId, labelArr) if areaMap != nil && len(areaMap) != 0 { data = append(data, areaMap) } } } } return data } func getConfiguration(conditionName, conditionType string, entId int64) []map[string]interface{} { data := IC.BiMysql.SelectBySql(`SELECT DISTINCT b.name,b.code FROM customer_data_yys_permissions a INNER JOIN customer_data_yys_permission_elements b on a.ent_id = ? and a.is_delete = 0 AND a.element_name = ? and FIND_IN_SET( b.code,a.element_value) and b.element_field=?`, entId, conditionName, conditionType) options := []map[string]interface{}{} if data != nil && len(*data) > 0 { options = append(options, map[string]interface{}{ "label": "全部", "value": "", }) for _, v := range *data { name := gconv.String(v["name"]) code := gconv.Int64(v["code"]) options = append(options, map[string]interface{}{ "label": name, "value": code, }) } } return options } func getLabel(entId int64) []map[string]interface{} { // 私有标签查询 data := []map[string]interface{}{} labelArr := IC.BiMysql.SelectBySql(`SELECT * FROM private_label WHERE ent_id = ? AND level > 0 ORDER BY id`, entId) if labelArr != nil && len(*labelArr) != 0 { // 使用切片来存储一级标签及其对应的二级、三级标签 labelIdArr := []int64{} labels := make(map[int64]map[string]interface{}) // 存储各级标签 labelOptions := make(map[int64][]map[string]interface{}) // 存储各级标签的选项 for _, m := range *labelArr { key := "tagname" switch len(labels) { case 1: key = "tagname1" case 2: key = "tagname2" } level := gconv.Int64(m["level"]) name := gconv.String(m["name"]) pid := gconv.Int64(m["pid"]) // 将 pid 转换为 int64 id := gconv.Int64(m["id"]) // 将 id 转换为 int64 if level == 1 { labelIdArr = append(labelIdArr, id) // 处理一级标签 labels[id] = map[string]interface{}{ "id": id, "key": key, "type": "dateTimeList", "label": name, "defaultVal": []string{}, "props": map[string]interface{}{ "multiple": false, }, "options": []map[string]interface{}{}, // 初始化选项 } } else { // 处理二级和三级标签 if _, exists := labels[pid]; exists { labelOptions[pid] = append(labelOptions[pid], map[string]interface{}{ "label": name, "value": name, }) } } } // 将标签及其选项添加到最终数据中 for _, m := range labelIdArr { for _, label := range labels { id := gconv.Int64(label["id"]) if id == m { if options, exists := labelOptions[label["id"].(int64)]; exists && len(options) > 0 { label["options"] = options } data = append(data, label) } } } } return data } func getArea(entid, positionId int64, labelArr *[]map[string]interface{}) map[string]interface{} { data := make(map[string]map[string][]string) // 私有标签查询 areaArr := IC.BiMysql.SelectBySql(` SELECT t0.area, t0.city, t0.district FROM bi_service.customer_data t0 INNER JOIN ( SELECT ent_id AS 企业_ID, MAX(CASE WHEN element_name = '行业' THEN element_value END) AS 行业要素值, MAX(CASE WHEN element_name = '运营商中标标签' THEN element_value END) AS 中标标签要素值, MAX(CASE WHEN element_name = '地区' THEN element_value END) AS 地区要素值, MAX(CASE WHEN element_name = '经营单位' THEN element_value END) AS 经营单位要素值 FROM bi_service.customer_data_yys_permissions WHERE is_delete = 0 and position_id=? and ent_id=? ) t1 ON t0.eid = t1.企业_ID WHERE (t1.行业要素值 IS NULL OR FIND_IN_SET(t0.INDUSTRY, t1.行业要素值)) AND (t1.地区要素值 IS NULL OR FIND_IN_SET(t0.REGION, t1.地区要素值)) AND (t1.中标标签要素值 IS NULL OR FIND_IN_SET(t0.WINNER_TAG, t1.中标标签要素值)) AND (t1.经营单位要素值 IS NULL OR FIND_IN_SET(t0.tagname2, t1.经营单位要素值)) AND (t0.STATUS = 1) GROUP BY area, city, district`, positionId, entid) if areaArr == nil || len(*areaArr) == 0 { //查找全局变量 areaArr = IC.BiMysql.SelectBySql(` SELECT area, city, district FROM customer_data WHERE eid = ? GROUP BY area, city, district ORDER BY area, city, district;`, entid) } if areaArr != nil && len(*areaArr) > 0 { for _, m := range *areaArr { area := gconv.String(m["area"]) city := gconv.String(m["city"]) district := gconv.String(m["district"]) if area == "" || city == "" { continue // 跳过无效的区域或城市 } if _, exists := data[area]; !exists { data[area] = make(map[string][]string) } if district != "" { data[area][city] = append(data[area][city], district) } else { data[area][city] = []string{} // 存储空数组以表示没有区 } } } isCity, isDistrict := false, false for _, v := range *labelArr { configName := gconv.String(v["config_name"]) if configName == "城市" { isCity = true } else if configName == "区县" { isDistrict = true } } if !isCity { //没有设置市区 for k := range data { data[k] = map[string][]string{} } } else if !isDistrict { //没有设置区县 for k, v := range data { for k1 := range v { v[k1] = []string{} } data[k] = v } } if len(data) > 0 { return map[string]interface{}{ "key": "area", "type": "component-popup", "label": "城市", "defaultVal": []string{}, "props": map[string]interface{}{ "multiple": false, }, "options": data, } } return map[string]interface{}{} } type Operator struct { EntId int64 //企业id 没有企业 企业id=0 Tag int64 PageNum int64 //当前页码 PageSize int64 //每页数量 Province string //省份 City string //城市 District string //区县 Subtype string //信息类型-二级 TopType string //信息类型-一级分类 PublishTime string //发布时间 SelectType string //搜索范围:标题;正文等 Price string //价格 Tagname string Tagname1 string Tagname2 string WordsMode int64 //搜索关键词模式;默认0:包含所有,1:包含任意 KeyWords string //关键词:多个空格隔开(主) PurchaseTime string //采购时间 OpeningTime string //开标时间 Industry string WinnerTag string PositionId int64 } func SearchList(operator Operator) (int64, *[]map[string]interface{}) { if operator.PageSize == 0 { operator.PageSize = 10 } start := (operator.PageNum - 1) * operator.PageSize if start < 0 { start = 0 } var query = `{"query":{"bool":{"must":[%s],"filter":[%s]}},"from":"%d","size":"%d","sort":[{"publishtime":{"order":"desc"}}], "minimum_should_match":1, "should":[{"exists":{"field":"bidamount"}},{"bool":{"should":[{"exists":{"field":"budget"}},{"bool":{"must_not":[{"exists":{"field":"budget"}}]}}],"must_not":[{"exists":{"field":"bidamount"}}]}}]}` shouldStr := `{"bool":{"should":[%s]}}` mustArr := []string{} filterArr := []string{ fmt.Sprintf(`{"match":{"ent_id":"%s"}}`, gconv.String(operator.EntId)), } //must处理 //filter处理 if operator.Tag == 1 { mustArr = append(mustArr, `{"match":{"subtype":["采购意向"]}}`) operator.Subtype = "" operator.TopType = "" } else if operator.Tag == 2 { mustArr = append(mustArr, `{"exists":{"field":"price"}}`) operator.Subtype = "" operator.TopType = "" } if operator.PublishTime != "" { pushTime := strings.Split(operator.PublishTime, "_") for k, v := range pushTime { if k == 0 { if v != "" { filterArr = append(filterArr, fmt.Sprint(`{"range":{"publishtime":{"gte":"%d"}}}`, v)) } } if k == 1 { if v != "" { filterArr = append(filterArr, fmt.Sprint(`{"range":{"publishtime":{"gte":"%d"}}}`, v)) } } } } //采购时间 if operator.PurchaseTime != "" { purchaseTime := strings.Split(operator.PurchaseTime, "_") for k, v := range purchaseTime { if k == 0 { if v != "" { filterArr = append(filterArr, fmt.Sprint(`{"range":{"expurasingtime":{"gte":"%d"}}}`, v)) } } if k == 1 { if v != "" { filterArr = append(filterArr, fmt.Sprint(`{"range":{"publishtime":{"gte":"%d"}}}`, v)) } } } } //开标时间 if operator.OpeningTime != "" { openingTime := strings.Split(operator.OpeningTime, "_") for k, v := range openingTime { if k == 0 { if v != "" { filterArr = append(filterArr, fmt.Sprint(`{"range":{"bidopentime":{"gte":"%d"}}}`, v)) } } if k == 1 { if v != "" { filterArr = append(filterArr, fmt.Sprint(`{"range":{"publishtime":{"gte":"%d"}}}`, v)) } } } } //关键词 if operator.KeyWords != "" { if operator.WordsMode == 1 { keyArr := []string{} for _, v := range strings.Split(operator.KeyWords, ",") { keyArr = append(keyArr, fmt.Sprintf(`{"match_phrase":{"%s":"%s"}`, operator.SelectType, v)) } mustArr = append(mustArr, fmt.Sprintf(shouldStr, strings.Join(keyArr, ","))) } else { for _, v := range strings.Split(operator.KeyWords, ",") { filterArr = append(filterArr, fmt.Sprintf(`{"match_phrase":{"%s":"%s"}}`, operator.SelectType, v)) } } } //信息类型一级 //{"match":{"toptype":"结果"}},{"terms":{"subtype":["结果变更","中标"]}} topType := []string{} subtype := []string{} if operator.TopType != "" { for k, v := range gconv.Map(operator.TopType) { topType = append(topType, k) for _, v1 := range gconv.Interfaces(v) { subtype = append(subtype, gconv.String(v1)) } } if len(topType) > 0 { filterArr = append(filterArr, fmt.Sprintf(`{"match":{"toptype":"%s"}}`), strings.Join(topType, `","`)) } //信息类型二级 if len(subtype) > 0 { filterArr = append(filterArr, fmt.Sprintf(`{"match":{"subtype":["%s"]}}`, strings.Join(subtype, `","`))) } } //私有标签1 if operator.Tagname != "" { keyArr := []string{} for _, v := range strings.Split(operator.Tagname, ",") { keyArr = append(keyArr, fmt.Sprintf(`{"match_phrase":{"tagname":"%s"}`, v)) } mustArr = append(mustArr, fmt.Sprintf(shouldStr, strings.Join(keyArr, ","))) } //私有标签2 if operator.Tagname1 != "" { keyArr := []string{} for _, v := range strings.Split(operator.Tagname1, ",") { keyArr = append(keyArr, fmt.Sprintf(`{"match_phrase":{"tagname":"%s"}`, v)) } mustArr = append(mustArr, fmt.Sprintf(shouldStr, strings.Join(keyArr, ","))) } //私有标签3 if operator.Tagname2 != "" { keyArr := []string{} for _, v := range strings.Split(operator.Tagname2, ",") { keyArr = append(keyArr, fmt.Sprintf(`{"match_phrase":{"tagname":"%s"}`, v)) } mustArr = append(mustArr, fmt.Sprintf(shouldStr, strings.Join(keyArr, ","))) } //价格处理 if operator.Price != "" { priceArr := strings.Split(operator.Price, "-") minPrice := gconv.Int64(priceArr[0]) maxPrice := gconv.Int64(priceArr[1]) bidamountArr := []string{` { "exists": { "field": "bidamount" } }`, fmt.Sprintf(` { "range": { "bidamount": { "gte": %s } } } `, gconv.String(minPrice))} budgetArr := []string{` { "exists": { "field": "budget" } }`, fmt.Sprintf(` { "range": { "budget": { "gte": %s } } } `, gconv.String(minPrice))} if maxPrice > 0 { bidamountArr = append(bidamountArr, fmt.Sprintf(` { "range": { "bidamount": { "lte": "%s" } } } `, gconv.String(minPrice))) budgetArr = append(budgetArr, fmt.Sprintf(` { "range": { "budget": { "lte": "%s" } } } `, gconv.String(minPrice))) } mustArr = append(mustArr, fmt.Sprintf(`{"bool":{"should":[{"bool":{"filter":[%s]}},{"bool":{"filter":[%s],"must_not":[{"exists":{"field":"bidamount"}}]}}]}}`, strings.Join(bidamountArr, ","), strings.Join(budgetArr, ","))) } //地区搜索 area := []string{} city := []string{} district := []string{} if operator.Province != "" { for k, v := range gconv.Map(operator.Province) { area = append(area, gconv.String(k)) for k1, v1 := range gconv.Map(v) { city = append(city, k1) for _, v3 := range gconv.SliceStr(v1) { district = append(district, fmt.Sprintf("%s_%s"), k1, v3) } } } if len(area) > 0 { query1 := "" query1 += `{"terms":{"area":[` for k, v := range strings.Split(operator.Province, ",") { if k > 0 { query1 += `,` } query1 += `"` + v + `"` } query1 += `]}}` filterArr = append(filterArr, query1) } //市--未登录用户不能根据市和地区筛选 if len(city) > 0 { query1 := "" query1 += `{"terms":{"city":[` for k, v := range strings.Split(operator.City, ",") { if k > 0 { query1 += `,` } query1 += `"` + v + `"` } query1 += `]}}` filterArr = append(filterArr, query1) } if len(district) > 0 { query1 := "" for k, v := range strings.Split(operator.District, ",") { if k > 0 { query1 += `,` } cityName := strings.Split(v, "_")[0] districtName := strings.Split(v, "_")[1] queryBoolMustAndDistrict := `{"bool":{"must":[{"terms":{"city":["%s"]}},{"terms":{"district":["%s"]}}]}}` query1 += fmt.Sprintf(queryBoolMustAndDistrict, cityName, districtName) } filterArr = append(filterArr, query1) } } //行业处理 if operator.Industry != "" { filterArr = append(filterArr, fmt.Sprintf(`{"match":{"industry":["%s"]}}`, strings.ReplaceAll(operator.Industry, ",", `","`))) } //中标人标签处理 if operator.WinnerTag != "" { filterArr = append(filterArr, fmt.Sprintf(`{"match":{"winner_tag":["%s"]}}`, strings.ReplaceAll(operator.WinnerTag, ",", `","`))) } //查看自己是否定制 configData := IC.BiMysql.SelectBySql(`SELECT MAX( CASE WHEN element_name = '行业' THEN element_value END ) AS industry, MAX( CASE WHEN element_name = '运营商中标标签' THEN element_value END ) AS winner_tag, MAX( CASE WHEN element_name = '地区' THEN element_value END ) AS region, MAX( CASE WHEN element_name = '经营单位' THEN element_value END ) AS tagname FROM bi_service.customer_data_yys_permissions WHERE is_delete = 0 AND position_id = ? AND ent_id = ?`, operator.PositionId, operator.EntId) if configData != nil && len(*configData) > 0 { tagname := gconv.String((*configData)[0]["tagname"]) region := gconv.String((*configData)[0]["region"]) winner_tag := gconv.String((*configData)[0]["winner_tag"]) industry := gconv.String((*configData)[0]["industry"]) if tagname != "" { filterArr = append(filterArr, fmt.Sprintf(`{"terms":{"tagname":["%s"]}}`, strings.Join(strings.Split(tagname, ","), `","`))) } if region != "" { filterArr = append(filterArr, fmt.Sprintf(`{"terms":{"region":["%s"]}}`, strings.Join(strings.Split(region, ","), `","`))) } if winner_tag != "" { filterArr = append(filterArr, fmt.Sprintf(`{"terms":{"winner_tag":["%s"]}}`, strings.Join(strings.Split(winner_tag, ","), `","`))) } if industry != "" { filterArr = append(filterArr, fmt.Sprintf(`{"terms":{"industry":["%s"]}}`, strings.Join(strings.Split(industry, ","), `","`))) } } //es sql拼接 queryStr := fmt.Sprintf(query, strings.Join(mustArr, ","), strings.Join(filterArr, ","), start, operator.PageSize) fmt.Println(queryStr) count, data := elastic.GetWithCount(yysIndex, yysType, "", queryStr) log.Println() return count, data }