Browse Source

fix: 多因子相关性

hsun 8 months ago
parent
commit
4cda949b5c

+ 11 - 0
controllers/data_manage/correlation/correlation_chart_classify.go

@@ -7,6 +7,7 @@ import (
 	"eta/eta_api/models/data_manage"
 	"eta/eta_api/models/system"
 	"eta/eta_api/services/data"
+	correlationServ "eta/eta_api/services/data/correlation"
 	"eta/eta_api/services/data/data_manage_permission"
 	"eta/eta_api/utils"
 	"fmt"
@@ -544,6 +545,15 @@ func (this *CorrelationChartClassifyController) DeleteChartClassify() {
 				}
 			}
 		}
+
+		// 删除图表关联
+		e = correlationServ.RemoveCorrelationRelate(chartInfo.ChartInfoId)
+		if e != nil {
+			br.Msg = "删除失败"
+			br.ErrMsg = fmt.Sprintf("删除相关性图表关联失败, %v", e)
+			return
+		}
+
 		//新增操作日志
 		{
 			chartLog := new(data_manage.ChartInfoLog)
@@ -560,6 +570,7 @@ func (this *CorrelationChartClassifyController) DeleteChartClassify() {
 			go data_manage.AddChartInfoLog(chartLog)
 		}
 	}
+
 	br.Ret = 200
 	br.Msg = "删除成功"
 	br.Success = true

+ 12 - 1
controllers/data_manage/correlation/correlation_chart_info.go

@@ -2714,6 +2714,7 @@ func (this *CorrelationChartInfoController) MultiFactorEdit() {
 		k := fmt.Sprintf("%d-%d", v.SeriesId, v.EdbInfoId)
 		edbUsed[k] = true
 	}
+
 	// 指标系列-图表关联
 	chartMappings := make([]*data_manage.FactorEdbSeriesChartMapping, 0)
 	{
@@ -2729,17 +2730,27 @@ func (this *CorrelationChartInfoController) MultiFactorEdit() {
 		}
 		chartMappings = items
 	}
+	existChartMappingIds := make([]int, 0) // 之前加的需要删除掉
+	updateChartMappings := make([]*data_manage.FactorEdbSeriesChartMapping, 0)
 	for _, v := range chartMappings {
+		if v.ChartInfoId == req.ChartInfoId {
+			existChartMappingIds = append(existChartMappingIds, v.FactorEdbSeriesChartMappingId)
+			continue
+		}
+
 		k := fmt.Sprintf("%d-%d", v.FactorEdbSeriesId, v.EdbInfoId)
 		v.EdbUsed = 0
 		if edbUsed[k] {
 			v.EdbUsed = 1
 		}
+		v.ChartInfoId = chartInfo.ChartInfoId
+		v.Source = chartSource
 		v.ModifyTime = time.Now().Local()
+		updateChartMappings = append(updateChartMappings, v)
 	}
 
 	// 更新图表/相关性图表/图表指标关联/指标系列图表关联
