Explorar el Código

修复国能数据同步问题并优化数据获取

- 添加修复数据功能,同步国能平台的原始指标编码
- 优化国能数据平台接口调用,支持分页查询
- 新增国能数据平台V2版本接口封装
- 更新相关模型和结构,以适应新的数据格式
Roc hace 6 meses
padre
commit
8b6b2f69eb

+ 5 - 0
init_serve/task.go

@@ -14,6 +14,11 @@ func InitTask() {
 		return
 	}
 
+	// 修复数据
+	if global.CONFIG.Gn.DataHost != `` {
+		index_data.FixData()
+	}
+
 	// 角色-用户-部门-分组同步
 	go services.ListenSyncRole()
 	go services.ListenSyncAdmin()

+ 1 - 0
models/gn/index.go

@@ -10,6 +10,7 @@ type IndexInfo struct {
 	Frequency         string `description:"频度"`
 	Unit              string `description:"单位"`
 	SourceName        string `description:"来源"`
+	SourceCode        string `description:"来源编码"`
 }
 
 // IndexData 指标数据

+ 77 - 3
models/index/edb_update_log.go

@@ -10,9 +10,9 @@ type EdbUpdateLog struct {
 	Id          int64     `gorm:"column:id;type:bigint(20) UNSIGNED;primaryKey;not null;" json:"id"`
 	OpDbName    string    `gorm:"column:op_db_name;type:varchar(255);comment:库名;default:NULL;" json:"op_db_name"` // 库名
 	OpTableName string    `gorm:"column:op_table_name;type:varchar(255);comment:表名;" json:"op_table_name"`        // 表名
-	OpType      string    `gorm:"column:op_type;type:varchar(16);comment:变更类型;default:NULL;" json:"op_type"`    // 变更类型
-	OldData     string    `gorm:"column:old_data;type:text;comment:历史数据;" json:"old_data"`                      // 历史数据
-	NewData     string    `gorm:"column:new_data;type:text;comment:新数据;" json:"new_data"`                        // 新数据
+	OpType      string    `gorm:"column:op_type;type:varchar(16);comment:变更类型;default:NULL;" json:"op_type"`      // 变更类型
+	OldData     string    `gorm:"column:old_data;type:text;comment:历史数据;" json:"old_data"`                        // 历史数据
+	NewData     string    `gorm:"column:new_data;type:text;comment:新数据;" json:"new_data"`                         // 新数据
 	CreateTime  time.Time `gorm:"column:create_time;type:timestamp;default:CURRENT_TIMESTAMP;" json:"create_time"`
 }
 
@@ -33,3 +33,77 @@ func (m *EdbUpdateLog) DeleteBeforeTenDayLog(beforeDate string) (err error) {
 
 	return
 }
