Browse Source

test 最近新增指标及目录

hsun 1 year ago
parent
commit
90d2bdd9bf

+ 10 - 2
controller/index_data/jiayue_index.go

@@ -149,7 +149,15 @@ func (j *JiaYueIndexController) GetIndexFrequency(c *gin.Context) {
 	return
 }
 
-// GetRecentNewIndex TODO: 获取最近新增的指标
+// GetRecentNewIndex
+// @Description 获取最近新增的指标数据
+// @Success 200 {string} string "获取成功"
+// @Router /jiayue/new_index [post]
 func (j *JiaYueIndexController) GetRecentNewIndex(c *gin.Context) {
-
+	data, e := indexDataService.GetNewIndexAndDataFromJiaYue()
+	if e != nil {
+		resp.FailMsg("获取失败", "获取嘉悦最近新增指标数据失败, err: "+e.Error(), c)
+		return
+	}
+	resp.OkData("获取成功", data, c)
 }

+ 113 - 0
models/jiayue/category.go

@@ -0,0 +1,113 @@
+package jiayue
+
+import (
+	"database/sql"
+	"eta/eta_bridge/global"
+	"fmt"
+)
+
+var (
+	IndexMenuTableName       = "DICT_CATEGORY"  // 指标目录表
+	IndexMenuRelateTableName = "INDEX_CATEGORY" // 指标目录关联表
+)
+
+type DictCategory struct {
+	Id          int    `description:"主键"`
+	ParentId    int    `description:"父级目录ID"`
+	ParentName  string `description:"父级目录名称"`
+	Type        string `description:"目录类型"`
+	Code        string `description:"目录编码"`
+	Name        string `description:"目录名称"`
+	Icon        string `description:"目录图标"`
+	Sorting     int    `description:"排序"`
+	Description string `description:"描述"`
+	UserId      int    `description:"创建用户"`
+	Path        string `description:"目录全路径"`
+}
+
+type DictCategorySql struct {
+	Id          int            `description:"主键" json:"ID"`
+	ParentId    sql.NullInt32  `description:"父级目录ID" json:"PARENT_ID"`
+	ParentName  sql.NullString `description:"父级目录名称" json:"PARENT_NAME"`
+	Type        sql.NullString `description:"目录类型" json:"TYPE"`
+	Code        sql.NullString `description:"目录编码" json:"CODE"`
+	Name        sql.NullString `description:"目录名称" json:"NAME"`
+	Icon        sql.NullString `description:"目录图标" json:"ICON"`
+	Sorting     sql.NullInt32  `description:"排序" json:"SORTING"`
+	Description sql.NullString `description:"描述" json:"DESCRIPTION"`
+	UserId      sql.NullInt32  `description:"创建用户" json:"USER_ID"`
+	Path        sql.NullString `description:"目录全路径" json:"PATH"`
+}
+
+// GetDictCategory 获取指标目录
+func GetDictCategory(condition string, pars []interface{}, orderRule string) (categories []DictCategory, err error) {
+	defer func() {
+		if err != nil {
+			global.LOG.Info("GetDictCategory Err:" + err.Error())
+		}
+	}()
+
+	// 查询字段固定, 避免嘉悦加了字段这边报错
+	fields := "C.ID, C.PARENT_ID, C.PARENT_NAME, C.TYPE, C.CODE, C.NAME, C.ICON, C.SORTING, C.DESCRIPTION, C.USER_ID, C.PATH"
+	querySql := fmt.Sprintf(`SELECT %s FROM %s AS C JOIN %s AS R ON C.ID = R.INDEX_ID WHERE %s %s`, fields, IndexMenuTableName, IndexMenuRelateTableName, condition, orderRule)
+	order := ``
+	if orderRule != "" {
+		order += fmt.Sprintf(` ORDER BY %s`, orderRule)
+	}
+
+	list, e := execDictCategory(querySql, pars)
+	if e != nil {
+		err = fmt.Errorf("execDictCategory err: %s", e.Error())
+		return
+	}
+	categories = list
+	return
+}
+
+// execDictCategory 执行目录查询SQL
+func execDictCategory(querySql string, pars []interface{}) (categories []DictCategory, err error) {
+	stmt, e := global.OracleJy.Prepare(querySql)
+	if e != nil {
+		err = fmt.Errorf("execDictCategory Prepare err: %s", e.Error())
+		return
+	}
+	rows, e := stmt.Query(pars...) //输入sql中对应参数的值
+	if e != nil {
+		err = fmt.Errorf("execDictCategory Query err: %s", e.Error())
+		return
+	}
+	defer func() {
+		_ = rows.Close()
+	}()
+
+	for rows.Next() {
+		var t DictCategorySql
+		e = rows.Scan(&t.Id, &t.ParentId, &t.ParentName, &t.Type, &t.Code, &t.Name, &t.Icon, &t.Sorting, &t.Description, &t.UserId, &t.Path)
+		if e != nil {
+			err = fmt.Errorf("execDictCategory Scan err:" + e.Error())
+			return
+		}
+		v := DictCategory{
+			Id:          t.Id,
+			ParentId:    int(t.ParentId.Int32),
+			ParentName:  t.ParentName.String,
+			Type:        t.Type.String,
+			Code:        t.Code.String,
+			Name:        t.Name.String,
+			Icon:        t.Icon.String,
+			Sorting:     int(t.Sorting.Int32),
+			Description: t.Description.String,
+			UserId:      int(t.UserId.Int32),
+			Path:        t.Path.String,
+		}
+		categories = append(categories, v)
+	}
+	if e = rows.Err(); e != nil {
+		err = fmt.Errorf("execDictCategory rows err: %s", e.Error())
+		return
+	}
+	defer func() {
+		_ = stmt.Close()
+	}()
+	return
+}

