operator.go 30 KB

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