edb_info.go 21 KB

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