浏览代码

fix:新增修复数据接口

Roc 1 年之前
父节点
当前提交
fde210b51a

+ 43 - 0
controllers/fix/custom_analysis.go

@@ -0,0 +1,43 @@
+package fix
+
+import (
+	"eta/eta_index_lib/controllers"
+	"eta/eta_index_lib/models"
+	"eta/eta_index_lib/services/fix"
+)
+
+// CustomAnalysisController 自定义分析
+type CustomAnalysisController struct {
+	controllers.BaseAuthController
+}
+
+var fixTable = false
+
+// FixTableV1
+// @Title 指标详情接口
+// @Description  指标详情接口
+// @Success 200 {object} models.EdbInfoDetailReq
+// @router /fix/v1 [post]
+func (this *CustomAnalysisController) FixTableV1() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+
+	if fixTable {
+		br.Ret = 200
+		br.Success = true
+		br.Msg = "请不要重复修复"
+		return
+	}
+	fixTable = true
+	//ETA1.0.2 自定义分析(生成指标数据修复)
+	fix.FixTableData()
+	//ETA1.0.2 自定义分析(修复excel与指标的关系)
+	fix.FixTableDataMapping()
+
+	br.Ret = 200
+	br.Success = true
+	br.Msg = "修复成功"
+}

+ 13 - 0
models/db.go

@@ -1,6 +1,7 @@
 package models
 
 import (
+	"eta/eta_index_lib/models/excel"
 	"eta/eta_index_lib/models/future_good"
 	"eta/eta_index_lib/models/supply_analysis"
 	"eta/eta_index_lib/utils"
@@ -65,6 +66,9 @@ func init() {
 
 	// 基础指标表
 	initBaseIndex()
+
+	// Eta表格相关
+	initExcel()
 }
 
 // initFutureGood 注册期货数据 数据表
@@ -101,3 +105,12 @@ func initBaseIndex() {
 		new(BaseFromMysteelChemicalData),
 	)
 }
+
+// initExcel Excel
+func initExcel() {
+	orm.RegisterModel(
+		new(excel.ExcelInfo),
+		new(excel.ExcelClassify),
+		new(excel.ExcelEdbMapping),
+	)
+}

+ 63 - 0
models/excel/excel_classify.go

@@ -0,0 +1,63 @@
+package excel
+
+import (
+	"github.com/beego/beego/v2/client/orm"
+	"time"
+)
+
+// ExcelClassify excel表格分类
+type ExcelClassify struct {
+	ExcelClassifyId   int       `orm:"column(excel_classify_id);pk"`
+	Source            int       `description:"表格来源,1:excel插件的表格,2:自定义表格,3:混合表格,4:自定义分析,默认:1"`
+	ExcelClassifyName string    `description:"分类名称"`
+	ParentId          int       `description:"父级id"`
+	SysUserId         int       `description:"创建人id"`
+	SysUserRealName   string    `description:"创建人姓名"`
+	Level             int       `description:"层级"`
+	UniqueCode        string    `description:"唯一编码"`
+	Sort              int       `description:"排序字段,越小越靠前,默认值:10"`
+	IsDelete          int       `description:"排序字段,越小越靠前,默认值:10"`
+	CreateTime        time.Time `description:"创建时间"`
+	ModifyTime        time.Time `description:"修改时间"`
+}
+
+// AddExcelClassify 添加excel分类
+func AddExcelClassify(item *ExcelClassify) (lastId int64, err error) {
+	o := orm.NewOrm()
+	lastId, err = o.Insert(item)
+	if err != nil {
+		return
+	}
+	item.ExcelClassifyId = int(lastId)
+
+	return
+}
+
+type ExcelClassifyItems struct {
+	ExcelClassifyId   int `description:"分类id"`
+	ExcelInfoId       int `description:"表格id"`
+	ExcelClassifyName string
+	ParentId          int
+	Level             int    `description:"层级"`
+	Sort              int    `description:"排序字段,越小越靠前,默认值:10"`
+	UniqueCode        string `description:"唯一编码"`
+	SysUserId         int    `description:"创建人id"`
+	SysUserRealName   string `description:"创建人姓名"`
+	StartDate         string `description:"自定义开始日期"`
+	Children          []*ExcelClassifyItems
+}
+
+// GetExcelClassifyByParentId
+// @Description: 根据父级分类id获取指标下面的分类列表
+// @author: Roc
+// @datetime2023-10-30 13:46:35
+// @param parentId int
+// @param source int
+// @return items []*ExcelClassifyItems
+// @return err error
+func GetExcelClassifyByParentId(parentId, source int) (items []*ExcelClassifyItems, err error) {
+	o := orm.NewOrm()
+	sql := ` SELECT * FROM excel_classify WHERE parent_id=? AND source = ? AND is_delete=0 order by sort asc,excel_classify_id asc`
+	_, err = o.Raw(sql, parentId, source).QueryRows(&items)
+	return
+}