+ 10 - 6
models/jiayue/dict.go

@@ -8,24 +8,28 @@ import (
 )
 
 var (
-	IndexTableName = "DICT_INDEX"
+	IndexTableName = "DICT_INDEX" // 指标主表
 )
 
-func GetDictIndex(condition string, pars []interface{}) (dictIndexList []DictIndex, err error) {
+func GetDictIndex(condition string, pars []interface{}, orderRule string) (dictIndexList []DictIndex, err error) {
 	defer func() {
 		if err != nil {
 			global.LOG.Info("获取桥接服务指标信息失败 Err:" + err.Error())
 		}
 	}()
-	selectVals := "ID, CODE, NAME, UNIT, FREQUENCY, DESCRIPTION, TABLE_NAME, SOURCE_TYPE, SOURCE_CODE, SOURCE_DESCRIPTION, INDUSTRY, TYPE, COMMODITY, SJB_ID, USER_ID, ROWS_COUNT, DATE_FIRST, DATE_LAST, TIME_LAST_UPDATE, TIME_LAST_REQUEST, PRIORITY, STATUS, SHORT_NAME, UPDATE_DESCRIPTION, FORECAST_FLAG, MANUAL_FLAG, VARIABLE_FLAG, MARKETDATA_FLAG, CREATE_USER, CREATE_TIME, UPDATE_USER, UPDATE_TIME"
-	sqlStatement := fmt.Sprintf("SELECT %s, 0 AS no FROM %s WHERE %s", selectVals, IndexTableName, condition)
+	fields := "ID, CODE, NAME, UNIT, FREQUENCY, DESCRIPTION, TABLE_NAME, SOURCE_TYPE, SOURCE_CODE, SOURCE_DESCRIPTION, INDUSTRY, TYPE, COMMODITY, SJB_ID, USER_ID, ROWS_COUNT, DATE_FIRST, DATE_LAST, TIME_LAST_UPDATE, TIME_LAST_REQUEST, PRIORITY, STATUS, SHORT_NAME, UPDATE_DESCRIPTION, FORECAST_FLAG, MANUAL_FLAG, VARIABLE_FLAG, MARKETDATA_FLAG, CREATE_USER, CREATE_TIME, UPDATE_USER, UPDATE_TIME"
+	order := ``
+	if orderRule != "" {
+		order += fmt.Sprintf(` ORDER BY %s`, orderRule)
+	}
+	sqlStatement := fmt.Sprintf("SELECT %s, 0 AS no FROM %s WHERE %s %s", fields, IndexTableName, condition, order)
+	global.LOG.Info("GetDictIndex SQL: %s", sqlStatement)
 	dictIndexList, err = getDictIndex(sqlStatement, pars)
 	if err != nil {
-		fmt.Printf("查询指标信息失败 %s", err)
 		err = fmt.Errorf("查询指标信息失败 %s", err)
 		return
 	}
-	fmt.Printf("查询指标信息成功")
+	//fmt.Printf("查询指标信息成功")
 	return
 }
 

+ 24 - 9
models/response/index_data.go

@@ -5,24 +5,39 @@ import (
 	"time"
 )
 
+// IndexResp 指标响应体
 type IndexResp struct {
-	Id             int         `description:"指标自增ID" json:"id"`
-	IndexCode      string      `description:"指标编码" json:"index_code"`
-	IndexName      string      `description:"指标名称" json:"index_name"`
-	Unit           string      `description:"单位" json:"unit"`
-	Frequency      string      `description:"频度" json:"frequency"`
-	LastDate       time.Time   `description:"指标最新时间" json:"last_date"`
-	LastUpdateTime time.Time   `description:"最新更新时间" json:"last_update_time"`
-	Status         int         `description:"指标状态" json:"status"`
-	IndexData      []IndexData `description:"指标数据" json:"index_data"`
+	Id             int           `description:"指标自增ID" json:"id"`
+	IndexCode      string        `description:"指标编码" json:"index_code"`
+	IndexName      string        `description:"指标名称" json:"index_name"`
+	Unit           string        `description:"单位" json:"unit"`
+	Frequency      string        `description:"频度" json:"frequency"`
+	LastDate       time.Time     `description:"指标最新时间" json:"last_date"`
+	LastUpdateTime time.Time     `description:"最新更新时间" json:"last_update_time"`
+	Status         int           `description:"指标状态" json:"status"`
+	IndexData      []IndexData   `description:"指标数据" json:"index_data"`
+	MenuData       IndexMenuData `description:"指标目录信息" json:"menu_data"`
 }
 
+// IndexData 指标数据
 type IndexData struct {
 	Val        float64   `json:"val"`
 	DataTime   time.Time `json:"data_time"`
 	UpdateTime time.Time `json:"update_time"`
 }
 
+// IndexMenuData 指标目录信息
+type IndexMenuData struct {
+	Id         int    `description:"目录ID" json:"id"`
+	Type       string `description:"目录类型" json:"type"`
+	Code       string `description:"目录编码" json:"code"`
+	Icon       string `description:"目录图标" json:"icon"`
+	Sort       int    `description:"排序" json:"sort"`
+	ParentId   int    `description:"父级目录ID" json:"parent_id"`
+	ParentName string `description:"父级目录名称" json:"parent_name"`
+	Path       string `description:"目录全路径" json:"path"`
+}
+
 // JiaYuePageIndexResp 指标分页列表响应体
 type JiaYuePageIndexResp struct {
 	Total int                `description:"数据总量"`

+ 1 - 0
routers/index_data.go

@@ -14,4 +14,5 @@ func InitIndexData(r *gin.RouterGroup) {
 	group.POST("jiayue/index_data", control.GetIndexData)
 	group.POST("jiayue/page_index", control.GetPageIndex)
 	group.POST("jiayue/frequency_list", control.GetIndexFrequency)
+	group.POST("jiayue/new_index", control.GetRecentNewIndex)
 }

+ 111 - 31
services/index_data/jiayue_platform.go

@@ -5,9 +5,11 @@ import (
 	"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 获取嘉悦指标信息
@@ -36,7 +38,7 @@ func GetIndexFromJiaYue(indexCode string, sourceArr []string) (data *response.In
 		indexPars = append(indexPars, sourceArr)
 	}
 
-	indexes, e := jiayue.GetDictIndex(indexCond, indexPars)
+	indexes, e := jiayue.GetDictIndex(indexCond, indexPars, "")
 	if e != nil {
 		err = fmt.Errorf("GetDictIndex err: %s", e.Error())
 		return
@@ -51,15 +53,11 @@ func GetIndexFromJiaYue(indexCode string, sourceArr []string) (data *response.In
 		return
 	}
 
-	data = new(response.IndexResp)
-	data.Id = index.Id
-	data.IndexCode = indexCode
-	data.IndexName = index.Name
-	data.Unit = index.Unit
-	data.Frequency = index.Frequency
-	data.LastDate = index.DateLast
-	data.LastUpdateTime = index.TimeLastUpdate
-	data.Status = index.Status
+	var (
+		indexData []jiayue.DictData
+		dictMenu  []jiayue.DictCategory
+	)
+	data = FormatItem2Resp(index, indexData, dictMenu)
 	return
 }
 
@@ -89,7 +87,7 @@ func GetIndexAndDataFromJiaYue(indexCode, startDate, endDate string, sourceArr [
 		indexPars = append(indexPars, sourceArr)
 	}
 
-	indexes, e := jiayue.GetDictIndex(indexCond, indexPars)
+	indexes, e := jiayue.GetDictIndex(indexCond, indexPars, "")
 	if e != nil {
 		err = fmt.Errorf("GetDictIndex err: %s", e.Error())
 		return
@@ -126,26 +124,9 @@ func GetIndexAndDataFromJiaYue(indexCode, startDate, endDate string, sourceArr [
 		err = fmt.Errorf("GetDictData err: %s", e.Error())
 		return
 	}
-	var list []response.IndexData
-	for _, v := range indexData {
-		t := response.IndexData{
-			Val:        v.IndexValue,
-			DataTime:   v.IndexDate,
-			UpdateTime: v.UpdateTime,
-		}
-		list = append(list, t)
-	}
-
-	data = new(response.IndexResp)
-	data.Id = index.Id
-	data.IndexData = list
-	data.IndexCode = indexCode
-	data.IndexName = index.Name
-	data.Unit = index.Unit
-	data.Frequency = index.Frequency
-	data.LastDate = index.DateLast
-	data.LastUpdateTime = index.TimeLastUpdate
-	data.Status = index.Status
+
+	var dictMenu []jiayue.DictCategory
+	data = FormatItem2Resp(index, indexData, dictMenu)
 	return
 }
 
@@ -194,3 +175,102 @@ func GetPageIndexesFromJiaYue(pageIndex, pageSize int, sourceArr []string, keywo
 	result = indexes
 	return
 }
+
+// GetNewIndexAndDataFromJiaYue 获取就近新增的指标和数据
+func GetNewIndexAndDataFromJiaYue() (indexList []*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)
+	// TODO:查询两个小时之前的数据(待定)
+	//timeBefore := time.Now().Local().Add(-2 * time.Hour)
+	//indexCond += ` CREATE_TIME <= :1 `
+	//indexPars = append(indexPars, timeBefore.Format(utils.FormatDateTime))
+
+	// TODO:测试
+	indexCond += ` UPDATE_TIME <= :1 `
+	indexPars = append(indexPars, time.Date(2023, 11, 3, 17, 0, 0, 0, time.Local).Format(utils.FormatDateTime))
+	sourceArr := []string{"webisite", "website", "website_gf"}
+	if len(sourceArr) > 0 {
+		indexCond += ` AND SOURCE_TYPE IN (:1)`
+		indexPars = append(indexPars, sourceArr)
+	}
+
+	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.GetDictCategory(menuCond, menuPars, "")
+		if e != nil {
+			err = fmt.Errorf("GetDictCategory err: %s", e.Error())
+			return
+		}
+
+		item := FormatItem2Resp(v, indexData, menus)
+		indexList = append(indexList, item)
+	}
+	return
+}
+
+// FormatItem2Resp 格式化指标响应体
+func FormatItem2Resp(item jiayue.DictIndex, dictData []jiayue.DictData, dictMenu []jiayue.DictCategory) (res *response.IndexResp) {
+	res = new(response.IndexResp)
+	res.Id = item.Id
+	res.IndexCode = item.Code
+	res.IndexName = item.Name
+	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]
+		res.MenuData.Id = firstMenu.Id
+		res.MenuData.Type = firstMenu.Type
+		res.MenuData.Code = firstMenu.Code
+		res.MenuData.Icon = firstMenu.Icon
+		res.MenuData.Sort = firstMenu.Sorting
+		res.MenuData.ParentId = firstMenu.ParentId
+		res.MenuData.ParentName = firstMenu.ParentName
+		res.MenuData.Path = firstMenu.Path
+	}
+	return
+}