Jelajahi Sumber

新增投研资源库中的图表和指标映射表

xyxie 1 Minggu lalu
induk
melakukan
5f907c0355

+ 6 - 0
models/data_manage/chart_edb_mapping.go

@@ -70,6 +70,12 @@ func GetChartMappingList(chartInfoId int) (list []*ChartEdbMapping, err error) {
 	err = o.Raw(sql, chartInfoId).Find(&list).Error
 	return
 }
+func GetChartMappingListOrderByEdbInfoId(chartInfoId int) (list []*ChartEdbMapping, err error) {
+	o := global.DbMap[utils.DbNameIndex]
+	sql := `SELECT * FROM chart_edb_mapping WHERE chart_info_id = ? ORDER BY edb_info_id ASC`
+	err = o.Raw(sql, chartInfoId).Find(&list).Error
+	return
+}
 
 func GetChartEdbMappingListByChartInfoIds(chartInfoIds string) (list []*ChartEdbInfoMapping, err error) {
 	o := global.DbMap[utils.DbNameIndex]

+ 48 - 0
models/data_manage/forum_chart_edb_mapping.go

@@ -0,0 +1,48 @@
+package data_manage
+
+import (
+	"time"
+
+	"eta/eta_api/global"
+	"eta/eta_api/utils"
+)
+
+type ForumChartEdbMapping struct {
+	ID int `gorm:"column:id;primaryKey" description:"主键"`
+	ChartInfoId int 
+	EdbInfoIds string 
+	CreatedTime time.Time 
+	ModifyTime time.Time 
+}
+
+// 根据chartInfoId获取edbInfoIds
+func GetForumEdbInfoIdsByChartInfoId(chartInfoId int) (edbInfoIds string, err error) {
+	o := global.DbMap[utils.DbNameIndex]
+	sql := `SELECT edb_info_ids FROM forum_chart_edb_mapping WHERE chart_info_id = ?`
+	err = o.Raw(sql, chartInfoId).Scan(&edbInfoIds).Error
+	return
+}
+
+// 新增
+func AddForumChartEdbMapping(chartInfoId int, edbInfoIds string) (err error) {
+	o := global.DbMap[utils.DbNameIndex]
+	sql := `INSERT INTO forum_chart_edb_mapping (chart_info_id, edb_info_ids, created_time, modify_time) VALUES (?, ?, ?, ?)`
+	err = o.Exec(sql, chartInfoId, edbInfoIds, time.Now(), time.Now()).Error
+	return
+}
+
+// 更新
+func UpdateForumChartEdbMapping(chartInfoId int, edbInfoIds string) (err error) {
+	o := global.DbMap[utils.DbNameIndex]
+	sql := `UPDATE forum_chart_edb_mapping SET edb_info_ids = ?, modify_time = ? WHERE chart_info_id = ?`
+	err = o.Exec(sql, edbInfoIds, time.Now(), chartInfoId).Error
+	return
+}
+
+// 删除
+func DeleteForumChartEdbMapping(chartInfoId int) (err error) {
+	o := global.DbMap[utils.DbNameIndex]
+	sql := `DELETE FROM forum_chart_edb_mapping WHERE chart_info_id = ?`
+	err = o.Exec(sql, chartInfoId).Error
+	return
+}

+ 61 - 28
services/eta_forum/eta_forum_hub.go

@@ -64,7 +64,7 @@ func UploadChart(chartInfoId int, description string, uploaderInfo *system.Admin
 		err = fmt.Errorf("系统处理中,请稍后重试!")
 		return
 	}
-	utils.Rc.SetNX(cacheKey, 1, 30*time.Second)
+	utils.Rc.SetNX(cacheKey, 1, 10*time.Minute)
 	defer func() {
 		if err != nil {
 			go alarm_msg.SendAlarmMsg(fmt.Sprintf("上传图表至社区失败:Err:%v,ErrMsg:%s", err, errMsg), 3)
@@ -92,15 +92,17 @@ func UploadChart(chartInfoId int, description string, uploaderInfo *system.Admin
 
 	//查询图表指标
 	//获取原图表关联的指标信息列表
-	chartMappingList, err := data_manage.GetChartMappingList(chartInfoId)
+	chartMappingList, err := data_manage.GetChartMappingListOrderByEdbInfoId(chartInfoId)
 	if err != nil {
 		errMsg = "获取图表关联的指标信息失败"
 		err = fmt.Errorf("获取图表关联的指标信息失败,Err:" + err.Error())
 		return
 	}
 	edbIds := make([]int, 0)
+	edbInfoStr := ""
 	for _, v := range chartMappingList {
 		edbIds = append(edbIds, v.EdbInfoId)
+		edbInfoStr += strconv.Itoa(v.EdbInfoId) + ","
 	}
 	chartSeriesList := make([]*data_manage.ChartSeries, 0)
 	chartSeriesEdbList := make([]*data_manage.ChartSeriesEdbMapping, 0)
@@ -126,7 +128,7 @@ func UploadChart(chartInfoId int, description string, uploaderInfo *system.Admin
 		edbInfoDataList []*AddEdbDataReq
 	)
 	//查询指标详情
-	edbInfoList, edbMappingList, edbInfoDataList, err = GetEdbListByEdbInfoId(edbIds)
+	edbInfoList, edbMappingList, edbInfoDataList, err = GetEdbListByEdbInfoId(edbIds, true)
 	if err != nil {
 		errMsg = "获取指标详情失败"
 		err = fmt.Errorf("获取指标详情失败,Err:" + err.Error())
@@ -184,6 +186,13 @@ func UploadChart(chartInfoId int, description string, uploaderInfo *system.Admin
 			err = fmt.Errorf("更新图表ID失败,Err:" + err.Error())
 			return
 		}
+		// 新增投研资源库里的图表和指标绑定关系
+		err = data_manage.AddForumChartEdbMapping(chartInfoId, edbInfoStr)
+		if err != nil {
+			errMsg = "新增投研资源库里的图表和指标绑定关系失败"
+			err = fmt.Errorf("新增投研资源库里的图表和指标绑定关系失败,Err:" + err.Error())
+			return
+		}
 	}
 
 	return
@@ -197,7 +206,7 @@ func UpdateChart(chartInfoId int) (err error, errMsg string) {
 		err = fmt.Errorf("系统处理中,请稍后重试!")
 		return
 	}
-	utils.Rc.SetNX(cacheKey, 1, 30*time.Second)
+	utils.Rc.SetNX(cacheKey, 1, 10*time.Minute)
 	defer func() {
 		if err != nil {
 			go alarm_msg.SendAlarmMsg(fmt.Sprintf("同步图表信息至社区失败:Err:%v,ErrMsg:%s", err, errMsg), 3)
@@ -226,15 +235,17 @@ func UpdateChart(chartInfoId int) (err error, errMsg string) {
 
 	//查询图表指标
 	//获取原图表关联的指标信息列表
-	chartMappingList, err := data_manage.GetChartMappingList(chartInfoId)
+	chartMappingList, err := data_manage.GetChartMappingListOrderByEdbInfoId(chartInfoId)
 	if err != nil {
 		errMsg = "获取图表关联的指标信息失败"
 		err = fmt.Errorf("获取图表关联的指标信息失败,Err:" + err.Error())
 		return
 	}
 	edbIds := make([]int, 0)
+	edbInfoStr := ""
 	for _, v := range chartMappingList {
 		edbIds = append(edbIds, v.EdbInfoId)
+		edbInfoStr += strconv.Itoa(v.EdbInfoId) + ","
 	}
 	chartSeriesList := make([]*data_manage.ChartSeries, 0)
 	chartSeriesEdbList := make([]*data_manage.ChartSeriesEdbMapping, 0)
@@ -259,7 +270,19 @@ func UpdateChart(chartInfoId int) (err error, errMsg string) {
 		edbInfoDataList []*AddEdbDataReq
 	)
 	//查询指标详情
-	edbInfoList, edbMappingList, edbInfoDataList, err = GetEdbListByEdbInfoId(edbIds)
+	isGetEdbData := false
+	// 查询投研资源库里的图表和指标绑定关系
+	oldEdbInfoStr, err := data_manage.GetForumEdbInfoIdsByChartInfoId(chartInfoId)
+	if err != nil {
+		errMsg = "获取投研资源库里的图表和指标绑定关系失败"
+		err = fmt.Errorf("获取投研资源库里的图表和指标绑定关系失败,Err:" + err.Error())
+		return
+	}
+	if oldEdbInfoStr != edbInfoStr { // 图表更换过指标需要重新获取指标数据
+		isGetEdbData = true
+	}
+	//查询指标详情
+	edbInfoList, edbMappingList, edbInfoDataList, err = GetEdbListByEdbInfoId(edbIds, isGetEdbData)
 	if err != nil {
 		errMsg = "获取指标详情失败"
 		err = fmt.Errorf("获取指标详情失败,Err:" + err.Error())
@@ -306,10 +329,17 @@ func UpdateChart(chartInfoId int) (err error, errMsg string) {
 		return
 	}
 
+	// 更新投研资源库里的图表和指标绑定关系
+	err = data_manage.UpdateForumChartEdbMapping(chartInfoId, edbInfoStr)
+	if err != nil {
+		errMsg = "更新投研资源库里的图表和指标绑定关系失败"
+		err = fmt.Errorf("更新投研资源库里的图表和指标绑定关系失败,Err:" + err.Error())
+		return
+	}
 	return
 }
 
-func GetEdbListByEdbInfoId(edbInfoIds []int) (edbInfoList []*data_manage.EdbInfo, edbMappingList []*data_manage.EdbInfoCalculateMapping, edbInfoDataList []*AddEdbDataReq, err error) {
+func GetEdbListByEdbInfoId(edbInfoIds []int, isGetEdbData bool) (edbInfoList []*data_manage.EdbInfo, edbMappingList []*data_manage.EdbInfoCalculateMapping, edbInfoDataList []*AddEdbDataReq, err error) {
 	//查询指标信息
 	//查询指标映射
 	//查询所有指标数据
@@ -339,30 +369,31 @@ func GetEdbListByEdbInfoId(edbInfoIds []int) (edbInfoList []*data_manage.EdbInfo
 		err = fmt.Errorf(" GetEdbInfoByIdList err: %s", err.Error())
 		return
 	}
+	if isGetEdbData {
+		for _, v := range edbInfoList {
+			if _, ok := chartEdbInfoIdMap[v.EdbInfoId]; !ok {
+				continue
+			}
+			var dataList []*data_manage.EdbDataBase
+			if v.Source == utils.DATA_SOURCE_BUSINESS && utils.UseMongo {
+				dataList, err = data_manage.GetEdbDataBaseMongoByEdbInfoId(v.EdbInfoId, v.Source, v.SubSource)
+			} else if v.Source == utils.DATA_SOURCE_THS && v.SubSource == utils.DATA_SUB_SOURCE_HIGH_FREQUENCY && utils.UseMongo {
+				dataList, err = data_manage.GetThsHfEdbDataBaseMongoByEdbInfoId(v.EdbInfoId, v.Source, v.SubSource)
+			} else {
+				dataList, err = data_manage.GetEdbDataBaseByEdbInfoId(v.EdbInfoId, v.Source, v.SubSource)
+			}
 
-	for _, v := range edbInfoList {
-		if _, ok := chartEdbInfoIdMap[v.EdbInfoId]; !ok {
-			continue
-		}
-		var dataList []*data_manage.EdbDataBase
-		if v.Source == utils.DATA_SOURCE_BUSINESS && utils.UseMongo {
-			dataList, err = data_manage.GetEdbDataBaseMongoByEdbInfoId(v.EdbInfoId, v.Source, v.SubSource)
-		} else if v.Source == utils.DATA_SOURCE_THS && v.SubSource == utils.DATA_SUB_SOURCE_HIGH_FREQUENCY && utils.UseMongo {
-			dataList, err = data_manage.GetThsHfEdbDataBaseMongoByEdbInfoId(v.EdbInfoId, v.Source, v.SubSource)
-		} else {
-			dataList, err = data_manage.GetEdbDataBaseByEdbInfoId(v.EdbInfoId, v.Source, v.SubSource)
-		}
+			if err != nil {
+				err = fmt.Errorf("查询指标数据失败 Err: %s", err.Error())
+				return
+			}
 
-		if err != nil {
-			err = fmt.Errorf("查询指标数据失败 Err: %s", err.Error())
-			return
+			tmp := new(AddEdbDataReq)
+			tmp.EdbCode = v.EdbCode
+			tmp.EdbType = v.EdbType
+			tmp.DataList = dataList
+			edbInfoDataList = append(edbInfoDataList, tmp)
 		}
-
-		tmp := new(AddEdbDataReq)
-		tmp.EdbCode = v.EdbCode
-		tmp.EdbType = v.EdbType
-		tmp.DataList = dataList
-		edbInfoDataList = append(edbInfoDataList, tmp)
 	}
 	return
 }
@@ -763,6 +794,8 @@ func ChartBatchUpdateAndUpload(chartClassifyInfo *data_manage.ChartClassify, sys
 		offset = (i + 1) * pageSize
 	}
 	utils.FileLog.Info("批量上传已经上架的分类下的图表到资源库中完成, 新增图表数据总数:", success)
+	// 更新指标数据
+	utils.Rc.LPush(utils.CACHE_KEY_EDB_DATA_UPDATE_LOG, []byte("1"))
 	return
 }
 

+ 4 - 0
utils/constants.go

@@ -577,3 +577,7 @@ const (
 	ChartClassifyResourceStatusDown = 2 // 图表分类下架状态
 	ChartClassifyResourceStatusDefault = 0 // 图表分类默认状态
 )
+
+const (
+	CACHE_KEY_EDB_DATA_UPDATE_LOG = "eta_forum:edb_data_update_log" // 经济数据库数据更新日志
+)