edb_info.go 26 KB

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