123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- // Package data
- // @Author gmy 2024/8/12 16:44:00
- package data
- import (
- "eta/eta_api/models/data_manage"
- "eta/eta_api/utils"
- "fmt"
- "github.com/beego/beego/v2/core/logs"
- "github.com/rdlucklib/rdluck_tools/paging"
- )
- // GetIndexPage 获取指标分页列表
- func GetIndexPage(classifyId string, searchParam string, currentIndex, pageSize int) (*data_manage.BaseFromLyIndexPage, error) {
- count, err := data_manage.GetLyIndexCount(classifyId, searchParam)
- if err != nil {
- return nil, err
- }
- lyIndexPage := data_manage.BaseFromLyIndexPage{}
- page := paging.GetPaging(currentIndex, pageSize, count)
- if count <= 0 {
- lyIndexPage.Paging = page
- return &lyIndexPage, nil
- }
- lyIndexList, err := data_manage.GetLyIndexPage(classifyId, searchParam, currentIndex, pageSize)
- if err != nil {
- return nil, err
- }
- // 获取指标编码列表
- var indexCodes []string
- for _, v := range lyIndexList {
- indexCodes = append(indexCodes, v.IndexCode)
- }
- // 获取指标数据
- lyIndexData, err := data_manage.GetLyLastUpdateTimeLastByIndexCode(indexCodes)
- if err != nil {
- return nil, err
- }
- // 将lyIndexData转换为map
- lyIndexDataMap := make(map[string]*data_manage.BaseFromLyData)
- for _, v := range lyIndexData {
- lyIndexDataMap[v.IndexCode] = v
- }
- lyData, err := data_manage.GetLyLastDataTimeByIndexCode(indexCodes)
- if err != nil {
- return nil, err
- }
- // 将lyData转换为map
- lyDataMap := make(map[string]*data_manage.BaseFromLyData)
- for _, v := range lyData {
- lyDataMap[v.IndexCode] = v
- }
- // 查询指标库是否已添加 不做另外补偿维护index表中的edb_exist字段,直接去查,因为维护会改变指标库指标添加代码结构
- edbInfoList, err := data_manage.GetEdbInfoByEdbCodeList(utils.DATA_SOURCE_LY, indexCodes)
- if err != nil {
- return nil, err
- }
- edbInfoMap := make(map[string]*data_manage.EdbInfo)
- for _, v := range edbInfoList {
- edbInfoMap[v.EdbCode] = v
- }
- if len(lyIndexData) > 0 {
- for _, v := range lyIndexList {
- if lyIndexDataMap[v.IndexCode] != nil {
- toYmd := utils.TimeFormatToYmd(lyIndexDataMap[v.IndexCode].ModifyTime)
- v.ModifyTimeMax = toYmd
- }
- if lyDataMap[v.IndexCode] != nil {
- v.Value = lyDataMap[v.IndexCode].Value
- }
- if edbInfoMap[v.IndexCode] != nil {
- v.EdbExist = utils.IS_YES
- }
- }
- }
- lyIndexPage.List = lyIndexList
- lyIndexPage.Paging = page
- return &lyIndexPage, nil
- }
- // GetIndexDataPage 获取指标分页数据列表
- func GetIndexDataPage(indexId, startSize, pageSize int) (*data_manage.BaseFromLyDataPage, error) {
- count, err := data_manage.GetLyDataCountByIndexId(indexId)
- if err != nil {
- return nil, err
- }
- lyDataPage := data_manage.BaseFromLyDataPage{}
- page := paging.GetPaging(startSize, pageSize, count)
- if count <= 0 {
- lyDataPage.Paging = page
- return &lyDataPage, nil
- }
- dataList, err := data_manage.GetLyDataPageByIndexId(indexId, startSize, pageSize)
- if err != nil {
- return nil, err
- }
- lyDataPage.List = dataList
- lyDataPage.Paging = page
- return &lyDataPage, nil
- }
- // LyIndexAddValidate 指标添加校验
- func LyIndexAddValidate(indexCodes []string) (*[]data_manage.IndexCheckData, error) {
- // 根据指标编码获取指标库 指标信息
- edbInfos, err := data_manage.GetEdbInfoByEdbCodeList(utils.DATA_SOURCE_LY, indexCodes)
- if err != nil {
- return nil, err
- }
- var respList []data_manage.IndexCheckData
- if len(edbInfos) > 0 {
- for _, ebdInfo := range edbInfos {
- respList = append(respList, data_manage.IndexCheckData{
- IndexCode: ebdInfo.EdbCode,
- IndexName: ebdInfo.EdbName,
- Unit: ebdInfo.Unit,
- Frequency: ebdInfo.Frequency,
- EdbInfoId: ebdInfo.EdbInfoId,
- ClassifyId: ebdInfo.ClassifyId,
- UniqueCode: ebdInfo.UniqueCode,
- })
- }
- }
- return &respList, nil
- }
- // LyIndexNameCheck 指标名称校验
- func LyIndexNameCheck(indexNames []string, resp []*data_manage.NameCheckResult) ([]*data_manage.NameCheckResult, error) {
- // 重名校验
- edbList, e := data_manage.GetEdbInfoByNameArr(indexNames, utils.EDB_INFO_TYPE)
- if e != nil {
- return nil, e
- }
- nameExists := make(map[string]bool)
- for _, edbInfo := range edbList {
- nameExists[edbInfo.EdbName] = true
- }
- if len(nameExists) > 0 {
- for _, v := range resp {
- v.Exist = nameExists[v.IndexName]
- }
- }
- return resp, nil
- }
- type LyIndexAddReq struct {
- EdbCode string `description:"指标编码"`
- EdbName string `description:"指标名称"`
- Frequency string `description:"频度"`
- Unit string `description:"单位"`
- ClassifyId int `description:"分类ID"`
- AdminId int `description:"管理员ID"`
- AdminRealName string `description:"管理员名称"`
- }
- // LyIndexAdd 批量添加指标
- func LyIndexAdd(req LyIndexAddReq, lang string) (edb *data_manage.EdbInfo, err error, errMsg string, skip bool) {
- if req.EdbCode == "" {
- err = fmt.Errorf("指标ID为空")
- return
- }
- defer func() {
- if err != nil {
- tips := fmt.Sprintf("BloombergIndexSource2Edb新增失败, Err: %s", err.Error())
- logs.Info(tips)
- utils.FileLog.Info(tips)
- }
- }()
- source := utils.DATA_SOURCE_LY
- // 是否已有指标数据
- dataList, e := data_manage.GetEdbDataAllByEdbCode(req.EdbCode, source, utils.DATA_SUB_SOURCE_EDB, utils.EDB_DATA_LIMIT)
- if e != nil {
- err = fmt.Errorf("获取指标数据失败, Err: %s", e.Error())
- return
- }
- // 新增指标数据
- if len(dataList) == 0 {
- res, e := AddEdbData(source, req.EdbCode, req.Frequency)
- if e != nil {
- err = fmt.Errorf("index_lib: 新增指标数据失败, Err: %s", e.Error())
- return
- }
- if res == nil {
- err = fmt.Errorf("index_lib: 新增指标数据失败, res nil")
- return
- }
- if res.Ret != 200 {
- err = fmt.Errorf("index_lib: 新增指标数据失败, Ret: %d", res.Ret)
- return
- }
- }
- // 是否新增过指标
- exist, e := data_manage.GetEdbInfoByEdbCode(source, req.EdbCode)
- if e != nil && e.Error() != utils.ErrNoRow() {
- err = fmt.Errorf("获取指标是否存在失败, err: %s", e.Error())
- return
- }
- if exist != nil {
- skip = true
- return
- }
- // 开始结束时间
- var startDate, endDate string
- minMax, e := data_manage.GetEdbInfoMaxAndMinInfo(source, utils.DATA_SUB_SOURCE_EDB, req.EdbCode)
- if e != nil && e.Error() != utils.ErrNoRow() {
- err = fmt.Errorf("MinMax: 获取指标极值失败, err: %s", e.Error())
- return
- }
- if minMax != nil {
- startDate = minMax.MinDate
- endDate = minMax.MaxDate
- }
- // 新增指标到指标库
- 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)
- if e != nil {
- errMsg = msg
- err = fmt.Errorf("EdbInfo: 新增指标失败, err: %s", e.Error())
- return
- }
- edb = edbInfo
- // 标记原始指标为已添加
- /*err = data_manage.UpdateLyIndexEdbExist(req.EdbCode, utils.IS_YES)
- if err != nil {
- err = fmt.Errorf("BaseIndex: 标记已添加指标库失败, err: %s", err.Error())
- return
- }*/
- // EdbInfoAdd方法已经新增es,这里不需要再新增???
- // 新增es
- // go AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
- return
- }
|