Sfoglia il codice sorgente

fix:stl配置保存校验

zqbao 6 mesi fa
parent
commit
c303ae1134

+ 6 - 3
controllers/data_manage/stl/stl.go

@@ -82,14 +82,17 @@ func (c *STLController) SaveConf() {
 		return
 	}
 
-	insertId, err := stl.SaveStlConfig(req, sysUser.AdminId)
+	configId, msg, err := stl.SaveStlConfig(req, sysUser.AdminId)
 	if err != nil {
-		br.Msg = "保存失败"
+		if msg == "" {
+			msg = "保存异常"
+		}
+		br.Msg = msg
 		br.ErrMsg = "保存失败,err:" + err.Error()
 		return
 	}
 	resp := new(response.SaveStlConfigResp)
-	resp.CalculateStlConfigId = insertId
+	resp.CalculateStlConfigId = configId
 
 	br.Data = resp
 	br.Msg = "保存成功"

+ 6 - 0
models/data_manage/stl/calculate_stl_config.go

@@ -20,6 +20,12 @@ func (c *CalculateStlConfig) Insert() (insertId int64, err error) {
 	return
 }
 
+func (c *CalculateStlConfig) Update(cols []string) (err error) {
+	o := orm.NewOrmUsingDB("data")
+	_, err = o.Update(c, cols...)
+	return
+}
+
 func GetCalculateStlConfigById(id int) (item *CalculateStlConfig, err error) {
 	o := orm.NewOrmUsingDB("data")
 	sql := "SELECT * FROM calculate_stl_config WHERE calculate_stl_config_id =?"

+ 112 - 7
services/data/stl/stl.go

@@ -35,6 +35,21 @@ var EDB_DATA_CALCULATE_STL_SEASONAL_CACHE = `eta:stl_decompose:seasonal:config_i
 var EDB_DATA_CALCULATE_STL_RESIDUAL_CACHE = `eta:stl_decompose:residual:config_id:`
 
 func GenerateStlEdbData(req *request.StlConfigReq, adminId int) (resp *response.StlPreviewResp, msg string, err error) {
+	config, err := stl.GetCalculateStlConfigById(req.CalculateStlConfigId)
+	if err != nil {
+		if err.Error() == utils.ErrNoRow() {
+			msg = "配置信息不存在,请重新计算"
+			return
+		}
+		msg = "获取配置信息失败"
+		return
+	}
+	var confReq request.StlConfigReq
+	if err = json.Unmarshal([]byte(config.Config), &confReq); err != nil {
+		msg = "预览失败"
+		err = fmt.Errorf("配置信息解析失败, err:%s", err.Error())
+		return
+	}
 	edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
 	if err != nil {
 		if err.Error() == utils.ErrNoRow() {
@@ -387,14 +402,104 @@ print(output)
 	return
 }
 
-func SaveStlConfig(req *request.StlConfigReq, adminId int) (insertId int64, err error) {
+func SaveStlConfig(req *request.StlConfigReq, adminId int) (configId int64, msg string, err error) {
+	edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
+	if err != nil {
+		if err.Error() == utils.ErrNoRow() {
+			msg = "指标不存在"
+			return
+		}
+		msg = "获取指标信息失败"
+		return
+	}
+	var condition string
+	var pars []interface{}
+	switch req.DataRangeType {
+	case ALL_DATE:
+	case LAST_N_YEARS:
+		condition += " AND data_time >=?"
+		year := time.Now().Year()
+		lastDate := time.Date(year-req.LastNYear, 1, 1, 0, 0, 0, 0, time.Local)
+		pars = append(pars, lastDate)
+	case RANGE_DATE:
+		condition = " AND data_time >=? AND data_time <=?"
+		pars = append(pars, req.StartDate, req.EndDate)
+	case RANGE_DATE_TO_NOW:
+		condition = " AND data_time >=?"
+		pars = append(pars, req.StartDate)
+	}
+	condition += " AND edb_code =?"
+	pars = append(pars, edbInfo.EdbCode)
+
+	edbData, err := data_manage.GetAllEdbDataListByCondition(condition, pars, edbInfo.Source, edbInfo.SubSource)
+	if err != nil {
+		msg = "获取指标数据失败"
+		return
+	}
+
+	var condMsg string
+	if req.Period < 2 || req.Period > len(edbData) {
+		condMsg += "period必须是一个大于等于2的正整数,且必须小于时间序列的长度"
+	}
+	if req.Seasonal < 3 || req.Seasonal%2 == 0 || req.Seasonal <= req.Period {
+		if condMsg != "" {
+			condMsg += "\n"
+		}
+		condMsg += "seasonal必须是一个大于等于3的奇整数,且必须大于period"
+	}
+	if req.Trend < 3 || req.Trend%2 == 0 || req.Trend <= req.Period {
+		if condMsg != "" {
+			condMsg += "\n"
+		}
+		condMsg += "trend必须是一个大于等于3的奇整数,且必须大于period"
+	}
+	if req.Fraction < 0 || req.Fraction > 1 {
+		if condMsg != "" {
+			condMsg += "\n"
+		}
+		condMsg += "fraction必须是一个介于[0-1]之间"
+	}
+	if 1 > req.TrendDeg || req.TrendDeg > 5 {
+		if condMsg != "" {
+			condMsg += "\n"
+		}
+		condMsg += "trend_deg请设置成1-5的整数"
+	}
+	if 1 > req.SeasonalDeg || req.SeasonalDeg > 5 {
+		if condMsg != "" {
+			condMsg += "\n"
+		}
+		condMsg += "seasonal_deg请设置成1-5的整数"
+	}
+	if 1 > req.LowPassDeg || req.LowPassDeg > 5 {
+		if condMsg != "" {
+			condMsg += "\n"
+		}
+		condMsg += "low_pass_deg请设置成1-5的整数"
+	}
+	if condMsg != "" {
+		msg = condMsg
+		err = fmt.Errorf("参数错误")
+		return
+	}
+	b, err := json.Marshal(req)
+	if err != nil {
+		return
+	}
 	conf := new(stl.CalculateStlConfig)
-	b, _ := json.Marshal(req)
-	conf.Config = string(b)
-	conf.SysUserId = adminId
-	conf.CreateTime = time.Now()
-	conf.ModifyTime = time.Now()
-	insertId, err = conf.Insert()
+	if req.CalculateStlConfigId > 0 {
+		conf.CalculateStlConfigId = req.CalculateStlConfigId
+		conf.Config = string(b)
+		conf.ModifyTime = time.Now()
+		err = conf.Update([]string{"Config", "ModifyTime"})
+		configId = int64(req.CalculateStlConfigId)
+	} else {
+		conf.Config = string(b)
+		conf.SysUserId = adminId
+		conf.CreateTime = time.Now()
+		conf.ModifyTime = time.Now()
+		configId, err = conf.Insert()
+	}
 	return
 }