edb_info.go 22 KB

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