+
+type EdbInfo struct {
+	EdbInfoId        int       `gorm:"primaryKey;autoIncrement;column:edb_info_id" description:"唯一标识"`
+	EdbInfoType      int       `gorm:"column:edb_info_type" description:"指标类型,0:普通指标,1:预测指标"`
+	SourceName       string    `gorm:"column:source_name" description:"来源名称"`
+	Source           int       `gorm:"column:source" description:"来源id"`
+	EdbCode          string    `gorm:"column:edb_code" description:"指标编码"`
+	EdbName          string    `gorm:"column:edb_name" description:"指标名称"`
+	EdbNameEn        string    `gorm:"column:edb_name_en" description:"英文指标名称"`
+	EdbNameSource    string    `gorm:"column:edb_name_source" description:"指标名称来源"`
+	Frequency        string    `gorm:"column:frequency" description:"频率"`
+	Unit             string    `gorm:"column:unit" description:"单位"`
+	UnitEn           string    `gorm:"column:unit_en" description:"英文单位"`
+	StartDate        string    `gorm:"column:start_date" description:"起始日期"`
+	EndDate          string    `gorm:"column:end_date" description:"终止日期"`
+	ClassifyId       int       `gorm:"column:classify_id" description:"分类id"`
+	SysUserId        int       `gorm:"column:sys_user_id" description:"系统用户ID"`
+	SysUserRealName  string    `gorm:"column:sys_user_real_name" description:"系统用户真实姓名"`
+	UniqueCode       string    `gorm:"column:unique_code" description:"指标唯一编码"`
+	CreateTime       time.Time `gorm:"column:create_time" description:"创建时间"`
+	ModifyTime       time.Time `gorm:"column:modify_time" description:"修改时间"`
+	BaseModifyTime   time.Time `gorm:"column:base_modify_time" description:"基础修改时间"`
+	MinValue         float64   `gorm:"column:min_value" description:"指标最小值"`
+	MaxValue         float64   `gorm:"column:max_value" description:"指标最大值"`
+	CalculateFormula string    `gorm:"column:calculate_formula" description:"计算公式"`
+	EdbType          int       `gorm:"column:edb_type" description:"指标类型:1:基础指标,2:计算指标"`
+	IsUpdate         int       `gorm:"column:is_update" description:"当天是否已更新,1:未更新,2:已更新"`
+	Sort             int       `gorm:"column:sort" description:"排序字段"`
+	LatestDate       string    `gorm:"column:latest_date" description:"数据最新日期(实际日期)"`
+	LatestValue      float64   `gorm:"column:latest_value" description:"数据最新值(实际值)"`
+	EndValue         float64   `gorm:"column:end_value" description:"数据的最新值(预测日期的最新值)"`
+	MoveType         int       `gorm:"column:move_type" description:"移动方式:1:领先(默认),2:滞后"`
+	MoveFrequency    string    `gorm:"column:move_frequency" description:"移动频度"`
+	NoUpdate         int8      `gorm:"column:no_update" description:"是否停止更新,0:继续更新;1:停止更新"`
+	ServerUrl        string    `gorm:"column:server_url" description:"服务器地址"`
+	ChartImage       string    `gorm:"column:chart_image" description:"图表图片"`
+	Calendar         string    `gorm:"column:calendar;default:公历" description:"公历/农历"`
+	EmptyType        int       `gorm:"column:empty_type" description:"空值处理类型(0查找前后35天,1不计算,2前值填充,3后值填充,4等于0)"`
+	MaxEmptyType     int       `gorm:"column:max_empty_type" description:"MAX、MIN公式空值处理类型(1、等于0;2、跳过空值)"`
+	DataDateType     string    `gorm:"column:data_date_type;size:255;default:交易日" description:"数据日期类型"`
+	ManualSave       int       `gorm:"column:manual_save" description:"是否有手动保存过上下限: 0-否; 1-是"`
+	TerminalCode     string    `gorm:"column:terminal_code" description:"终端编码,用于配置在机器上"`
+	DataUpdateTime   string    `gorm:"column:data_update_time" description:"最近一次数据发生变化的时间"`
+	ErDataUpdateDate string    `gorm:"column:er_data_update_date" description:"本次更新,数据发生变化的最早日期"`
+	SourceIndexName  string    `gorm:"column:source_index_name" description:"数据源中的指标名称"`
+	SubSource        int       `gorm:"column:sub_source" description:"子数据来源:0:经济数据库,1:日期序列"`
+	SubSourceName    string    `gorm:"column:sub_source_name" description:"子数据来源名称"`
+	IndicatorCode    string    `gorm:"column:indicator_code" description:"指标代码"`
+	StockCode        string    `gorm:"column:stock_code" description:"证券代码"`
+	Extra            string    `gorm:"column:extra" description:"指标的额外配置"`
+	OriginalEdbCode  string    `description:"指标原始编码"`
+}
+
+// GetAllBaseEdbInfo
+// @Description: 获取所有基础指标(只有国能数据节点的数据)
+// @return item
+// @return err
+func GetAllBaseEdbInfo() (item []*EdbInfo, err error) {
+	sql := ` SELECT * FROM edb_info WHERE 1=1 AND "edb_info_type" = ? AND "edb_type" = ?`
+	pars := []interface{}{0, 1}
+	err = global.MYSQL["index"].Raw(sql, pars...).Find(&item).Error
+
+	return
+}
+
+// Update
+// @Description: 更新指定参数
+// @receiver m
+// @param cols
+// @return err
+func (m *EdbInfo) Update(cols []string) (err error) {
+	err = global.MYSQL["data"].Select(cols).Updates(m).Error
+	return
+}

+ 46 - 5
services/index_data/gn_base_data_platform.go

