123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- // Package data
- // @Author gmy 2024/8/22 9:44:00
- package data
- import (
- "eta/eta_api/models/data_manage"
- "eta/eta_api/utils"
- "fmt"
- )
- // FenWeiIndexAddValidate 指标添加校验
- func FenWeiIndexAddValidate(indexCodes []string) (*[]data_manage.FenWeiIndexCheckData, error) {
- // 根据指标编码获取指标库 指标信息
- edbInfos, err := data_manage.GetEdbInfoByEdbCodeList(utils.DATA_SOURCE_FENWEI, indexCodes)
- if err != nil {
- return nil, err
- }
- var respList []data_manage.FenWeiIndexCheckData
- if len(edbInfos) > 0 {
- for _, ebdInfo := range edbInfos {
- respList = append(respList, data_manage.FenWeiIndexCheckData{
- IndexCode: ebdInfo.EdbCode,
- IndexName: ebdInfo.EdbName,
- Unit: ebdInfo.Unit,
- Frequency: ebdInfo.Frequency,
- EdbInfoId: ebdInfo.EdbInfoId,
- ClassifyId: ebdInfo.ClassifyId,
- UniqueCode: ebdInfo.UniqueCode,
- })
- }
- }
- return &respList, nil
- }
- // FenWeiIndexNameCheck 指标名称校验
- func FenWeiIndexNameCheck(indexNames []string, resp []*data_manage.FenWeiNameCheckResult) ([]*data_manage.FenWeiNameCheckResult, 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
- }
- // FenWeiIndexAdd 批量添加指标
- func FenWeiIndexAdd(req data_manage.FenWeiIndexAddReq, 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())
- utils.FileLog.Info(tips)
- }
- }()
- source := utils.DATA_SOURCE_FENWEI
- // 是否已有指标数据
- 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
- }
- // GetFenWeiIndexInfo 获取指标信息-分页
- func GetFenWeiIndexInfo(keyWord string, classifyIdList []string, frequencyList []string) (fenWeiIndexInfoList []*data_manage.BaseFromFenweiIndex, err error) {
- // 获取指标
- var condition string
- var pars []interface{}
- if keyWord != "" {
- condition += ` AND CONCAT(index_name,index_code) LIKE '%` + keyWord + `%'`
- }
- if len(classifyIdList) > 0 {
- condition += ` AND 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] + `)`
- }
- fenWeiIndexInfoList, err = data_manage.GetFenWeiIndexInfoPage(condition, pars)
- if err != nil {
- return
- }
- return
- }
|