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. 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. // SearchEdbInfoData
  129. // @Description: 查询es中的指标数据
  130. // @author: Roc
  131. // @datetime 2024-11-29 10:22:25
  132. // @param keywordStr string
  133. // @param from int
  134. // @param size int
  135. // @param filterSource int
  136. // @param source int
  137. // @param frequency string
  138. // @param noPermissionEdbInfoIdList []int
  139. // @param noPermissionEdbClassifyIdList []int
  140. // @param collectEdbInfoIdList []int
  141. // @param edbTypeList []int
  142. // @param edbInfoType int 指标类型,0:ETA指标库(基础指标+计算指标);1:预测指标
  143. // @param edbAuth int 指标权限范围,0-全部;1-我的;2-公共
  144. // @param sysUserId int
  145. // @return total int64
  146. // @return list []*data_manage.EdbInfoList
  147. // @return err error
  148. 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) {
  149. indexName := utils.DATA_INDEX_NAME
  150. list = make([]*data_manage.EdbInfoList, 0)
  151. defer func() {
  152. if err != nil {
  153. fmt.Println("SearchEdbInfoData Err:", err.Error())
  154. }
  155. }()
  156. highlight := elastic.NewHighlight()
  157. highlight = highlight.Fields(elastic.NewHighlighterField("EdbCode"), elastic.NewHighlighterField("EdbName"))
  158. highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  159. mustMap := make([]interface{}, 0)
  160. mustNotMap := make([]interface{}, 0)
  161. switch filterSource {
  162. case 2:
  163. mustMap = append(mustMap, map[string]interface{}{
  164. "term": map[string]interface{}{
  165. "Frequency.keyword": "月度",
  166. //"Frequency.keyword": "月度",
  167. },
  168. })
  169. case 3:
  170. case 4:
  171. mustMap = append(mustMap, map[string]interface{}{
  172. "term": map[string]interface{}{
  173. "EdbType": 1,
  174. },
  175. })
  176. case 5:
  177. mustMap = append(mustMap, map[string]interface{}{
  178. "term": map[string]interface{}{
  179. "Source": 6,
  180. },
  181. })
  182. case 6:
  183. mustMap = append(mustMap, map[string]interface{}{
  184. "match": map[string]interface{}{
  185. "Frequency.keyword": "年度",
  186. },
  187. })
  188. }
  189. //指标来源
  190. if source > 0 {
  191. mustMap = append(mustMap, map[string]interface{}{
  192. "term": map[string]interface{}{
  193. "Source": source,
  194. //"Frequency.keyword": "月度",
  195. },
  196. })
  197. }
  198. if frequency != "" {
  199. mustMap = append(mustMap, map[string]interface{}{
  200. "term": map[string]interface{}{
  201. "Frequency.keyword": frequency,
  202. //"Frequency.keyword": "月度",
  203. },
  204. })
  205. }
  206. // noPermissionEdbInfoIdList 无权限指标id
  207. if len(noPermissionEdbInfoIdList) > 0 {
  208. mustNotMap = append(mustNotMap, map[string]interface{}{
  209. "terms": map[string]interface{}{
  210. "EdbInfoId": noPermissionEdbInfoIdList,
  211. //"Frequency.keyword": "月度",
  212. },
  213. })
  214. }
  215. // noPermissionEdbInfoIdList 无权限指标id
  216. if len(noPermissionEdbClassifyIdList) > 0 {
  217. mustNotMap = append(mustNotMap, map[string]interface{}{
  218. "terms": map[string]interface{}{
  219. "ClassifyId": noPermissionEdbClassifyIdList,
  220. //"Frequency.keyword": "月度",
  221. },
  222. })
  223. }
  224. // collectEdbInfoIdList 收藏的指标id
  225. if len(collectEdbInfoIdList) > 0 {
  226. mustMap = append(mustMap, map[string]interface{}{
  227. "terms": map[string]interface{}{
  228. "EdbInfoId": collectEdbInfoIdList,
  229. },
  230. })
  231. }
  232. // searchClassifyIdList 指定分类id列表
  233. if len(searchClassifyIdList) > 0 {
  234. mustMap = append(mustMap, map[string]interface{}{
  235. "terms": map[string]interface{}{
  236. "ClassifyId": searchClassifyIdList,
  237. },
  238. })
  239. }
  240. // searchPublicClassifyIdList 指定公共分类id列表
  241. if len(searchPublicClassifyIdList) > 0 {
  242. mustMap = append(mustMap, map[string]interface{}{
  243. "terms": map[string]interface{}{
  244. "EdbPublicClassifyId": searchPublicClassifyIdList,
  245. },
  246. })
  247. }
  248. // 指标类型:0-基础+计算;1-基础指标;2-计算指标;3-预测指标
  249. if len(edbTypeList) > 0 {
  250. mustMap = append(mustMap, map[string]interface{}{
  251. "terms": map[string]interface{}{
  252. "EdbType": edbTypeList,
  253. },
  254. })
  255. }
  256. if edbInfoType >= 0 {
  257. mustMap = append(mustMap, map[string]interface{}{
  258. "term": map[string]interface{}{
  259. "EdbInfoType": edbInfoType,
  260. },
  261. })
  262. }
  263. shouldMapList := make([]map[string]interface{}, 0)
  264. // 指标名称、编码匹配
  265. if keywordStr != `` {
  266. // 默认使用中文名字字段去匹配
  267. keywordNameKey := `EdbName`
  268. shouldMap := map[string]interface{}{
  269. "should": []interface{}{
  270. map[string]interface{}{
  271. "match": map[string]interface{}{
  272. "EdbCode": keywordStr,
  273. //"Frequency.keyword": "月度",
  274. },
  275. },
  276. map[string]interface{}{
  277. "match": map[string]interface{}{
  278. keywordNameKey: keywordStr,
  279. //"Frequency.keyword": "月度",
  280. },
  281. },
  282. },
  283. }
  284. shouldMapList = append(shouldMapList, shouldMap)
  285. }
  286. // 指标与用户的权限匹配
  287. {
  288. shouldTermList := make([]map[string]interface{}, 0)
  289. //指标权限范围,0-全部;1-我的;2-公共
  290. switch edbAuth {
  291. case 1:
  292. // 自己的指标
  293. shouldTermList = append(shouldTermList, map[string]interface{}{
  294. "term": map[string]interface{}{
  295. "SysUserId": sysUserId,
  296. },
  297. })
  298. case 2:
  299. // 公开的指标
  300. shouldTermList = append(shouldTermList, map[string]interface{}{
  301. "term": map[string]interface{}{
  302. "PublicStatus": utils.DataPublicSuccess,
  303. },
  304. })
  305. default:
  306. // 自己的指标
  307. shouldTermList = append(shouldTermList, map[string]interface{}{
  308. "term": map[string]interface{}{
  309. "SysUserId": sysUserId,
  310. },
  311. })
  312. // 分享给我的指标
  313. shouldTermList = append(shouldTermList, map[string]interface{}{
  314. "terms": map[string]interface{}{
  315. "SharedUserIdList": []int{sysUserId},
  316. },
  317. })
  318. // 公开的指标
  319. shouldTermList = append(shouldTermList, map[string]interface{}{
  320. "term": map[string]interface{}{
  321. "PublicStatus": utils.DataPublicSuccess,
  322. },
  323. })
  324. }
  325. // 如果是包含了数据查看(基础指标)的搜索,那么就需要加上这个条件;否则只看自己的指标+共享指标+公开指标
  326. if edbInfoType == 0 && utils.InArrayByInt(edbTypeList, 1) {
  327. // 基础指标(数据查看)
  328. shouldTermList = append(shouldTermList, map[string]interface{}{
  329. "term": map[string]interface{}{
  330. "EdbType": 1,
  331. },
  332. }, map[string]interface{}{
  333. "term": map[string]interface{}{
  334. "EdbInfoType": 0,
  335. },
  336. })
  337. }
  338. shouldMap := map[string]interface{}{
  339. "should": shouldTermList,
  340. }
  341. shouldMapList = append(shouldMapList, shouldMap)
  342. }
  343. // 排序
  344. sortList := make([]interface{}, 0)
  345. // 如果没有关键字,那么就走指标id倒序
  346. for orderKey, orderType := range sortMap {
  347. sortEdbInfoId := map[string]interface{}{
  348. orderKey: map[string]interface{}{
  349. "order": orderType,
  350. },
  351. }
  352. sortList = append(sortList, sortEdbInfoId)
  353. }
  354. return searchEdbInfoData(indexName, mustMap, mustNotMap, shouldMapList, sortList, from, size)
  355. }
  356. // searchEdbInfoData 查询es中的指标数量
  357. func searchEdbInfoDataTotal(indexName string, query elastic.Query) (total int64, err error) {
  358. defer func() {
  359. if err != nil {
  360. fmt.Println("searchEdbInfoDataV2 Err:", err.Error())
  361. }
  362. }()
  363. client := utils.EsClient
  364. //根据条件数量统计
  365. requestTotalHits := client.Count(indexName).Query(query)
  366. total, err = requestTotalHits.Do(context.Background())
  367. if err != nil {
  368. return
  369. }
  370. return
  371. }
  372. // searchEdbInfoData 查询es中的指标数据
  373. func searchEdbInfoDataList(indexName string, query elastic.Query, sortList []*elastic.FieldSort, from, size int) (list []*data_manage.EdbInfoList, err error) {
  374. list = make([]*data_manage.EdbInfoList, 0)
  375. defer func() {
  376. if err != nil {
  377. fmt.Println("searchEdbInfoDataV2 Err:", err.Error())
  378. }
  379. }()
  380. client := utils.EsClient
  381. // 高亮
  382. //highlight := elastic.NewHighlight()
  383. //highlight = highlight.Fields(elastic.NewHighlighterField("EdbCode"), elastic.NewHighlighterField("EdbName"))
  384. //highlight = highlight.PreTags("<font color='red'>").PostTags("</font>")
  385. //request := client.Search(indexName).Highlight(highlight).From(from).Size(size) // sets the JSON request
  386. request := client.Search(indexName).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. }