edb_info_es.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package data
  2. import (
  3. "eta_gn/eta_api/models/data_manage"
  4. "eta_gn/eta_api/models/data_manage/request"
  5. "eta_gn/eta_api/services/data/data_manage_permission"
  6. "eta_gn/eta_api/services/elastic"
  7. "eta_gn/eta_api/utils"
  8. "fmt"
  9. "github.com/rdlucklib/rdluck_tools/paging"
  10. "strconv"
  11. "strings"
  12. )
  13. // AddOrEditEdbInfoToEs 添加/修改ES中的指标
  14. func AddOrEditEdbInfoToEs(edbInfoId int) {
  15. var err error
  16. defer func() {
  17. if err != nil {
  18. utils.FileLog.Error("添加/修改ES中的指标失败; err:%s", err.Error())
  19. }
  20. }()
  21. //添加es
  22. itemInfo, err := data_manage.GetEdbInfoEsByCondition("AND edb_info_id=?", []interface{}{edbInfoId})
  23. if err != nil {
  24. return
  25. }
  26. obj := data_manage.EdbInfoShare{}
  27. list, _ := obj.GetListByEdbInfoId(edbInfoId)
  28. sharedList := make([]int, 0)
  29. for _, v := range list {
  30. sharedList = append(sharedList, v.SysUserId)
  31. }
  32. itemInfo.SharedUserIdList = sharedList
  33. err = elastic.EsAddOrEditEdbInfoData(utils.DATA_INDEX_NAME, strconv.Itoa(itemInfo.EdbInfoId), itemInfo)
  34. }
  35. // AddOrEditAllEdbInfoToEs 修复ES中的所有指标
  36. func AddOrEditAllEdbInfoToEs() {
  37. //添加es
  38. total, itemInfoList, _ := data_manage.GetEsEdbInfo("", []interface{}{}, 0, 100000)
  39. obj := data_manage.EdbInfoShare{}
  40. for k, itemInfo := range itemInfoList {
  41. list, _ := obj.GetListByEdbInfoId(itemInfo.EdbInfoId)
  42. sharedList := make([]int, 0)
  43. for _, v := range list {
  44. sharedList = append(sharedList, v.SysUserId)
  45. }
  46. itemInfo.SharedUserIdList = sharedList
  47. elastic.EsAddOrEditEdbInfoData(utils.DATA_INDEX_NAME, strconv.Itoa(itemInfo.EdbInfoId), itemInfo)
  48. fmt.Println("剩余", int(total)-k-1, "条指标数据")
  49. }
  50. }
  51. // DeleteEdbInfoToEs 删除ES中的指标
  52. func DeleteEdbInfoToEs(edbInfoId int) {
  53. //添加es
  54. go elastic.EsDeleteEdbInfoData(utils.DATA_INDEX_NAME, strconv.Itoa(edbInfoId))
  55. }
  56. // GetGeneralEdbEsSearchParams
  57. // @Description: 指标通用es搜索参数匹配
  58. // @author: Roc
  59. // @datetime 2024-12-23 11:10:42
  60. // @param edbTypeStr string 指标类型:0-基础和计算;1-基础指标;2-计算指标;3-预测指标
  61. // @param sysUserId int
  62. // @param reqEdbAuth int 指标权限范围,0-全部;1-我的;2-公共
  63. // @param edbCollect int 指标收藏状态:0-全部;1-已收藏
  64. // @param classifyId int
  65. // @return noPermissionEdbInfoIdList []int
  66. // @return noPermissionEdbClassifyIdList []int
  67. // @return collectEdbInfoIdList []int
  68. // @return searchClassifyIdList []int
  69. // @return searchPublicClassifyIdList []int
  70. // @return edbTypeList []int
  71. // @return edbInfoType int
  72. // @return edbAuth int 指标权限范围,0-全部;1-我的;2-公共
  73. // @return searchUserId int
  74. // @return err error
  75. // @return errMsg string
  76. func GetGeneralEdbEsSearchParams(edbTypeStr, frequency string, sysUserId, reqEdbAuth, edbCollect, classifyId int) (noPermissionEdbInfoIdList, noPermissionEdbClassifyIdList, collectEdbInfoIdList, searchClassifyIdList, searchPublicClassifyIdList, edbTypeList []int, edbInfoType, edbAuth, searchUserId int, frequencyList []string, err error, errMsg string) {
  77. // 指标类型数组:1-基础指标;2-计算指标;3-预测指标
  78. edbTypeList = make([]int, 0)
  79. edbInfoType = -1 // 指标范围
  80. frequencyList = []string{}
  81. if frequency != `` {
  82. frequencyList = strings.Split(frequency, `,`)
  83. }
  84. // 搜索用户id
  85. searchUserId = sysUserId
  86. {
  87. if edbTypeStr == `` || edbTypeStr == `0` {
  88. edbTypeList = []int{1, 2}
  89. edbInfoType = 0
  90. } else if edbTypeStr == `1` { // 数据查看(基础指标),不应该固定创建人的
  91. edbTypeList = []int{1}
  92. edbInfoType = 0
  93. searchUserId = 0
  94. } else {
  95. var hasEdb, hasPredictEdb bool
  96. tmpEdbTypeList := strings.Split(edbTypeStr, `,`)
  97. for _, v := range tmpEdbTypeList {
  98. edbType, tmpErr := strconv.Atoi(v)
  99. if tmpErr != nil {
  100. err = tmpErr
  101. errMsg = "EdbType异常"
  102. return
  103. }
  104. // 指标类型
  105. switch edbType {
  106. case 1, 2:
  107. hasEdb = true
  108. edbTypeList = append(edbTypeList, edbType)
  109. case 3:
  110. hasPredictEdb = true
  111. edbTypeList = []int{1, 2}
  112. }
  113. }
  114. // 只有数据查看和指标加工
  115. if hasEdb && !hasPredictEdb {
  116. edbInfoType = 0
  117. } else if !hasEdb && hasPredictEdb {
  118. // 只有预测指标
  119. edbInfoType = 1
  120. }
  121. }
  122. }
  123. edbAuth = reqEdbAuth
  124. // 筛选分类id列表
  125. searchClassifyIdList = make([]int, 0)
  126. searchPublicClassifyIdList = make([]int, 0)
  127. // 父级分类id
  128. if classifyId > 0 {
  129. switch edbAuth {
  130. case 1: // 1-我的;2-公共
  131. allChildClassifyItemList, tmpErr, _ := GetAllChildClassifyByParentId(classifyId)
  132. if tmpErr != nil {
  133. err = tmpErr
  134. return
  135. }
  136. searchClassifyIdList = append(searchClassifyIdList, classifyId)
  137. for _, v := range allChildClassifyItemList {
  138. searchClassifyIdList = append(searchClassifyIdList, v.ClassifyId)
  139. }
  140. case 2: // 1-我的;2-公共
  141. obj := data_manage.EdbPublicClassify{}
  142. allChildClassifyItemList, tmpErr, _ := obj.GetAllChildClassifyByParentId(classifyId)
  143. if tmpErr != nil {
  144. err = tmpErr
  145. return
  146. }
  147. searchPublicClassifyIdList = append(searchPublicClassifyIdList, classifyId)
  148. for _, v := range allChildClassifyItemList {
  149. searchPublicClassifyIdList = append(searchPublicClassifyIdList, v.EdbPublicClassifyId)
  150. }
  151. default:
  152. if edbTypeStr == `1` { // 如果仅仅是基础指标,那么也需要查询分类
  153. allChildClassifyItemList, tmpErr, _ := GetAllChildClassifyByParentId(classifyId)
  154. if tmpErr != nil {
  155. err = tmpErr
  156. return
  157. }
  158. searchClassifyIdList = append(searchClassifyIdList, classifyId)
  159. for _, v := range allChildClassifyItemList {
  160. searchClassifyIdList = append(searchClassifyIdList, v.ClassifyId)
  161. }
  162. }
  163. }
  164. }
  165. // 无权限指标 和 无权限指标分类id(只考虑)
  166. noPermissionEdbInfoIdList, noPermissionEdbClassifyIdList, err = data_manage_permission.GetUserAllEdbAndClassifyNoPermissionListV2(sysUserId)
  167. if err != nil {
  168. return
  169. }
  170. // 收藏的指标id
  171. collectEdbInfoIdList = make([]int, 0)
  172. if edbCollect == 1 {
  173. collectEdbInfoIdList, err = data_manage.GetUserAllCollectEdbInfoIdList(sysUserId)
  174. if err != nil {
  175. return
  176. }
  177. }
  178. return
  179. }
  180. // GetAllGeneralEdbInfoListByGeneralEdbEsSearchReq
  181. // @Description: 获取所有指标列表(通用)
  182. // @author: Roc
  183. // @datetime 2024-12-23 11:31:51
  184. // @param req request.GeneralEdbEsSearchReq
  185. // @param userId int
  186. // @return edbInfoList []*data_manage.EdbInfoList
  187. // @return err error
  188. // @return errMsg string
  189. func GetAllGeneralEdbInfoListByGeneralEdbEsSearchReq(req request.GeneralEdbEsSearchReq, userId int) (edbInfoList []*data_manage.EdbInfoList, err error, errMsg string) {
  190. keyword := req.Keyword
  191. if keyword == `` {
  192. keyword = req.KeyWord
  193. }
  194. // 获取es搜索参数
  195. noPermissionEdbInfoIdList, noPermissionEdbClassifyIdList, collectEdbInfoIdList, searchClassifyIdList, searchPublicClassifyIdList, edbTypeList, edbInfoType, edbAuth, searchUserId, frequencyList, err, errMsg := GetGeneralEdbEsSearchParams(req.EdbType, req.Frequency, userId, req.EdbAuth, req.EdbCollect, req.ClassifyId)
  196. if err != nil {
  197. return
  198. }
  199. sortMap := make(map[string]string)
  200. // 如果没有搜索关键词,则默认根据指标编码倒序排序
  201. if keyword == `` {
  202. sortMap["EdbInfoId"] = `desc`
  203. }
  204. notFrequencyList := make([]string, 0)
  205. if req.NotFrequency != "" {
  206. notFrequencyList = strings.Split(req.NotFrequency, ",")
  207. }
  208. _, edbInfoList, err = getAllEdbInfoDataByGeneralEdbEsSearch(keyword, 1, req.FilterSource, req.Source, frequencyList, notFrequencyList, noPermissionEdbInfoIdList, noPermissionEdbClassifyIdList, collectEdbInfoIdList, searchClassifyIdList, searchPublicClassifyIdList, edbTypeList, edbInfoType, edbAuth, searchUserId, sortMap)
  209. if err != nil {
  210. return
  211. }
  212. return
  213. }
  214. // getAllEdbInfoDataByGeneralEdbEsSearch
  215. // @Description: 获取所有的指标列表(通用es搜索接口)
  216. // @author: Roc
  217. // @datetime 2024-12-23 11:30:33
  218. // @param keyword string
  219. // @param currPage int
  220. // @param filterSource int
  221. // @param source int
  222. // @param frequency string
  223. // @param noPermissionEdbInfoIdList []int
  224. // @param noPermissionEdbClassifyIdList []int
  225. // @param collectEdbInfoIdList []int
  226. // @param searchClassifyIdList []int
  227. // @param searchPublicClassifyIdList []int
  228. // @param edbTypeList []int
  229. // @param edbInfoType int
  230. // @param edbAuth int
  231. // @param sysUserId int
  232. // @param sortMap map[string]string
  233. // @return total int64
  234. // @return list []*data_manage.EdbInfoList
  235. // @return err error
  236. func getAllEdbInfoDataByGeneralEdbEsSearch(keyword string, currPage, 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) {
  237. // 每页获取数据的数量
  238. pageSize := 5000
  239. var startSize int
  240. if currPage <= 0 {
  241. currPage = 1
  242. }
  243. startSize = paging.StartIndex(currPage, pageSize)
  244. // 普通的搜索
  245. total, list, err = elastic.SearchEdbInfoData(keyword, startSize, pageSize, filterSource, source, frequencyList, notFrequencyList, noPermissionEdbInfoIdList, noPermissionEdbClassifyIdList, collectEdbInfoIdList, searchClassifyIdList, searchPublicClassifyIdList, edbTypeList, edbInfoType, edbAuth, sysUserId, sortMap)
  246. if err != nil {
  247. return
  248. }
  249. page := paging.GetPaging(currPage, pageSize, int(total))
  250. if !page.IsEnd {
  251. _, nextList, tmpErr := getAllEdbInfoDataByGeneralEdbEsSearch(keyword, page.NextIndex, filterSource, source, frequencyList, notFrequencyList, noPermissionEdbInfoIdList, noPermissionEdbClassifyIdList, collectEdbInfoIdList, searchClassifyIdList, searchPublicClassifyIdList, edbTypeList, edbInfoType, edbAuth, sysUserId, sortMap)
  252. if tmpErr != nil {
  253. err = tmpErr
  254. return
  255. }
  256. list = append(list, nextList...)
  257. }
  258. return
  259. }