operator.go 30 KB

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