+ 48 - 0
models/excel/excel_edb_mapping.go

@@ -0,0 +1,48 @@
+package excel
+
+import (
+	"github.com/beego/beego/v2/client/orm"
+	"time"
+)
+
+// ExcelEdbMapping excel与指标的关系表
+type ExcelEdbMapping struct {
+	ExcelEdbMappingId int       `orm:"column(excel_edb_mapping_id);pk"`
+	ExcelInfoId       int       `description:"excel的id"`
+	Source            int       `description:"表格来源,1:excel插件的表格,2:自定义表格,3:混合表格,4:自定义分析,默认:1"`
+	EdbInfoId         int       `description:"计算指标id"`
+	CreateTime        time.Time `description:"创建时间"`
+	ModifyTime        time.Time `description:"修改时间"`
+}
+
+// AddExcelEdbMappingMulti 批量添加excel与指标的关系
+func AddExcelEdbMappingMulti(items []*ExcelEdbMapping) (err error) {
+	o := orm.NewOrm()
+	_, err = o.InsertMulti(len(items), items)
+	return
+}
+
+// Add 添加excel与指标的关系
+func (e *ExcelEdbMapping) Add() (err error) {
+	o := orm.NewOrm()
+	_, err = o.Insert(e)
+	return
+}
+
+type ExcelEdbMappingItem struct {
+	EdbInfoId        int    `description:"指标id"`
+	UniqueCode       string `description:"唯一编码"`
+	EdbName          string `description:"指标名称"`
+	ClassifyId       int    `description:"分类id"`
+	Frequency        string `description:"频度"`
+	Unit             string `description:"单位"`
+	CalculateFormula string `json:"-"`
+	DateSequenceStr  string `description:"日期序列公式"`
+	DataSequenceStr  string `description:"数据序列公式"`
+}
+
+// CalculateFormula 计算公式
+type CalculateFormula struct {
+	DateSequenceStr string `json:"DateSequenceStr"`
+	DataSequenceStr string `json:"DataSequenceStr"`
+}

+ 112 - 0
models/excel/excel_info.go