@@ -67,11 +67,12 @@ type GnEdbIndexResp struct {
 	INDEXORIGINCODE string      `json:"INDEX_ORIGIN_CODE" description:"原始指标编码"`
 	DATAUNIT        string      `json:"DATA_UNIT" description:"数据单位"`
 	//INDEXDESC        interface{} `json:"INDEX_DESC" description:"指标简介"`
-	CREATEORIGINTIME string      `json:"CREATE_ORIGIN_TIME" description:"原始创建时间"`
-	UPDATEORIGINTIME string      `json:"UPDATE_ORIGIN_TIME" description:"原始修改时间"`
-	UPDATETIME       string      `json:"UPDATE_TIME" description:"修改时间"`
-	INDEXORIGINNAME  string      `json:"INDEX_ORIGIN_NAME" description:"原始指标名称"`
-	DES              interface{} `json:"DES" description:"备注"`
+	CREATEORIGINTIME string `json:"CREATE_ORIGIN_TIME" description:"原始创建时间"`
+	UPDATEORIGINTIME string `json:"UPDATE_ORIGIN_TIME" description:"原始修改时间"`
+	UPDATETIME       string `json:"UPDATE_TIME" description:"修改时间"`
+	INDEXORIGINNAME  string `json:"INDEX_ORIGIN_NAME" description:"原始指标名称"`
+	//DES              interface{} `json:"DES" description:"备注"`
+	UNIQUE_ID string `json:"UNIQUE_ID" description:"唯一id"`
 }
 
 type IndexInfoReq struct {
@@ -148,9 +149,11 @@ type GnEdbIndexValueResp struct {
 	//CREATEORIGINTIME interface{} `json:"CREATE_ORIGIN_TIME" description:"原始创建时间"`
 	ID         int    `json:"ID" description:""`
 	CREATETIME string `json:"CREATE_TIME" description:"创建时间"`
+	UNIQUE_ID  string `json:"UNIQUE_ID" description:"唯一id"`
 }
 
 type IndexValueReq struct {
+	UniqueId string `json:"UNIQUE_ID"`
 	IndexId  string `json:"INDEX_ID"`
 	DataDate string `json:"DATA_DATE"`
 	Size     int    `json:"size"`
@@ -442,3 +445,41 @@ func getJson3() []byte {
 
 	return []byte(str)
 }
+
+// GetIndexInfoList
+// @Description: 获取指标列表
+// @param param
+// @param num
+// @return resp
+// @return err
+func GetIndexInfoListV2(param IndexInfoV2Req) (resp GnEdbIndexListResp, err error) {
+	defer func() {
+		if err != nil {
+			go alarm_msg.SendAlarmMsg("更新SMM的token失败;ERR:"+err.Error(), 3)
+		}
+	}()
+
+	urlStr := global.CONFIG.Gn.DataEdbListUrl
+
+	// 获取token
+	token, err := GetGnAccessToken(false)
+	if err != nil {
+		return
+	}
+
+	// 请求
+	result, err := HttpPostGn(urlStr, token, param, 0)
+	if err != nil {
+		return
+	}
+
+	//result := getJson2()
+
+	//  解析响应结果
+	err = json.Unmarshal(result, &resp)
+	if err != nil {
+		return
+	}
+
+	return
+}

+ 10 - 0
services/index_data/gn_base_data_platform_v2.go

@@ -0,0 +1,10 @@
+package index_data
+
+type IndexInfoV2Req struct {
+	//IndexId  string `json:"INDEX_ID"`
+	UpdateTime string `json:"UPDATE_TIME"`
+	Size       int    `json:"size"`
+	PageNo     int    `json:"pageNo"`
+	EdbCode    string `json:"INDEX_ORIGIN_CODE"`
+	SourceCode string `json:"DATA_SOURCE_CODE"`
+}

+ 97 - 2
services/index_data/gn_platform.go

@@ -3,6 +3,7 @@ package index_data
 import (
 	"eta_gn/eta_bridge/global"
 	"eta_gn/eta_bridge/models/gn"
+	"eta_gn/eta_bridge/models/index"
 	"eta_gn/eta_bridge/services/alarm_msg"
 	"eta_gn/eta_bridge/utils"
 	"fmt"
@@ -61,12 +62,13 @@ func GetPageIndexesFromGn(pageIndex, pageSize int, modifyTime, indexId string) (
 			ClassifyNameOne:   strings.TrimSpace(v.INDEXLEVELONE),
 			ClassifyNameTwo:   strings.TrimSpace(v.INDEXLEVELTWO),
 			ClassifyNameThree: strings.TrimSpace(v.INDEXLEVELTHREE),
-			DataIndexCode:     fmt.Sprint(v.INDEXID),
+			DataIndexCode:     fmt.Sprint(v.UNIQUE_ID),
 			SourceEdbCode:     strings.TrimSpace(v.INDEXORIGINCODE),
 			EdbName:           strings.TrimSpace(v.INDEXORIGINNAME),
 			Frequency:         strings.TrimSpace(v.DATAFREQ),
 			Unit:              strings.TrimSpace(v.DATAUNIT),
 			SourceName:        strings.TrimSpace(v.DATASOURCE),
+			SourceCode:        strings.TrimSpace(v.DATASOURCECODE),
 		})
 	}
 
@@ -102,7 +104,7 @@ func GetIndexAndDataFromGn(indexCode, startDate, endDate string) (list []gn.Inde
 	}
 
 	req := IndexValueReq{
-		IndexId:  indexCode,
+		UniqueId: indexCode,
 		DataDate: dataDate,
 		Size:     500,
 		PageNo:   1,
@@ -181,3 +183,96 @@ func getIndexAndDataFromGn(req IndexValueReq) (list []gn.IndexData, page *paging
 
 	return
 }
+
+func FixData() {
+	edbInfoList, err := index.GetAllBaseEdbInfo()
+	if err != nil {
+		fmt.Println("获取指标列表失败:", err)
+		return
+	}
+
+	sourceMap := map[int]string{
+		96:  `S02`,
+		97:  `S04`,
+		98:  `S03`,
+		99:  `S05`,
+		100: `S08`,
+		101: `S01`,
+	}
+
+	for _, v := range edbInfoList {
+		list, _, err := getData(1, 100, v.EdbCode, sourceMap[v.Source])
+		if err != nil {
+			fmt.Println("获取指标数据失败:", err)
+			continue
+		}
+		if len(list) > 0 {
+			uniqueCode := list[0].DataIndexCode
+			fmt.Println(v.EdbCode, " === ", uniqueCode)
+			//v.OriginalEdbCode = uniqueCode
+			//err = v.Update([]string{"OriginalEdbCode"})
+			//if err != nil {
+			//	fmt.Println("修改指标原始编码失败:", err)
+			//	continue
+			//}
+		}
+	}
+}
+
+func getData(pageIndex, pageSize int, edbCode, dataSource string) (list []gn.IndexInfo, page *paging.PagingItem, err error) {
+	defer func() {
+		if err != nil {
+			global.LOG.Info("GetPageIndexesFromGn Err: " + err.Error())
+			go alarm_msg.SendAlarmMsg("GetPageIndexesFromGn Err: "+err.Error(), 3)
+			return
+		}
+	}()
+
+	req := IndexInfoV2Req{
+		UpdateTime: "",
+		Size:       pageSize,
+		PageNo:     pageIndex,
+		EdbCode:    edbCode,
+		SourceCode: dataSource,
+	}
+	resp, err := GetIndexInfoListV2(req)
+	if err != nil {
+		return
+	}
+
+	page = paging.GetPaging(resp.Data.PageNo, resp.Data.Size, resp.Data.Total)
+	list = make([]gn.IndexInfo, 0)
+
+	for _, v := range resp.Data.Records {
+		//if v.INDEXLEVELTHREE == `` {
+		//	continue
+		//}
+
+		// 必须保证有一级目录
+		if v.INDEXLEVELONE == `` {
+			v.INDEXLEVELONE = `未分类`
+		}
+
+		//if v.INDEXLEVELTWO == `` {
+		//	v.INDEXLEVELTWO = `未分类`
+		//}
+		//if v.INDEXLEVELTHREE == `` {
+		//	v.INDEXLEVELTHREE = `未分类`
+		//}
+
+		list = append(list, gn.IndexInfo{
+			ClassifyNameOne:   strings.TrimSpace(v.INDEXLEVELONE),
+			ClassifyNameTwo:   strings.TrimSpace(v.INDEXLEVELTWO),
+			ClassifyNameThree: strings.TrimSpace(v.INDEXLEVELTHREE),
+			DataIndexCode:     fmt.Sprint(v.UNIQUE_ID),
+			SourceEdbCode:     strings.TrimSpace(v.INDEXORIGINCODE),
+			EdbName:           strings.TrimSpace(v.INDEXORIGINNAME),
+			Frequency:         strings.TrimSpace(v.DATAFREQ),
+			Unit:              strings.TrimSpace(v.DATAUNIT),
+			SourceName:        strings.TrimSpace(v.DATASOURCE),
+			SourceCode:        strings.TrimSpace(v.DATASOURCECODE),
+		})
+	}
+
+	return
+}