123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715 |
- package models
- import (
- "errors"
- "fmt"
- "github.com/beego/beego/v2/client/orm"
- "github.com/shopspring/decimal"
- "github.com/yidane/formula"
- "hongze/hongze_edb_lib/services"
- "hongze/hongze_edb_lib/utils"
- "strconv"
- "strings"
- "time"
- )
- // EdbInfoCalculateSaveReq 计算(运算)指标请求参数
- type EdbInfoCalculateSaveReq struct {
- AdminId int `description:"添加人id"`
- AdminName string `description:"添加人名称"`
- EdbInfoId int `description:"指标id"`
- EdbName string `description:"指标名称"`
- Frequency string `description:"频率"`
- Unit string `description:"单位"`
- ClassifyId int `description:"分类id"`
- CalculateFormula string `description:"计算公式"`
- EdbInfoIdArr []EdbInfoFromTag
- }
- // EdbInfoFromTag 计算指标的关联指标
- type EdbInfoFromTag struct {
- EdbInfoId int `description:"指标id"`
- FromTag string `description:"指标对应标签"`
- }
- // CalculateItems 计算(运算)指标信息
- type CalculateItems struct {
- EdbInfoId int
- DataMap map[string]float64
- }
- // AddCalculate 新增计算(运算)指标
- func AddCalculate(edbInfoIdArr []*EdbInfo, edbInfoId int, edbCode, formulaStr string, edbInfoIdBytes []string) (err error) {
- o := orm.NewOrm()
- defer func() {
- if err != nil {
- utils.FileLog.Info(fmt.Sprintf("Calculate Err:%s" + err.Error()))
- }
- }()
- 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)
- dataList, err := 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 := services.CheckFormula(formulaStr)
- addSql := ` INSERT INTO edb_data_calculate(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
- nowStr := time.Now().Format(utils.FormatDateTime)
- var isAdd bool
- for sk, sv := range saveDataMap {
- formulaStr = strings.ToUpper(formulaStr)
- formulaFormStr := ReplaceFormula(edbInfoIdArr, sv, formulaMap, formulaStr, edbInfoIdBytes)
- if formulaStr == "" {
- return
- }
- if formulaFormStr != "" {
- expression := formula.NewExpression(formulaFormStr)
- calResult, err := expression.Evaluate()
- if err != nil {
- err = errors.New("计算失败: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
- }
- //需要存入的数据
- {
- dataTime, _ := time.ParseInLocation(utils.FormatDate, sk, time.Local)
- timestamp := dataTime.UnixNano() / 1e6
- timeStr := fmt.Sprintf("%d", timestamp)
- addSql += "("
- addSql += strconv.Itoa(edbInfoId) + "," + "'" + edbCode + "'" + "," + "'" + sk + "'" + "," + utils.SubFloatToString(calVal, 4) + "," + "'" + nowStr + "'" +
- "," + "'" + nowStr + "'" + "," + "'" + timeStr + "'"
- addSql += "),"
- isAdd = true
- }
- } else {
- fmt.Println("formulaFormStr is empty")
- }
- }
- if isAdd {
- addSql = strings.TrimRight(addSql, ",")
- _, err = o.Raw(addSql).Exec()
- if err != nil {
- fmt.Println("AddEdbDataCalculate Err:" + err.Error())
- //errMsg = " tx.Exec Err :" + err.Error()
- return
- }
- }
- return
- }
- // AddCalculateInfo 新增计算(运算)指标
- func AddCalculateInfo(req EdbInfoCalculateSaveReq, calculateMappingList []*EdbInfoCalculateMapping, edbInfoList []*EdbInfo, edbCode, uniqueCode string, edbInfoIdBytes []string) (edbInfo *EdbInfo, err error) {
- o := orm.NewOrm()
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- fmt.Println("AddCalculateHcz,Err:" + err.Error())
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
- edbInfo = &EdbInfo{
- EdbInfoId: 0,
- SourceName: "指标运算",
- Source: utils.DATA_SOURCE_CALCULATE,
- EdbCode: edbCode,
- EdbName: req.EdbName,
- EdbNameSource: req.EdbName,
- Frequency: req.Frequency,
- Unit: req.Unit,
- StartDate: "",
- EndDate: "",
- ClassifyId: req.ClassifyId,
- SysUserId: req.AdminId,
- SysUserRealName: req.AdminName,
- UniqueCode: uniqueCode,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- MinValue: 0,
- MaxValue: 0,
- CalculateFormula: req.CalculateFormula,
- EdbType: 2,
- Sort: 0,
- MoveType: 0,
- MoveFrequency: "",
- NoUpdate: 0,
- ServerUrl: "",
- EdbInfoType: 0,
- EdbNameEn: "",
- UnitEn: "",
- LatestDate: "",
- LatestValue: 0,
- ChartImage: "",
- }
- lastId, err := to.Insert(edbInfo)
- if err != nil {
- return
- }
- edbInfo.EdbInfoId = int(lastId)
- //关联关系表
- {
- //处理关联指标
- for _, v := range calculateMappingList {
- v.EdbCode = edbCode
- v.EdbInfoId = edbInfo.EdbInfoId
- }
- if len(calculateMappingList) > 0 {
- _, err = to.InsertMulti(len(calculateMappingList), calculateMappingList)
- if err != nil {
- return
- }
- }
- }
- //计算数据
- err = refreshAllCalculate(to, edbInfoList, edbInfo.EdbInfoId, edbInfo.Source, edbInfo.EdbCode, edbInfo.CalculateFormula, "", "", edbInfoIdBytes)
- return
- }
- // EditCalculateInfo 编辑计算(运算)指标
- func EditCalculateInfo(edbInfo *EdbInfo, req EdbInfoCalculateSaveReq, formulaMap map[string]string, edbInfoIdBytes []string, needCalculate bool) (err error, errMsg string) {
- o := orm.NewOrm()
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- fmt.Println("AddCalculateHcz,Err:" + err.Error())
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
- // 修改指标信息
- edbInfo.EdbName = req.EdbName
- edbInfo.EdbNameSource = req.EdbName
- edbInfo.Frequency = req.Frequency
- edbInfo.Unit = req.Unit
- edbInfo.ClassifyId = req.ClassifyId
- edbInfo.CalculateFormula = req.CalculateFormula
- edbInfo.ModifyTime = time.Now()
- _, err = to.Update(edbInfo, "EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId", "CalculateFormula", "ModifyTime")
- if err != nil {
- return
- }
- if needCalculate {
- // 清除计算数据
- sql := `DELETE FROM edb_data_calculate WHERE edb_info_id=?`
- _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
- if err != nil {
- return
- }
- // 清除关联关系表
- sql = `DELETE FROM edb_info_calculate_mapping WHERE edb_info_id=?`
- _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
- if err != nil {
- return
- }
- edbInfoList := make([]*EdbInfo, 0)
- calculateMappingList := make([]*EdbInfoCalculateMapping, 0)
- for k, v := range req.EdbInfoIdArr {
- fromEdbInfo, tmpErr := GetEdbInfoById(v.EdbInfoId)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- edbInfoList = append(edbInfoList, fromEdbInfo)
- //关联关系表
- {
- calculateMappingItem := &EdbInfoCalculateMapping{
- EdbInfoCalculateMappingId: 0,
- EdbInfoId: req.EdbInfoId,
- Source: edbInfo.Source,
- SourceName: edbInfo.SourceName,
- EdbCode: edbInfo.EdbCode,
- FromEdbInfoId: fromEdbInfo.EdbInfoId,
- FromEdbCode: fromEdbInfo.EdbCode,
- FromEdbName: fromEdbInfo.EdbName,
- FromSource: fromEdbInfo.Source,
- FromSourceName: fromEdbInfo.SourceName,
- FromTag: v.FromTag,
- Sort: k + 1,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- }
- calculateMappingList = append(calculateMappingList, calculateMappingItem)
- }
- }
- //预先计算,判断公式是否正常
- ok, _ := CheckFormula2(edbInfoList, formulaMap, req.CalculateFormula, edbInfoIdBytes)
- if !ok {
- errMsg = "生成计算指标失败,请使用正确的计算公式"
- err = errors.New(errMsg)
- return
- }
- //关联关系表
- {
- if len(calculateMappingList) > 0 {
- _, err = to.InsertMulti(len(calculateMappingList), calculateMappingList)
- if err != nil {
- return
- }
- }
- }
- //计算数据
- err = refreshAllCalculate(to, edbInfoList, edbInfo.EdbInfoId, edbInfo.Source, edbInfo.EdbCode, edbInfo.CalculateFormula, "", "", edbInfoIdBytes)
- }
- return
- }
- // GetCalculateEdbInfo 获取计算指标的关联指标id
- func GetCalculateEdbInfo(edbInfoId int) (from_edb_info_id string, err error) {
- o := orm.NewOrm()
- sql := ` SELECT GROUP_CONCAT(from_edb_info_id ORDER BY sort ASC SEPARATOR ',') AS from_edb_info_id
- FROM edb_info_calculate_mapping
- WHERE edb_info_id=?
- GROUP BY edb_info_id
- `
- err = o.Raw(sql, edbInfoId).QueryRow(&from_edb_info_id)
- return
- }
- // DeleteCalculateEdbInfo 删除指标信息
- func DeleteCalculateEdbInfo(edbInfoId int) (err error) {
- o := orm.NewOrm()
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
- sql := `DELETE FROM edb_data_calculate WHERE edb_info_id=?`
- _, err = to.Raw(sql, edbInfoId).Exec()
- if err != nil {
- return
- }
- sql = `DELETE FROM edb_info_calculate_mapping WHERE edb_info_id=?`
- _, err = to.Raw(sql, edbInfoId).Exec()
- return
- }
- // RefreshAllCalculate 刷新全部数据
- func RefreshAllCalculate(edbInfoIdArr []*EdbInfo, edbInfoId, source int, edbCode, formulaStr, startDate, endDate string, edbInfoIdBytes []string) (err error) {
- o := orm.NewOrm()
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- fmt.Println("RefreshAllCalculate,Err:" + err.Error())
- _ = to.Rollback()
- } else {
- _ = to.Commit()
- }
- }()
- fmt.Println(startDate, endDate)
- //计算数据
- err = refreshAllCalculate(to, edbInfoIdArr, edbInfoId, source, edbCode, formulaStr, startDate, endDate, edbInfoIdBytes)
- return
- }
- // refreshAllCalculate 刷新全部数据
- func refreshAllCalculate(to orm.TxOrmer, edbInfoIdArr []*EdbInfo, edbInfoId, source int, edbCode, formulaStr, startDate, endDate string, edbInfoIdBytes []string) (err error) {
- realSaveDataMap := make(map[string]map[int]float64)
- saveDataMap := make(map[string]map[int]float64)
- dateList := make([]string, 0)
- for edbInfoIndex, 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)
- //}
- //fmt.Println("v.Source:", v.Source)
- dataList, err := GetEdbDataListAllByTo(to, condition, pars, v.Source, 1)
- if err != nil {
- return err
- }
- for _, dv := range dataList {
- if val, ok := realSaveDataMap[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
- realSaveDataMap[dv.DataTime] = temp
- }
- // saveDataMap
- if val, ok := saveDataMap[dv.DataTime]; ok {
- if _, ok := val[v.EdbInfoId]; !ok {
- val[v.EdbInfoId] = dv.Value
- }
- } else {
- temp2 := make(map[int]float64)
- temp2[v.EdbInfoId] = dv.Value
- saveDataMap[dv.DataTime] = temp2
- }
- // 以第一个指标的日期作为基准日期
- if edbInfoIndex == 0 {
- dateList = append(dateList, dv.DataTime)
- }
- }
- }
- //数据处理,将日期内不全的数据做补全
- handleDateSaveDataMap(dateList, realSaveDataMap, saveDataMap, edbInfoIdArr)
- formulaMap := services.CheckFormula(formulaStr)
- addSql := ` INSERT INTO edb_data_calculate(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
- var isAdd bool
- //获取指标所有数据
- dataList := make([]*EdbData, 0)
- dataTableName := GetEdbDataTableName(source)
- sql := `SELECT * FROM %s WHERE edb_info_id=? `
- sql = fmt.Sprintf(sql, dataTableName)
- _, err = to.Raw(sql, edbInfoId).QueryRows(&dataList)
- if err != nil {
- return err
- }
- dataMap := make(map[string]string)
- removeDateMap := make(map[string]string) //需要移除的日期
- for _, v := range dataList {
- dataMap[v.DataTime] = v.Value
- removeDateMap[v.DataTime] = v.DataTime
- }
- edbInfoIdStr := strconv.Itoa(edbInfoId)
- existDataMap := make(map[string]string)
- for sk, sv := range saveDataMap {
- //fmt.Println(sk, sv)
- formulaStr = strings.ToUpper(formulaStr)
- formulaFormStr := ReplaceFormula(edbInfoIdArr, sv, formulaMap, formulaStr, edbInfoIdBytes)
- //计算公式异常,那么就移除该指标
- if formulaFormStr == `` {
- //removeDateList = append(removeDateList, sk)
- continue
- }
- //utils.FileLog.Info(fmt.Sprintf("formulaFormStr:%s", formulaFormStr))
- fmt.Println(fmt.Sprintf("%s:formulaFormStr:%s", sk, 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("计算失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
- fmt.Println(err)
- return err
- }
- // 如果计算结果是NAN,那么就退出当前循环
- if calResult.IsNan() {
- continue
- }
- calVal, err := calResult.Float64()
- if err != nil {
- err = errors.New("计算失败:获取计算值失败 Err:" + err.Error() + ";formulaStr:" + formulaFormStr)
- fmt.Println(err)
- return err
- }
- // 有计算出来值,那么就从待删除指标中移除
- delete(removeDateMap, sk)
- saveValue := decimal.NewFromFloat(calVal).RoundCeil(4).String() //utils.SubFloatToString(calVal, 4)
- if existVal, ok := dataMap[sk]; !ok {
- dataTime, _ := time.ParseInLocation(utils.FormatDate, sk, time.Local)
- timestamp := dataTime.UnixNano() / 1e6
- timeStr := fmt.Sprintf("%d", timestamp)
- if _, existOk := existDataMap[sk]; !existOk {
- addSql += GetAddSql(edbInfoIdStr, edbCode, sk, timeStr, saveValue)
- isAdd = true
- }
- existDataMap[sk] = sk
- } else {
- existValDecimal, err := decimal.NewFromString(existVal)
- existStr := existValDecimal.String()
- if existStr != saveValue {
- sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
- sql = fmt.Sprintf(sql, dataTableName)
- _, err = to.Raw(sql, saveValue, edbInfoId, sk).Exec()
- if err != nil {
- return err
- }
- }
- }
- }
- if isAdd {
- addSql = strings.TrimRight(addSql, ",")
- _, err = to.Raw(addSql).Exec()
- if err != nil {
- fmt.Println("RefreshAllCalculate add Err", err.Error())
- return
- }
- }
- if len(removeDateMap) > 0 {
- removeDateList := make([]string, 0) //需要移除的日期
- for k := range removeDateMap {
- removeDateList = append(removeDateList, k)
- }
- removeDateStr := strings.Join(removeDateList, `","`)
- removeDateStr = `"` + removeDateStr + `"`
- //如果拼接指标变更了,那么需要删除所有的指标数据
- tableName := GetEdbDataTableName(source)
- sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (%s) `, tableName, removeDateStr)
- _, err = to.Raw(sql, edbInfoId).Exec()
- if err != nil {
- err = fmt.Errorf("删除计算失败的计算指标数据失败,Err:" + err.Error())
- return
- }
- }
- return
- }
- // ReplaceFormula 替换计算方式
- func ReplaceFormula(edbInfoIdArr []*EdbInfo, valArr map[int]float64, formulaMap map[string]string, formulaStr string, edbInfoIdBytes []string) string {
- funMap := GetFormulaMap()
- for k, v := range funMap {
- formulaStr = strings.Replace(formulaStr, k, v, -1)
- }
- replaceCount := 0
- for dk, dv := range edbInfoIdArr {
- var isReplace bool
- formulaStr, isReplace = GetFormulaReplace(dk, dv.EdbInfoId, formulaStr, edbInfoIdBytes, formulaMap, valArr)
- if isReplace {
- replaceCount++
- }
- }
- for k, v := range funMap {
- formulaStr = strings.Replace(formulaStr, v, k, -1)
- }
- if replaceCount == len(formulaMap) {
- return formulaStr
- } else {
- return ""
- }
- }
- func GetFormulaReplace(index, edbInfoId int, formulaStr string, edbInfoIdBytes []string, formulaMap map[string]string, valArr map[int]float64) (formulaResult string, isReplace bool) {
- formulaResult = formulaStr
- dKey := edbInfoIdBytes[index]
- if _, ok := formulaMap[dKey]; ok { //公式中存在
- if val, valOk := valArr[edbInfoId]; valOk { //值存在
- dvStr := fmt.Sprintf("%v", val)
- formulaResult = strings.Replace(formulaStr, dKey, dvStr, -1)
- isReplace = true
- }
- }
- return
- }
- // GetFormulaMap 获取计算公式的map
- func GetFormulaMap() map[string]string {
- funMap := make(map[string]string)
- funMap["MAX"] = "[@@]"
- funMap["MIN"] = "[@!]"
- funMap["ABS"] = "[@#]"
- funMap["CEIL"] = "[@$]"
- funMap["COS"] = "[@%]"
- funMap["FLOOR"] = "[@^]"
- funMap["MOD"] = "[@&]"
- funMap["POW"] = "[@*]"
- funMap["ROUND"] = "[@(]"
- return funMap
- }
- // EdbInfoCalculateBatchSaveReq 添加计算指标的请求参数
- type EdbInfoCalculateBatchSaveReq struct {
- AdminId int `description:"添加人id"`
- AdminName string `description:"添加人名称"`
- EdbInfoId int `description:"指标id"`
- EdbName string `description:"指标名称"`
- Frequency string `description:"频度"`
- Unit string `description:"单位"`
- ClassifyId int `description:"分类id"`
- Formula string `description:"N值/移动天数"`
- FromEdbInfoId int `description:"计算来源指标id"`
- Source int `description:"来源:1:同花顺,2:wind,3:彭博,4:指标运算,5:累计值转月,6:同比值,7:同差值,8:N数值移动平均计算,12:环比值,13:环差值,14:升频"`
- CalculateFormula string `description:"计算公式"`
- EdbInfoIdArr []EdbInfoCalculateEdbInfoIdReq `description:"关联指标列表"`
- MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
- MoveFrequency string `description:"移动频度:天/周/月/季/年"`
- Calendar string `description:"公历/农历"`
- }
- // EdbInfoCalculateEdbInfoIdReq 新增/编辑请求 关联的指标列表
- type EdbInfoCalculateEdbInfoIdReq struct {
- EdbInfoId int `description:"指标id"`
- FromTag string `description:"指标对应标签"`
- MoveValue int `description:"移动的值"`
- }
- // EdbInfoCalculateBatchEditReq 编辑计算指标的请求参数
- type EdbInfoCalculateBatchEditReq struct {
- EdbName string `description:"指标名称"`
- Frequency string `description:"频度"`
- Unit string `description:"单位"`
- ClassifyId int `description:"分类id"`
- Formula string `description:"N值"`
- EdbInfoId int `description:"编辑指标id"`
- FromEdbInfoId int `description:"计算来源指标id"`
- Source int `description:"来源:1:同花顺,2:wind,3:彭博,4:指标运算,5:累计值转月,6:同比值,7:同差值,8:N数值移动平均计算,12:环比值,13:环差值,14:升频"`
- MoveType int `description:"移动方式:1:领先(默认),2:滞后"`
- MoveFrequency string `description:"移动频度:天/周/月/季/年"`
- EdbInfoIdArr []struct {
- EdbInfoId int `description:"指标id"`
- FromTag string `description:"指标对应标签"`
- MoveValue int `description:"移动的值"`
- }
- Calendar string `description:"公历/农历" orm:"default(公历)"`
- }
- // CheckFormula2 校验公式是否正常(比如说除法的分母不能为0之类的,实际上就是用预设的字段数据做一次计算)
- func CheckFormula2(edbInfoArr []*EdbInfo, formulaMap map[string]string, formulaStr string, edbInfoIdBytes []string) (ok bool, err error) {
- valArr := make(map[int]float64)
- for _, v := range edbInfoArr {
- valArr[v.EdbInfoId] = 100
- }
- formulaStr = strings.ToUpper(formulaStr)
- formulaFormStr := ReplaceFormula(edbInfoArr, valArr, formulaMap, formulaStr, edbInfoIdBytes)
- if formulaFormStr == "" {
- return
- }
- expression := formula.NewExpression(formulaFormStr)
- _, err = expression.Evaluate()
- if err != nil {
- } else {
- ok = true
- }
- return
- }
- // 处理整个数据
- func handleDateSaveDataMap(dateList []string, realSaveDataMap, saveDataMap map[string]map[int]float64, edbInfoIdArr []*EdbInfo) {
- for _, date := range dateList {
- tmpDataMap := realSaveDataMap[date]
- for _, edbInfo := range edbInfoIdArr {
- tmpEdbInfoId := edbInfo.EdbInfoId // 当前指标id
- // 如果该日期不存在该指标数据,那么需要找寻前后日期的数据,进行填补
- if _, ok := tmpDataMap[tmpEdbInfoId]; !ok {
- //day := 0
- //switch edbInfo.Frequency {
- //case "周度":
- // day = 7
- //case "旬度":
- // day = 15
- //case "月度":
- // day = 30
- //case "季度":
- // day = 90
- //case "年度":
- // day = 365
- //}
- // 需求池 255 指标运算文案修改,补数据遍历区间修改(2023-3-7 09:37:23修改)
- day := 35
- handleDateDataMap(realSaveDataMap, saveDataMap, date, tmpEdbInfoId, day)
- }
- }
- }
- }
- // handleDateDataMap 处理单个日期的数据
- func handleDateDataMap(realSaveDataMap, saveDataMap map[string]map[int]float64, date string, edbInfoId, day int) {
- currDate, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
- // 后一天
- nextDateDayStr := currDate.AddDate(0, 0, 1).Format(utils.FormatDate)
- // 前一天
- preDateDayStr := currDate.AddDate(0, 0, -1).Format(utils.FormatDate)
- for i := 1; i <= day; i++ {
- // 下个日期的数据
- {
- if i >= 1 {
- nextDateDayStr = currDate.AddDate(0, 0, i).Format(utils.FormatDate)
- }
- if findDataMap, hasFindDataMap := realSaveDataMap[nextDateDayStr]; hasFindDataMap { // 下一个日期有数据
- if val, hasFindItem := findDataMap[edbInfoId]; hasFindItem {
- saveDataMap[date][edbInfoId] = val
- return
- }
- }
- }
- // 上个日期的数据
- {
- if i >= 1 {
- preDateDayStr = currDate.AddDate(0, 0, -i).Format(utils.FormatDate)
- }
- if findDataMap, hasFindDataMap := realSaveDataMap[preDateDayStr]; hasFindDataMap { // 下一个日期有数据
- if val, hasFindItem := findDataMap[edbInfoId]; hasFindItem {
- saveDataMap[date][edbInfoId] = val
- return
- }
- }
- }
- }
- }
|