浏览代码

fix:新增/编辑python代码时,添加匹配代码中的指标,方便更新时,同步更新关联指标

Roc 2 年之前
父节点
当前提交
9a2061199a
共有 4 个文件被更改,包括 142 次插入16 次删除
  1. 12 1
      controllers/base_from_python.go
  2. 122 2
      models/base_from_python.go
  3. 8 0
      models/edb_info.go
  4. 0 13
      services/base_from_python.go

+ 12 - 1
controllers/base_from_python.go

@@ -219,8 +219,11 @@ func (this *PythonController) Add() {
 		return
 	}
 
+	//匹配所有的关联指标
+	edbInfoList := models.AnalysisPythonCode(req.PythonCode, req.EdbName)
+
 	//pythonCode
-	err = models.AddPythonEdb(edbInfo.EdbInfoId, edbInfo.EdbCode, edbData)
+	err = models.AddPythonEdb(edbInfo.EdbInfoId, edbInfo.EdbCode, edbData, edbInfoList)
 	if err != nil {
 		br.Msg = "生成python指标失败"
 		br.Msg = "生成python指标失败,AddPythonEdb Err:" + err.Error()
@@ -378,6 +381,14 @@ func (this *PythonController) Edit() {
 		return
 	}
 
+	//匹配所有的关联指标
+	edbInfoList := models.AnalysisPythonCode(req.PythonCode, req.EdbName)
+	err = models.EditEdbInfoCalculateMapping(edbInfo.EdbInfoId, edbInfo.EdbCode, edbInfoList)
+	if err != nil {
+		br.Msg = "修改python运算指标失败"
+		br.Msg = "修改python运算指标失败,存储python代码失败,EditEdbPythonCode Err:" + err.Error()
+		return
+	}
 	//刷新数据
 	err = models.RefreshAllPythonEdb(edbInfo, edbData)
 	if err != nil {

+ 122 - 2
models/base_from_python.go

@@ -24,15 +24,36 @@ type EdbDataPython struct {
 }
 
 // AddPythonEdb 新增python运算指标
-func AddPythonEdb(edbInfoId int, edbCode string, item services.EdbDataFromPython) (err error) {
+func AddPythonEdb(edbInfoId int, edbCode string, item services.EdbDataFromPython, edbInfoList []*EdbInfo) (err error) {
 	var errMsg string
 	o := orm.NewOrm()
 	defer func() {
 		if err != nil {
-			go utils.SendEmail(utils.APP_NAME_CN+"【"+utils.RunMode+"】"+"失败提醒", " 同花顺数据获取失败:err:"+errMsg, utils.EmailSendToUsers)
+			go utils.SendEmail(utils.APP_NAME_CN+"【"+utils.RunMode+"】"+"失败提醒", " python代码运算数据获取失败:err:"+errMsg, utils.EmailSendToUsers)
 		}
 	}()
 
+	//添加指标关系
+	for _, tmpEdbInfo := range edbInfoList {
+		calculateMappingItem := new(EdbInfoCalculateMapping)
+		calculateMappingItem.CreateTime = time.Now()
+		calculateMappingItem.ModifyTime = time.Now()
+		calculateMappingItem.Sort = 1
+		calculateMappingItem.EdbCode = edbCode
+		calculateMappingItem.EdbInfoId = edbInfoId
+		calculateMappingItem.FromEdbInfoId = tmpEdbInfo.EdbInfoId
+		calculateMappingItem.FromEdbCode = tmpEdbInfo.EdbCode
+		calculateMappingItem.FromEdbName = tmpEdbInfo.EdbName
+		calculateMappingItem.FromSource = tmpEdbInfo.Source
+		calculateMappingItem.FromSourceName = tmpEdbInfo.SourceName
+		calculateMappingItem.Source = tmpEdbInfo.Source
+		calculateMappingItem.SourceName = tmpEdbInfo.SourceName
+		_, err = o.Insert(calculateMappingItem)
+		if err != nil {
+			return
+		}
+	}
+
 	var isAdd bool
 	addSql := ` INSERT INTO edb_data_python (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
 	for k, dateTimeStr := range item.Date {
@@ -225,6 +246,63 @@ func RefreshAllPythonEdb(edbInfo *EdbInfo, item services.EdbDataFromPython) (err
 	return
 }
 
+// EditEdbInfoCalculateMapping 更新关联关系表
+func EditEdbInfoCalculateMapping(edbInfoId int, edbCode string, edbInfoList []*EdbInfo) (err error) {
+	o := orm.NewOrm()
+
+	var existCondition string
+	var existPars []interface{}
+	existCondition += " AND edb_info_id=? "
+	existPars = append(existPars, edbInfoId)
+
+	//查询出所有的关联指标
+	existList, err := GetEdbInfoCalculateListByCondition(existCondition, existPars)
+	if err != nil {
+		err = fmt.Errorf("判断指标是否改变失败,Err:" + err.Error())
+		return
+	}
+	existEdbInfoIdMap := make(map[int]int)
+	for _, v := range existList {
+		existEdbInfoIdMap[v.FromEdbInfoId] = v.FromEdbInfoId
+	}
+	//添加指标关系
+	for _, tmpEdbInfo := range edbInfoList {
+		if _, ok := existEdbInfoIdMap[tmpEdbInfo.EdbInfoId]; ok {
+			//如果存在,那么就移除map里面的东西
+			delete(existEdbInfoIdMap, tmpEdbInfo.EdbInfoId)
+		} else {
+			calculateMappingItem := new(EdbInfoCalculateMapping)
+			calculateMappingItem.CreateTime = time.Now()
+			calculateMappingItem.ModifyTime = time.Now()
+			calculateMappingItem.Sort = 1
+			calculateMappingItem.EdbCode = edbCode
+			calculateMappingItem.EdbInfoId = edbInfoId
+			calculateMappingItem.FromEdbInfoId = tmpEdbInfo.EdbInfoId
+			calculateMappingItem.FromEdbCode = tmpEdbInfo.EdbCode
+			calculateMappingItem.FromEdbName = tmpEdbInfo.EdbName
+			calculateMappingItem.FromSource = tmpEdbInfo.Source
+			calculateMappingItem.FromSourceName = tmpEdbInfo.SourceName
+			calculateMappingItem.Source = tmpEdbInfo.Source
+			calculateMappingItem.SourceName = tmpEdbInfo.SourceName
+			_, err = o.Insert(calculateMappingItem)
+			if err != nil {
+				return
+			}
+		}
+	}
+
+	for _, v := range existEdbInfoIdMap {
+		//删除,计算指标关联的,基础指标的关联关系
+		sql := ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? and from_edb_info_id=?`
+		_, err = o.Raw(sql, edbInfoId, v).Exec()
+		if err != nil {
+			err = errors.New("删除计算指标关联关系失败,Err:" + err.Error())
+			return
+		}
+	}
+	return
+}
+
 // GetAllEdbDataPythonByEdbInfoId 根据指标id获取全部的数据
 func GetAllEdbDataPythonByEdbInfoId(edbInfoId int) (items []*EdbDataPython, err error) {
 	o := orm.NewOrm()
@@ -264,3 +342,45 @@ type AddPythonEdbReq struct {
 	ClassifyId int    `description:"分类id"`
 	PythonCode string `description:"python代码"`
 }
+
+// AnalysisPythonCode 解析Python代码,获取关联code
+func AnalysisPythonCode(pythonCode, edbName string) (edbInfoList []*EdbInfo) {
+	tmpEdbCodeList := make([]string, 0) //临时指标code
+	edbCodeLen := 0                     //指标数
+
+	tmpList := strings.Split(pythonCode, "\n")
+	for _, v := range tmpList {
+		if strings.Contains(v, "edb_code") {
+			edbCodeLen++
+			tmpCodeStrList := strings.Split(v, "edb_code")
+			if len(tmpCodeStrList) > 1 {
+				//根据单引号获取
+				tmpCodeStrList2 := strings.Split(tmpCodeStrList[1], "'")
+				if len(tmpCodeStrList2) > 1 {
+					if tmpCodeStrList2[1] != "" {
+						tmpEdbCodeList = append(tmpEdbCodeList, tmpCodeStrList2[1])
+					}
+				}
+				//根据双引号获取
+				tmpCodeStrList3 := strings.Split(tmpCodeStrList[1], `"`)
+				if len(tmpCodeStrList3) > 1 {
+					if tmpCodeStrList3[1] != "" {
+						tmpEdbCodeList = append(tmpEdbCodeList, tmpCodeStrList3[1])
+					}
+				}
+			}
+		}
+	}
+	for _, v := range tmpEdbCodeList {
+		fmt.Println(v)
+		item, _ := GetEdbInfoOnlyByEdbCode(v)
+		if item != nil {
+			edbInfoList = append(edbInfoList, item)
+		}
+	}
+	if len(edbInfoList) != edbCodeLen {
+		//code匹配失败,需要短信提醒
+		go utils.SendEmail(utils.APP_NAME_CN+"%s【"+utils.RunMode+"】"+"失败提醒", fmt.Sprintf("python代码关联指标匹配失败,指标名称:%s;实际关联%d个,匹配上%d个", edbName, edbCodeLen, len(edbInfoList)), utils.EmailSendToUsers)
+	}
+	return
+}

+ 8 - 0
models/edb_info.go

@@ -191,6 +191,14 @@ func GetEdbInfoByEdbCode(source int, edbCode string) (item *EdbInfo, err error)
 	return
 }
 
+// GetEdbInfoOnlyByEdbCode 仅根据指标code获取指标信息
+func GetEdbInfoOnlyByEdbCode(edbCode string) (item *EdbInfo, err error) {
+	o := orm.NewOrm()
+	sql := ` SELECT * FROM edb_info WHERE edb_code=? `
+	err = o.Raw(sql, edbCode).QueryRow(&item)
+	return
+}
+
 // GetEdbInfoCalculateListByCondition 获取指标关系列表
 func GetEdbInfoCalculateListByCondition(condition string, pars []interface{}) (items []*EdbInfoCalculateMapping, err error) {
 	o := orm.NewOrm()

+ 0 - 13
services/base_from_python.go

@@ -73,8 +73,6 @@ func ExecPythonCode(edbCode, reqCode string) (dataMap EdbDataFromPython, err err
 		}
 	}()
 
-	AnalysisPythonCode(reqCode)
-
 	//获取python文件的绝对地址
 	pythonFile, err := getPythonFileAbsolutePath(edbCode)
 	if err != nil {
@@ -183,14 +181,3 @@ func getPythonFrontStr() string {
 func getPythonLaterStr() string {
 	return "\nprint(\"result=\", result.to_json())\ndb.close()"
 }
-
-// AnalysisPythonCode 解析Python代码,获取关联code
-func AnalysisPythonCode(pythonCode string) (dataMap EdbDataFromPython, err error, errMsg string) {
-	//edbCodeList := make([]string, 0)
-
-	tmpList := strings.Split(pythonCode, "\n")
-	for _, v := range tmpList {
-		fmt.Println(v)
-	}
-	return
-}