base_from_rzd_index_service.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. // Package data
  2. // @Author gmy 2024/8/22 9:44:00
  3. package data
  4. import (
  5. "eta/eta_api/models/data_manage"
  6. "eta/eta_api/utils"
  7. "fmt"
  8. "github.com/rdlucklib/rdluck_tools/paging"
  9. )
  10. // RzdIndexData 获取睿姿得指标数据
  11. func RzdIndexData(classifyId int, frequency string, currentIndex, startSize, pageSize int) (dataList []*data_manage.BaseFromRzdIndexList, err error) {
  12. // 获取指标
  13. var condition string
  14. var pars []interface{}
  15. if classifyId >= 0 {
  16. // 递归查询子集分类
  17. classifyIdList, err := RzdClassifyRecursiveQuery(classifyId)
  18. if err != nil {
  19. return nil, err
  20. }
  21. classifyIdList = append(classifyIdList, classifyId)
  22. interfaceClassifyIdList := make([]interface{}, len(classifyIdList))
  23. for i, id := range classifyIdList {
  24. interfaceClassifyIdList[i] = id
  25. }
  26. condition += ` AND base_from_rzd_classify_id in (` + utils.GetOrmInReplace(len(classifyIdList)) + `)`
  27. pars = append(pars, interfaceClassifyIdList...)
  28. }
  29. if frequency != "" {
  30. condition += ` AND frequency=? `
  31. pars = append(pars, frequency)
  32. }
  33. indexes, err := data_manage.GetRzdIndex(condition, pars)
  34. if err != nil {
  35. return nil, err
  36. }
  37. indexCodes := make([]string, 0)
  38. for _, v := range indexes {
  39. indexCodes = append(indexCodes, v.IndexCode)
  40. }
  41. indexCounts, e := data_manage.GetRzdIndexDataCountGroup(indexCodes)
  42. if e != nil {
  43. return nil, err
  44. }
  45. countMap := make(map[string]int)
  46. for _, v := range indexCounts {
  47. countMap[v.IndexCode] = v.Count
  48. }
  49. // 判断是否存在于指标库
  50. edbCodeList, err := data_manage.GetEdbInfoByEdbCodeList(utils.DATA_SOURCE_RZD, indexCodes)
  51. if err != nil {
  52. return
  53. }
  54. edbCodeMap := make(map[string]*data_manage.EdbInfo)
  55. for _, v := range edbCodeList {
  56. edbCodeMap[v.EdbCode] = v
  57. }
  58. resultList := make([]*data_manage.BaseFromRzdIndexList, 0)
  59. for _, v := range indexes {
  60. product := new(data_manage.BaseFromRzdIndexList)
  61. product.BaseFromRzdIndexId = v.BaseFromRzdIndexId
  62. product.BaseFromRzdClassifyId = v.BaseFromRzdClassifyId
  63. product.Unit = v.Unit
  64. product.IndexCode = v.IndexCode
  65. product.IndexName = v.IndexName
  66. product.Frequency = v.Frequency
  67. product.CreateTime = v.CreateTime
  68. product.ModifyTime = v.ModifyTime
  69. edbInfo := edbCodeMap[v.IndexCode]
  70. if edbInfo != nil {
  71. product.EdbInfoId = edbInfo.EdbInfoId
  72. }
  73. total := countMap[v.IndexCode]
  74. page := paging.GetPaging(currentIndex, pageSize, total)
  75. indexDataList, e := data_manage.GetRzdIndexData(v.IndexCode, startSize, pageSize)
  76. if e != nil {
  77. return nil, err
  78. }
  79. if indexDataList == nil {
  80. indexDataList = make([]*data_manage.BaseFromRzdData, 0)
  81. }
  82. product.DataList = indexDataList
  83. product.Paging = page
  84. resultList = append(resultList, product)
  85. }
  86. return resultList, nil
  87. }
  88. // RzdClassifyRecursiveQuery 递归查询分类
  89. func RzdClassifyRecursiveQuery(classifyId int) ([]int, error) {
  90. var classifyIdList []int
  91. // 查询当前分类 ID 的子分类
  92. rzdClassifyList, err := GetRzdClassifyItemByParentId(classifyId)
  93. if err != nil {
  94. return nil, err
  95. }
  96. // 遍历子分类
  97. for _, classify := range rzdClassifyList {
  98. // 将当前子分类的 ID 添加到列表中
  99. classifyIdList = append(classifyIdList, classify.BaseFromRzdClassifyId)
  100. // 递归查询当前子分类的子分类
  101. childIds, err := RzdClassifyRecursiveQuery(classify.BaseFromRzdClassifyId)
  102. if err != nil {
  103. return nil, err
  104. }
  105. // 合并子分类的 ID
  106. classifyIdList = append(classifyIdList, childIds...)
  107. }
  108. return classifyIdList, nil
  109. }
  110. func GetRzdIndexFrequency(classify int) ([]*string, error) {
  111. classifyIdList, err := RzdClassifyRecursiveQuery(classify)
  112. if err != nil {
  113. return nil, err
  114. }
  115. classifyIdList = append(classifyIdList, classify)
  116. frequencyNameList, err := data_manage.GetRzdIndexFrequency(classifyIdList)
  117. if err != nil {
  118. return nil, err
  119. }
  120. return frequencyNameList, nil
  121. }
  122. // RzdIndexAddValidate 指标添加校验
  123. func RzdIndexAddValidate(req *data_manage.BaseFromRzdIndexBatchAddCheckReq) ([]*data_manage.BaseFromRzdIndexAndData, error) {
  124. var condition string
  125. var pars []interface{}
  126. if req.IsCheckAll {
  127. if len(req.ClassifyIdList) > 0 {
  128. condition += ` AND base_from_rzd_classify_id in (` + utils.GetOrmInReplace(len(req.ClassifyIdList)) + `)`
  129. for _, id := range req.ClassifyIdList {
  130. pars = append(pars, id)
  131. }
  132. }
  133. if len(req.FrequencyList) > 0 {
  134. condition += ` AND frequency in (` + utils.GetOrmInReplace(len(req.FrequencyList)) + `)`
  135. for _, frequency := range req.FrequencyList {
  136. pars = append(pars, frequency)
  137. }
  138. }
  139. if req.SearchParams != "" {
  140. condition += ` AND (index_name like ? or index_code like ?)`
  141. pars = append(pars, "%"+req.SearchParams+"%", "%"+req.SearchParams+"%")
  142. }
  143. if len(req.IndexCodes) > 0 {
  144. condition += ` AND index_code not in (` + utils.GetOrmInReplace(len(req.IndexCodes)) + `)`
  145. for _, code := range req.IndexCodes {
  146. pars = append(pars, code)
  147. }
  148. }
  149. } else {
  150. if len(req.IndexCodes) > 0 {
  151. condition += ` AND index_code in (` + utils.GetOrmInReplace(len(req.IndexCodes)) + `)`
  152. for _, code := range req.IndexCodes {
  153. pars = append(pars, code)
  154. }
  155. }
  156. }
  157. count, err := data_manage.GetRzdIndexNotExistEdbInfoCount(condition, pars)
  158. if err != nil {
  159. return nil, err
  160. }
  161. if count > 30 {
  162. return nil, fmt.Errorf("批量添加指标数量不得超过30个")
  163. }
  164. condition += ` ORDER BY base_from_rzd_index_id asc`
  165. indexList, err := data_manage.GetRzdIndexNotExistEdbInfoPage(condition, pars)
  166. if err != nil {
  167. return nil, err
  168. }
  169. return indexList, nil
  170. }
  171. // RzdIndexNameCheck 指标名称校验
  172. func RzdIndexNameCheck(indexNames []string, resp []*data_manage.RzdNameCheckResult) ([]*data_manage.RzdNameCheckResult, error) {
  173. // 重名校验
  174. edbList, e := data_manage.GetEdbInfoByNameArr(indexNames, utils.EDB_INFO_TYPE)
  175. if e != nil {
  176. return nil, e
  177. }
  178. nameExists := make(map[string]bool)
  179. for _, edbInfo := range edbList {
  180. nameExists[edbInfo.EdbName] = true
  181. }
  182. if len(nameExists) > 0 {
  183. for _, v := range resp {
  184. v.Exist = nameExists[v.IndexName]
  185. }
  186. }
  187. return resp, nil
  188. }
  189. // RzdIndexAdd 批量添加指标
  190. func RzdIndexAdd(req data_manage.RzdIndexAddReq, lang string) (edb *data_manage.EdbInfo, err error, errMsg string, skip bool) {
  191. if req.EdbCode == "" {
  192. err = fmt.Errorf("指标ID为空")
  193. return
  194. }
  195. defer func() {
  196. if err != nil {
  197. tips := fmt.Sprintf("ruizide指标数据新增失败, Err: %s", err.Error())
  198. utils.FileLog.Info(tips)
  199. }
  200. }()
  201. source := utils.DATA_SOURCE_RZD
  202. // 是否已有指标数据
  203. dataList, e := data_manage.GetEdbDataAllByEdbCode(req.EdbCode, source, utils.DATA_SUB_SOURCE_EDB, utils.EDB_DATA_LIMIT)
  204. if e != nil {
  205. err = fmt.Errorf("获取指标数据失败, Err: %s", e.Error())
  206. return
  207. }
  208. // 新增指标数据
  209. if len(dataList) == 0 {
  210. res, e := AddEdbData(source, req.EdbCode, req.Frequency)
  211. if e != nil {
  212. err = fmt.Errorf("index_lib: 新增指标数据失败, Err: %s", e.Error())
  213. return
  214. }
  215. if res == nil {
  216. err = fmt.Errorf("index_lib: 新增指标数据失败, res nil")
  217. return
  218. }
  219. if res.Ret != 200 {
  220. err = fmt.Errorf("index_lib: 新增指标数据失败, Ret: %d", res.Ret)
  221. return
  222. }
  223. }
  224. // 是否新增过指标
  225. exist, e := data_manage.GetEdbInfoByEdbCode(source, req.EdbCode)
  226. if e != nil && e.Error() != utils.ErrNoRow() {
  227. err = fmt.Errorf("获取指标是否存在失败, err: %s", e.Error())
  228. return
  229. }
  230. if exist != nil {
  231. skip = true
  232. return
  233. }
  234. // 开始结束时间
  235. var startDate, endDate string
  236. minMax, e := data_manage.GetEdbInfoMaxAndMinInfo(source, utils.DATA_SUB_SOURCE_EDB, req.EdbCode)
  237. if e != nil && e.Error() != utils.ErrNoRow() {
  238. err = fmt.Errorf("MinMax: 获取指标极值失败, err: %s", e.Error())
  239. return
  240. }
  241. if minMax != nil {
  242. startDate = minMax.MinDate
  243. endDate = minMax.MaxDate
  244. }
  245. // 新增指标到指标库
  246. edbInfo, e, msg, _ := EdbInfoAdd(source, utils.DATA_SUB_SOURCE_EDB, req.ClassifyId, req.EdbCode, req.EdbName, req.Frequency, req.Unit, startDate, endDate, req.AdminId, req.AdminRealName, lang)
  247. if e != nil {
  248. errMsg = msg
  249. err = fmt.Errorf("EdbInfo: 新增指标失败, err: %s", e.Error())
  250. return
  251. }
  252. edb = edbInfo
  253. // EdbInfoAdd方法已经新增es,这里不需要再新增???
  254. // 新增es
  255. // go AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
  256. return
  257. }
  258. // GetRzdIndexInfo 获取指标信息-分页
  259. func GetRzdIndexInfo(keyWord string, classifyIdList []string, frequencyList []string, currentIndex, startSize, pageSize int) (rzdIndexInfoList *data_manage.BaseFromRzdIndexPage, err error) {
  260. // 获取指标
  261. var condition string
  262. var pars []interface{}
  263. if keyWord != "" {
  264. condition += ` AND (index_name like ? or index_code like ?)`
  265. pars = append(pars, "%"+keyWord+"%", "%"+keyWord+"%")
  266. }
  267. if len(classifyIdList) > 0 {
  268. condition += ` AND base_from_rzd_classify_id IN (`
  269. for _, v := range classifyIdList {
  270. condition += `?,`
  271. pars = append(pars, v)
  272. }
  273. condition = condition[:len(condition)-1] + `)`
  274. }
  275. if len(frequencyList) > 0 {
  276. condition += ` AND frequency IN (`
  277. for _, v := range frequencyList {
  278. condition += `?,`
  279. pars = append(pars, v)
  280. }
  281. condition = condition[:len(condition)-1] + `)`
  282. }
  283. count, err := data_manage.GetRzdIndexInfoCount(condition, pars)
  284. if err != nil {
  285. return nil, err
  286. }
  287. indexPage := data_manage.BaseFromRzdIndexPage{}
  288. page := paging.GetPaging(currentIndex, pageSize, count)
  289. if count <= 0 {
  290. indexPage.Paging = page
  291. return &indexPage, nil
  292. }
  293. condition += ` ORDER BY base_from_rzd_index_id asc`
  294. // 分页
  295. condition += ` LIMIT ?, ?`
  296. pars = append(pars, startSize, pageSize)
  297. indexInfoPage, err := data_manage.GetRzdIndexInfoPage(condition, pars)
  298. if err != nil {
  299. return nil, err
  300. }
  301. var indexCodes []string
  302. for _, indexInfo := range indexInfoPage {
  303. indexCodes = append(indexCodes, indexInfo.IndexCode)
  304. }
  305. IndexDataList, err := data_manage.GetRzdLastUpdateTimeLastByIndexCode(indexCodes)
  306. if err != nil {
  307. return nil, err
  308. }
  309. var indexDataMap = make(map[string]*data_manage.BaseFromRzdData, 0)
  310. for _, data := range IndexDataList {
  311. indexDataMap[data.IndexCode] = data
  312. }
  313. for _, indexInfo := range indexInfoPage {
  314. if indexDataMap[indexInfo.IndexCode] == nil {
  315. continue
  316. }
  317. indexInfo.ModifyTimeMax = indexDataMap[indexInfo.IndexCode].DataTime
  318. indexInfo.Value = indexDataMap[indexInfo.IndexCode].Value
  319. }
  320. indexPage.List = indexInfoPage
  321. indexPage.Paging = page
  322. return &indexPage, nil
  323. }
  324. // GetRzdIndexDetail 获取指标详情
  325. func GetRzdIndexDetail(indexCode string, currentIndex, startSize, pageSize int) (rzdIndexInfoList *data_manage.BaseFromRzdIndexList, err error) {
  326. // 获取指标
  327. var condition string
  328. var pars []interface{}
  329. if indexCode != "" {
  330. condition += ` and index_code = ?`
  331. pars = append(pars, indexCode)
  332. }
  333. rzdIndexList, err := data_manage.GetRzdIndex(condition, pars)
  334. if err != nil {
  335. return nil, err
  336. }
  337. var rzdIndex *data_manage.BaseFromRzdIndexList
  338. if len(rzdIndexList) > 0 {
  339. rzdIndex = rzdIndexList[0]
  340. // 查询指标数据
  341. var condition string
  342. var pars []interface{}
  343. condition += ` and index_code = ?`
  344. pars = append(pars, indexCode)
  345. count, err := data_manage.GetBaseFormRzdDataByConditionCount(condition, pars)
  346. if err != nil {
  347. return nil, err
  348. }
  349. pagingItem := paging.GetPaging(currentIndex, pageSize, count)
  350. if count <= 0 {
  351. rzdIndex.Paging = pagingItem
  352. } else {
  353. condition += ` ORDER BY data_time desc`
  354. condition += ` LIMIT ?, ?`
  355. pars = append(pars, startSize, pageSize)
  356. dataList, err := data_manage.GetBaseFormRzdDataByCondition(condition, pars)
  357. if err != nil {
  358. return nil, err
  359. }
  360. rzdIndex.Paging = pagingItem
  361. rzdIndex.DataList = dataList
  362. }
  363. // 查询是否在指标库
  364. edbInfo, err := data_manage.GetEdbInfoByEdbCode(utils.DATA_SOURCE_RZD, indexCode)
  365. if err != nil && err.Error() != utils.ErrNoRow() {
  366. return nil, err
  367. }
  368. if edbInfo != nil {
  369. rzdIndex.EdbInfoId = edbInfo.EdbInfoId
  370. }
  371. }
  372. return rzdIndex, nil
  373. }
  374. // GetRzdIndexList 获取指标列表
  375. func GetRzdIndexList(searchParams string) (rzdIndexInfoList []*data_manage.BaseFromRzdIndexList, err error) {
  376. // 获取指标
  377. var condition string
  378. var pars []interface{}
  379. if searchParams != "" {
  380. condition += ` and index_code like ? or index_name like ?`
  381. pars = append(pars, "%"+searchParams+"%", "%"+searchParams+"%")
  382. }
  383. rzdIndexList, err := data_manage.GetRzdIndex(condition, pars)
  384. if err != nil {
  385. return nil, err
  386. }
  387. return rzdIndexList, nil
  388. }