@@ -0,0 +1,112 @@
+package excel
+
+import (
+	"github.com/beego/beego/v2/client/orm"
+	"time"
+)
+
+// ExcelInfo excel表格详情表
+type ExcelInfo struct {
+	ExcelInfoId     int       `orm:"column(excel_info_id);pk"`
+	Source          int       `description:"表格来源,1:excel插件的表格,2:自定义表格,3:混合表格,4:自定义分析,默认:1"`
+	ExcelType       int       `description:"表格类型,1:指标列,2:日期列,默认:1"`
+	ExcelName       string    `description:"表格名称"`
+	UniqueCode      string    `description:"表格唯一编码"`
+	ExcelClassifyId int       `description:"表格分类id"`
+	SysUserId       int       `description:"操作人id"`
+	SysUserRealName string    `description:"操作人真实姓名"`
+	Content         string    `description:"表格内容"`
+	ExcelImage      string    `description:"表格图片"`
+	FileUrl         string    `description:"表格下载地址"`
+	Sort            int       `description:"排序字段,数字越小越排前面"`
+	IsDelete        int       `description:"是否删除,0:未删除,1:已删除"`
+	ModifyTime      time.Time `description:"最近修改日期"`
+	CreateTime      time.Time `description:"创建日期"`
+}
+
+// GetNoContentExcelInfoAll 获取不含content的表格列表 用于分类展示
+func GetNoContentExcelInfoAll(source, userId int) (items []*ExcelClassifyItems, err error) {
+	o := orm.NewOrm()
+	sql := ` SELECT excel_info_id,excel_classify_id,excel_name AS excel_classify_name,
+             unique_code,sys_user_id,sys_user_real_name
+            FROM excel_info where is_delete=0 AND source = ?  `
+
+	pars := []interface{}{source}
+
+	if userId > 0 {
+		sql += ` AND sys_user_id = ? `
+		pars = append(pars, userId)
+	}
+	sql += `  ORDER BY sort asc,create_time desc `
+	_, err = o.Raw(sql, pars...).QueryRows(&items)
+	return
+}
+
+// UpdateExcelInfoClassifyId 更改表格分类
+func UpdateExcelInfoClassifyId(classifyId, excelInfoId int) (err error) {
+	o := orm.NewOrm()
+	sql := ` update excel_info set excel_classify_id = ? WHERE excel_info_id=? `
+	_, err = o.Raw(sql, classifyId, excelInfoId).Exec()
+
+	return
+}
+
+// GetAllExcelInfoBySource 根据来源获取包含content的表格列表
+func GetAllExcelInfoBySource(source int) (items []*ExcelInfo, err error) {
+	o := orm.NewOrm()
+	sql := ` SELECT * FROM excel_info where is_delete=0  AND source = ?  ORDER BY sort asc,create_time desc `
+	_, err = o.Raw(sql, source).QueryRows(&items)
+	return
+}
+
+// TableDataReq 自定义表格请求参数
+type TableDataReq struct {
+	EdbInfoIdList []int             `description:"指标id列表,从左至右,从上到下的顺序"`
+	Sort          int               `description:"日期排序,1:倒序,2:正序"`
+	Data          []EdbInfoData     `description:"数据列表"`
+	TextRowData   [][]ManualDataReq `description:"文本列表"`
+}
+
+// EdbInfoData 自定义表格的数据
+type EdbInfoData struct {
+	EdbInfoId    int             `description:"指标ID"`
+	Tag          string          `description:"标签"`
+	EdbName      string          `description:"指标名称"`
+	EdbAliasName string          `description:"指标别名"`
+	Frequency    string          `description:"频度"`
+	Unit         string          `description:"单位"`
+	Data         []ManualDataReq `description:"单元格数据列表"`
+}
+
+// ManualDataReq 自定义表格的单元格数据
+type ManualDataReq struct {
+	DataType            int               `description:"数据类型,1:普通的,2:插值法,3:手动输入,4:公式计算,5:预测值"`
+	DataTime            string            `description:"所属日期"`
+	DataTimeType        int               `description:"日期类型,1:实际日期;2:未来日期"`
+	ShowValue           string            `description:"展示值"`
+	Value               string            `description:"实际值(计算公式)"`
+	RelationEdbInfoList []RelationEdbInfo `description:"关联指标(计算公式中关联的指标,用于计算的时候去匹配)"`
+}
+
+// RelationEdbInfo 自定义表格中单元格的关联指标
+type RelationEdbInfo struct {
+	Tag string `description:"指标标签"`
+	Row string `description:"第几行"`
+}
+
+// MixedTableReq 混合表格保存请求参数
+type MixedTableReq struct {
+	CellRelation string                    `description:"单元格关系"`
+	Data         [][]MixedTableCellDataReq `description:"混合表格单元格参数"`
+}
+
+// MixedTableCellDataReq 混合表格单元格参数
+type MixedTableCellDataReq struct {
+	Uid          string `description:"单元格唯一标识"`
+	DataType     int    `description:"数据类型,1:日期,2:指标,3:自定义文本,4:插值"`
+	DataTime     string `description:"所属日期"`
+	DataTimeType int    `description:"日期类型:0:手动输入日期;1:导入系统日期;;3:导入指标日期(指标库的最新日期);"`
+	EdbInfoId    int    `description:"指标id"`
+	ShowValue    string `description:"展示值"`
+	Value        string `description:"实际值"`
+}

+ 9 - 0
routers/commentsRouter.go

