// Package data // @Author gmy 2024/8/22 9:44:00 package data import ( "eta/eta_api/models/data_manage" "eta/eta_api/utils" "fmt" "github.com/rdlucklib/rdluck_tools/paging" ) // RzdIndexData 获取睿姿得指标数据 func RzdIndexData(classifyId int, frequency string, currentIndex, startSize, pageSize int) (dataList []*data_manage.BaseFromRzdIndexList, err error) { // 获取指标 var condition string var pars []interface{} if classifyId >= 0 { // 递归查询子集分类 classifyIdList, err := RzdClassifyRecursiveQuery(classifyId) if err != nil { return nil, err } classifyIdList = append(classifyIdList, classifyId) interfaceClassifyIdList := make([]interface{}, len(classifyIdList)) for i, id := range classifyIdList { interfaceClassifyIdList[i] = id } condition += ` AND base_from_rzd_classify_id in (` + utils.GetOrmInReplace(len(classifyIdList)) + `)` pars = append(pars, interfaceClassifyIdList...) } if frequency != "" { condition += ` AND frequency=? ` pars = append(pars, frequency) } indexes, err := data_manage.GetRzdIndex(condition, pars) if err != nil { return nil, err } indexCodes := make([]string, 0) for _, v := range indexes { indexCodes = append(indexCodes, v.IndexCode) } indexCounts, e := data_manage.GetRzdIndexDataCountGroup(indexCodes) if e != nil { return nil, err } countMap := make(map[string]int) for _, v := range indexCounts { countMap[v.IndexCode] = v.Count } // 判断是否存在于指标库 edbCodeList, err := data_manage.GetEdbInfoByEdbCodeList(utils.DATA_SOURCE_RZD, indexCodes) if err != nil { return } edbCodeMap := make(map[string]*data_manage.EdbInfo) for _, v := range edbCodeList { edbCodeMap[v.EdbCode] = v } resultList := make([]*data_manage.BaseFromRzdIndexList, 0) for _, v := range indexes { product := new(data_manage.BaseFromRzdIndexList) product.BaseFromRzdIndexId = v.BaseFromRzdIndexId product.BaseFromRzdClassifyId = v.BaseFromRzdClassifyId product.Unit = v.Unit product.IndexCode = v.IndexCode product.IndexName = v.IndexName product.Frequency = v.Frequency product.CreateTime = v.CreateTime product.ModifyTime = v.ModifyTime edbInfo := edbCodeMap[v.IndexCode] if edbInfo != nil { product.EdbInfoId = edbInfo.EdbInfoId } total := countMap[v.IndexCode] page := paging.GetPaging(currentIndex, pageSize, total) indexDataList, e := data_manage.GetRzdIndexData(v.IndexCode, startSize, pageSize) if e != nil { return nil, err } if indexDataList == nil { indexDataList = make([]*data_manage.BaseFromRzdData, 0) } product.DataList = indexDataList product.Paging = page resultList = append(resultList, product) } return resultList, nil } // RzdClassifyRecursiveQuery 递归查询分类 func RzdClassifyRecursiveQuery(classifyId int) ([]int, error) { var classifyIdList []int // 查询当前分类 ID 的子分类 rzdClassifyList, err := GetRzdClassifyItemByParentId(classifyId) if err != nil { return nil, err } // 遍历子分类 for _, classify := range rzdClassifyList { // 将当前子分类的 ID 添加到列表中 classifyIdList = append(classifyIdList, classify.BaseFromRzdClassifyId) // 递归查询当前子分类的子分类 childIds, err := RzdClassifyRecursiveQuery(classify.BaseFromRzdClassifyId) if err != nil { return nil, err } // 合并子分类的 ID classifyIdList = append(classifyIdList, childIds...) } return classifyIdList, nil } func GetRzdIndexFrequency(classify int) ([]*string, error) { classifyIdList, err := RzdClassifyRecursiveQuery(classify) if err != nil { return nil, err } classifyIdList = append(classifyIdList, classify) frequencyNameList, err := data_manage.GetRzdIndexFrequency(classifyIdList) if err != nil { return nil, err } return frequencyNameList, nil } // RzdIndexAddValidate 指标添加校验 func RzdIndexAddValidate(req *data_manage.BaseFromRzdIndexBatchAddCheckReq) ([]*data_manage.BaseFromRzdIndexAndData, error) { var condition string var pars []interface{} if req.IsCheckAll { if len(req.ClassifyIdList) > 0 { condition += ` AND base_from_rzd_classify_id in (` + utils.GetOrmInReplace(len(req.ClassifyIdList)) + `)` for _, id := range req.ClassifyIdList { pars = append(pars, id) } } if len(req.FrequencyList) > 0 { condition += ` AND frequency in (` + utils.GetOrmInReplace(len(req.FrequencyList)) + `)` for _, frequency := range req.FrequencyList { pars = append(pars, frequency) } } if req.SearchParams != "" { condition += ` AND (index_name like ? or index_code like ?)` pars = append(pars, "%"+req.SearchParams+"%", "%"+req.SearchParams+"%") } if len(req.IndexCodes) > 0 { condition += ` AND index_code not in (` + utils.GetOrmInReplace(len(req.IndexCodes)) + `)` for _, code := range req.IndexCodes { pars = append(pars, code) } } } else { if len(req.IndexCodes) > 0 { condition += ` AND index_code in (` + utils.GetOrmInReplace(len(req.IndexCodes)) + `)` for _, code := range req.IndexCodes { pars = append(pars, code) } } } count, err := data_manage.GetRzdIndexNotExistEdbInfoCount(condition, pars) if err != nil { return nil, err } if count > 30 { return nil, fmt.Errorf("批量添加指标数量不得超过30个") } condition += ` ORDER BY base_from_rzd_index_id asc` indexList, err := data_manage.GetRzdIndexNotExistEdbInfoPage(condition, pars) if err != nil { return nil, err } return indexList, nil } // RzdIndexNameCheck 指标名称校验 func RzdIndexNameCheck(indexNames []string, resp []*data_manage.RzdNameCheckResult) ([]*data_manage.RzdNameCheckResult, 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 } // RzdIndexAdd 批量添加指标 func RzdIndexAdd(req data_manage.RzdIndexAddReq, 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("ruizide指标数据新增失败, Err: %s", err.Error()) utils.FileLog.Info(tips) } }() source := utils.DATA_SOURCE_RZD // 是否已有指标数据 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 // EdbInfoAdd方法已经新增es,这里不需要再新增??? // 新增es // go AddOrEditEdbInfoToEs(edbInfo.EdbInfoId) return } // GetRzdIndexInfo 获取指标信息-分页 func GetRzdIndexInfo(keyWord string, classifyIdList []string, frequencyList []string, currentIndex, startSize, pageSize int) (rzdIndexInfoList *data_manage.BaseFromRzdIndexPage, err error) { // 获取指标 var condition string var pars []interface{} if keyWord != "" { condition += ` AND (index_name like ? or index_code like ?)` pars = append(pars, "%"+keyWord+"%", "%"+keyWord+"%") } if len(classifyIdList) > 0 { condition += ` AND base_from_rzd_classify_id IN (` for _, v := range classifyIdList { condition += `?,` pars = append(pars, v) } condition = condition[:len(condition)-1] + `)` } if len(frequencyList) > 0 { condition += ` AND frequency IN (` for _, v := range frequencyList { condition += `?,` pars = append(pars, v) } condition = condition[:len(condition)-1] + `)` } count, err := data_manage.GetRzdIndexInfoCount(condition, pars) if err != nil { return nil, err } indexPage := data_manage.BaseFromRzdIndexPage{} page := paging.GetPaging(currentIndex, pageSize, count) if count <= 0 { indexPage.Paging = page return &indexPage, nil } condition += ` ORDER BY base_from_rzd_index_id asc` // 分页 condition += ` LIMIT ?, ?` pars = append(pars, startSize, pageSize) indexInfoPage, err := data_manage.GetRzdIndexInfoPage(condition, pars) if err != nil { return nil, err } var indexCodes []string for _, indexInfo := range indexInfoPage { indexCodes = append(indexCodes, indexInfo.IndexCode) } IndexDataList, err := data_manage.GetRzdLastUpdateTimeLastByIndexCode(indexCodes) if err != nil { return nil, err } var indexDataMap = make(map[string]*data_manage.BaseFromRzdData, 0) for _, data := range IndexDataList { indexDataMap[data.IndexCode] = data } for _, indexInfo := range indexInfoPage { if indexDataMap[indexInfo.IndexCode] == nil { continue } indexInfo.ModifyTimeMax = indexDataMap[indexInfo.IndexCode].DataTime indexInfo.Value = indexDataMap[indexInfo.IndexCode].Value } indexPage.List = indexInfoPage indexPage.Paging = page return &indexPage, nil } // GetRzdIndexDetail 获取指标详情 func GetRzdIndexDetail(indexCode string, currentIndex, startSize, pageSize int) (rzdIndexInfoList *data_manage.BaseFromRzdIndexList, err error) { // 获取指标 var condition string var pars []interface{} if indexCode != "" { condition += ` and index_code = ?` pars = append(pars, indexCode) } rzdIndexList, err := data_manage.GetRzdIndex(condition, pars) if err != nil { return nil, err } var rzdIndex *data_manage.BaseFromRzdIndexList if len(rzdIndexList) > 0 { rzdIndex = rzdIndexList[0] // 查询指标数据 var condition string var pars []interface{} condition += ` and index_code = ?` pars = append(pars, indexCode) count, err := data_manage.GetBaseFormRzdDataByConditionCount(condition, pars) if err != nil { return nil, err } pagingItem := paging.GetPaging(currentIndex, pageSize, count) if count <= 0 { rzdIndex.Paging = pagingItem } else { condition += ` ORDER BY data_time desc` condition += ` LIMIT ?, ?` pars = append(pars, startSize, pageSize) dataList, err := data_manage.GetBaseFormRzdDataByCondition(condition, pars) if err != nil { return nil, err } rzdIndex.Paging = pagingItem rzdIndex.DataList = dataList } // 查询是否在指标库 edbInfo, err := data_manage.GetEdbInfoByEdbCode(utils.DATA_SOURCE_RZD, indexCode) if err != nil && err.Error() != utils.ErrNoRow() { return nil, err } if edbInfo != nil { rzdIndex.EdbInfoId = edbInfo.EdbInfoId } } return rzdIndex, nil } // GetRzdIndexList 获取指标列表 func GetRzdIndexList(searchParams string) (rzdIndexInfoList []*data_manage.BaseFromRzdIndexList, err error) { // 获取指标 var condition string var pars []interface{} if searchParams != "" { condition += ` and index_code like ? or index_name like ?` pars = append(pars, "%"+searchParams+"%", "%"+searchParams+"%") } rzdIndexList, err := data_manage.GetRzdIndex(condition, pars) if err != nil { return nil, err } return rzdIndexList, nil }