operator.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  1. package service
  2. import (
  3. "app.yhyue.com/moapp/jybase/common"
  4. ME "app.yhyue.com/moapp/jybase/encrypt"
  5. elastic "app.yhyue.com/moapp/jybase/es"
  6. "app.yhyue.com/moapp/jybase/redis"
  7. IC "bp.jydev.jianyu360.cn/BaseService/jyMicroservices/jyBXCore/rpc/init"
  8. "fmt"
  9. "github.com/gogf/gf/v2/util/gconv"
  10. "log"
  11. "strings"
  12. "time"
  13. )
  14. var (
  15. yysIndex = "bidding_customer"
  16. yysType = "bidding_customer"
  17. )
  18. func GetCriteria(entId, positionId int64) map[int]interface{} {
  19. result := map[int]interface{}{}
  20. //for i := 0; i < 1; i++ {
  21. for i := 0; i < 3; i++ {
  22. result[i] = getGetCriteriaType(entId, positionId, int64(i))
  23. }
  24. return result
  25. }
  26. func getGetCriteriaType(entId, positionId, tag int64) []map[string]interface{} {
  27. data := []map[string]interface{}{}
  28. if tag == 1 {
  29. data = append(data, map[string]interface{}{
  30. "key": "purchaseTime",
  31. "type": "dateTimeList",
  32. "label": "预计采购时间",
  33. "defaultVal": "",
  34. "props": map[string]interface{}{
  35. "multiple": false,
  36. "endTimeFeature": true,
  37. },
  38. "options": []map[string]interface{}{
  39. {
  40. "label": "全部", "value": "",
  41. },
  42. {
  43. "label": "本月", "value": "thisMonth",
  44. },
  45. {
  46. "label": "下月", "value": "nextMonth",
  47. },
  48. {
  49. "label": "自定义", "value": "exact",
  50. },
  51. },
  52. })
  53. } else if tag == 2 {
  54. data = append(data, map[string]interface{}{
  55. "key": "openingTime",
  56. "type": "dateTimeList",
  57. "label": "开标日期",
  58. "defaultVal": "",
  59. "props": map[string]interface{}{
  60. "multiple": false,
  61. "endTimeFeature": true,
  62. },
  63. "options": []map[string]interface{}{
  64. {
  65. "label": "全部", "value": "",
  66. },
  67. {
  68. "label": "本周", "value": "thisWeek",
  69. },
  70. {
  71. "label": "下周", "value": "nextWeek",
  72. },
  73. {
  74. "label": "本月", "value": "thisMonth",
  75. },
  76. {
  77. "label": "自定义", "value": "exact",
  78. },
  79. },
  80. })
  81. }
  82. //发布时间
  83. data = append(data, map[string]interface{}{
  84. "key": "publishTime",
  85. "type": "dateTimeList",
  86. "label": "发布时间",
  87. "defaultVal": "",
  88. "props": map[string]interface{}{
  89. "multiple": false,
  90. "endTimeFeature": true,
  91. },
  92. "options": []map[string]interface{}{
  93. {
  94. "label": "全部", "value": "",
  95. },
  96. {
  97. "label": "昨天", "value": "yesterday",
  98. },
  99. {
  100. "label": "上周", "value": "lastWeek",
  101. },
  102. {
  103. "label": "上一个月", "value": "lastMonth",
  104. },
  105. {
  106. "label": "自定义", "value": "exact",
  107. },
  108. },
  109. })
  110. //私有标签处理
  111. getlalArr := getLabel(entId)
  112. if len(getlalArr) > 0 {
  113. data = append(data, getlalArr...)
  114. }
  115. //查询都显示什么数据
  116. labelArr := IC.BiMysql.SelectBySql(`SELECT * FROM customer_data_yys_config WHERE ent_id = ? and config_type="筛选条件" and config_class=? ORDER BY id`, entId, tag)
  117. if labelArr != nil && len(*labelArr) > 0 {
  118. for _, v := range *labelArr {
  119. configName := gconv.String(v["config_name"])
  120. switch configName {
  121. case "行业":
  122. industryArr, _ := getConfiguration("行业", "industry", entId, positionId)
  123. if len(industryArr) > 0 {
  124. data = append(data, map[string]interface{}{
  125. "key": "industry",
  126. "type": "selectList",
  127. "label": "行业",
  128. "defaultVal": []string{""},
  129. "props": map[string]interface{}{
  130. "multiple": true,
  131. },
  132. "options": industryArr,
  133. })
  134. }
  135. case "中标人标签":
  136. winnerArr, _ := getConfiguration("运营商中标标签", "winner_tag", entId, positionId)
  137. if len(winnerArr) > 0 {
  138. data = append(data, map[string]interface{}{
  139. "key": "winnerTag",
  140. "type": "selectList",
  141. "label": "中标人标签",
  142. "defaultVal": []string{""},
  143. "props": map[string]interface{}{
  144. "multiple": true,
  145. },
  146. "options": winnerArr,
  147. })
  148. }
  149. case "信息类型":
  150. //查看是否有二级信息
  151. isExist := false
  152. for _, v := range *labelArr {
  153. configName1 := gconv.String(v["config_name"])
  154. if configName1 == "信息类型二级" {
  155. isExist = true
  156. }
  157. }
  158. if isExist {
  159. data = append(data, map[string]interface{}{
  160. "key": "topType",
  161. "type": "component-popup-infotype",
  162. "label": "信息类型",
  163. "cascader": true,
  164. "collectionMark": 1,
  165. "defaultVal": "",
  166. "childLabel": "信息类型二级",
  167. "props": map[string]interface{}{
  168. "multiple": false,
  169. },
  170. "options": []map[string]interface{}{
  171. {
  172. "label": "全部",
  173. "value": "",
  174. "children": []map[string]interface{}{},
  175. },
  176. {
  177. "label": "预告",
  178. "value": "预告",
  179. "children": []map[string]interface{}{
  180. {"label": "预告", "value": "预告"},
  181. {"label": "预审", "value": "预审"},
  182. {"label": "预审结果", "value": "预审结果"},
  183. {"label": "论证意见", "value": "论证意见"},
  184. {"label": "需求公示", "value": "需求公示"},
  185. {"label": "其它", "value": "其它"},
  186. },
  187. }, {
  188. "label": "招标",
  189. "value": "招标",
  190. "children": []map[string]interface{}{
  191. {"label": "变更", "value": "变更"},
  192. {"label": "邀标", "value": "邀标"},
  193. {"label": "询价", "value": "询价"},
  194. {"label": "竞谈", "value": "竞谈"},
  195. {"label": "单一", "value": "单一"},
  196. {"label": "竞价", "value": "竞价"},
  197. {"label": "招标", "value": "招标"},
  198. {"label": "其它", "value": "其它"},
  199. },
  200. }, {
  201. "label": "结果",
  202. "value": "结果",
  203. "children": []map[string]interface{}{
  204. {"label": "废标", "value": "废标"},
  205. {"label": "流标", "value": "流标"},
  206. {"label": "结果变更", "value": "结果变更"},
  207. {"label": "中标", "value": "中标"},
  208. {"label": "成交", "value": "成交"},
  209. {"label": "其它", "value": "其它"},
  210. },
  211. }, {
  212. "label": "采购意向",
  213. "value": "采购意向",
  214. "children": []map[string]interface{}{
  215. {"label": "采购意向", "value": "采购意向"},
  216. },
  217. }, {
  218. "label": "拟建",
  219. "value": "拟建",
  220. "children": []map[string]interface{}{
  221. {"label": "拟建", "value": "拟建"},
  222. },
  223. }, {
  224. "label": "其它",
  225. "value": "其它",
  226. "children": []map[string]interface{}{
  227. {"label": "合同", "value": "合同"},
  228. {"label": "验收", "value": "验收"},
  229. {"label": "违规", "value": "违规"},
  230. {"label": "其它", "value": "其它"},
  231. },
  232. }}})
  233. } else {
  234. data = append(data, map[string]interface{}{
  235. "key": "topType",
  236. "type": "selectList",
  237. "label": "信息类型",
  238. "cascader": true,
  239. "collectionMark": 1,
  240. "defaultVal": "",
  241. "props": map[string]interface{}{
  242. "multiple": false,
  243. },
  244. "options": []map[string]interface{}{
  245. {
  246. "label": "全部",
  247. "value": "",
  248. "children": []map[string]interface{}{},
  249. },
  250. {
  251. "label": "预告",
  252. "value": "预告",
  253. "children": []map[string]interface{}{},
  254. }, {
  255. "label": "招标",
  256. "value": "招标",
  257. "children": []map[string]interface{}{},
  258. }, {
  259. "label": "结果",
  260. "value": "结果",
  261. "children": []map[string]interface{}{},
  262. }, {
  263. "label": "采购意向",
  264. "value": "采购意向",
  265. "children": []map[string]interface{}{},
  266. }, {
  267. "label": "拟建",
  268. "value": "拟建",
  269. "children": []map[string]interface{}{},
  270. }, {
  271. "label": "其它",
  272. "value": "其它",
  273. "children": []map[string]interface{}{},
  274. }}})
  275. }
  276. case "省份":
  277. areaMap := getArea(entId, positionId, labelArr)
  278. if areaMap != nil && len(areaMap) != 0 {
  279. data = append(data, areaMap)
  280. }
  281. }
  282. }
  283. }
  284. return data
  285. }
  286. func getConfiguration(conditionName, conditionType string, entId int64, positionId int64) ([]map[string]interface{}, map[string]interface{}) {
  287. dataMap := make(map[string]interface{})
  288. filedType := ""
  289. if conditionType == "industry" {
  290. filedType = "2"
  291. } else {
  292. filedType = "3"
  293. }
  294. data := IC.BiMysql.SelectBySql(`SELECT
  295. DISTINCT b.name,b.code as code
  296. FROM
  297. customer_data_yys_permissions a
  298. INNER JOIN d_yys_analyze_dimensions b
  299. on
  300. a.position_id=?
  301. and a.ent_id = ?
  302. and a.is_delete = 0
  303. AND a.element_name = ?
  304. and FIND_IN_SET( b.code,a.element_value)
  305. inner join d_yys_dimensions_order c on b.name=c.name and b.type= c.type and b.type=?
  306. order by c.id `, positionId, entId, conditionName, filedType)
  307. options := []map[string]interface{}{}
  308. if data == nil {
  309. return options, dataMap
  310. }
  311. if len(*data) == 0 {
  312. data = IC.BiMysql.SelectBySql(`SELECT
  313. DISTINCT a.name,a.code
  314. FROM
  315. d_yys_analyze_dimensions a
  316. inner join d_yys_dimensions_order b on a.name=b.name and a.type=b.type and
  317. a.ent_id = ?
  318. and a.type=? order by b.id`, entId, filedType)
  319. }
  320. if data != nil && len(*data) > 0 {
  321. options = append(options, map[string]interface{}{
  322. "label": "全部",
  323. "value": "",
  324. })
  325. for _, v := range *data {
  326. name := gconv.String(v["name"])
  327. code := gconv.Int64(v["code"])
  328. options = append(options, map[string]interface{}{
  329. "label": name,
  330. "value": code,
  331. })
  332. dataMap[gconv.String(code)] = name
  333. }
  334. }
  335. return options, dataMap
  336. }
  337. func getLabel(entId int64) []map[string]interface{} {
  338. // 私有标签查询
  339. data := []map[string]interface{}{}
  340. labelArr := IC.BiMysql.SelectBySql(`SELECT * FROM private_label WHERE ent_id = ? AND level > 0 ORDER BY id`, entId)
  341. if labelArr != nil && len(*labelArr) != 0 {
  342. // 使用切片来存储一级标签及其对应的二级、三级标签
  343. labelIdArr := []int64{}
  344. labels := make(map[int64]map[string]interface{}) // 存储各级标签
  345. labelOptions := make(map[int64][]map[string]interface{}) // 存储各级标签的选项
  346. for _, m := range *labelArr {
  347. key := "tagname"
  348. switch len(labels) {
  349. case 1:
  350. key = "tagname1"
  351. case 2:
  352. key = "tagname2"
  353. }
  354. level := gconv.Int64(m["level"])
  355. name := gconv.String(m["name"])
  356. pid := gconv.Int64(m["pid"]) // 将 pid 转换为 int64
  357. id := gconv.Int64(m["id"]) // 将 id 转换为 int64
  358. if level == 1 {
  359. labelIdArr = append(labelIdArr, id)
  360. // 处理一级标签
  361. labels[id] = map[string]interface{}{
  362. "id": id,
  363. "key": key,
  364. "type": "selectList",
  365. "label": name,
  366. "defaultVal": []string{""},
  367. "props": map[string]interface{}{
  368. "multiple": true,
  369. },
  370. "options": []map[string]interface{}{}, // 初始化选项
  371. }
  372. } else {
  373. // 处理二级和三级标签
  374. if _, exists := labels[pid]; exists {
  375. if len(labelOptions[pid]) == 0 {
  376. labelOptions[pid] = append(labelOptions[pid], map[string]interface{}{
  377. "label": "全部",
  378. "value": "",
  379. })
  380. }
  381. labelOptions[pid] = append(labelOptions[pid], map[string]interface{}{
  382. "label": name,
  383. "value": name,
  384. })
  385. }
  386. }
  387. }
  388. // 将标签及其选项添加到最终数据中
  389. for _, m := range labelIdArr {
  390. for _, label := range labels {
  391. id := gconv.Int64(label["id"])
  392. if id == m {
  393. if options, exists := labelOptions[label["id"].(int64)]; exists && len(options) > 0 {
  394. label["options"] = options
  395. }
  396. data = append(data, label)
  397. }
  398. }
  399. }
  400. }
  401. return data
  402. }
  403. func getArea(entid, positionId int64, labelArr *[]map[string]interface{}) map[string]interface{} {
  404. data := make(map[string]map[string][]string)
  405. // 私有标签查询
  406. areaArr := IC.BiMysql.SelectBySql(`
  407. SELECT
  408. t0.area, t0.city, t0.district
  409. FROM
  410. bi_service.customer_data t0
  411. INNER JOIN (
  412. SELECT
  413. ent_id AS 企业_ID,
  414. MAX(CASE WHEN element_name = '行业' THEN element_value END) AS 行业要素值,
  415. MAX(CASE WHEN element_name = '运营商中标标签' THEN element_value END) AS 中标标签要素值,
  416. MAX(CASE WHEN element_name = '地区' THEN element_value END) AS 地区要素值,
  417. MAX(CASE WHEN element_name = '经营单位' THEN element_value END) AS 经营单位要素值
  418. FROM
  419. bi_service.customer_data_yys_permissions
  420. WHERE
  421. is_delete = 0 and position_id=? and ent_id=?
  422. ) t1 ON t0.eid = t1.企业_ID
  423. WHERE
  424. (t1.行业要素值 IS NULL OR FIND_IN_SET(t0.INDUSTRY, t1.行业要素值)) AND
  425. (t1.地区要素值 IS NULL OR FIND_IN_SET(t0.REGION, t1.地区要素值)) AND
  426. (t1.中标标签要素值 IS NULL OR FIND_IN_SET(t0.WINNER_TAG, t1.中标标签要素值)) AND
  427. (t1.经营单位要素值 IS NULL OR FIND_IN_SET(t0.tagname2, t1.经营单位要素值)) AND
  428. (t0.STATUS = 1)
  429. GROUP BY area, city, district`, positionId, entid)
  430. if areaArr == nil || len(*areaArr) == 0 {
  431. //查找全局变量
  432. areaArr = IC.BiMysql.SelectBySql(`
  433. SELECT
  434. area,
  435. city,
  436. district
  437. FROM
  438. customer_data
  439. WHERE
  440. eid = ?
  441. GROUP BY
  442. area,
  443. city,
  444. district
  445. ORDER BY
  446. area,
  447. city,
  448. district;`, entid)
  449. }
  450. if areaArr != nil && len(*areaArr) > 0 {
  451. for _, m := range *areaArr {
  452. area := gconv.String(m["area"])
  453. city := gconv.String(m["city"])
  454. district := gconv.String(m["district"])
  455. if area == "" || city == "" {
  456. continue // 跳过无效的区域或城市
  457. }
  458. if _, exists := data[area]; !exists {
  459. data[area] = make(map[string][]string)
  460. }
  461. if district != "" {
  462. data[area][city] = append(data[area][city], district)
  463. } else {
  464. data[area][city] = []string{} // 存储空数组以表示没有区
  465. }
  466. }
  467. }
  468. isCity, isDistrict := false, false
  469. for _, v := range *labelArr {
  470. configName := gconv.String(v["config_name"])
  471. if configName == "城市" {
  472. isCity = true
  473. } else if configName == "区县" {
  474. isDistrict = true
  475. }
  476. }
  477. if !isCity {
  478. //没有设置市区
  479. for k := range data {
  480. data[k] = map[string][]string{}
  481. }
  482. } else if !isDistrict {
  483. //没有设置区县
  484. for k, v := range data {
  485. for k1 := range v {
  486. v[k1] = []string{}
  487. }
  488. data[k] = v
  489. }
  490. }
  491. if len(data) > 0 {
  492. return map[string]interface{}{
  493. "key": "province",
  494. "type": "component-popup-area",
  495. "label": "城市",
  496. "defaultVal": "",
  497. "options": data,
  498. }
  499. }
  500. return map[string]interface{}{}
  501. }
  502. type Operator struct {
  503. UserId string
  504. EntId int64 //企业id 没有企业 企业id=0
  505. Tag int64
  506. PageNum int64 //当前页码
  507. PageSize int64 //每页数量
  508. Province string //省份
  509. TopType string //信息类型-二级
  510. PublishTime string //发布时间
  511. SelectType string //搜索范围:标题;正文等
  512. Price string //价格
  513. Tagname string
  514. Tagname1 string
  515. Tagname2 string
  516. WordsMode int64 //搜索关键词模式;默认0:包含所有,1:包含任意
  517. KeyWords string //关键词:多个空格隔开(主)
  518. PurchaseTime string //采购时间
  519. OpeningTime string //开标时间
  520. Industry string
  521. WinnerTag string
  522. PositionId int64
  523. Order int64
  524. }
  525. func SearchList(operator Operator) (int64, *[]map[string]interface{}, int64) {
  526. if operator.PageSize == 0 {
  527. operator.PageSize = 10
  528. }
  529. start := (operator.PageNum - 1) * operator.PageSize
  530. if start < 0 {
  531. start = 0
  532. }
  533. var query = `{"query":{"bool":{"must":[%s],"filter":[%s],"minimum_should_match":1,
  534. "should":[{"exists":{"field":"bidamount"}},{"bool":{"should":[{"exists":{"field":"budget"}},{"bool":{"must_not":[{"exists":{"field":"budget"}}]}}],"must_not":[{"exists":{"field":"bidamount"}}]}}]}},"from":"%d","size":"%d","sort":[%s]
  535. }`
  536. shouldStr := `{"bool":{"should":[%s]}}`
  537. mustArr := []string{}
  538. filterArr := []string{
  539. fmt.Sprintf(`{"match":{"ent_id":"%s"}}`, gconv.String(operator.EntId)),
  540. }
  541. //must处理
  542. //filter处理
  543. if operator.Tag == 1 {
  544. filterArr = append(filterArr, `{"match":{"toptype":"采购意向"}}`)
  545. operator.TopType = ""
  546. } else if operator.Tag == 2 {
  547. mustArr = append(mustArr, `{"exists":{"field":"bidopentime"}}`)
  548. operator.TopType = ""
  549. }
  550. if operator.PublishTime != "" {
  551. pushTime := strings.Split(operator.PublishTime, "_")
  552. for k, v := range pushTime {
  553. if k == 0 {
  554. if v != "" {
  555. filterArr = append(filterArr, fmt.Sprintf(`{"range":{"publishtime":{"gte":"%s"}}}`, v))
  556. }
  557. }
  558. if k == 1 {
  559. if v != "" {
  560. filterArr = append(filterArr, fmt.Sprintf(`{"range":{"publishtime":{"lt":"%s"}}}`, v))
  561. }
  562. }
  563. }
  564. }
  565. //采购时间
  566. if operator.PurchaseTime != "" {
  567. purchaseTime := strings.Split(operator.PurchaseTime, "_")
  568. for k, v := range purchaseTime {
  569. if k == 0 {
  570. if v != "" {
  571. filterArr = append(filterArr, fmt.Sprintf(`{"range":{"expurasingtime":{"gte":"%s"}}}`, v))
  572. }
  573. }
  574. if k == 1 {
  575. if v != "" {
  576. filterArr = append(filterArr, fmt.Sprintf(`{"range":{"expurasingtime":{"lt":"%s"}}}`, v))
  577. }
  578. }
  579. }
  580. }
  581. //开标时间
  582. if operator.OpeningTime != "" {
  583. openingTime := strings.Split(operator.OpeningTime, "_")
  584. for k, v := range openingTime {
  585. if k == 0 {
  586. if v != "" {
  587. filterArr = append(filterArr, fmt.Sprintf(`{"range":{"bidopentime":{"gte":"%s"}}}`, v))
  588. }
  589. }
  590. if k == 1 {
  591. if v != "" {
  592. filterArr = append(filterArr, fmt.Sprintf(`{"range":{"bidopentime":{"lt":"%s"}}}`, v))
  593. }
  594. }
  595. }
  596. }
  597. //关键词
  598. if operator.KeyWords != "" {
  599. go keyHandle(operator.KeyWords, operator.UserId)
  600. if operator.WordsMode == 1 {
  601. keyArr := []string{}
  602. for _, v := range strings.Split(operator.KeyWords, ",") {
  603. keyArr = append(keyArr, fmt.Sprintf(`{"match_phrase":{"%s":"%s"}}`, operator.SelectType, v))
  604. }
  605. mustArr = append(mustArr, fmt.Sprintf(shouldStr, strings.Join(keyArr, ",")))
  606. } else {
  607. for _, v := range strings.Split(operator.KeyWords, ",") {
  608. filterArr = append(filterArr, fmt.Sprintf(`{"match_phrase":{"%s":"%s"}}`, operator.SelectType, v))
  609. }
  610. }
  611. }
  612. //信息类型一级
  613. //{"match":{"toptype":"结果"}},{"terms":{"subtype":["结果变更","中标"]}}
  614. topType := []string{}
  615. subtype := []string{}
  616. if operator.TopType != "" {
  617. for k, v := range gconv.Map(operator.TopType) {
  618. topType = append(topType, k)
  619. for _, v1 := range gconv.Interfaces(v) {
  620. subtype = append(subtype, gconv.String(v1))
  621. }
  622. }
  623. if len(topType) == 1 {
  624. if len(topType) > 0 {
  625. filterArr = append(filterArr, fmt.Sprintf(`{"match":{"toptype":"%s"}}`, strings.Join(topType, `","`)))
  626. }
  627. //信息类型二级
  628. if len(subtype) > 0 {
  629. filterArr = append(filterArr, fmt.Sprintf(`{"terms":{"subtype":["%s"]}}`, strings.Join(subtype, `","`)))
  630. }
  631. }
  632. }
  633. //私有标签1
  634. if operator.Tagname != "" {
  635. keyArr := []string{}
  636. for _, v := range strings.Split(operator.Tagname, ",") {
  637. keyArr = append(keyArr, fmt.Sprintf(`{"wildcard":{"tagname":"*%s"}}`, v))
  638. }
  639. mustArr = append(mustArr, fmt.Sprintf(shouldStr, strings.Join(keyArr, ",")))
  640. }
  641. //私有标签2
  642. if operator.Tagname1 != "" {
  643. keyArr := []string{}
  644. for _, v := range strings.Split(operator.Tagname1, ",") {
  645. keyArr = append(keyArr, fmt.Sprintf(`{"wildcard":{"tagname1":"*%s*"}}`, v))
  646. }
  647. mustArr = append(mustArr, fmt.Sprintf(shouldStr, strings.Join(keyArr, ",")))
  648. }
  649. //私有标签3
  650. if operator.Tagname2 != "" {
  651. keyArr := []string{}
  652. for _, v := range strings.Split(operator.Tagname2, ",") {
  653. keyArr = append(keyArr, fmt.Sprintf(`{"wildcard":{"tagname2":"%s"}}`, v))
  654. }
  655. mustArr = append(mustArr, fmt.Sprintf(shouldStr, strings.Join(keyArr, ",")))
  656. }
  657. //价格处理
  658. if operator.Price != "" {
  659. priceArr := strings.Split(operator.Price, "_")
  660. minPrice := gconv.Int64(priceArr[0]) * 10000
  661. maxPrice := gconv.Int64(priceArr[1]) * 10000
  662. bidamountArr := []string{`
  663. {
  664. "exists": {
  665. "field": "bidamount"
  666. }
  667. }`, fmt.Sprintf(`
  668. {
  669. "range": {
  670. "bidamount": {
  671. "gte": "%s"
  672. }
  673. }
  674. }
  675. `, gconv.String(minPrice))}
  676. budgetArr := []string{`
  677. {
  678. "exists": {
  679. "field": "budget"
  680. }
  681. }`, fmt.Sprintf(`
  682. {
  683. "range": {
  684. "budget": {
  685. "gte": "%s"
  686. }
  687. }
  688. }
  689. `, gconv.String(minPrice))}
  690. if maxPrice > 0 {
  691. bidamountArr = append(bidamountArr, fmt.Sprintf(`
  692. {
  693. "range": {
  694. "bidamount": {
  695. "lte": "%s"
  696. }
  697. }
  698. }
  699. `, gconv.String(maxPrice)))
  700. budgetArr = append(budgetArr, fmt.Sprintf(`
  701. {
  702. "range": {
  703. "budget": {
  704. "lte": "%s"
  705. }
  706. }
  707. }
  708. `, gconv.String(maxPrice)))
  709. }
  710. mustArr = append(mustArr, fmt.Sprintf(`{"bool":{"should":[{"bool":{"filter":[%s]}},{"bool":{"filter":[%s],"must_not":[{"exists":{"field":"bidamount"}}]}}]}}`, strings.Join(bidamountArr, ","), strings.Join(budgetArr, ",")))
  711. }
  712. //地区搜索
  713. area := []string{}
  714. city := []string{}
  715. district := []string{}
  716. if operator.Province != "" {
  717. for k, v := range gconv.Map(operator.Province) {
  718. area = append(area, gconv.String(k))
  719. for k1, v1 := range gconv.Map(v) {
  720. city = append(city, k1)
  721. for _, v3 := range gconv.SliceStr(v1) {
  722. district = append(district, v3)
  723. }
  724. }
  725. }
  726. if len(area) > 0 {
  727. query1 := ""
  728. query1 += `{"terms":{"area":[`
  729. for k, v := range area {
  730. if k > 0 {
  731. query1 += `,`
  732. }
  733. query1 += `"` + v + `"`
  734. }
  735. query1 += `]}}`
  736. filterArr = append(filterArr, query1)
  737. }
  738. //市--未登录用户不能根据市和地区筛选
  739. if len(city) > 0 {
  740. query1 := ""
  741. query1 += `{"terms":{"city":[`
  742. for k, v := range city {
  743. if k > 0 {
  744. query1 += `,`
  745. }
  746. query1 += `"` + v + `"`
  747. }
  748. query1 += `]}}`
  749. filterArr = append(filterArr, query1)
  750. }
  751. if len(district) > 0 {
  752. query1 := ""
  753. query1 += `{"terms":{"district":[`
  754. for k, v := range district {
  755. if k > 0 {
  756. query1 += `,`
  757. }
  758. query1 += `"` + v + `"`
  759. }
  760. query1 += `]}}`
  761. filterArr = append(filterArr, query1)
  762. }
  763. }
  764. //行业处理
  765. if operator.Industry != "" {
  766. filterArr = append(filterArr, fmt.Sprintf(`{"terms":{"industry":["%s"]}}`, strings.ReplaceAll(operator.Industry, ",", `","`)))
  767. }
  768. //中标人标签处理
  769. if operator.WinnerTag != "" {
  770. filterArr = append(filterArr, fmt.Sprintf(`{"terms":{"winner_tag":["%s"]}}`, strings.ReplaceAll(operator.WinnerTag, ",", `","`)))
  771. }
  772. //查看自己是否定制
  773. configData := IC.BiMysql.SelectBySql(`SELECT
  774. MAX( CASE WHEN element_name = '行业' THEN element_value END ) AS industry,
  775. MAX( CASE WHEN element_name = '运营商中标标签' THEN element_value END ) AS winner_tag,
  776. MAX( CASE WHEN element_name = '地区' THEN element_value END ) AS region,
  777. MAX( CASE WHEN element_name = '经营单位' THEN element_value END ) AS tagname
  778. FROM
  779. bi_service.customer_data_yys_permissions
  780. WHERE
  781. is_delete = 0
  782. AND position_id = ?
  783. AND ent_id = ?`, operator.PositionId, operator.EntId)
  784. if configData != nil && len(*configData) > 0 {
  785. tagname := gconv.String((*configData)[0]["tagname"])
  786. region := gconv.String((*configData)[0]["region"])
  787. winner_tag := gconv.String((*configData)[0]["winner_tag"])
  788. industry := gconv.String((*configData)[0]["industry"])
  789. if tagname != "" {
  790. filterArr = append(filterArr, fmt.Sprintf(`{"terms":{"tagname":["%s"]}}`, strings.Join(strings.Split(tagname, ","), `","`)))
  791. }
  792. if region != "" {
  793. filterArr = append(filterArr, fmt.Sprintf(`{"terms":{"region":["%s"]}}`, strings.Join(strings.Split(region, ","), `","`)))
  794. }
  795. if winner_tag != "" {
  796. filterArr = append(filterArr, fmt.Sprintf(`{"terms":{"winner_tag":["%s"]}}`, strings.Join(strings.Split(winner_tag, ","), `","`)))
  797. }
  798. if industry != "" {
  799. filterArr = append(filterArr, fmt.Sprintf(`{"terms":{"industry":["%s"]}}`, strings.Join(strings.Split(industry, ","), `","`)))
  800. }
  801. }
  802. //es sql拼接
  803. queryStr := ""
  804. orderStr := ""
  805. mysqlOrderStr := ""
  806. if operator.Tag == 0 {
  807. if operator.Order == 0 {
  808. orderStr = `{"publishtime":{"order":"desc"}}`
  809. mysqlOrderStr = ` order by a.publishtime desc`
  810. } else {
  811. orderStr = `{"publishtime":{"order":"asc"}}`
  812. mysqlOrderStr = ` order by a.publishtime asc`
  813. }
  814. } else if operator.Tag == 1 {
  815. if operator.Order == 0 {
  816. orderStr = `{"publishtime":{"order":"desc"}}`
  817. mysqlOrderStr = ` order by a.publishtime desc`
  818. } else {
  819. orderStr = `{"expurasingtime":{"order":"asc"}}`
  820. mysqlOrderStr = ` order by a.expurasingtime asc`
  821. }
  822. } else if operator.Tag == 2 {
  823. if operator.Order == 0 {
  824. orderStr = `{"publishtime":{"order":"desc"}}`
  825. mysqlOrderStr = ` order by a.publishtime desc`
  826. } else {
  827. orderStr = `{"bidopentime":{"order":"asc"}}`
  828. mysqlOrderStr = ` order by a.bidopentime asc`
  829. }
  830. }
  831. queryStr = fmt.Sprintf(query, strings.Join(mustArr, ","), strings.Join(filterArr, ","), start, operator.PageSize, orderStr)
  832. fmt.Println(queryStr)
  833. start1 := time.Now().Unix()
  834. fmt.Println(start1)
  835. var count int64
  836. var data *[]map[string]interface{}
  837. count, data = elastic.GetWithCount(yysIndex, yysType, "", queryStr)
  838. end1 := time.Now().Unix()
  839. fmt.Println(end1)
  840. fmt.Println("时差", end1-start1)
  841. if len(*data) > 0 {
  842. //查询tidb真实数据
  843. idArr := []interface{}{gconv.String(operator.EntId)}
  844. wh := []string{}
  845. for _, v := range *data {
  846. id := gconv.String(v["id"])
  847. idArr = append(idArr, id)
  848. wh = append(wh, "?")
  849. }
  850. sqlStr := fmt.Sprintf(` SELECT
  851. a.*,b.encrypt_id
  852. FROM
  853. customer_data a
  854. LEFT JOIN customer_data_yys_project_winner b
  855. on a.eid=?
  856. and a.projectId =b.projectId
  857. and a.s_winner=b.winner_name
  858. where a.id in (%s)
  859. %s `, strings.Join(wh, ","), mysqlOrderStr)
  860. fmt.Println(sqlStr, idArr)
  861. data = IC.BiMysql.SelectBySql(sqlStr,
  862. idArr...)
  863. if data != nil && len(*data) > 0 {
  864. _, winnerMap := getConfiguration("运营商中标标签", "winner_tag", operator.EntId, operator.PositionId)
  865. _, industryMap := getConfiguration("行业", "industry", operator.EntId, operator.PositionId)
  866. fileArr := &[]map[string]interface{}{}
  867. if operator.Tag == 0 {
  868. fileArr = IC.BiMysql.SelectBySql("select * from field_order ")
  869. } else {
  870. fileArr = IC.BiMysql.SelectBySql("select * from field_order where FIND_IN_SET(?,config_class)", operator.Tag)
  871. }
  872. //中标单位处理
  873. for i, v := range *data {
  874. newData := map[string]interface{}{}
  875. v["publishtime"] = TimeHandle(gconv.String(v["publishtime"]))
  876. v["bidopentime"] = TimeHandle(gconv.String(v["bidopentime"]))
  877. v["expurasingtime"] = TimeHandle(gconv.String(v["expurasingtime"]))
  878. v["industry"] = industryMap[gconv.String(v["industry"])]
  879. v["winner_tag"] = winnerMap[gconv.String(v["winner_tag"])]
  880. v["bidendtime"] = TimeHandle(gconv.String(v["bidendtime"]))
  881. for _, m := range *fileArr {
  882. key := gconv.String(m["file"])
  883. newData[key] = v[key]
  884. }
  885. infoid := gconv.String(v["infoid"])
  886. newData["infoid"] = ME.EncodeArticleId2ByCheck(infoid)
  887. (*data)[i] = newData
  888. }
  889. }
  890. total := count
  891. count = gconv.Int64(common.If(count > 2000, 2000, count))
  892. return count, data, total
  893. } else {
  894. return count, &[]map[string]interface{}{}, 0
  895. }
  896. }
  897. func TimeHandle(timeStr string) int64 {
  898. if timeStr == "" {
  899. return 0
  900. }
  901. // 定义一个布局,需要与时间字符串格式相匹配
  902. layout := "2006-01-02 15:04:05"
  903. // 使用Parse将字符串转换为time.Time类型
  904. t, err := time.Parse(layout, timeStr)
  905. if err != nil {
  906. fmt.Println(err)
  907. return 0
  908. }
  909. // 将time.Time类型转换为时间戳(单位:秒)
  910. return t.Unix()
  911. }
  912. func keyHandle(keys, userId string) {
  913. //历史记录
  914. history := redis.GetStr("other", "s_"+userId)
  915. keyArr := SearchHistory(history, keys)
  916. if len(keyArr) > 0 {
  917. if b := redis.Put("other", "s_"+userId, strings.Join(keyArr, ","), -1); !b {
  918. log.Println("保存搜索记录异常,用户id:", userId)
  919. }
  920. }
  921. }
  922. // SearchHistory 格式化 关键词搜索历史记录
  923. func SearchHistory(history, searchValue string) (arrS []string) {
  924. //主关键词
  925. var searchKeys = strings.Split(searchValue, IC.C.JYKeyMark)
  926. //附加词
  927. //关键词 和 附加词 合并,作为新的关键词历史搜索记录
  928. if len(searchKeys) > 0 {
  929. arrS = strings.Split(history, ",")
  930. //新增历史记录
  931. if history == "" {
  932. arrS = make([]string, 0)
  933. }
  934. for _, sv := range searchKeys {
  935. for k, v := range arrS {
  936. if v == strings.TrimSpace(sv) {
  937. arrS = append(arrS[:k], arrS[k+1:]...)
  938. break
  939. }
  940. }
  941. }
  942. arrS = append(arrS, searchKeys...)
  943. if len(arrS) > 10 {
  944. arrS = arrS[len(arrS)-10:]
  945. }
  946. }
  947. return arrS
  948. }