edb_info.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. package elastic
  2. import (
  3. "context"
  4. "encoding/json"
  5. "eta_gn/eta_api/models/data_manage"
  6. "eta_gn/eta_api/utils"
  7. "fmt"
  8. "github.com/olivere/elastic/v7"
  9. )
  10. // EsAddOrEditEdbInfoData 新增/修改es中的指标数据
  11. func EsAddOrEditEdbInfoData(indexName, docId string, item *data_manage.EdbInfoEs) (err error) {
  12. defer func() {
  13. if err != nil {
  14. fmt.Println("EsAddOrEditData Err:", err.Error())
  15. }
  16. }()
  17. client := utils.EsClient
  18. resp, err := client.Index().Index(indexName).Id(docId).BodyJson(item).Do(context.Background())
  19. if err != nil {
  20. fmt.Println("新增失败:", err.Error())
  21. return err
  22. }
  23. fmt.Println(resp)
  24. if resp.Status == 0 {
  25. fmt.Println("新增成功", resp.Result)
  26. err = nil
  27. } else {
  28. fmt.Println("AddData", resp.Status, resp.Result)
  29. }
  30. return
  31. }
  32. // searchEdbInfoData 查询es中的指标数据
  33. func searchEdbInfoData(indexName string, mustMap, mustNotMap []interface{}, shouldMapList []map[string]interface{}, sortList []interface{}, from, size int) (total int64, list []*data_manage.EdbInfoList, err error) {
  34. list = make([]*data_manage.EdbInfoList, 0)
  35. defer func() {
  36. if err != nil {
  37. fmt.Println("searchEdbInfoData Err:", err.Error())
  38. }
  39. }()
  40. client := utils.EsClient
  41. //queryString := elastic.NewQueryStringQuery(keywordStr)
  42. //boolQueryJson, err := json.Marshal(queryString)
  43. //if err != nil {
  44. // fmt.Println("boolQueryJson err:", err)
  45. //} else {
  46. // fmt.Println("boolQueryJson ", string(boolQueryJson))
  47. //}
  48. highlight := elastic.NewHighlight()
  49. highlight = highlight.Fields(elastic.NewHighlighterField("EdbCode"), elastic.NewHighlighterField("EdbName"))
  50. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  51. //query := elastic.RawStringQuery(`{"match_all":{}}`)
  52. //关键字匹配
  53. for _, shouldMap := range shouldMapList {
  54. mustMap = append(mustMap, map[string]interface{}{
  55. "bool": shouldMap,
  56. })
  57. }
  58. queryMap := map[string]interface{}{
  59. "query": map[string]interface{}{
  60. "bool": map[string]interface{}{
  61. "must": mustMap,
  62. "must_not": mustNotMap,
  63. //"should": shouldMap,
  64. },
  65. },
  66. }
  67. //根据条件数量统计
  68. requestTotalHits := client.Count(indexName).BodyJson(queryMap)
  69. total, err = requestTotalHits.Do(context.Background())
  70. if err != nil {
  71. return
  72. }
  73. queryMap["from"] = from
  74. queryMap["size"] = size
  75. // 如果有指定排序,那么就按照排序来
  76. if len(sortList) > 0 {
  77. queryMap["sort"] = sortList
  78. }
  79. jsonBytes, _ := json.Marshal(queryMap)
  80. fmt.Println(string(jsonBytes))
  81. //queryString := elastic.NewMatchQuery("EdbCode", keywordStr)
  82. //request := client.Search(indexName).Highlight(highlight).From(from).Size(size).Query(queryString)
  83. request := client.Search(indexName).Highlight(highlight).Source(queryMap) // sets the JSON request
  84. //requestJson, err := json.Marshal(request)
  85. //if err != nil {
  86. // fmt.Println("requestJson err:", err)
  87. //}
  88. //fmt.Println("requestJson ", string(requestJson))
  89. searchMap := make(map[string]string)
  90. searchResp, err := request.Do(context.Background())
  91. if err != nil {
  92. return
  93. }
  94. fmt.Println(searchResp)
  95. fmt.Println(searchResp.Status)
  96. if searchResp.Status != 0 {
  97. return
  98. }
  99. //total = searchResp.TotalHits()
  100. if searchResp.Hits != nil {
  101. for _, v := range searchResp.Hits.Hits {
  102. if _, ok := searchMap[v.Id]; !ok {
  103. itemJson, tmpErr := v.Source.MarshalJSON()
  104. if tmpErr != nil {
  105. err = tmpErr
  106. fmt.Println("movieJson err:", err)
  107. return
  108. }
  109. edbInfoItem := new(data_manage.EdbInfoList)
  110. tmpErr = json.Unmarshal(itemJson, &edbInfoItem)
  111. if tmpErr != nil {
  112. fmt.Println("json.Unmarshal movieJson err:", tmpErr)
  113. err = tmpErr
  114. return
  115. }
  116. if len(v.Highlight["EdbCode"]) > 0 {
  117. edbInfoItem.EdbCode = v.Highlight["EdbCode"][0]
  118. }
  119. if len(v.Highlight["EdbName"]) > 0 {
  120. edbInfoItem.EdbCode = v.Highlight["EdbName"][0]
  121. }
  122. list = append(list, edbInfoItem)
  123. searchMap[v.Id] = v.Id
  124. }
  125. }
  126. }
  127. return
  128. }
  129. // SearchEdbInfoData
  130. // @Description: 查询es中的指标数据
  131. // @author: Roc
  132. // @datetime 2024-11-29 10:22:25
  133. // @param keywordStr string
  134. // @param from int
  135. // @param size int
  136. // @param filterSource int
  137. // @param source int
  138. // @param frequency string
  139. // @param noPermissionEdbInfoIdList []int
  140. // @param noPermissionEdbClassifyIdList []int
  141. // @param collectEdbInfoIdList []int
  142. // @param edbTypeList []int
  143. // @param edbInfoType int 指标类型,0:ETA指标库(基础指标+计算指标);1:预测指标
  144. // @param edbAuth int 指标权限范围,0-全部;1-我的;2-公共
  145. // @param sysUserId int
  146. // @return total int64
  147. // @return list []*data_manage.EdbInfoList
  148. // @return err error
  149. func SearchEdbInfoData(keywordStr string, from, size, filterSource, source int, frequency string, noPermissionEdbInfoIdList, noPermissionEdbClassifyIdList, collectEdbInfoIdList, edbTypeList []int, edbInfoType, edbAuth, sysUserId int, sortMap map[string]string) (total int64, list []*data_manage.EdbInfoList, err error) {
  150. indexName := utils.DATA_INDEX_NAME
  151. list = make([]*data_manage.EdbInfoList, 0)
  152. defer func() {
  153. if err != nil {
  154. fmt.Println("SearchEdbInfoData Err:", err.Error())
  155. }
  156. }()
  157. highlight := elastic.NewHighlight()
  158. highlight = highlight.Fields(elastic.NewHighlighterField("EdbCode"), elastic.NewHighlighterField("EdbName"))
  159. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  160. mustMap := make([]interface{}, 0)
  161. mustNotMap := make([]interface{}, 0)
  162. switch filterSource {
  163. case 2:
  164. mustMap = append(mustMap, map[string]interface{}{
  165. "term": map[string]interface{}{
  166. "Frequency.keyword": "月度",
  167. //"Frequency.keyword": "月度",
  168. },
  169. })
  170. case 3:
  171. case 4:
  172. mustMap = append(mustMap, map[string]interface{}{
  173. "term": map[string]interface{}{
  174. "EdbType": 1,
  175. },
  176. })
  177. case 5:
  178. mustMap = append(mustMap, map[string]interface{}{
  179. "term": map[string]interface{}{
  180. "Source": 6,
  181. },
  182. })
  183. case 6:
  184. mustMap = append(mustMap, map[string]interface{}{
  185. "match": map[string]interface{}{
  186. "Frequency.keyword": "年度",
  187. },
  188. })
  189. }
  190. //指标来源
  191. if source > 0 {
  192. mustMap = append(mustMap, map[string]interface{}{
  193. "term": map[string]interface{}{
  194. "Source": source,
  195. //"Frequency.keyword": "月度",
  196. },
  197. })
  198. }
  199. if frequency != "" {
  200. mustMap = append(mustMap, map[string]interface{}{
  201. "term": map[string]interface{}{
  202. "Frequency.keyword": frequency,
  203. //"Frequency.keyword": "月度",
  204. },
  205. })
  206. }
  207. // noPermissionEdbInfoIdList 无权限指标id
  208. if len(noPermissionEdbInfoIdList) > 0 {
  209. mustNotMap = append(mustNotMap, map[string]interface{}{
  210. "terms": map[string]interface{}{
  211. "EdbInfoId": noPermissionEdbInfoIdList,
  212. //"Frequency.keyword": "月度",
  213. },
  214. })
  215. }
  216. // noPermissionEdbInfoIdList 无权限指标id
  217. if len(noPermissionEdbClassifyIdList) > 0 {
  218. mustNotMap = append(mustNotMap, map[string]interface{}{
  219. "terms": map[string]interface{}{
  220. "ClassifyId": noPermissionEdbClassifyIdList,
  221. //"Frequency.keyword": "月度",
  222. },
  223. })
  224. }
  225. // collectEdbInfoIdList 收藏的指标id
  226. if len(collectEdbInfoIdList) > 0 {
  227. mustMap = append(mustMap, map[string]interface{}{
  228. "terms": map[string]interface{}{
  229. "EdbInfoId": collectEdbInfoIdList,
  230. },
  231. })
  232. }
  233. // 指标类型:0-基础+计算;1-基础指标;2-计算指标;3-预测指标
  234. if len(edbTypeList) > 0 {
  235. mustMap = append(mustMap, map[string]interface{}{
  236. "terms": map[string]interface{}{
  237. "EdbType": edbTypeList,
  238. },
  239. })
  240. }
  241. if edbInfoType >= 0 {
  242. mustMap = append(mustMap, map[string]interface{}{
  243. "term": map[string]interface{}{
  244. "EdbInfoType": edbInfoType,
  245. },
  246. })
  247. }
  248. shouldMapList := make([]map[string]interface{}, 0)
  249. // 指标名称、编码匹配
  250. if keywordStr != `` {
  251. // 默认使用中文名字字段去匹配
  252. keywordNameKey := `EdbName`
  253. shouldMap := map[string]interface{}{
  254. "should": []interface{}{
  255. map[string]interface{}{
  256. "match": map[string]interface{}{
  257. "EdbCode": keywordStr,
  258. //"Frequency.keyword": "月度",
  259. },
  260. },
  261. map[string]interface{}{
  262. "match": map[string]interface{}{
  263. keywordNameKey: keywordStr,
  264. //"Frequency.keyword": "月度",
  265. },
  266. },
  267. },
  268. }
  269. shouldMapList = append(shouldMapList, shouldMap)
  270. }
  271. // 指标与用户的权限匹配
  272. {
  273. shouldTermList := make([]map[string]interface{}, 0)
  274. //指标权限范围,0-全部;1-我的;2-公共
  275. switch edbAuth {
  276. case 1:
  277. // 自己的指标
  278. shouldTermList = append(shouldTermList, map[string]interface{}{
  279. "term": map[string]interface{}{
  280. "SysUserId": sysUserId,
  281. },
  282. })
  283. case 2:
  284. // 公开的指标
  285. shouldTermList = append(shouldTermList, map[string]interface{}{
  286. "term": map[string]interface{}{
  287. "PublicStatus": utils.DataPublicSuccess,
  288. },
  289. })
  290. default:
  291. // 自己的指标
  292. shouldTermList = append(shouldTermList, map[string]interface{}{
  293. "term": map[string]interface{}{
  294. "SysUserId": sysUserId,
  295. },
  296. })
  297. // 分享给我的指标
  298. shouldTermList = append(shouldTermList, map[string]interface{}{
  299. "terms": map[string]interface{}{
  300. "SharedUserIdList": []int{sysUserId},
  301. },
  302. })
  303. // 公开的指标
  304. shouldTermList = append(shouldTermList, map[string]interface{}{
  305. "term": map[string]interface{}{
  306. "PublicStatus": utils.DataPublicSuccess,
  307. },
  308. })
  309. }
  310. // 基础指标(数据加工)
  311. shouldTermList = append(shouldTermList, map[string]interface{}{
  312. "term": map[string]interface{}{
  313. "EdbType": 1,
  314. },
  315. }, map[string]interface{}{
  316. "term": map[string]interface{}{
  317. "EdbInfoType": 0,
  318. },
  319. })
  320. shouldMap := map[string]interface{}{
  321. "should": shouldTermList,
  322. }
  323. shouldMapList = append(shouldMapList, shouldMap)
  324. }
  325. // 排序
  326. sortList := make([]interface{}, 0)
  327. // 如果没有关键字,那么就走指标id倒序
  328. for orderKey, orderType := range sortMap {
  329. sortEdbInfoId := map[string]interface{}{
  330. orderKey: map[string]interface{}{
  331. "order": orderType,
  332. },
  333. }
  334. sortList = append(sortList, sortEdbInfoId)
  335. }
  336. return searchEdbInfoData(indexName, mustMap, mustNotMap, shouldMapList, sortList, from, size)
  337. }
  338. // searchEdbInfoData 查询es中的指标数量
  339. func searchEdbInfoDataTotal(indexName string, query elastic.Query) (total int64, err error) {
  340. defer func() {
  341. if err != nil {
  342. fmt.Println("searchEdbInfoDataV2 Err:", err.Error())
  343. }
  344. }()
  345. client := utils.EsClient
  346. //根据条件数量统计
  347. requestTotalHits := client.Count(indexName).Query(query)
  348. total, err = requestTotalHits.Do(context.Background())
  349. if err != nil {
  350. return
  351. }
  352. return
  353. }
  354. // searchEdbInfoData 查询es中的指标数据
  355. func searchEdbInfoDataList(indexName string, query elastic.Query, sortList []*elastic.FieldSort, from, size int) (list []*data_manage.EdbInfoList, err error) {
  356. list = make([]*data_manage.EdbInfoList, 0)
  357. defer func() {
  358. if err != nil {
  359. fmt.Println("searchEdbInfoDataV2 Err:", err.Error())
  360. }
  361. }()
  362. client := utils.EsClient
  363. // 高亮
  364. highlight := elastic.NewHighlight()
  365. highlight = highlight.Fields(elastic.NewHighlighterField("EdbCode"), elastic.NewHighlighterField("EdbName"))
  366. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  367. request := client.Search(indexName).Highlight(highlight).From(from).Size(size) // sets the JSON request
  368. // 如果有指定排序,那么就按照排序来
  369. if len(sortList) > 0 {
  370. for _, v := range sortList {
  371. request = request.SortBy(v)
  372. }
  373. }
  374. searchMap := make(map[string]string)
  375. searchResp, err := request.Query(query).Do(context.Background())
  376. if err != nil {
  377. return
  378. }
  379. //fmt.Println(searchResp)
  380. //fmt.Println(searchResp.Status)
  381. if searchResp.Status != 0 {
  382. return
  383. }
  384. //total = searchResp.TotalHits()
  385. if searchResp.Hits != nil {
  386. for _, v := range searchResp.Hits.Hits {
  387. if _, ok := searchMap[v.Id]; !ok {
  388. itemJson, tmpErr := v.Source.MarshalJSON()
  389. if tmpErr != nil {
  390. err = tmpErr
  391. fmt.Println("movieJson err:", err)
  392. return
  393. }
  394. edbInfoItem := new(data_manage.EdbInfoList)
  395. tmpErr = json.Unmarshal(itemJson, &edbInfoItem)
  396. if tmpErr != nil {
  397. fmt.Println("json.Unmarshal movieJson err:", tmpErr)
  398. err = tmpErr
  399. return
  400. }
  401. if len(v.Highlight["EdbCode"]) > 0 {
  402. edbInfoItem.EdbCode = v.Highlight["EdbCode"][0]
  403. }
  404. if len(v.Highlight["EdbName"]) > 0 {
  405. edbInfoItem.EdbCode = v.Highlight["EdbName"][0]
  406. }
  407. list = append(list, edbInfoItem)
  408. searchMap[v.Id] = v.Id
  409. }
  410. }
  411. }
  412. return
  413. }
  414. // searchEdbInfoData 查询es中的指标数据
  415. func searchEdbInfoDataV2(indexName string, query elastic.Query, sortList []*elastic.FieldSort, from, size int) (total int64, list []*data_manage.EdbInfoList, err error) {
  416. total, err = searchEdbInfoDataTotal(indexName, query)
  417. if err != nil {
  418. return
  419. }
  420. // 获取列表数据
  421. list, err = searchEdbInfoDataList(indexName, query, sortList, from, size)
  422. if err != nil {
  423. return
  424. }
  425. return
  426. }
  427. // SearchEdbInfoDataByShared
  428. // @Description: 查询es中的指标数据
  429. // @author: Roc
  430. // @datetime 2024-11-29 10:22:25
  431. // @param keywordStr string
  432. // @param from int
  433. // @param size int
  434. // @param filterSource int
  435. // @param source int
  436. // @param frequency string
  437. // @param noPermissionEdbInfoIdList []int
  438. // @param noPermissionEdbClassifyIdList []int
  439. // @param collectEdbInfoIdList []int
  440. // @param edbTypeList []int
  441. // @param edbInfoType int 指标类型,0:ETA指标库(基础指标+计算指标);1:预测指标
  442. // @param edbAuth int 指标权限范围,0-全部;1-我的;2-公共
  443. // @param sysUserId int
  444. // @return total int64
  445. // @return list []*data_manage.EdbInfoList
  446. // @return err error
  447. func SearchEdbInfoDataByShared(keywordStr string, from, size, edbShare int, sourceList, classifyIdList, edbTypeList []int, edbInfoType, edbAuth, sysUserId int, sortMap map[string]string) (total int64, list []*data_manage.EdbInfoList, err error) {
  448. indexName := utils.DATA_INDEX_NAME
  449. list = make([]*data_manage.EdbInfoList, 0)
  450. defer func() {
  451. if err != nil {
  452. fmt.Println("SearchEdbInfoData Err:", err.Error())
  453. }
  454. }()
  455. query := elastic.NewBoolQuery()
  456. //指标来源
  457. if len(sourceList) > 0 {
  458. termsList := make([]interface{}, 0)
  459. for _, v := range sourceList {
  460. termsList = append(termsList, v)
  461. }
  462. query = query.Must(elastic.NewTermsQuery("Source", termsList...))
  463. }
  464. // classifyIdList 指定分类下的指标
  465. if len(classifyIdList) > 0 {
  466. termsList := make([]interface{}, 0)
  467. for _, v := range classifyIdList {
  468. termsList = append(termsList, v)
  469. }
  470. query = query.Must(elastic.NewTermsQuery("ClassifyId", termsList...))
  471. }
  472. // 指标类型:0-基础+计算;1-基础指标;2-计算指标;3-预测指标
  473. if len(edbTypeList) > 0 {
  474. termsList := make([]interface{}, 0)
  475. for _, v := range edbTypeList {
  476. termsList = append(termsList, v)
  477. }
  478. query = query.Must(elastic.NewTermsQuery("EdbType", termsList...))
  479. }
  480. // 如果指定了分享状态,那么就添加分享状态的筛选
  481. // 0:全部,1:未共享,2:已共享
  482. switch edbShare {
  483. case 1:
  484. // 筛选 SharedUserIdList 为空的文档
  485. query = query.MustNot(elastic.NewExistsQuery("SharedUserIdList"))
  486. case 2:
  487. // 筛选 SharedUserIdList 不为空的文档
  488. query = query.Must(elastic.NewExistsQuery("SharedUserIdList"))
  489. }
  490. if edbInfoType >= 0 {
  491. query = query.Must(elastic.NewTermQuery("EdbInfoType", edbInfoType))
  492. }
  493. // 指标名称、编码匹配
  494. if keywordStr != `` {
  495. // 默认使用中文名字字段去匹配
  496. keywordNameKey := `EdbName`
  497. query = query.Must(elastic.NewMultiMatchQuery(keywordStr, keywordNameKey, "EdbCode"))
  498. }
  499. // 指标与用户的权限匹配
  500. {
  501. //指标权限范围,0-全部;1-我的;2-公共
  502. switch edbAuth {
  503. case 1:
  504. // 自己的指标
  505. query = query.Must(elastic.NewTermQuery(`SysUserId`, sysUserId))
  506. case 2:
  507. // 公开的指标
  508. query = query.Must(elastic.NewTermQuery(`PublicStatus`, utils.DataPublicSuccess))
  509. default:
  510. tmpShouldQuery := elastic.NewBoolQuery()
  511. // 自己的指标
  512. tmpShouldQuery = tmpShouldQuery.Should(elastic.NewTermQuery(`SysUserId`, sysUserId))
  513. // 分享给我的指标
  514. tmpShouldQuery = tmpShouldQuery.Should(elastic.NewTermsQuery(`SharedUserIdList`, sysUserId))
  515. //公开的指标
  516. tmpShouldQuery = tmpShouldQuery.Should(elastic.NewTermQuery(`PublicStatus`, utils.DataPublicSuccess))
  517. //shouldQuery = shouldQuery.Should(tmpShouldQuery)
  518. query = query.Must(tmpShouldQuery)
  519. }
  520. }
  521. // 排序
  522. sortList := make([]*elastic.FieldSort, 0)
  523. // 如果没有关键字,那么就走指标id倒序
  524. for orderKey, orderType := range sortMap {
  525. switch orderType {
  526. case "asc":
  527. sortList = append(sortList, elastic.NewFieldSort(orderKey).Asc())
  528. case "desc":
  529. sortList = append(sortList, elastic.NewFieldSort(orderKey).Desc())
  530. }
  531. }
  532. return searchEdbInfoDataV2(indexName, query, sortList, from, size)
  533. }
  534. // SearchEdbInfoDataByPublic
  535. // @Description: 查询es中的指标数据
  536. // @author: Roc
  537. // @datetime 2024-12-05 13:33:36
  538. // @param keywordStr string
  539. // @param from int
  540. // @param size int
  541. // @param edbPublicList []int
  542. // @param sourceList []int
  543. // @param classifyIdList []int
  544. // @param publicClassifyIdList []int
  545. // @param edbTypeList []int
  546. // @param edbInfoType int
  547. // @param edbAuth int
  548. // @param sysUserId int
  549. // @param sortMap map[string]string
  550. // @return total int64
  551. // @return list []*data_manage.EdbInfoList
  552. // @return err error
  553. func SearchEdbInfoDataByPublic(keywordStr string, from, size int, edbPublicList, sourceList, classifyIdList, publicClassifyIdList, edbTypeList []int, edbInfoType, edbAuth, sysUserId int, sortMap map[string]string) (total int64, list []*data_manage.EdbInfoList, err error) {
  554. indexName := utils.DATA_INDEX_NAME
  555. list = make([]*data_manage.EdbInfoList, 0)
  556. defer func() {
  557. if err != nil {
  558. fmt.Println("SearchEdbInfoData Err:", err.Error())
  559. }
  560. }()
  561. query := elastic.NewBoolQuery()
  562. //指标来源
  563. if len(sourceList) > 0 {
  564. termsList := make([]interface{}, 0)
  565. for _, v := range sourceList {
  566. termsList = append(termsList, v)
  567. }
  568. query = query.Must(elastic.NewTermsQuery("Source", termsList...))
  569. }
  570. // classifyIdList 指定分类下的指标
  571. if len(classifyIdList) > 0 {
  572. termsList := make([]interface{}, 0)
  573. for _, v := range classifyIdList {
  574. termsList = append(termsList, v)
  575. }
  576. query = query.Must(elastic.NewTermsQuery("ClassifyId", termsList...))
  577. }
  578. // publicClassifyIdList 指定公共分类下的指标
  579. if len(publicClassifyIdList) > 0 {
  580. termsList := make([]interface{}, 0)
  581. for _, v := range publicClassifyIdList {
  582. termsList = append(termsList, v)
  583. }
  584. query = query.Must(elastic.NewTermsQuery("EdbPublicClassifyId", termsList...))
  585. }
  586. // 指标类型:0-基础+计算;1-基础指标;2-计算指标;3-预测指标
  587. if len(edbTypeList) > 0 {
  588. termsList := make([]interface{}, 0)
  589. for _, v := range edbTypeList {
  590. termsList = append(termsList, v)
  591. }
  592. query = query.Must(elastic.NewTermsQuery("EdbType", termsList...))
  593. }
  594. // 如果指定了指标公开状态,那么就添加指标公开状态的筛选
  595. // 公开状态;0:未公开;1:审批中;2:已驳回;3:已公开
  596. if len(edbPublicList) > 0 {
  597. termsList := make([]interface{}, 0)
  598. for _, v := range edbPublicList {
  599. termsList = append(termsList, v)
  600. }
  601. query = query.Must(elastic.NewTermsQuery("PublicStatus", termsList...))
  602. }
  603. if edbInfoType >= 0 {
  604. query = query.Must(elastic.NewTermQuery("EdbInfoType", edbInfoType))
  605. }
  606. // 指标名称、编码匹配
  607. if keywordStr != `` {
  608. // 默认使用中文名字字段去匹配
  609. keywordNameKey := `EdbName`
  610. query = query.Must(elastic.NewMultiMatchQuery(keywordStr, keywordNameKey, "EdbCode"))
  611. }
  612. // 指标与用户的权限匹配
  613. {
  614. //指标权限范围,0-全部;1-我的;2-公共
  615. switch edbAuth {
  616. case 1:
  617. // 自己的指标
  618. query = query.Must(elastic.NewTermQuery(`SysUserId`, sysUserId))
  619. case 2:
  620. // 公开的指标
  621. query = query.Must(elastic.NewTermQuery(`PublicStatus`, utils.DataPublicSuccess))
  622. default:
  623. tmpShouldQuery := elastic.NewBoolQuery()
  624. // 自己的指标
  625. tmpShouldQuery = tmpShouldQuery.Should(elastic.NewTermQuery(`SysUserId`, sysUserId))
  626. // 分享给我的指标
  627. tmpShouldQuery = tmpShouldQuery.Should(elastic.NewTermsQuery(`SharedUserIdList`, sysUserId))
  628. //公开的指标
  629. tmpShouldQuery = tmpShouldQuery.Should(elastic.NewTermQuery(`PublicStatus`, utils.DataPublicSuccess))
  630. //shouldQuery = shouldQuery.Should(tmpShouldQuery)
  631. query = query.Must(tmpShouldQuery)
  632. }
  633. }
  634. // 排序
  635. sortList := make([]*elastic.FieldSort, 0)
  636. // 如果没有关键字,那么就走指标id倒序
  637. for orderKey, orderType := range sortMap {
  638. switch orderType {
  639. case "asc":
  640. sortList = append(sortList, elastic.NewFieldSort(orderKey).Asc())
  641. case "desc":
  642. sortList = append(sortList, elastic.NewFieldSort(orderKey).Desc())
  643. }
  644. }
  645. return searchEdbInfoDataV2(indexName, query, sortList, from, size)
  646. }
  647. // EsDeleteEdbInfoData 删除es中的指标数据
  648. func EsDeleteEdbInfoData(indexName, docId string) (err error) {
  649. defer func() {
  650. if err != nil {
  651. fmt.Println("EsDeleteEdbInfoData Err:", err.Error())
  652. }
  653. }()
  654. client := utils.EsClient
  655. resp, err := client.Delete().Index(indexName).Id(docId).Do(context.Background())
  656. fmt.Println(resp)
  657. if err != nil {
  658. return
  659. }
  660. if resp.Status == 0 {
  661. fmt.Println("删除成功")
  662. } else {
  663. fmt.Println("AddData", resp.Status, resp.Result)
  664. }
  665. return
  666. }