edb_info.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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. shouldMap := map[string]interface{}{
  311. "should": shouldTermList,
  312. }
  313. shouldMapList = append(shouldMapList, shouldMap)
  314. }
  315. // 排序
  316. sortList := make([]interface{}, 0)
  317. // 如果没有关键字,那么就走指标id倒序
  318. for orderKey, orderType := range sortMap {
  319. sortEdbInfoId := map[string]interface{}{
  320. orderKey: map[string]interface{}{
  321. "order": orderType,
  322. },
  323. }
  324. sortList = append(sortList, sortEdbInfoId)
  325. }
  326. return searchEdbInfoData(indexName, mustMap, mustNotMap, shouldMapList, sortList, from, size)
  327. }
  328. // searchEdbInfoData 查询es中的指标数量
  329. func searchEdbInfoDataTotal(indexName string, query elastic.Query) (total int64, err error) {
  330. defer func() {
  331. if err != nil {
  332. fmt.Println("searchEdbInfoDataV2 Err:", err.Error())
  333. }
  334. }()
  335. client := utils.EsClient
  336. //根据条件数量统计
  337. requestTotalHits := client.Count(indexName).Query(query)
  338. total, err = requestTotalHits.Do(context.Background())
  339. if err != nil {
  340. return
  341. }
  342. return
  343. }
  344. // searchEdbInfoData 查询es中的指标数据
  345. func searchEdbInfoDataList(indexName string, query elastic.Query, sortList []*elastic.FieldSort, from, size int) (list []*data_manage.EdbInfoList, err error) {
  346. list = make([]*data_manage.EdbInfoList, 0)
  347. defer func() {
  348. if err != nil {
  349. fmt.Println("searchEdbInfoDataV2 Err:", err.Error())
  350. }
  351. }()
  352. client := utils.EsClient
  353. // 高亮
  354. highlight := elastic.NewHighlight()
  355. highlight = highlight.Fields(elastic.NewHighlighterField("EdbCode"), elastic.NewHighlighterField("EdbName"))
  356. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  357. request := client.Search(indexName).Highlight(highlight).From(from).Size(size) // sets the JSON request
  358. // 如果有指定排序,那么就按照排序来
  359. if len(sortList) > 0 {
  360. for _, v := range sortList {
  361. request = request.SortBy(v)
  362. }
  363. }
  364. searchMap := make(map[string]string)
  365. searchResp, err := request.Query(query).Do(context.Background())
  366. if err != nil {
  367. return
  368. }
  369. //fmt.Println(searchResp)
  370. //fmt.Println(searchResp.Status)
  371. if searchResp.Status != 0 {
  372. return
  373. }
  374. //total = searchResp.TotalHits()
  375. if searchResp.Hits != nil {
  376. for _, v := range searchResp.Hits.Hits {
  377. if _, ok := searchMap[v.Id]; !ok {
  378. itemJson, tmpErr := v.Source.MarshalJSON()
  379. if tmpErr != nil {
  380. err = tmpErr
  381. fmt.Println("movieJson err:", err)
  382. return
  383. }
  384. edbInfoItem := new(data_manage.EdbInfoList)
  385. tmpErr = json.Unmarshal(itemJson, &edbInfoItem)
  386. if tmpErr != nil {
  387. fmt.Println("json.Unmarshal movieJson err:", tmpErr)
  388. err = tmpErr
  389. return
  390. }
  391. if len(v.Highlight["EdbCode"]) > 0 {
  392. edbInfoItem.EdbCode = v.Highlight["EdbCode"][0]
  393. }
  394. if len(v.Highlight["EdbName"]) > 0 {
  395. edbInfoItem.EdbCode = v.Highlight["EdbName"][0]
  396. }
  397. list = append(list, edbInfoItem)
  398. searchMap[v.Id] = v.Id
  399. }
  400. }
  401. }
  402. return
  403. }
  404. // searchEdbInfoData 查询es中的指标数据
  405. func searchEdbInfoDataV2(indexName string, query elastic.Query, sortList []*elastic.FieldSort, from, size int) (total int64, list []*data_manage.EdbInfoList, err error) {
  406. total, err = searchEdbInfoDataTotal(indexName, query)
  407. if err != nil {
  408. return
  409. }
  410. // 获取列表数据
  411. list, err = searchEdbInfoDataList(indexName, query, sortList, from, size)
  412. if err != nil {
  413. return
  414. }
  415. return
  416. }
  417. // SearchEdbInfoDataByShared
  418. // @Description: 查询es中的指标数据
  419. // @author: Roc
  420. // @datetime 2024-11-29 10:22:25
  421. // @param keywordStr string
  422. // @param from int
  423. // @param size int
  424. // @param filterSource int
  425. // @param source int
  426. // @param frequency string
  427. // @param noPermissionEdbInfoIdList []int
  428. // @param noPermissionEdbClassifyIdList []int
  429. // @param collectEdbInfoIdList []int
  430. // @param edbTypeList []int
  431. // @param edbInfoType int 指标类型,0:ETA指标库(基础指标+计算指标);1:预测指标
  432. // @param edbAuth int 指标权限范围,0-全部;1-我的;2-公共
  433. // @param sysUserId int
  434. // @return total int64
  435. // @return list []*data_manage.EdbInfoList
  436. // @return err error
  437. 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) {
  438. indexName := utils.DATA_INDEX_NAME
  439. list = make([]*data_manage.EdbInfoList, 0)
  440. defer func() {
  441. if err != nil {
  442. fmt.Println("SearchEdbInfoData Err:", err.Error())
  443. }
  444. }()
  445. query := elastic.NewBoolQuery()
  446. //指标来源
  447. if len(sourceList) > 0 {
  448. termsList := make([]interface{}, 0)
  449. for _, v := range sourceList {
  450. termsList = append(termsList, v)
  451. }
  452. query = query.Must(elastic.NewTermsQuery("Source", termsList...))
  453. }
  454. // classifyIdList 指定分类下的指标
  455. if len(classifyIdList) > 0 {
  456. termsList := make([]interface{}, 0)
  457. for _, v := range classifyIdList {
  458. termsList = append(termsList, v)
  459. }
  460. query = query.Must(elastic.NewTermsQuery("ClassifyId", termsList...))
  461. }
  462. // 指标类型:0-基础+计算;1-基础指标;2-计算指标;3-预测指标
  463. if len(edbTypeList) > 0 {
  464. termsList := make([]interface{}, 0)
  465. for _, v := range edbTypeList {
  466. termsList = append(termsList, v)
  467. }
  468. query = query.Must(elastic.NewTermsQuery("EdbType", termsList...))
  469. }
  470. // 如果指定了分享状态,那么就添加分享状态的筛选
  471. // 0:全部,1:未共享,2:已共享
  472. switch edbShare {
  473. case 1:
  474. // 筛选 SharedUserIdList 为空的文档
  475. query = query.MustNot(elastic.NewExistsQuery("SharedUserIdList"))
  476. case 2:
  477. // 筛选 SharedUserIdList 不为空的文档
  478. query = query.Must(elastic.NewExistsQuery("SharedUserIdList"))
  479. }
  480. if edbInfoType >= 0 {
  481. query = query.Must(elastic.NewTermQuery("EdbInfoType", edbInfoType))
  482. }
  483. // 指标名称、编码匹配
  484. if keywordStr != `` {
  485. // 默认使用中文名字字段去匹配
  486. keywordNameKey := `EdbName`
  487. query = query.Must(elastic.NewMultiMatchQuery(keywordStr, keywordNameKey, "EdbCode"))
  488. }
  489. // 指标与用户的权限匹配
  490. {
  491. //指标权限范围,0-全部;1-我的;2-公共
  492. switch edbAuth {
  493. case 1:
  494. // 自己的指标
  495. query = query.Must(elastic.NewTermQuery(`SysUserId`, sysUserId))
  496. case 2:
  497. // 公开的指标
  498. query = query.Must(elastic.NewTermQuery(`PublicStatus`, utils.DataPublicSuccess))
  499. default:
  500. tmpShouldQuery := elastic.NewBoolQuery()
  501. // 自己的指标
  502. tmpShouldQuery = tmpShouldQuery.Should(elastic.NewTermQuery(`SysUserId`, sysUserId))
  503. // 分享给我的指标
  504. tmpShouldQuery = tmpShouldQuery.Should(elastic.NewTermsQuery(`SharedUserIdList`, sysUserId))
  505. //公开的指标
  506. tmpShouldQuery = tmpShouldQuery.Should(elastic.NewTermQuery(`PublicStatus`, utils.DataPublicSuccess))
  507. //shouldQuery = shouldQuery.Should(tmpShouldQuery)
  508. query = query.Must(tmpShouldQuery)
  509. }
  510. }
  511. // 排序
  512. sortList := make([]*elastic.FieldSort, 0)
  513. // 如果没有关键字,那么就走指标id倒序
  514. for orderKey, orderType := range sortMap {
  515. switch orderType {
  516. case "asc":
  517. sortList = append(sortList, elastic.NewFieldSort(orderKey).Asc())
  518. case "desc":
  519. sortList = append(sortList, elastic.NewFieldSort(orderKey).Desc())
  520. }
  521. }
  522. return searchEdbInfoDataV2(indexName, query, sortList, from, size)
  523. }
  524. // SearchEdbInfoDataByPublic
  525. // @Description: 查询es中的指标数据
  526. // @author: Roc
  527. // @datetime 2024-12-05 13:33:36
  528. // @param keywordStr string
  529. // @param from int
  530. // @param size int
  531. // @param edbPublicList []int
  532. // @param sourceList []int
  533. // @param classifyIdList []int
  534. // @param publicClassifyIdList []int
  535. // @param edbTypeList []int
  536. // @param edbInfoType int
  537. // @param edbAuth int
  538. // @param sysUserId int
  539. // @param sortMap map[string]string
  540. // @return total int64
  541. // @return list []*data_manage.EdbInfoList
  542. // @return err error
  543. 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) {
  544. indexName := utils.DATA_INDEX_NAME
  545. list = make([]*data_manage.EdbInfoList, 0)
  546. defer func() {
  547. if err != nil {
  548. fmt.Println("SearchEdbInfoData Err:", err.Error())
  549. }
  550. }()
  551. query := elastic.NewBoolQuery()
  552. //指标来源
  553. if len(sourceList) > 0 {
  554. termsList := make([]interface{}, 0)
  555. for _, v := range sourceList {
  556. termsList = append(termsList, v)
  557. }
  558. query = query.Must(elastic.NewTermsQuery("Source", termsList...))
  559. }
  560. // classifyIdList 指定分类下的指标
  561. if len(classifyIdList) > 0 {
  562. termsList := make([]interface{}, 0)
  563. for _, v := range classifyIdList {
  564. termsList = append(termsList, v)
  565. }
  566. query = query.Must(elastic.NewTermsQuery("ClassifyId", termsList...))
  567. }
  568. // publicClassifyIdList 指定公共分类下的指标
  569. if len(publicClassifyIdList) > 0 {
  570. termsList := make([]interface{}, 0)
  571. for _, v := range publicClassifyIdList {
  572. termsList = append(termsList, v)
  573. }
  574. query = query.Must(elastic.NewTermsQuery("EdbPublicClassifyId", termsList...))
  575. }
  576. // 指标类型:0-基础+计算;1-基础指标;2-计算指标;3-预测指标
  577. if len(edbTypeList) > 0 {
  578. termsList := make([]interface{}, 0)
  579. for _, v := range edbTypeList {
  580. termsList = append(termsList, v)
  581. }
  582. query = query.Must(elastic.NewTermsQuery("EdbType", termsList...))
  583. }
  584. // 如果指定了指标公开状态,那么就添加指标公开状态的筛选
  585. // 公开状态;0:未公开;1:审批中;2:已驳回;3:已公开
  586. if len(edbPublicList) > 0 {
  587. termsList := make([]interface{}, 0)
  588. for _, v := range edbPublicList {
  589. termsList = append(termsList, v)
  590. }
  591. query = query.Must(elastic.NewTermsQuery("PublicStatus", termsList...))
  592. }
  593. if edbInfoType >= 0 {
  594. query = query.Must(elastic.NewTermQuery("EdbInfoType", edbInfoType))
  595. }
  596. // 指标名称、编码匹配
  597. if keywordStr != `` {
  598. // 默认使用中文名字字段去匹配
  599. keywordNameKey := `EdbName`
  600. query = query.Must(elastic.NewMultiMatchQuery(keywordStr, keywordNameKey, "EdbCode"))
  601. }
  602. // 指标与用户的权限匹配
  603. {
  604. //指标权限范围,0-全部;1-我的;2-公共
  605. switch edbAuth {
  606. case 1:
  607. // 自己的指标
  608. query = query.Must(elastic.NewTermQuery(`SysUserId`, sysUserId))
  609. case 2:
  610. // 公开的指标
  611. query = query.Must(elastic.NewTermQuery(`PublicStatus`, utils.DataPublicSuccess))
  612. default:
  613. tmpShouldQuery := elastic.NewBoolQuery()
  614. // 自己的指标
  615. tmpShouldQuery = tmpShouldQuery.Should(elastic.NewTermQuery(`SysUserId`, sysUserId))
  616. // 分享给我的指标
  617. tmpShouldQuery = tmpShouldQuery.Should(elastic.NewTermsQuery(`SharedUserIdList`, sysUserId))
  618. //公开的指标
  619. tmpShouldQuery = tmpShouldQuery.Should(elastic.NewTermQuery(`PublicStatus`, utils.DataPublicSuccess))
  620. //shouldQuery = shouldQuery.Should(tmpShouldQuery)
  621. query = query.Must(tmpShouldQuery)
  622. }
  623. }
  624. // 排序
  625. sortList := make([]*elastic.FieldSort, 0)
  626. // 如果没有关键字,那么就走指标id倒序
  627. for orderKey, orderType := range sortMap {
  628. switch orderType {
  629. case "asc":
  630. sortList = append(sortList, elastic.NewFieldSort(orderKey).Asc())
  631. case "desc":
  632. sortList = append(sortList, elastic.NewFieldSort(orderKey).Desc())
  633. }
  634. }
  635. return searchEdbInfoDataV2(indexName, query, sortList, from, size)
  636. }
  637. // EsDeleteEdbInfoData 删除es中的指标数据
  638. func EsDeleteEdbInfoData(indexName, docId string) (err error) {
  639. defer func() {
  640. if err != nil {
  641. fmt.Println("EsDeleteEdbInfoData Err:", err.Error())
  642. }
  643. }()
  644. client := utils.EsClient
  645. resp, err := client.Delete().Index(indexName).Id(docId).Do(context.Background())
  646. fmt.Println(resp)
  647. if err != nil {
  648. return
  649. }
  650. if resp.Status == 0 {
  651. fmt.Println("删除成功")
  652. } else {
  653. fmt.Println("AddData", resp.Status, resp.Result)
  654. }
  655. return
  656. }