-	e = data_manage.UpdateMultiFactorCorrelationChartAndEdb(chartInfo, edbMappings, chartCorrelate, chartMappings, chartUpdateCols, correlateUpdateCols)
+	e = data_manage.UpdateMultiFactorCorrelationChartAndEdb(chartInfo, edbMappings, chartCorrelate, updateChartMappings, existChartMappingIds, chartUpdateCols, correlateUpdateCols)
 	if e != nil {
 		br.Msg = "保存失败"
 		br.ErrMsg = fmt.Sprintf("新增多因子相关性图表失败, Err: %v", e)

+ 15 - 4
models/data_manage/chart_info_correlation.go

@@ -1,6 +1,7 @@
 package data_manage
 
 import (
+	"eta/eta_api/utils"
 	"fmt"
 	"github.com/beego/beego/v2/client/orm"
 	"strings"
@@ -274,7 +275,7 @@ func CreateMultiFactorCorrelationChartAndEdb(chartInfo *ChartInfo, edbMappingLis
 }
 
 // UpdateMultiFactorCorrelationChartAndEdb 编辑多因子相关性图表
-func UpdateMultiFactorCorrelationChartAndEdb(chartInfo *ChartInfo, edbMappingList []*ChartEdbMapping, correlationInfo *ChartInfoCorrelation, chartMappings []*FactorEdbSeriesChartMapping, chartUpdateCols, correlateUpdateCols []string) (err error) {
+func UpdateMultiFactorCorrelationChartAndEdb(chartInfo *ChartInfo, edbMappingList []*ChartEdbMapping, correlationInfo *ChartInfoCorrelation, chartMappings []*FactorEdbSeriesChartMapping, existsChartMappingIds []int, chartUpdateCols, correlateUpdateCols []string) (err error) {
 	o := orm.NewOrmUsingDB("data")
 	tx, e := o.Begin()
 	if e != nil {
@@ -321,10 +322,20 @@ func UpdateMultiFactorCorrelationChartAndEdb(chartInfo *ChartInfo, edbMappingLis
 		return
 	}
 
+	// 删除原关联
+	chartMappingOb := new(FactorEdbSeriesChartMapping)
+	if len(existsChartMappingIds) > 0 {
+		sql = fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, chartMappingOb.TableName(), chartMappingOb.Cols().PrimaryId, utils.GetOrmInReplace(len(existsChartMappingIds)))
+		_, e = tx.Raw(sql, existsChartMappingIds).Exec()
+		if e != nil {
+			err = fmt.Errorf("clear chart mapping err: %v", e)
+			return
+		}
+	}
+
 	// 指标系列-图表关联
 	if len(chartMappings) > 0 {
-		chartMappingOb := new(FactorEdbSeriesChartMapping)
-		sql = fmt.Sprintf(`UPDATE %s SET %s = ?, %s = ? WHERE %s = ?`, chartMappingOb.TableName(), chartMappingOb.Cols().EdbUsed, chartMappingOb.Cols().ModifyTime, chartMappingOb.Cols().PrimaryId)
+		sql = fmt.Sprintf(`UPDATE %s SET %s = ?, %s = ?, %s = ?, %s = ? WHERE %s = ?`, chartMappingOb.TableName(), chartMappingOb.Cols().ChartInfoId, chartMappingOb.Cols().Source, chartMappingOb.Cols().EdbUsed, chartMappingOb.Cols().ModifyTime, chartMappingOb.Cols().PrimaryId)
 		p, e := o.Raw(sql).Prepare()
 		if e != nil {
 			err = fmt.Errorf("sql prepare err: %v", e)
@@ -334,7 +345,7 @@ func UpdateMultiFactorCorrelationChartAndEdb(chartInfo *ChartInfo, edbMappingLis
 			_ = p.Close()
 		}()
 		for _, v := range chartMappings {
-			_, e = p.Exec(v.EdbUsed, v.ModifyTime, v.FactorEdbSeriesChartMappingId)
+			_, e = p.Exec(v.ChartInfoId, v.Source, v.EdbUsed, v.ModifyTime, v.FactorEdbSeriesChartMappingId)
 			if e != nil {
 				err = fmt.Errorf("update exec err: %v", e)
 				return

+ 10 - 0
models/data_manage/factor_edb_series.go

@@ -94,6 +94,16 @@ func (m *FactorEdbSeries) MultiRemove(ids []int) (err error) {
 	return
 }
 
+func (m *FactorEdbSeries) RemoveByCondition(condition string, pars []interface{}) (err error) {
+	if condition == "" {
+		return
+	}
+	o := orm.NewOrmUsingDB("data")
+	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
+	_, err = o.Raw(sql, pars).Exec()
+	return
+}
+
 func (m *FactorEdbSeries) GetItemById(id int) (item *FactorEdbSeries, err error) {
 	o := orm.NewOrmUsingDB("data")
 	sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)

+ 10 - 0
models/data_manage/factor_edb_series_chart_mapping.go

@@ -103,6 +103,16 @@ func (m *FactorEdbSeriesChartMapping) MultiRemove(ids []int) (err error) {
 	return
 }
 
+func (m *FactorEdbSeriesChartMapping) RemoveByCondition(condition string, pars []interface{}) (err error) {
+	if condition == "" {
+		return
+	}
+	o := orm.NewOrmUsingDB("data")
+	sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, m.TableName(), condition)
+	_, err = o.Raw(sql, pars).Exec()
+	return
+}
+
 func (m *FactorEdbSeriesChartMapping) GetItemById(id int) (item *FactorEdbSeriesChartMapping, err error) {
 	o := orm.NewOrmUsingDB("data")
 	sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)

+ 74 - 23
services/data/correlation/chart_info.go

@@ -1546,34 +1546,85 @@ func GetFactorChartDataByChartId(chartInfoId int, extraConfig string) (xEdbIdVal
 	return
 }
 
-// FormatChartEdbInfoMappings 补充指标信息
-func FormatChartEdbInfoMappings(chartInfoId int, mappings []*data_manage.ChartEdbInfoMapping) (edbList []*data_manage.ChartEdbInfoMapping, err error) {
-	edbList = make([]*data_manage.ChartEdbInfoMapping, 0)
-	if len(mappings) == 0 {
+// RemoveCorrelationRelate 删除相关性图表关联信息
+func RemoveCorrelationRelate(chartInfoId int) (err error) {
+	if chartInfoId <= 0 {
+		return
+	}
+	// 相关性图表
+	chartCorrelate := new(data_manage.ChartInfoCorrelation)
+	if e := chartCorrelate.GetItemById(chartInfoId); e != nil && e.Error() != utils.ErrNoRow() {
+		err = fmt.Errorf("获取相关性图表信息失败, %v", e)
+		return
+	}
+	if chartCorrelate == nil {
 		return
 	}
 
-	for _, v := range mappings {
-		if chartInfoId <= 0 {
-			v.IsAxis = 1
-			v.LeadValue = 0
-			v.LeadUnit = ""
-			v.ChartEdbMappingId = 0
-			v.ChartInfoId = 0
-			v.IsOrder = false
-			v.EdbInfoType = 1
-			v.ChartStyle = ""
-			v.ChartColor = ""
-			v.ChartWidth = 0
-		} else {
-			v.LeadUnitEn = data.GetLeadUnitEn(v.LeadUnit)
-			v.LeadUnitEn = data.GetLeadUnitEn(v.LeadUnit)
+	// 删除相关性图
+	if e := chartCorrelate.Delete(); e != nil {
+		err = fmt.Errorf("删除相关性图表失败, %v", e)
+		return
+	}
+
+	// 多因子
+	if chartCorrelate.AnalysisMode != 1 {
+		return
+	}
+	seriesIds := make([]int, 0)
+
+	// 删除图表关联
+	chartMappingOb := new(data_manage.FactorEdbSeriesChartMapping)
+	{
+		cond := fmt.Sprintf(" AND %s = ?", chartMappingOb.Cols().ChartInfoId)
+		pars := make([]interface{}, 0)
+		pars = append(pars, chartCorrelate.CorrelationChartInfoId)
+		items, e := chartMappingOb.GetItemsByCondition(cond, pars, []string{}, "")
+		if e != nil {
+			err = fmt.Errorf("获取图表关联指标系列失败, %v", e)
+			return
 		}
-		v.FrequencyEn = data.GetFrequencyEn(v.Frequency)
-		if v.Unit == `无` {
-			v.Unit = ``
+		for _, v := range items {
+			if !utils.InArrayByInt(seriesIds, v.FactorEdbSeriesId) {
+				seriesIds = append(seriesIds, v.FactorEdbSeriesId)
+			}
+		}
+		removeCond := fmt.Sprintf(" %s = ?", chartMappingOb.Cols().ChartInfoId)
+		if e = chartMappingOb.RemoveByCondition(removeCond, pars); e != nil {
+			err = fmt.Errorf("删除图表关联指标系列失败, %v", e)
+			return
+		}
+	}
+
+	// 删除系列
+	if len(seriesIds) == 0 {
+		return
+	}
+	seriesOb := new(data_manage.FactorEdbSeries)
+	if e := seriesOb.MultiRemove(seriesIds); e != nil {
+		err = fmt.Errorf("删除系列失败, %v", e)
+		return
+	}
+
+	edbMappingOb := new(data_manage.FactorEdbSeriesMapping)
+	{
+		cond := fmt.Sprintf(" %s IN (%s)", edbMappingOb.Cols().FactorEdbSeriesId, utils.GetOrmInReplace(len(seriesIds)))
+		pars := make([]interface{}, 0)
+		pars = append(pars, seriesIds)
+		if e := edbMappingOb.RemoveByCondition(cond, pars); e != nil {
+			err = fmt.Errorf("删除系列指标失败, %v", e)
+			return
+		}
+	}
+	calculateOb := new(data_manage.FactorEdbSeriesCalculateData)
+	{
+		cond := fmt.Sprintf(" %s IN (%s)", calculateOb.Cols().FactorEdbSeriesId, utils.GetOrmInReplace(len(seriesIds)))
+		pars := make([]interface{}, 0)
+		pars = append(pars, seriesIds)
+		if e := calculateOb.RemoveByCondition(cond, pars); e != nil {
+			err = fmt.Errorf("删除系列指标计算失败, %v", e)
+			return
 		}
-		edbList = append(edbList, v)
 	}
 	return
 }