edb_info.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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. // 如果有指定排序,那么就按照排序来
  388. if len(sortList) > 0 {
  389. for _, v := range sortList {
  390. request = request.SortBy(v)
  391. }
  392. }
  393. searchMap := make(map[string]string)
  394. searchResp, err := request.Query(query).Do(context.Background())
  395. if err != nil {
  396. return
  397. }
  398. //fmt.Println(searchResp)
  399. //fmt.Println(searchResp.Status)
  400. if searchResp.Status != 0 {
  401. return
  402. }
  403. //total = searchResp.TotalHits()
  404. if searchResp.Hits != nil {
  405. for _, v := range searchResp.Hits.Hits {
  406. if _, ok := searchMap[v.Id]; !ok {
  407. itemJson, tmpErr := v.Source.MarshalJSON()
  408. if tmpErr != nil {
  409. err = tmpErr
  410. fmt.Println("movieJson err:", err)
  411. return
  412. }
  413. edbInfoItem := new(data_manage.EdbInfoList)
  414. tmpErr = json.Unmarshal(itemJson, &edbInfoItem)
  415. if tmpErr != nil {
  416. fmt.Println("json.Unmarshal movieJson err:", tmpErr)
  417. err = tmpErr
  418. return
  419. }
  420. if len(v.Highlight["EdbCode"]) > 0 {
  421. edbInfoItem.EdbCode = v.Highlight["EdbCode"][0]
  422. }
  423. if len(v.Highlight["EdbName"]) > 0 {
  424. edbInfoItem.EdbCode = v.Highlight["EdbName"][0]
  425. }
  426. list = append(list, edbInfoItem)
  427. searchMap[v.Id] = v.Id
  428. }
  429. }
  430. }
  431. return
  432. }
  433. // searchEdbInfoData 查询es中的指标数据
  434. func searchEdbInfoDataV2(indexName string, query elastic.Query, sortList []*elastic.FieldSort, from, size int) (total int64, list []*data_manage.EdbInfoList, err error) {
  435. total, err = searchEdbInfoDataTotal(indexName, query)
  436. if err != nil {
  437. return
  438. }
  439. // 获取列表数据
  440. list, err = searchEdbInfoDataList(indexName, query, sortList, from, size)
  441. if err != nil {
  442. return
  443. }
  444. return
  445. }
  446. // SearchEdbInfoDataByShared
  447. // @Description: 查询es中的指标数据
  448. // @author: Roc
  449. // @datetime 2024-11-29 10:22:25
  450. // @param keywordStr string
  451. // @param from int
  452. // @param size int
  453. // @param filterSource int
  454. // @param source int
  455. // @param frequency string
  456. // @param noPermissionEdbInfoIdList []int
  457. // @param noPermissionEdbClassifyIdList []int
  458. // @param collectEdbInfoIdList []int
  459. // @param edbTypeList []int
  460. // @param edbInfoType int 指标类型,0:ETA指标库(基础指标+计算指标);1:预测指标
  461. // @param edbAuth int 指标权限范围,0-全部;1-我的;2-公共
  462. // @param sysUserId int
  463. // @return total int64
  464. // @return list []*data_manage.EdbInfoList
  465. // @return err error
  466. 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) {
  467. indexName := utils.DATA_INDEX_NAME
  468. list = make([]*data_manage.EdbInfoList, 0)
  469. defer func() {
  470. if err != nil {
  471. fmt.Println("SearchEdbInfoData Err:", err.Error())
  472. }
  473. }()
  474. query := elastic.NewBoolQuery()
  475. //指标来源
  476. if len(sourceList) > 0 {
  477. termsList := make([]interface{}, 0)
  478. for _, v := range sourceList {
  479. termsList = append(termsList, v)
  480. }
  481. query = query.Must(elastic.NewTermsQuery("Source", termsList...))
  482. }
  483. // classifyIdList 指定分类下的指标
  484. if len(classifyIdList) > 0 {
  485. termsList := make([]interface{}, 0)
  486. for _, v := range classifyIdList {
  487. termsList = append(termsList, v)
  488. }
  489. query = query.Must(elastic.NewTermsQuery("ClassifyId", termsList...))
  490. }
  491. // 指标类型:0-基础+计算;1-基础指标;2-计算指标;3-预测指标
  492. if len(edbTypeList) > 0 {
  493. termsList := make([]interface{}, 0)
  494. for _, v := range edbTypeList {
  495. termsList = append(termsList, v)
  496. }
  497. query = query.Must(elastic.NewTermsQuery("EdbType", termsList...))
  498. }
  499. // 如果指定了分享状态,那么就添加分享状态的筛选
  500. // 0:全部,1:未共享,2:已共享
  501. switch edbShare {
  502. case 1:
  503. // 筛选 SharedUserIdList 为空的文档
  504. query = query.MustNot(elastic.NewExistsQuery("SharedUserIdList"))
  505. case 2:
  506. // 筛选 SharedUserIdList 不为空的文档
  507. query = query.Must(elastic.NewExistsQuery("SharedUserIdList"))
  508. }
  509. if edbInfoType >= 0 {
  510. query = query.Must(elastic.NewTermQuery("EdbInfoType", edbInfoType))
  511. }
  512. // 指标名称、编码匹配
  513. if keywordStr != `` {
  514. // 默认使用中文名字字段去匹配
  515. keywordNameKey := `EdbName`
  516. query = query.Must(elastic.NewMultiMatchQuery(keywordStr, keywordNameKey, "EdbCode"))
  517. }
  518. // 指标与用户的权限匹配
  519. {
  520. //指标权限范围,0-全部;1-我的;2-公共
  521. switch edbAuth {
  522. case 1:
  523. // 自己的指标
  524. query = query.Must(elastic.NewTermQuery(`SysUserId`, sysUserId))
  525. case 2:
  526. // 公开的指标
  527. query = query.Must(elastic.NewTermQuery(`PublicStatus`, utils.DataPublicSuccess))
  528. default:
  529. tmpShouldQuery := elastic.NewBoolQuery()
  530. // 自己的指标
  531. tmpShouldQuery = tmpShouldQuery.Should(elastic.NewTermQuery(`SysUserId`, sysUserId))
  532. // 分享给我的指标
  533. tmpShouldQuery = tmpShouldQuery.Should(elastic.NewTermsQuery(`SharedUserIdList`, sysUserId))
  534. //公开的指标
  535. tmpShouldQuery = tmpShouldQuery.Should(elastic.NewTermQuery(`PublicStatus`, utils.DataPublicSuccess))
  536. //shouldQuery = shouldQuery.Should(tmpShouldQuery)
  537. query = query.Must(tmpShouldQuery)
  538. }
  539. }
  540. // 排序
  541. sortList := make([]*elastic.FieldSort, 0)
  542. // 如果没有关键字,那么就走指标id倒序
  543. for orderKey, orderType := range sortMap {
  544. switch orderType {
  545. case "asc":
  546. sortList = append(sortList, elastic.NewFieldSort(orderKey).Asc())
  547. case "desc":
  548. sortList = append(sortList, elastic.NewFieldSort(orderKey).Desc())
  549. }
  550. }
  551. return searchEdbInfoDataV2(indexName, query, sortList, from, size)
  552. }
  553. // SearchEdbInfoDataByPublic
  554. // @Description: 查询es中的指标数据
  555. // @author: Roc
  556. // @datetime 2024-12-05 13:33:36
  557. // @param keywordStr string
  558. // @param from int
  559. // @param size int
  560. // @param edbPublicList []int
  561. // @param sourceList []int
  562. // @param classifyIdList []int
  563. // @param publicClassifyIdList []int
  564. // @param edbTypeList []int
  565. // @param edbInfoType int
  566. // @param edbAuth int
  567. // @param sysUserId int
  568. // @param sortMap map[string]string
  569. // @return total int64
  570. // @return list []*data_manage.EdbInfoList
  571. // @return err error
  572. 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) {
  573. indexName := utils.DATA_INDEX_NAME
  574. list = make([]*data_manage.EdbInfoList, 0)
  575. defer func() {
  576. if err != nil {
  577. fmt.Println("SearchEdbInfoData Err:", err.Error())
  578. }
  579. }()
  580. query := elastic.NewBoolQuery()
  581. //指标来源
  582. if len(sourceList) > 0 {
  583. termsList := make([]interface{}, 0)
  584. for _, v := range sourceList {
  585. termsList = append(termsList, v)
  586. }
  587. query = query.Must(elastic.NewTermsQuery("Source", termsList...))
  588. }
  589. // classifyIdList 指定分类下的指标
  590. if len(classifyIdList) > 0 {
  591. termsList := make([]interface{}, 0)
  592. for _, v := range classifyIdList {
  593. termsList = append(termsList, v)
  594. }
  595. query = query.Must(elastic.NewTermsQuery("ClassifyId", termsList...))
  596. }
  597. // publicClassifyIdList 指定公共分类下的指标
  598. if len(publicClassifyIdList) > 0 {
  599. termsList := make([]interface{}, 0)
  600. for _, v := range publicClassifyIdList {
  601. termsList = append(termsList, v)
  602. }
  603. query = query.Must(elastic.NewTermsQuery("EdbPublicClassifyId", termsList...))
  604. }
  605. // 指标类型:0-基础+计算;1-基础指标;2-计算指标;3-预测指标
  606. if len(edbTypeList) > 0 {
  607. termsList := make([]interface{}, 0)
  608. for _, v := range edbTypeList {
  609. termsList = append(termsList, v)
  610. }
  611. query = query.Must(elastic.NewTermsQuery("EdbType", termsList...))
  612. }
  613. // 如果指定了指标公开状态,那么就添加指标公开状态的筛选
  614. // 公开状态;0:未公开;1:审批中;2:已驳回;3:已公开
  615. if len(edbPublicList) > 0 {
  616. termsList := make([]interface{}, 0)
  617. for _, v := range edbPublicList {
  618. termsList = append(termsList, v)
  619. }
  620. query = query.Must(elastic.NewTermsQuery("PublicStatus", termsList...))
  621. }
  622. if edbInfoType >= 0 {
  623. query = query.Must(elastic.NewTermQuery("EdbInfoType", edbInfoType))
  624. }
  625. // 指标名称、编码匹配
  626. if keywordStr != `` {
  627. // 默认使用中文名字字段去匹配
  628. keywordNameKey := `EdbName`
  629. query = query.Must(elastic.NewMultiMatchQuery(keywordStr, keywordNameKey, "EdbCode"))
  630. }
  631. // 指标与用户的权限匹配
  632. {
  633. //指标权限范围,0-全部;1-我的;2-公共
  634. switch edbAuth {
  635. case 1:
  636. // 自己的指标
  637. query = query.Must(elastic.NewTermQuery(`SysUserId`, sysUserId))
  638. case 2:
  639. // 公开的指标
  640. query = query.Must(elastic.NewTermQuery(`PublicStatus`, utils.DataPublicSuccess))
  641. default:
  642. tmpShouldQuery := elastic.NewBoolQuery()
  643. // 自己的指标
  644. tmpShouldQuery = tmpShouldQuery.Should(elastic.NewTermQuery(`SysUserId`, sysUserId))
  645. // 分享给我的指标
  646. tmpShouldQuery = tmpShouldQuery.Should(elastic.NewTermsQuery(`SharedUserIdList`, sysUserId))
  647. //公开的指标
  648. tmpShouldQuery = tmpShouldQuery.Should(elastic.NewTermQuery(`PublicStatus`, utils.DataPublicSuccess))
  649. //shouldQuery = shouldQuery.Should(tmpShouldQuery)
  650. query = query.Must(tmpShouldQuery)
  651. }
  652. }
  653. // 排序
  654. sortList := make([]*elastic.FieldSort, 0)
  655. // 如果没有关键字,那么就走指标id倒序
  656. for orderKey, orderType := range sortMap {
  657. switch orderType {
  658. case "asc":
  659. sortList = append(sortList, elastic.NewFieldSort(orderKey).Asc())
  660. case "desc":
  661. sortList = append(sortList, elastic.NewFieldSort(orderKey).Desc())
  662. }
  663. }
  664. return searchEdbInfoDataV2(indexName, query, sortList, from, size)
  665. }
  666. // EsDeleteEdbInfoData 删除es中的指标数据
  667. func EsDeleteEdbInfoData(indexName, docId string) (err error) {
  668. defer func() {
  669. if err != nil {
  670. fmt.Println("EsDeleteEdbInfoData Err:", err.Error())
  671. }
  672. }()
  673. client := utils.EsClient
  674. resp, err := client.Delete().Index(indexName).Id(docId).Do(context.Background())
  675. fmt.Println(resp)
  676. if err != nil {
  677. return
  678. }
  679. if resp.Status == 0 {
  680. fmt.Println("删除成功")
  681. } else {
  682. fmt.Println("AddData", resp.Status, resp.Result)
  683. }
  684. return
  685. }