operator.go 28 KB

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