package data import ( "errors" "fmt" "hongze/hongze_task/models/data_manage" "hongze/hongze_task/utils" "strconv" "strings" "time" "github.com/yidane/formula" ) //指标运算刷新数据 func RefreshCalculate(edbInfoIdArr []*data_manage.EdbInfo, edbInfoId int, edbCode, formulaStr, startDate, endDate string, edbInfoIdBytes []string) (err error) { defer func() { if err != nil { utils.FileLog.Info("Calculate Err:%s" + err.Error()) go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "RefreshCalculate ErrMsg:"+err.Error(), utils.EmailSendToUsers) } }() saveDataMap := make(map[string]map[int]float64) for _, v := range edbInfoIdArr { var condition string var pars []interface{} condition += " AND edb_info_id=? " pars = append(pars, v.EdbInfoId) if startDate != "" { condition += " AND data_time>=? " pars = append(pars, startDate) } if endDate != "" { condition += " AND data_time<=? " pars = append(pars, endDate) } dataList, err := data_manage.GetEdbDataListAll(condition, pars, v.Source, 1) if err != nil { return err } dataMap := make(map[string]float64) for _, dv := range dataList { if val, ok := saveDataMap[dv.DataTime]; ok { if _, ok := val[v.EdbInfoId]; !ok { val[v.EdbInfoId] = dv.Value } } else { temp := make(map[int]float64) temp[v.EdbInfoId] = dv.Value saveDataMap[dv.DataTime] = temp } } item := new(CalculateItems) item.EdbInfoId = v.EdbInfoId item.DataMap = dataMap } formulaMap := CheckFormula(formulaStr) addSql := ` INSERT INTO edb_data_calculate(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values ` nowStr := time.Now().Format(utils.FormatDateTime) var isAdd bool removeDateList := make([]string, 0) //需要移除的日期 for sk, sv := range saveDataMap { formulaStr = strings.ToUpper(formulaStr) formulaFormStr := ReplaceFormula(edbInfoIdArr, sv, formulaMap, formulaStr, edbInfoIdBytes) //if formulaFormStr != "" && !strings.Contains(formulaFormStr, "/0") { if formulaFormStr != "" { expression := formula.NewExpression(formulaFormStr) calResult, err := expression.Evaluate() if err != nil { // 分母为0的报错 if strings.Contains(err.Error(), "divide by zero") { removeDateList = append(removeDateList, sk) continue } err = errors.New("计算失败,edbCode:" + fmt.Sprint(edbCode) + ":Err:" + err.Error() + ";formulaStr:" + formulaFormStr) fmt.Println(err) return err } calVal, err := calResult.Float64() if err != nil { err = errors.New("计算失败:获取计算值失败 Err:" + err.Error() + ";formulaStr:" + formulaFormStr) fmt.Println(err) return err } count, err := data_manage.GetEdbDataCalculateByCodeAndDate(edbCode, sk) if err != nil && err.Error() != utils.ErrNoRow() { return err } if count <= 0 { //需要存入的数据 dataTime, _ := time.Parse(utils.FormatDate, sk) timestamp := dataTime.UnixNano() / 1e6 timeStr := fmt.Sprintf("%d", timestamp) addSql += "(" addSql += strconv.Itoa(edbInfoId) + "," + "'" + edbCode + "'" + "," + "'" + sk + "'" + "," + utils.SubFloatToString(calVal, 4) + "," + "'" + nowStr + "'" + "," + "'" + nowStr + "'" + "," + "1" addSql += "," + "'" + timeStr + "'" addSql += ")," isAdd = true } else { calVal = utils.FixFloat(calVal, 4) err = data_manage.ModifyEdbDataCalculate(int64(edbInfoId), sk, calVal) if err != nil { return err } } } } if isAdd { addSql = strings.TrimRight(addSql, ",") err = data_manage.AddEdbDataCalculateBySql(addSql) if err != nil { fmt.Println("AddEdbDataCalculate Err:" + err.Error()) return err } } if len(removeDateList) > 0 { removeDateStr := strings.Join(removeDateList, `","`) removeDateStr = `"` + removeDateStr + `"` //如果拼接指标变更了,那么需要删除所有的指标数据 sql := fmt.Sprintf(` DELETE FROM edb_data_calculate WHERE edb_info_id = ? and data_time in (%s) `, removeDateStr) err = data_manage.DeleteEdbDataCalculateBySql(sql, edbInfoId) if err != nil { err = fmt.Errorf("删除计算失败的计算指标数据失败,Err:" + err.Error()) return } } return }