operator.go 28 KB

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