operator.go 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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)
  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)
  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. data := IC.BiMysql.SelectBySql(`SELECT
  289. DISTINCT b.name,b.element_value as code
  290. FROM
  291. customer_data_yys_permissions a INNER JOIN customer_data_yys_permission_elements b
  292. on
  293. a.position_id=?
  294. and a.ent_id = ?
  295. and a.is_delete = 0
  296. AND a.element_name = ?
  297. and FIND_IN_SET( b.element_value,a.element_value)
  298. and b.element_field=? order by code `, positionId, entId, conditionName, conditionType)
  299. options := []map[string]interface{}{}
  300. if data == nil {
  301. return options, dataMap
  302. }
  303. if len(*data) == 0 {
  304. data = IC.BiMysql.SelectBySql(`SELECT
  305. DISTINCT name,element_value as code
  306. FROM
  307. customer_data_yys_permission_elements where
  308. ent_id = ?
  309. and element_field=? and pcode!="" order by code`, entId, conditionType)
  310. }
  311. if data != nil && len(*data) > 0 {
  312. options = append(options, map[string]interface{}{
  313. "label": "全部",
  314. "value": "",
  315. })
  316. for _, v := range *data {
  317. name := gconv.String(v["name"])
  318. code := gconv.Int64(v["code"])
  319. options = append(options, map[string]interface{}{
  320. "label": name,
  321. "value": code,
  322. })
  323. dataMap[gconv.String(code)] = name
  324. }
  325. }
  326. return options, dataMap
  327. }
  328. func getLabel(entId int64) []map[string]interface{} {
  329. // 私有标签查询
  330. data := []map[string]interface{}{}
  331. labelArr := IC.BiMysql.SelectBySql(`SELECT * FROM private_label WHERE ent_id = ? AND level > 0 ORDER BY id`, entId)
  332. if labelArr != nil && len(*labelArr) != 0 {
  333. // 使用切片来存储一级标签及其对应的二级、三级标签
  334. labelIdArr := []int64{}
  335. labels := make(map[int64]map[string]interface{}) // 存储各级标签
  336. labelOptions := make(map[int64][]map[string]interface{}) // 存储各级标签的选项
  337. for _, m := range *labelArr {
  338. key := "tagname"
  339. switch len(labels) {
  340. case 1:
  341. key = "tagname1"
  342. case 2:
  343. key = "tagname2"
  344. }
  345. level := gconv.Int64(m["level"])
  346. name := gconv.String(m["name"])
  347. pid := gconv.Int64(m["pid"]) // 将 pid 转换为 int64
  348. id := gconv.Int64(m["id"]) // 将 id 转换为 int64
  349. if level == 1 {
  350. labelIdArr = append(labelIdArr, id)
  351. // 处理一级标签
  352. labels[id] = map[string]interface{}{
  353. "id": id,
  354. "key": key,
  355. "type": "selectList",
  356. "label": name,
  357. "defaultVal": []string{""},
  358. "props": map[string]interface{}{
  359. "multiple": true,
  360. },
  361. "options": []map[string]interface{}{}, // 初始化选项
  362. }
  363. } else {
  364. // 处理二级和三级标签
  365. if _, exists := labels[pid]; exists {
  366. if len(labelOptions[pid]) == 0 {
  367. labelOptions[pid] = append(labelOptions[pid], map[string]interface{}{
  368. "label": "全部",
  369. "value": "",
  370. })
  371. }
  372. labelOptions[pid] = append(labelOptions[pid], map[string]interface{}{
  373. "label": name,
  374. "value": name,
  375. })
  376. }
  377. }
  378. }
  379. // 将标签及其选项添加到最终数据中
  380. for _, m := range labelIdArr {
  381. for _, label := range labels {
  382. id := gconv.Int64(label["id"])
  383. if id == m {
  384. if options, exists := labelOptions[label["id"].(int64)]; exists && len(options) > 0 {
  385. label["options"] = options
  386. }
  387. data = append(data, label)
  388. }
  389. }
  390. }
  391. }
  392. return data
  393. }
  394. func getArea(entid, positionId int64, labelArr *[]map[string]interface{}) map[string]interface{} {
  395. data := make(map[string]map[string][]string)
  396. // 私有标签查询
  397. areaArr := IC.BiMysql.SelectBySql(`
  398. SELECT
  399. t0.area, t0.city, t0.district
  400. FROM
  401. bi_service.customer_data t0
  402. INNER JOIN (
  403. SELECT
  404. ent_id AS 企业_ID,
  405. MAX(CASE WHEN element_name = '行业' THEN element_value END) AS 行业要素值,
  406. MAX(CASE WHEN element_name = '运营商中标标签' THEN element_value END) AS 中标标签要素值,
  407. MAX(CASE WHEN element_name = '地区' THEN element_value END) AS 地区要素值,
  408. MAX(CASE WHEN element_name = '经营单位' THEN element_value END) AS 经营单位要素值
  409. FROM
  410. bi_service.customer_data_yys_permissions
  411. WHERE
  412. is_delete = 0 and position_id=? and ent_id=?
  413. ) t1 ON t0.eid = t1.企业_ID
  414. WHERE
  415. (t1.行业要素值 IS NULL OR FIND_IN_SET(t0.INDUSTRY, t1.行业要素值)) AND
  416. (t1.地区要素值 IS NULL OR FIND_IN_SET(t0.REGION, t1.地区要素值)) AND
  417. (t1.中标标签要素值 IS NULL OR FIND_IN_SET(t0.WINNER_TAG, t1.中标标签要素值)) AND
  418. (t1.经营单位要素值 IS NULL OR FIND_IN_SET(t0.tagname2, t1.经营单位要素值)) AND
  419. (t0.STATUS = 1)
  420. GROUP BY area, city, district`, positionId, entid)
  421. if areaArr == nil || len(*areaArr) == 0 {
  422. //查找全局变量
  423. areaArr = IC.BiMysql.SelectBySql(`
  424. SELECT
  425. area,
  426. city,
  427. district
  428. FROM
  429. customer_data
  430. WHERE
  431. eid = ?
  432. GROUP BY
  433. area,
  434. city,
  435. district
  436. ORDER BY
  437. area,
  438. city,
  439. district;`, entid)
  440. }
  441. if areaArr != nil && len(*areaArr) > 0 {
  442. for _, m := range *areaArr {
  443. area := gconv.String(m["area"])
  444. city := gconv.String(m["city"])
  445. district := gconv.String(m["district"])
  446. if area == "" || city == "" {
  447. continue // 跳过无效的区域或城市
  448. }
  449. if _, exists := data[area]; !exists {
  450. data[area] = make(map[string][]string)
  451. }
  452. if district != "" {
  453. data[area][city] = append(data[area][city], district)
  454. } else {
  455. data[area][city] = []string{} // 存储空数组以表示没有区
  456. }
  457. }
  458. }
  459. isCity, isDistrict := false, false
  460. for _, v := range *labelArr {
  461. configName := gconv.String(v["config_name"])
  462. if configName == "城市" {
  463. isCity = true
  464. } else if configName == "区县" {
  465. isDistrict = true
  466. }
  467. }
  468. if !isCity {
  469. //没有设置市区
  470. for k := range data {
  471. data[k] = map[string][]string{}
  472. }
  473. } else if !isDistrict {
  474. //没有设置区县
  475. for k, v := range data {
  476. for k1 := range v {
  477. v[k1] = []string{}
  478. }
  479. data[k] = v
  480. }
  481. }
  482. if len(data) > 0 {
  483. return map[string]interface{}{
  484. "key": "province",
  485. "type": "component-popup-area",
  486. "label": "城市",
  487. "defaultVal": "",
  488. "options": data,
  489. }
  490. }
  491. return map[string]interface{}{}
  492. }
  493. type Operator struct {
  494. UserId string
  495. EntId int64 //企业id 没有企业 企业id=0
  496. Tag int64
  497. PageNum int64 //当前页码
  498. PageSize int64 //每页数量
  499. Province string //省份
  500. TopType string //信息类型-二级
  501. PublishTime string //发布时间
  502. SelectType string //搜索范围:标题;正文等
  503. Price string //价格
  504. Tagname string
  505. Tagname1 string
  506. Tagname2 string
  507. WordsMode int64 //搜索关键词模式;默认0:包含所有,1:包含任意
  508. KeyWords string //关键词:多个空格隔开(主)
  509. PurchaseTime string //采购时间
  510. OpeningTime string //开标时间
  511. Industry string
  512. WinnerTag string
  513. PositionId int64
  514. Order int64
  515. }
  516. func SearchList(operator Operator) (int64, *[]map[string]interface{}, int64) {
  517. if operator.PageSize == 0 {
  518. operator.PageSize = 10
  519. }
  520. start := (operator.PageNum - 1) * operator.PageSize
  521. if start < 0 {
  522. start = 0
  523. }
  524. var query = `{"query":{"bool":{"must":[%s],"filter":[%s],"minimum_should_match":1,
  525. "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]
  526. }`
  527. shouldStr := `{"bool":{"should":[%s]}}`
  528. mustArr := []string{}
  529. filterArr := []string{
  530. fmt.Sprintf(`{"match":{"ent_id":"%s"}}`, gconv.String(operator.EntId)),
  531. }
  532. //must处理
  533. //filter处理
  534. if operator.Tag == 1 {
  535. filterArr = append(filterArr, `{"match":{"toptype":"采购意向"}}`)
  536. operator.TopType = ""
  537. } else if operator.Tag == 2 {
  538. mustArr = append(mustArr, `{"exists":{"field":"bidopentime"}}`)
  539. operator.TopType = ""
  540. }
  541. if operator.PublishTime != "" {
  542. pushTime := strings.Split(operator.PublishTime, "_")
  543. for k, v := range pushTime {
  544. if k == 0 {
  545. if v != "" {
  546. filterArr = append(filterArr, fmt.Sprintf(`{"range":{"publishtime":{"gte":"%s"}}}`, v))
  547. }
  548. }
  549. if k == 1 {
  550. if v != "" {
  551. filterArr = append(filterArr, fmt.Sprintf(`{"range":{"publishtime":{"lt":"%s"}}}`, v))
  552. }
  553. }
  554. }
  555. }
  556. //采购时间
  557. if operator.PurchaseTime != "" {
  558. purchaseTime := strings.Split(operator.PurchaseTime, "_")
  559. for k, v := range purchaseTime {
  560. if k == 0 {
  561. if v != "" {
  562. filterArr = append(filterArr, fmt.Sprintf(`{"range":{"expurasingtime":{"gte":"%s"}}}`, v))
  563. }
  564. }
  565. if k == 1 {
  566. if v != "" {
  567. filterArr = append(filterArr, fmt.Sprintf(`{"range":{"expurasingtime":{"lt":"%s"}}}`, v))
  568. }
  569. }
  570. }
  571. }
  572. //开标时间
  573. if operator.OpeningTime != "" {
  574. openingTime := strings.Split(operator.OpeningTime, "_")
  575. for k, v := range openingTime {
  576. if k == 0 {
  577. if v != "" {
  578. filterArr = append(filterArr, fmt.Sprintf(`{"range":{"bidopentime":{"gte":"%s"}}}`, v))
  579. }
  580. }
  581. if k == 1 {
  582. if v != "" {
  583. filterArr = append(filterArr, fmt.Sprintf(`{"range":{"bidopentime":{"lt":"%s"}}}`, v))
  584. }
  585. }
  586. }
  587. }
  588. //关键词
  589. if operator.KeyWords != "" {
  590. go keyHandle(operator.KeyWords, operator.UserId)
  591. if operator.WordsMode == 1 {
  592. keyArr := []string{}
  593. for _, v := range strings.Split(operator.KeyWords, ",") {
  594. keyArr = append(keyArr, fmt.Sprintf(`{"match_phrase":{"%s":"%s"}}`, operator.SelectType, v))
  595. }
  596. mustArr = append(mustArr, fmt.Sprintf(shouldStr, strings.Join(keyArr, ",")))
  597. } else {
  598. for _, v := range strings.Split(operator.KeyWords, ",") {
  599. filterArr = append(filterArr, fmt.Sprintf(`{"match_phrase":{"%s":"%s"}}`, operator.SelectType, v))
  600. }
  601. }
  602. }
  603. //信息类型一级
  604. //{"match":{"toptype":"结果"}},{"terms":{"subtype":["结果变更","中标"]}}
  605. topType := []string{}
  606. subtype := []string{}
  607. if operator.TopType != "" {
  608. for k, v := range gconv.Map(operator.TopType) {
  609. topType = append(topType, k)
  610. for _, v1 := range gconv.Interfaces(v) {
  611. subtype = append(subtype, gconv.String(v1))
  612. }
  613. }
  614. if len(topType) > 0 {
  615. filterArr = append(filterArr, fmt.Sprintf(`{"match":{"toptype":"%s"}}`, strings.Join(topType, `","`)))
  616. }
  617. //信息类型二级
  618. if len(subtype) > 0 {
  619. filterArr = append(filterArr, fmt.Sprintf(`{"terms":{"subtype":["%s"]}}`, strings.Join(subtype, `","`)))
  620. }
  621. }
  622. //私有标签1
  623. if operator.Tagname != "" {
  624. keyArr := []string{}
  625. for _, v := range strings.Split(operator.Tagname, ",") {
  626. keyArr = append(keyArr, fmt.Sprintf(`{"wildcard":{"tagname":"*%s"}}`, v))
  627. }
  628. mustArr = append(mustArr, fmt.Sprintf(shouldStr, strings.Join(keyArr, ",")))
  629. }
  630. //私有标签2
  631. if operator.Tagname1 != "" {
  632. keyArr := []string{}
  633. for _, v := range strings.Split(operator.Tagname1, ",") {
  634. keyArr = append(keyArr, fmt.Sprintf(`{"wildcard":{"tagname1":"*%s*"}}`, v))
  635. }
  636. mustArr = append(mustArr, fmt.Sprintf(shouldStr, strings.Join(keyArr, ",")))
  637. }
  638. //私有标签3
  639. if operator.Tagname2 != "" {
  640. keyArr := []string{}
  641. for _, v := range strings.Split(operator.Tagname2, ",") {
  642. keyArr = append(keyArr, fmt.Sprintf(`{"wildcard":{"tagname2":"%s"}}`, v))
  643. }
  644. mustArr = append(mustArr, fmt.Sprintf(shouldStr, strings.Join(keyArr, ",")))
  645. }
  646. //价格处理
  647. if operator.Price != "" {
  648. priceArr := strings.Split(operator.Price, "_")
  649. minPrice := gconv.Int64(priceArr[0]) * 10000
  650. maxPrice := gconv.Int64(priceArr[1]) * 10000
  651. bidamountArr := []string{`
  652. {
  653. "exists": {
  654. "field": "bidamount"
  655. }
  656. }`, fmt.Sprintf(`
  657. {
  658. "range": {
  659. "bidamount": {
  660. "gte": "%s"
  661. }
  662. }
  663. }
  664. `, gconv.String(minPrice))}
  665. budgetArr := []string{`
  666. {
  667. "exists": {
  668. "field": "budget"
  669. }
  670. }`, fmt.Sprintf(`
  671. {
  672. "range": {
  673. "budget": {
  674. "gte": "%s"
  675. }
  676. }
  677. }
  678. `, gconv.String(minPrice))}
  679. if maxPrice > 0 {
  680. bidamountArr = append(bidamountArr, fmt.Sprintf(`
  681. {
  682. "range": {
  683. "bidamount": {
  684. "lte": "%s"
  685. }
  686. }
  687. }
  688. `, gconv.String(maxPrice)))
  689. budgetArr = append(budgetArr, fmt.Sprintf(`
  690. {
  691. "range": {
  692. "budget": {
  693. "lte": "%s"
  694. }
  695. }
  696. }
  697. `, gconv.String(maxPrice)))
  698. }
  699. mustArr = append(mustArr, fmt.Sprintf(`{"bool":{"should":[{"bool":{"filter":[%s]}},{"bool":{"filter":[%s],"must_not":[{"exists":{"field":"bidamount"}}]}}]}}`, strings.Join(bidamountArr, ","), strings.Join(budgetArr, ",")))
  700. }
  701. //地区搜索
  702. area := []string{}
  703. city := []string{}
  704. district := []string{}
  705. if operator.Province != "" {
  706. for k, v := range gconv.Map(operator.Province) {
  707. area = append(area, gconv.String(k))
  708. for k1, v1 := range gconv.Map(v) {
  709. city = append(city, k1)
  710. for _, v3 := range gconv.SliceStr(v1) {
  711. district = append(district, v3)
  712. }
  713. }
  714. }
  715. if len(area) > 0 {
  716. query1 := ""
  717. query1 += `{"terms":{"area":[`
  718. for k, v := range area {
  719. if k > 0 {
  720. query1 += `,`
  721. }
  722. query1 += `"` + v + `"`
  723. }
  724. query1 += `]}}`
  725. filterArr = append(filterArr, query1)
  726. }
  727. //市--未登录用户不能根据市和地区筛选
  728. if len(city) > 0 {
  729. query1 := ""
  730. query1 += `{"terms":{"city":[`
  731. for k, v := range city {
  732. if k > 0 {
  733. query1 += `,`
  734. }
  735. query1 += `"` + v + `"`
  736. }
  737. query1 += `]}}`
  738. filterArr = append(filterArr, query1)
  739. }
  740. if len(district) > 0 {
  741. query1 := ""
  742. query1 += `{"terms":{"district":[`
  743. for k, v := range district {
  744. if k > 0 {
  745. query1 += `,`
  746. }
  747. query1 += `"` + v + `"`
  748. }
  749. query1 += `]}}`
  750. filterArr = append(filterArr, query1)
  751. }
  752. }
  753. //行业处理
  754. if operator.Industry != "" {
  755. filterArr = append(filterArr, fmt.Sprintf(`{"terms":{"industry":["%s"]}}`, strings.ReplaceAll(operator.Industry, ",", `","`)))
  756. }
  757. //中标人标签处理
  758. if operator.WinnerTag != "" {
  759. filterArr = append(filterArr, fmt.Sprintf(`{"terms":{"winner_tag":["%s"]}}`, strings.ReplaceAll(operator.WinnerTag, ",", `","`)))
  760. }
  761. //查看自己是否定制
  762. /*configData := IC.BiMysql.SelectBySql(`SELECT
  763. MAX( CASE WHEN element_name = '行业' THEN element_value END ) AS industry,
  764. MAX( CASE WHEN element_name = '运营商中标标签' THEN element_value END ) AS winner_tag,
  765. MAX( CASE WHEN element_name = '地区' THEN element_value END ) AS region,
  766. MAX( CASE WHEN element_name = '经营单位' THEN element_value END ) AS tagname
  767. FROM
  768. bi_service.customer_data_yys_permissions
  769. WHERE
  770. is_delete = 0
  771. AND position_id = ?
  772. AND ent_id = ?`, operator.PositionId, operator.EntId)
  773. if configData != nil && len(*configData) > 0 {
  774. tagname := gconv.String((*configData)[0]["tagname"])
  775. region := gconv.String((*configData)[0]["region"])
  776. winner_tag := gconv.String((*configData)[0]["winner_tag"])
  777. industry := gconv.String((*configData)[0]["industry"])
  778. if tagname != "" {
  779. filterArr = append(filterArr, fmt.Sprintf(`{"terms":{"tagname":["%s"]}}`, strings.Join(strings.Split(tagname, ","), `","`)))
  780. }
  781. if region != "" {
  782. filterArr = append(filterArr, fmt.Sprintf(`{"terms":{"region":["%s"]}}`, strings.Join(strings.Split(region, ","), `","`)))
  783. }
  784. if winner_tag != "" {
  785. filterArr = append(filterArr, fmt.Sprintf(`{"terms":{"winner_tag":["%s"]}}`, strings.Join(strings.Split(winner_tag, ","), `","`)))
  786. }
  787. if industry != "" {
  788. filterArr = append(filterArr, fmt.Sprintf(`{"terms":{"industry":["%s"]}}`, strings.Join(strings.Split(industry, ","), `","`)))
  789. }
  790. }*/
  791. //es sql拼接
  792. queryStr := ""
  793. orderStr := ""
  794. mysqlOrderStr := ""
  795. if operator.Tag == 0 {
  796. if operator.Order == 0 {
  797. orderStr = `{"publishtime":{"order":"desc"}}`
  798. mysqlOrderStr = ` order by a.publishtime desc`
  799. } else {
  800. orderStr = `{"publishtime":{"order":"asc"}}`
  801. mysqlOrderStr = ` order by a.publishtime asc`
  802. }
  803. } else if operator.Tag == 1 {
  804. if operator.Order == 0 {
  805. orderStr = `{"publishtime":{"order":"desc"}}`
  806. mysqlOrderStr = ` order by a.publishtime desc`
  807. } else {
  808. orderStr = `{"expurasingtime":{"order":"asc"}}`
  809. mysqlOrderStr = ` order by a.expurasingtime asc`
  810. }
  811. } else if operator.Tag == 2 {
  812. if operator.Order == 0 {
  813. orderStr = `{"publishtime":{"order":"desc"}}`
  814. mysqlOrderStr = ` order by a.publishtime desc`
  815. } else {
  816. orderStr = `{"bidopentime":{"order":"asc"}}`
  817. mysqlOrderStr = ` order by a.bidopentime asc`
  818. }
  819. }
  820. queryStr = fmt.Sprintf(query, strings.Join(mustArr, ","), strings.Join(filterArr, ","), start, operator.PageSize, orderStr)
  821. fmt.Println(queryStr)
  822. start1 := time.Now().Unix()
  823. fmt.Println(start1)
  824. var count int64
  825. var data *[]map[string]interface{}
  826. count, data = elastic.GetWithCount(yysIndex, yysType, "", queryStr)
  827. end1 := time.Now().Unix()
  828. fmt.Println(end1)
  829. fmt.Println("时差", end1-start1)
  830. if len(*data) > 0 {
  831. //查询tidb真实数据
  832. idArr := []interface{}{gconv.String(operator.EntId)}
  833. wh := []string{}
  834. for _, v := range *data {
  835. id := gconv.String(v["id"])
  836. idArr = append(idArr, id)
  837. wh = append(wh, "?")
  838. }
  839. sqlStr := fmt.Sprintf(` SELECT
  840. a.*,b.encrypt_id
  841. FROM
  842. customer_data a
  843. LEFT JOIN customer_data_yys_project_winner b
  844. on a.eid=?
  845. and a.projectId =b.projectId
  846. and a.s_winner=b.winner_name
  847. where a.id in (%s)
  848. %s `, strings.Join(wh, ","), mysqlOrderStr)
  849. fmt.Println(sqlStr, idArr)
  850. data = IC.BiMysql.SelectBySql(sqlStr,
  851. idArr...)
  852. if data != nil && len(*data) > 0 {
  853. _, winnerMap := getConfiguration("运营商中标标签", "winner_tag", operator.EntId)
  854. _, industryMap := getConfiguration("行业", "industry", operator.EntId)
  855. fileArr := &[]map[string]interface{}{}
  856. if operator.Tag == 0 {
  857. fileArr = IC.BiMysql.SelectBySql("select * from field_order ")
  858. } else {
  859. fileArr = IC.BiMysql.SelectBySql("select * from field_order where FIND_IN_SET(?,config_class)", operator.Tag)
  860. }
  861. //中标单位处理
  862. for i, v := range *data {
  863. newData := map[string]interface{}{}
  864. v["publishtime"] = TimeHandle(gconv.String(v["publishtime"]))
  865. v["bidopentime"] = TimeHandle(gconv.String(v["bidopentime"]))
  866. v["expurasingtime"] = TimeHandle(gconv.String(v["expurasingtime"]))
  867. v["industry"] = industryMap[gconv.String(v["industry"])]
  868. v["winner_tag"] = winnerMap[gconv.String(v["winner_tag"])]
  869. v["bidendtime"] = TimeHandle(gconv.String(v["bidendtime"]))
  870. for _, m := range *fileArr {
  871. key := gconv.String(m["file"])
  872. newData[key] = v[key]
  873. }
  874. v["infoid"] = ME.EncodeArticleId2ByCheck(gconv.String(v["infoid"]))
  875. (*data)[i] = newData
  876. }
  877. }
  878. total := count
  879. count = gconv.Int64(common.If(count > 2000, 2000, count))
  880. return count, data, total
  881. } else {
  882. return count, &[]map[string]interface{}{}, 0
  883. }
  884. }
  885. func TimeHandle(timeStr string) int64 {
  886. if timeStr == "" {
  887. return 0
  888. }
  889. // 定义一个布局,需要与时间字符串格式相匹配
  890. layout := "2006-01-02 15:04:05"
  891. // 使用Parse将字符串转换为time.Time类型
  892. t, err := time.Parse(layout, timeStr)
  893. if err != nil {
  894. fmt.Println(err)
  895. return 0
  896. }
  897. // 将time.Time类型转换为时间戳(单位:秒)
  898. return t.Unix()
  899. }
  900. func keyHandle(keys, userId string) {
  901. //历史记录
  902. history := redis.GetStr("other", "s_"+userId)
  903. keyArr := SearchHistory(history, keys)
  904. if len(keyArr) > 0 {
  905. if b := redis.Put("other", "s_"+userId, strings.Join(keyArr, ","), -1); !b {
  906. log.Println("保存搜索记录异常,用户id:", userId)
  907. }
  908. }
  909. }
  910. // SearchHistory 格式化 关键词搜索历史记录
  911. func SearchHistory(history, searchValue string) (arrS []string) {
  912. //主关键词
  913. var searchKeys = strings.Split(searchValue, IC.C.JYKeyMark)
  914. //附加词
  915. //关键词 和 附加词 合并,作为新的关键词历史搜索记录
  916. if len(searchKeys) > 0 {
  917. arrS = strings.Split(history, ",")
  918. //新增历史记录
  919. if history == "" {
  920. arrS = make([]string, 0)
  921. }
  922. for _, sv := range searchKeys {
  923. for k, v := range arrS {
  924. if v == strings.TrimSpace(sv) {
  925. arrS = append(arrS[:k], arrS[k+1:]...)
  926. break
  927. }
  928. }
  929. }
  930. arrS = append(arrS, searchKeys...)
  931. if len(arrS) > 10 {
  932. arrS = arrS[len(arrS)-10:]
  933. }
  934. }
  935. return arrS
  936. }