operator.go 28 KB

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