package index_data import ( "eta/eta_bridge/global" "eta/eta_bridge/models/jiayue" "eta/eta_bridge/models/response" "eta/eta_bridge/services/alarm_msg" "eta/eta_bridge/utils" "fmt" "strconv" "strings" "time" ) // GetIndexFromJiaYue 获取嘉悦指标信息 func GetIndexFromJiaYue(indexCode string, sourceArr []string, indexCodeRequired int) (data *response.IndexResp, err error) { defer func() { if err != nil { global.LOG.Info("GetIndexFromJiaYue Err: " + err.Error()) go alarm_msg.SendAlarmMsg("GetIndexFromJiaYue Err: "+err.Error(), 3) return } }() indexCond := `` indexPars := make([]interface{}, 0) if indexCodeRequired == 1 { indexCond = ` SOURCE_CODE = :1 ` indexPars = append(indexPars, indexCode) } else { // 以ID为指标编码 indexId, _ := strconv.Atoi(indexCode) if indexId <= 0 { global.LOG.Info(indexCode + "指标ID不存在") return } indexCond = ` ID = :1` indexPars = append(indexPars, indexId) } if len(sourceArr) > 0 { indexCond += fmt.Sprintf(` AND SOURCE_TYPE IN (%s)`, utils.GetOracleInReplace(len(sourceArr))) for _, s := range sourceArr { indexPars = append(indexPars, s) } } indexes, e := jiayue.GetDictIndex(indexCond, indexPars, "") if e != nil { err = fmt.Errorf("GetDictIndex err: %s", e.Error()) return } if len(indexes) <= 0 { global.LOG.Info(indexCode + "指标信息不存在") return } index := indexes[0] if index.Id <= 0 { err = fmt.Errorf("指标信息有误") return } var ( indexData []jiayue.DictData dictMenu []jiayue.DictCategory ) data = FormatJiaYueIndexItem2Resp(index, indexData, dictMenu) return } // GetIndexAndDataFromJiaYue 获取指标和数据 func GetIndexAndDataFromJiaYue(indexCode, startDate, endDate string, sourceArr []string, indexCodeRequired int) (data *response.IndexResp, err error) { defer func() { if err != nil { global.LOG.Info("GetIndexAndDataFromJiaYue Err: " + err.Error()) go alarm_msg.SendAlarmMsg("GetIndexAndDataFromJiaYue Err: "+err.Error(), 3) return } }() indexCond := `` indexPars := make([]interface{}, 0) if indexCodeRequired == 1 { indexCond = ` SOURCE_CODE = :1 ` indexPars = append(indexPars, indexCode) } else { // 以ID为指标编码 indexId, _ := strconv.Atoi(indexCode) if indexId <= 0 { global.LOG.Info(indexCode + "指标ID不存在") return } indexCond = ` ID = :1` indexPars = append(indexPars, indexId) } if len(sourceArr) > 0 { indexCond += fmt.Sprintf(` AND SOURCE_TYPE IN (%s)`, utils.GetOracleInReplace(len(sourceArr))) for _, s := range sourceArr { indexPars = append(indexPars, s) } } indexes, e := jiayue.GetDictIndex(indexCond, indexPars, "") if e != nil { err = fmt.Errorf("GetDictIndex err: %s", e.Error()) return } if len(indexes) <= 0 { global.LOG.Info(indexCode + "指标信息不存在") return } index := indexes[0] if index.Id <= 0 { err = fmt.Errorf("指标信息有误") return } if index.TableName == "" { err = fmt.Errorf("指标信息数据表名为空") return } // 指标数据 dataCond := ` INDEX_ID = :1` dataPars := make([]interface{}, 0) dataPars = append(dataPars, index.Id) if startDate != "" { dataCond += ` AND INDEX_DATE >= TO_DATE (:1, 'yyyy-mm-dd')` dataPars = append(dataPars, startDate) } if endDate != "" { dataCond += ` AND INDEX_DATE <= TO_DATE (:1, 'yyyy-mm-dd')` dataPars = append(dataPars, endDate) } indexData, e := jiayue.GetDictData(index.TableName, dataCond, dataPars) if e != nil { err = fmt.Errorf("GetDictData err: %s", e.Error()) return } // 指标目录 menuCond := ` R.INDEX_ID = :1` menuPars := make([]interface{}, 0) menuPars = append(menuPars, index.Id) menus, e := jiayue.GetIndexCategory(menuCond, menuPars, "") if e != nil { err = fmt.Errorf("GetDictCategory err: %s", e.Error()) return } data = FormatJiaYueIndexItem2Resp(index, indexData, menus) return } // GetPageIndexesFromJiaYue 分页获取指标数据 func GetPageIndexesFromJiaYue(pageIndex, pageSize int, sourceArr []string, keyword, frequency string, sortField, sortRule int) (total int, result []jiayue.DictIndex, err error) { defer func() { if err != nil { global.LOG.Info("GetPageIndexesFromJiaYue Err: " + err.Error()) go alarm_msg.SendAlarmMsg("GetPageIndexesFromJiaYue Err: "+err.Error(), 3) return } }() indexCond := ` 1=1` indexPars := make([]interface{}, 0) // 筛选项 if len(sourceArr) > 0 { indexCond += fmt.Sprintf(` AND SOURCE_TYPE IN (%s)`, utils.GetOracleInReplace(len(sourceArr))) for _, s := range sourceArr { indexPars = append(indexPars, s) } } keyword = strings.TrimSpace(keyword) if keyword != "" { kw := fmt.Sprint("%", keyword, "%") indexCond += ` AND (ID LIKE :1 OR SOURCE_CODE LIKE :1 OR NAME LIKE :1)` indexPars = append(indexPars, kw, kw, kw) } if frequency != "" { indexCond += ` AND FREQUENCY = :1` indexPars = append(indexPars, frequency) } // 总记录数 t, e := jiayue.GetDictIndexCount(indexCond, indexPars) if e != nil { err = fmt.Errorf("GetDictIndexCount err: %s", e.Error()) return } total = t // 列表数据 indexes, e := jiayue.GetDictPageIndex(indexCond, indexPars, pageIndex, pageSize, sortField, sortRule) if e != nil { err = fmt.Errorf("GetDictPageIndex err: %s", e.Error()) return } result = indexes return } // GetNewIndexAndDataFromJiaYue 获取就近新增的指标和数据 func GetNewIndexAndDataFromJiaYue() (indexList []*response.IndexResp, err error) { defer func() { if err != nil { global.LOG.Info("GetNewIndexAndDataFromJiaYue Err: " + err.Error()) go alarm_msg.SendAlarmMsg("GetNewIndexAndDataFromJiaYue Err: "+err.Error(), 3) return } }() indexCond := ` 1=1` indexPars := make([]interface{}, 0) // 同步多久前至今的新指标 m := global.CONFIG.Business.JiaYueIndexSyncMinute if m == 0 { m = -30 // 默认半小时 } timeBefore := time.Now().Local().Add(time.Duration(m) * time.Minute).Format(utils.FormatDateTime) indexCond += ` AND CREATE_TIME >= TO_DATE (:1, 'yyyy-mm-dd hh24:mi:ss') ` indexPars = append(indexPars, timeBefore) // 测试 //endTime := time.Date(2023, 6, 8, 0, 0, 0, 0, time.Local).Format(utils.FormatDateTime) //indexCond += fmt.Sprintf(`CREATE_TIME <= TO_DATE ('%s', 'yyyy-mm-dd hh24:mi:ss')`, endTime) //sourceArr := []string{"bloomberg", "bloomberg_tmp"} //if len(sourceArr) > 0 { // str := `` // for _, s := range sourceArr { // str += fmt.Sprintf(`'%s',`, s) // } // str = strings.TrimRight(str, ",") // indexCond += fmt.Sprintf(` AND SOURCE_TYPE IN (%s)`, str) //} //indexCond = ` ID = :1` //indexPars = append(indexPars, 180399) indexes, e := jiayue.GetDictIndex(indexCond, indexPars, "CREATE_TIME ASC") if e != nil { err = fmt.Errorf("GetDictIndex err: %s", e.Error()) return } if len(indexes) <= 0 { global.LOG.Info("无新指标需要同步") return } // 每个指标对应的数据表不定, 所以这里还是用循环去查 indexList = make([]*response.IndexResp, 0) for _, v := range indexes { // 指标数据 //dataCond := " INDEX_ID = :1" //dataPars := make([]interface{}, 0) //dataPars = append(dataPars, v.Id) //indexData, e := jiayue.GetDictData(v.TableName, dataCond, dataPars) //if e != nil { // err = fmt.Errorf("GetDictData err: %s", e.Error()) // return //} // // 指标目录 //menuCond := ` R.INDEX_ID = :1` //menuPars := make([]interface{}, 0) //menuPars = append(menuPars, v.Id) //menus, e := jiayue.GetIndexCategory(menuCond, menuPars, "") //if e != nil { // err = fmt.Errorf("GetDictCategory err: %s", e.Error()) // return //} var ( indexData []jiayue.DictData dictMenu []jiayue.DictCategory ) item := FormatJiaYueIndexItem2Resp(v, indexData, dictMenu) indexList = append(indexList, item) } return } // FormatJiaYueIndexItem2Resp 格式化指标响应体 func FormatJiaYueIndexItem2Resp(item jiayue.DictIndex, dictData []jiayue.DictData, dictMenu []jiayue.DictCategory) (res *response.IndexResp) { res = new(response.IndexResp) res.Id = item.Id res.IndexCode = item.SourceCode // 注意这里的Code取的是表中的SOURCE_CODE res.IndexName = item.Name res.SourceType = item.SourceType res.Unit = item.Unit res.Frequency = item.Frequency res.LastDate = item.DateLast res.LastUpdateTime = item.TimeLastUpdate res.Status = item.Status if len(dictData) > 0 { for _, d := range dictData { res.IndexData = append(res.IndexData, response.IndexData{ Val: d.IndexValue, DataTime: d.IndexDate, UpdateTime: d.UpdateTime, }) } } if len(dictMenu) > 0 { firstMenu := dictMenu[0] r := FormatJiaYueDictCategory2Resp(firstMenu) res.MenuData = *r } return } // FormatJiaYueDictCategory2Resp 格式化指标目录响应体 func FormatJiaYueDictCategory2Resp(item jiayue.DictCategory) (res *response.IndexMenuData) { res = new(response.IndexMenuData) res.Id = item.Id res.Type = item.Type res.Code = item.Code res.Name = item.Name res.Icon = item.Icon res.Sort = item.Sorting res.ParentId = item.ParentId res.ParentName = item.ParentName res.Path = item.Path return } type JiaYueImportIndexResp struct { FirstMenu string `description:"一级目录"` SecondMenu string `description:"二级目录"` ThirdMenu string `description:"三级目录"` FourthMenu string `description:"四级目录"` FifthMenu string `description:"五级目录"` SixthMenu string `description:"六级目录"` Id int `description:"指标ID"` IndexName string `description:"指标名称"` IndexCode string `description:"指标编码"` Unit string `description:"单位"` Frequency string `description:"频度"` SourceType string `description:"来源"` } func GetJiaYueImportData(sourceArr []string) (items []JiaYueImportIndexResp, err error) { defer func() { if err != nil { global.LOG.Info("GetJiaYueImportData Err: " + err.Error()) go alarm_msg.SendAlarmMsg("GetJiaYueImportData Err: "+err.Error(), 3) return } }() // 获取指标 indexCond := ` 1=1` indexPars := make([]interface{}, 0) if len(sourceArr) == 0 { sourceArr = []string{"webisite", "website", "website_gf", "platts", "reuter", "reuter_vessel_q", "路透", "bloomberg", "bloomberg_tmp", "wind", "wind_p", "wind_stop", "wind_tmp"} } indexCond += fmt.Sprintf(` AND SOURCE_TYPE IN (%s)`, utils.GetOracleInReplace(len(sourceArr))) if len(sourceArr) > 0 { for _, s := range sourceArr { indexPars = append(indexPars, s) } } indexes, e := jiayue.GetDictIndex(indexCond, indexPars, "SOURCE_TYPE ASC, ID ASC") if e != nil { err = fmt.Errorf("GetDictIndex err: %s", e.Error()) return } if len(indexes) <= 0 { global.LOG.Info("无指标需要导入") return } index := indexes[0] if index.Id <= 0 { err = fmt.Errorf("指标信息有误") return } if index.TableName == "" { err = fmt.Errorf("指标信息数据表名为空") return } // 获取全部目录 categories, e := jiayue.GetDictCategory(``, make([]interface{}, 0), "") if e != nil { err = fmt.Errorf("获取嘉业指标目录列表失败, err: %s", e.Error()) return } categoryMap := make(map[int]jiayue.DictCategory) for _, v := range categories { categoryMap[v.Id] = v } items = make([]JiaYueImportIndexResp, 0) for _, v := range indexes { var t JiaYueImportIndexResp t.Id = v.Id t.IndexName = v.Name t.IndexCode = v.SourceCode t.Unit = v.Unit t.Frequency = v.Frequency t.SourceType = v.SourceType // 目录 menuCond := ` R.INDEX_ID = :1` menuPars := make([]interface{}, 0) menuPars = append(menuPars, t.Id) menus, e := jiayue.GetIndexCategory(menuCond, menuPars, "") if e != nil { err = fmt.Errorf("GetDictCategory err: %s", e.Error()) return } if len(menus) == 0 { continue } menu := menus[0] path := strings.TrimLeft(menu.Path, "/") // "path": "/61293/67576/67577/67579/67583" menuIds := make([]int, 0) menuIdArr := strings.Split(path, "/") for _, m := range menuIdArr { mid, _ := strconv.Atoi(m) menuIds = append(menuIds, mid) } // 取出目录名称 for k, m := range menuIds { mu := categoryMap[m] if mu.Id <= 0 { continue } if k == 0 { t.FirstMenu = mu.Name } if k == 1 { t.SecondMenu = mu.Name } if k == 2 { t.ThirdMenu = mu.Name } if k == 3 { t.FourthMenu = mu.Name } if k == 4 { t.FifthMenu = mu.Name } if k == 5 { t.SixthMenu = mu.Name } } items = append(items, t) } return }