@@ -7,6 +7,15 @@ import (
 
 func init() {
 
+    beego.GlobalControllerRouter["eta/eta_index_lib/controllers/fix:CustomAnalysisController"] = append(beego.GlobalControllerRouter["eta/eta_index_lib/controllers/fix:CustomAnalysisController"],
+        beego.ControllerComments{
+            Method: "FixTableV1",
+            Router: `/fix/v1`,
+            AllowHTTPMethods: []string{"post"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
     beego.GlobalControllerRouter["eta/eta_index_lib/controllers/future_good:FutureGoodEdbInfoController"] = append(beego.GlobalControllerRouter["eta/eta_index_lib/controllers/future_good:FutureGoodEdbInfoController"],
         beego.ControllerComments{
             Method: "Add",

+ 6 - 0
routers/router.go

@@ -9,6 +9,7 @@ package routers
 
 import (
 	"eta/eta_index_lib/controllers"
+	"eta/eta_index_lib/controllers/fix"
 	"eta/eta_index_lib/controllers/future_good"
 	"eta/eta_index_lib/controllers/open"
 	beego "github.com/beego/beego/v2/server/web"
@@ -186,6 +187,11 @@ func init() {
 				&controllers.EdbInfoCalculateController{},
 			),
 		),
+		beego.NSNamespace("/fix",
+			beego.NSInclude(
+				&fix.CustomAnalysisController{},
+			),
+		),
 	)
 	beego.AddNamespace(ns)
 }

+ 270 - 0
services/fix/custom_analysis.go

@@ -0,0 +1,270 @@
+package fix
+
+import (
+	"encoding/json"
+	excelModel "eta/eta_index_lib/models/excel"
+	"eta/eta_index_lib/utils"
+	"fmt"
+	"time"
+)
+
+// FixTableData ETA1.0.2 自定义分析(生成指标数据修复)
+func FixTableData() {
+	// 获取一级分类
+	classifyList, err := excelModel.GetExcelClassifyByParentId(0, utils.EXCEL_DEFAULT)
+	if err != nil && err.Error() != utils.ErrNoRow() {
+		fmt.Println("数据修复失败,Err:" + err.Error())
+		return
+	}
+	timeTableMap := make(map[int]int)
+	mixTableMap := make(map[int]int)
+	for _, v := range classifyList {
+		// 时间序列表格
+		classify := &excelModel.ExcelClassify{
+			//ExcelClassifyId:   0,
+			ExcelClassifyName: v.ExcelClassifyName,
+			ParentId:          v.ParentId,
+			Source:            utils.TIME_TABLE,
+			SysUserId:         v.SysUserId,
+			SysUserRealName:   v.SysUserRealName,
+			Level:             v.Level,
+			UniqueCode:        utils.MD5(fmt.Sprint(v.UniqueCode, "_", utils.TIME_TABLE)),
+			Sort:              v.Sort,
+			CreateTime:        time.Now(),
+			ModifyTime:        time.Now(),
+		}
+		_, err = excelModel.AddExcelClassify(classify)
+		timeTableMap[v.ExcelClassifyId] = classify.ExcelClassifyId
+
+		// 混合表格
+		classify2 := &excelModel.ExcelClassify{
+			//ExcelClassifyId:   0,
+			ExcelClassifyName: v.ExcelClassifyName,
+			ParentId:          v.ParentId,
+			Source:            utils.MIXED_TABLE,
+			SysUserId:         v.SysUserId,
+			SysUserRealName:   v.SysUserRealName,
+			Level:             v.Level,
+			UniqueCode:        utils.MD5(fmt.Sprint(v.UniqueCode, "_", utils.MIXED_TABLE)),
+			Sort:              v.Sort,
+			CreateTime:        time.Now(),
+			ModifyTime:        time.Now(),
+		}
+		_, err = excelModel.AddExcelClassify(classify2)
+		mixTableMap[v.ExcelClassifyId] = classify2.ExcelClassifyId
+	}
+
+	// 修改时间序列表
+	{
+		// 获取时间序列表
+		timeTableExcelList, err := excelModel.GetNoContentExcelInfoAll(utils.TIME_TABLE, 0)
+		if err != nil && err.Error() != utils.ErrNoRow() {
+			fmt.Println("获取时间序列表列表失败,Err:" + err.Error())
+			return
+		}
+
+		for _, v := range timeTableExcelList {
+			classifyId, ok := timeTableMap[v.ExcelClassifyId]
+			if !ok {
+				continue
+			}
+			excelModel.UpdateExcelInfoClassifyId(classifyId, v.ExcelInfoId)
+		}
+	}
+
+	// 修改混合序列表
+	{
+		// 获取时间序列表
+		mixTableExcelList, err := excelModel.GetNoContentExcelInfoAll(utils.MIXED_TABLE, 0)
+		if err != nil && err.Error() != utils.ErrNoRow() {
+			fmt.Println("获取时间序列表列表失败,Err:" + err.Error())
+			return
+		}
+
+		for _, v := range mixTableExcelList {
+			classifyId, ok := mixTableMap[v.ExcelClassifyId]
+			if !ok {
+				continue
+			}
+			excelModel.UpdateExcelInfoClassifyId(classifyId, v.ExcelInfoId)
+		}
+	}
+
+	fmt.Println("完成生成指标数据修复")
+}
+
+// FixTableDataMapping ETA1.0.2 自定义分析(修复excel与指标的关系)
+func FixTableDataMapping() {
+
+	// 修改时间序列表
+	{
+		// 获取时间序列表
+		timeTableExcelList, err := excelModel.GetAllExcelInfoBySource(utils.TIME_TABLE)
+		if err != nil && err.Error() != utils.ErrNoRow() {
+			fmt.Println("获取时间序列表列表失败,Err:" + err.Error())
+			return
+		}
+
+		for _, v := range timeTableExcelList {
+
+			var tableData excelModel.TableDataReq
+			err = json.Unmarshal([]byte(v.Content), &tableData)
+			if err != nil {
+				fmt.Println(v.ExcelInfoId, "json转结构体失败,Err:"+err.Error())
+				continue
+			}
+			if len(tableData.EdbInfoIdList) > 0 {
+				excelEdbMappingList := make([]*excelModel.ExcelEdbMapping, 0)
+				for _, edbInfoId := range tableData.EdbInfoIdList {
+					excelEdbMappingList = append(excelEdbMappingList, &excelModel.ExcelEdbMapping{
+						//ExcelEdbMappingId: 0,
+						ExcelInfoId: v.ExcelInfoId,
+						Source:      v.Source,
+						EdbInfoId:   edbInfoId,
+						CreateTime:  time.Now(),
+						ModifyTime:  time.Now(),
+					})
+				}
+				err = excelModel.AddExcelEdbMappingMulti(excelEdbMappingList)
+				if err != nil {
+					fmt.Println(v.ExcelInfoId, "自定义表格关系保存失败,Err:"+err.Error())
+					continue
+				}
+			}
+
+		}
+	}
+
+	// 修改混合序列表
+	{
+		// 获取时间序列表
+		mixTableExcelList, err := excelModel.GetAllExcelInfoBySource(utils.MIXED_TABLE)
+		if err != nil && err.Error() != utils.ErrNoRow() {
+			fmt.Println("获取时间序列表列表失败,Err:" + err.Error())
+			return
+		}
+
+		for _, excelInfo := range mixTableExcelList {
+			var result excelModel.MixedTableReq
+			err = json.Unmarshal([]byte(excelInfo.Content), &result)
+			if err != nil {
+				fmt.Println(excelInfo.ExcelInfoId, "修改混合序列表,json转结构体失败,Err:"+err.Error())
+				continue
+			}
+			newResult, tmpErr, _ := GetMixedTableCellData(result.CellRelation, result.Data)
+			if tmpErr != nil {
+				fmt.Println(excelInfo.ExcelInfoId, "获取最新的数据失败,Err:"+err.Error())
+				continue
+			}
+			edbInfoIdList := make([]int, 0)
+			edbInfoIdMap := make(map[int]int)
+			for _, tmpV := range newResult {
+				for _, v := range tmpV {
+					if v.EdbInfoId > 0 {
+						if _, ok := edbInfoIdMap[v.EdbInfoId]; !ok {
+							edbInfoIdMap[v.EdbInfoId] = v.EdbInfoId
+							edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
+						}
+					}
+				}
+			}
+
+			if len(edbInfoIdList) > 0 {
+				excelEdbMappingList := make([]*excelModel.ExcelEdbMapping, 0)
+				for _, edbInfoId := range edbInfoIdList {
+					excelEdbMappingList = append(excelEdbMappingList, &excelModel.ExcelEdbMapping{
+						//ExcelEdbMappingId: 0,
+						ExcelInfoId: excelInfo.ExcelInfoId,
+						Source:      excelInfo.Source,
+						EdbInfoId:   edbInfoId,
+						CreateTime:  time.Now(),
+						ModifyTime:  time.Now(),
+					})
+				}
+				err = excelModel.AddExcelEdbMappingMulti(excelEdbMappingList)
+				if err != nil {
+					fmt.Println(excelInfo.ExcelInfoId, "混合表格关系保存失败,Err:"+err.Error())
+					continue
+				}
+			}
+
+		}
+
+	}
+
+	fmt.Println("完成excel与指标的关系修复")
+}
+
+// CellRelationConf
+// @Description: 单元格的关系配置结构体
+type CellRelationConf struct {
+	CellRelation
+	//Type         int          `json:"type" description:"数据类型,跟MixedTableCellDataReq的DataType保持一致"`
+	//Key          string       `json:"key" description:"单元格的唯一标识"`
+	RelationDate CellRelation `json:"relation_date"`
+	RelationEdb  CellRelation `json:"relation_edb"`
+}
+
+// CellRelation
+// @Description: 单元格的关系结构体
+type CellRelation struct {
+	Type int    `json:"type" description:"数据类型,跟MixedTableCellDataReq的DataType保持一致"`
+	Key  string `json:"key" description:"单元格的唯一标识"`
+}
+
+// GetMixedTableCellData 获取混合表格数据
+func GetMixedTableCellData(cellRelationConf string, config [][]excelModel.MixedTableCellDataReq) (newMixedTableCellDataList [][]excelModel.MixedTableCellDataReq, err error, errMsg string) {
+	// 单元格关系配置x信息
+	cellRelationConfMap := make(map[string]CellRelationConf)
+	cellRelationConfList := make([]CellRelationConf, 0)
+	if cellRelationConf != `` {
+		err = json.Unmarshal([]byte(cellRelationConf), &cellRelationConfList)
+		if err != nil {
+			return
+		}
+
+		for _, v := range cellRelationConfList {
+			cellRelationConfMap[v.Key] = v
+		}
+	}
+
+	// 找出所有的关联指标id
+	config, _, _, err, errMsg = handleConfig(config)
+	if err != nil {
+		return
+	}
+
+	newMixedTableCellDataList = config
+
+	return
+}
+
+// 单元格的数据类型
+const (
+	DateDT          = iota + 1 //日期
+	EdbDT                      // 指标类型
+	CustomTextDT               // 自定义文本
+	InsertDataDT               // 插值
+	PopInsertDataDT            // 弹框插值
+)
+
+func handleConfig(configList [][]excelModel.MixedTableCellDataReq) (newConfig [][]excelModel.MixedTableCellDataReq, edbInfoIdList []int, dataEdbInfoIdList []int, err error, errMsg string) {
+	edbInfoIdList = make([]int, 0)
+	dataEdbInfoIdList = make([]int, 0)
+
+	for ck, rowList := range configList {
+		for _, cell := range rowList {
+			switch cell.DataType {
+			case EdbDT: // 指标信息
+				edbInfoIdList = append(edbInfoIdList, cell.EdbInfoId)
+			case InsertDataDT, PopInsertDataDT: // 插值、弹框插值
+				dataEdbInfoIdList = append(dataEdbInfoIdList, cell.EdbInfoId)
+			}
+		}
+		configList[ck] = rowList
+	}
+
+	newConfig = configList
+
+	return
+}

+ 8 - 0
utils/constants.go

@@ -241,3 +241,11 @@ const (
 	DEFAULT_EDB_TYPE   = 1 //基础指标
 	CALCULATE_EDB_TYPE = 2 //计算指标
 )
+
+// ETA表格
+const (
+	EXCEL_DEFAULT         = 1 // 自定义excel
+	TIME_TABLE            = 2 // 时间序列表格
+	MIXED_TABLE           = 3 // 混合表格
+	CUSTOM_ANALYSIS_TABLE = 4 // 自定义分析表格
+)