1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102 |
- package data
- import (
- "encoding/json"
- "errors"
- "eta/eta_api/models/data_manage"
- "eta/eta_api/models/data_manage/request"
- "eta/eta_api/models/system"
- "eta/eta_api/services/data/data_manage_permission"
- "eta/eta_api/utils"
- "fmt"
- "github.com/shopspring/decimal"
- "strconv"
- "strings"
- "time"
- )
- func AddPredictEdbInfo(sourceEdbInfoId, classifyId int, edbName string, ruleList []request.RuleConfig, sysUserId int, sysUserName, requestBody, requestUrl string) (edbInfo *data_manage.EdbInfo, err error, errMsg string) {
- var sourceEdbInfo *data_manage.EdbInfo
-
- {
- sourceEdbInfo, err = data_manage.GetEdbInfoById(sourceEdbInfoId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- errMsg = "新增失败"
- err = errors.New("获取来源指标失败,Err:" + err.Error())
- return
- }
- if sourceEdbInfo == nil {
- errMsg = "找不到该来源指标"
- err = nil
- return
- }
-
- if sourceEdbInfo.EdbInfoType != 0 {
- errMsg = "来源指标异常,不是普通的指标"
- return
- }
- if !utils.InArrayByStr([]string{"日度", "周度", "月度"}, sourceEdbInfo.Frequency) {
- errMsg = "预测指标只支持选择日度、周度、月度的指标"
- return
- }
- }
- var classifyInfo *data_manage.EdbClassify
-
- {
- classifyInfo, err = data_manage.GetEdbClassifyById(classifyId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- errMsg = "新增失败"
- err = errors.New("获取预测指标分类失败,Err:" + err.Error())
- return
- }
- if classifyInfo == nil {
- errMsg = "找不到该预测指标分类"
- err = nil
- return
- }
-
- if classifyInfo.ClassifyType != 1 {
- errMsg = "预测指标分类异常,不是预测指标分类"
- return
- }
- }
- edbName = strings.Trim(edbName, " ")
- edbCode := sourceEdbInfo.EdbCode + "_" + time.Now().Format(utils.FormatShortDateTimeUnSpace)
-
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- }
-
- var condition string
- var pars []interface{}
- condition += " AND edb_info_type=? "
- pars = append(pars, 1)
- condition += " AND edb_name=? "
- pars = append(pars, edbName)
- count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
- if err != nil {
- errMsg = "判断指标名称是否存在失败"
- err = errors.New("判断指标名称是否存在失败,Err:" + err.Error())
- return
- }
- if count > 0 {
- errMsg = "指标名称已存在,请重新填写"
- return
- }
- timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
- edbInfo = &data_manage.EdbInfo{
-
- EdbInfoType: 1,
- SourceName: "预测指标",
- Source: utils.DATA_SOURCE_PREDICT,
- EdbCode: edbCode,
- EdbName: edbName,
- EdbNameSource: edbName,
- Frequency: sourceEdbInfo.Frequency,
- Unit: sourceEdbInfo.Unit,
- StartDate: sourceEdbInfo.StartDate,
- ClassifyId: classifyId,
- SysUserId: sysUserId,
- SysUserRealName: sysUserName,
- UniqueCode: utils.MD5(utils.DATA_PREFIX + "_" + timestamp),
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- MinValue: sourceEdbInfo.MinValue,
- MaxValue: sourceEdbInfo.MaxValue,
- CalculateFormula: sourceEdbInfo.CalculateFormula,
- EdbType: 1,
-
- LatestDate: sourceEdbInfo.LatestDate,
- LatestValue: sourceEdbInfo.LatestValue,
- MoveType: sourceEdbInfo.MoveType,
- MoveFrequency: sourceEdbInfo.MoveFrequency,
- NoUpdate: sourceEdbInfo.NoUpdate,
- ServerUrl: "",
- }
-
- calculateMappingList := make([]*data_manage.EdbInfoCalculateMapping, 0)
- fromEdbMap := make(map[int]int)
-
- calculateMappingItem := &data_manage.EdbInfoCalculateMapping{
-
-
- Source: edbInfo.Source,
- SourceName: edbInfo.SourceName,
- EdbCode: edbInfo.EdbCode,
- FromEdbInfoId: sourceEdbInfo.EdbInfoId,
- FromEdbCode: sourceEdbInfo.EdbCode,
- FromEdbName: sourceEdbInfo.EdbName,
- FromSource: sourceEdbInfo.Source,
- FromSourceName: sourceEdbInfo.SourceName,
-
- Sort: 1,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- }
- fromEdbMap[sourceEdbInfoId] = sourceEdbInfoId
- calculateMappingList = append(calculateMappingList, calculateMappingItem)
-
- predictEdbConfList := make([]*data_manage.PredictEdbConf, 0)
- for _, v := range ruleList {
-
- ruleEndDate, tmpErr := time.ParseInLocation(utils.FormatDate, v.EndDate, time.Local)
- if tmpErr != nil {
- errMsg = "规则配置的截止日期异常,请重新填写"
- return
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- switch v.RuleType {
- case 8:
- valInt, tmpErr := strconv.Atoi(v.Value)
- if tmpErr != nil {
- errMsg = "N期段线性外推值的N值异常"
- return
- }
- if valInt <= 1 {
- errMsg = "N期段线性外推值的N值必须大于1"
- return
- }
- case 9:
- for _, v := range v.EdbInfoIdArr {
- fromEdbMap[v.EdbInfoId] = v.EdbInfoId
- }
- }
- tmpPredictEdbConf := &data_manage.PredictEdbConf{
- PredictEdbInfoId: 0,
- SourceEdbInfoId: sourceEdbInfoId,
- RuleType: v.RuleType,
-
- Value: v.Value,
- EndDate: ruleEndDate,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- }
- edbInfo.EndDate = v.EndDate
- predictEdbConfList = append(predictEdbConfList, tmpPredictEdbConf)
- }
- err = data_manage.AddPredictEdb(edbInfo, calculateMappingItem, predictEdbConfList)
- if err != nil {
- errMsg = "保存失败"
- err = errors.New("保存失败,Err:" + err.Error())
- return
- }
-
- {
- edbLog := new(data_manage.EdbInfoLog)
- edbLog.EdbInfoId = edbInfo.EdbInfoId
- edbLog.SourceName = edbInfo.SourceName
- edbLog.Source = edbInfo.Source
- edbLog.EdbCode = edbInfo.EdbCode
- edbLog.EdbName = edbInfo.EdbName
- edbLog.ClassifyId = edbInfo.ClassifyId
- edbLog.SysUserId = sysUserId
- edbLog.SysUserRealName = sysUserName
- edbLog.CreateTime = time.Now()
- edbLog.Content = requestBody
- edbLog.Status = "新增指标"
- edbLog.Method = requestUrl
- go data_manage.AddEdbInfoLog(edbLog)
- }
-
- AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
- return
- }
- func EditPredictEdbInfo(edbInfoId, classifyId int, edbName string, ruleList []request.RuleConfig, sysUserId int, sysUserName, requestBody, requestUrl string) (edbInfo *data_manage.EdbInfo, err error, errMsg string) {
-
- {
- edbInfo, err = data_manage.GetEdbInfoById(edbInfoId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- errMsg = "修改失败"
- err = errors.New("获取预测指标失败,Err:" + err.Error())
- return
- }
- if edbInfo == nil {
- errMsg = "找不到该预测指标"
- err = nil
- return
- }
-
- if edbInfo.EdbInfoType != 1 {
- errMsg = "指标异常,不是预测指标"
- return
- }
- }
- var predictEdbConf *data_manage.PredictEdbConf
-
- {
-
- predictEdbConfList, tmpErr := data_manage.GetPredictEdbConfListById(edbInfo.EdbInfoId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- errMsg = "修改失败"
- err = errors.New("获取预测指标配置信息失败,Err:" + tmpErr.Error())
- return
- }
- if len(predictEdbConfList) == 0 {
- errMsg = "找不到该预测指标配置"
- err = nil
- return
- }
- predictEdbConf = predictEdbConfList[0]
- }
-
- var condition string
- var pars []interface{}
- condition += " AND edb_info_id<>? "
- pars = append(pars, edbInfoId)
- condition += " AND edb_info_type=? "
- pars = append(pars, 1)
- condition += " AND edb_name=? "
- pars = append(pars, edbName)
- count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
- if err != nil {
- errMsg = "判断指标名称是否存在失败"
- err = errors.New("判断指标名称是否存在失败,Err:" + err.Error())
- return
- }
- if count > 0 {
- errMsg = "指标名称已存在,请重新填写"
- return
- }
- edbInfo.EdbName = edbName
- edbInfo.EdbNameSource = edbName
- edbInfo.ClassifyId = classifyId
- edbInfo.ModifyTime = time.Now()
- updateEdbInfoCol := []string{"EdbName", "EdbNameSource", "ClassifyId", "EndDate", "ModifyTime"}
-
- predictEdbConfList := make([]*data_manage.PredictEdbConf, 0)
- for _, v := range ruleList {
-
- ruleEndDate, tmpErr := time.ParseInLocation(utils.FormatDate, v.EndDate, time.Local)
- if tmpErr != nil {
- errMsg = "规则配置的截止日期异常,请重新填写"
- return
- }
- switch v.RuleType {
- case 8:
- valInt, tmpErr := strconv.Atoi(v.Value)
- if tmpErr != nil {
- errMsg = "N期段线性外推值的N值异常"
- return
- }
- if valInt <= 1 {
- errMsg = "N期段线性外推值的N值必须大于1"
- return
- }
- case 9:
- }
- tmpPredictEdbConf := &data_manage.PredictEdbConf{
- PredictEdbInfoId: edbInfoId,
- SourceEdbInfoId: predictEdbConf.SourceEdbInfoId,
- RuleType: v.RuleType,
-
- Value: v.Value,
- EndDate: ruleEndDate,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- }
- predictEdbConfList = append(predictEdbConfList, tmpPredictEdbConf)
- edbInfo.EndDate = v.EndDate
- }
- err = data_manage.EditPredictEdb(edbInfo, predictEdbConfList, updateEdbInfoCol)
- if err != nil {
- errMsg = "保存失败"
- err = errors.New("保存失败,Err:" + err.Error())
- return
- }
-
- {
- edbLog := new(data_manage.EdbInfoLog)
- edbLog.EdbInfoId = edbInfo.EdbInfoId
- edbLog.SourceName = edbInfo.SourceName
- edbLog.Source = edbInfo.Source
- edbLog.EdbCode = edbInfo.EdbCode
- edbLog.EdbName = edbInfo.EdbName
- edbLog.ClassifyId = edbInfo.ClassifyId
- edbLog.SysUserId = sysUserId
- edbLog.SysUserRealName = sysUserName
- edbLog.CreateTime = time.Now()
- edbLog.Content = requestBody
- edbLog.Status = "编辑指标"
- edbLog.Method = requestUrl
- go data_manage.AddEdbInfoLog(edbLog)
- }
-
- AddOrEditEdbInfoToEs(edbInfoId)
-
- go EdbInfoRefreshAllFromBaseV2(edbInfo.EdbInfoId, true, false)
- return
- }
- func RefreshPredictEdbInfo(edbInfoId int, refreshAll bool) (edbInfo *data_manage.EdbInfo, isAsync bool, err error, errMsg string) {
-
- {
- edbInfo, err = data_manage.GetEdbInfoById(edbInfoId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- errMsg = "刷新失败"
- err = errors.New("获取预测指标失败,Err:" + err.Error())
- return
- }
- if edbInfo == nil {
- errMsg = "找不到该预测指标"
- err = nil
- return
- }
-
- if edbInfo.EdbInfoType != 1 {
- errMsg = "指标异常,不是预测指标"
- return
- }
- }
- err, isAsync = EdbInfoRefreshAllFromBaseV2(edbInfo.EdbInfoId, refreshAll, false)
- return
- }
- func MovePredictEdbInfo(edbInfoId, classifyId, prevEdbInfoId, nextEdbInfoId int, sysUser *system.Admin, requestBody, requestUrl string) (err error, errMsg string) {
-
- count, _ := data_manage.GetEdbClassifyCountById(classifyId)
- if count <= 0 {
- errMsg = "分类已被删除,不可移动,请刷新页面"
- return
- }
- edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
- if err != nil {
- if err != nil && err.Error() != utils.ErrNoRow() {
- errMsg = "移动失败"
- err = errors.New("获取预测指标失败,Err:" + err.Error())
- return
- }
- if edbInfo == nil {
- errMsg = "找不到该预测指标"
- err = nil
- return
- }
- return
- }
- var haveOperaAuth bool
-
- {
- haveOperaAuth, err = data_manage_permission.CheckEdbPermissionByEdbInfoId(edbInfo.EdbInfoId, edbInfo.ClassifyId, edbInfo.IsJoinPermission, sysUser.AdminId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("校验指标权限失败,Err:" + err.Error())
- return
- }
- }
-
- button := GetEdbOpButton(sysUser, edbInfo.SysUserId, edbInfo.EdbType, edbInfo.EdbInfoType, haveOperaAuth)
- if !button.MoveButton {
- errMsg = "无权限操作"
- err = nil
- return
- }
-
- if edbInfo.ClassifyId != classifyId {
- err = data_manage.MoveEdbInfo(edbInfoId, classifyId)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("移动预测指标失败,Err:" + err.Error())
- return
- }
- }
- updateCol := make([]string, 0)
-
- if prevEdbInfoId > 0 {
- prevEdbInfo, tmpErr := data_manage.GetEdbInfoById(prevEdbInfoId)
- if tmpErr != nil {
- errMsg = "移动失败"
- err = errors.New("获取上一个兄弟节点分类信息失败,Err:" + tmpErr.Error())
- return
- }
-
- if nextEdbInfoId > 0 {
-
- nextEdbInfo, tmpErr := data_manage.GetEdbInfoById(nextEdbInfoId)
- if tmpErr != nil {
- errMsg = "移动失败"
- err = errors.New("获取下一个兄弟节点分类信息失败,Err:" + tmpErr.Error())
- return
- }
-
- if prevEdbInfo.Sort == nextEdbInfo.Sort || prevEdbInfo.Sort == edbInfo.Sort {
-
- updateSortStr := `sort + 2`
- _ = data_manage.UpdateEdbInfoSortByClassifyId(prevEdbInfo.ClassifyId, prevEdbInfo.Sort, prevEdbInfo.EdbInfoId, updateSortStr)
- } else {
-
- if nextEdbInfo.Sort-prevEdbInfo.Sort == 1 {
-
- updateSortStr := `sort + 1`
- _ = data_manage.UpdateEdbInfoSortByClassifyId(prevEdbInfo.ClassifyId, prevEdbInfo.Sort, prevEdbInfo.EdbInfoId, updateSortStr)
- }
- }
- }
- edbInfo.Sort = prevEdbInfo.Sort + 1
- edbInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- } else {
- firstClassify, tmpErr := data_manage.GetFirstEdbInfoByClassifyId(classifyId)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- errMsg = "移动失败"
- err = errors.New("获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + err.Error())
- return
- }
-
- if firstClassify != nil && firstClassify.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = data_manage.UpdateEdbInfoSortByClassifyId(firstClassify.ClassifyId, 0, firstClassify.EdbInfoId-1, updateSortStr)
- }
- edbInfo.Sort = 0
- edbInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- }
-
- if len(updateCol) > 0 {
- err = edbInfo.Update(updateCol)
- if err != nil {
- errMsg = "移动失败"
- err = errors.New("修改失败,Err:" + err.Error())
- return
- }
-
- AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
- }
-
- {
- edbLog := new(data_manage.EdbInfoLog)
- edbLog.EdbInfoId = edbInfo.EdbInfoId
- edbLog.SourceName = edbInfo.SourceName
- edbLog.Source = edbInfo.Source
- edbLog.EdbCode = edbInfo.EdbCode
- edbLog.EdbName = edbInfo.EdbName
- edbLog.ClassifyId = edbInfo.ClassifyId
- edbLog.SysUserId = sysUser.AdminId
- edbLog.SysUserRealName = sysUser.RealName
- edbLog.CreateTime = time.Now()
- edbLog.Content = requestBody
- edbLog.Status = "移动指标"
- edbLog.Method = requestUrl
- go data_manage.AddEdbInfoLog(edbLog)
- }
- return
- }
- func GetChartPredictEdbInfoDataListByConfList(predictEdbConfList []data_manage.PredictEdbConfAndData, filtrateStartDateStr, latestDateStr, endDateStr, frequency, dataDateType string, realPredictEdbInfoData []*data_manage.EdbDataList) (predictEdbInfoData []*data_manage.EdbDataList, minValue, maxValue float64, err error, errMsg string) {
- endDate, err := time.ParseInLocation(utils.FormatDate, endDateStr, time.Local)
- if err != nil {
- return
- }
- latestDate, err := time.ParseInLocation(utils.FormatDate, latestDateStr, time.Local)
- if err != nil {
- return
- }
-
- startDate := latestDate
-
- if filtrateStartDateStr != `` {
- filtrateStartDate, tmpErr := time.ParseInLocation(utils.FormatDate, filtrateStartDateStr, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
-
- if filtrateStartDate.After(latestDate) {
- startDate = filtrateStartDate.AddDate(0, 0, -1)
- }
- }
-
-
- existMap := make(map[string]float64)
- for _, v := range realPredictEdbInfoData {
-
- existMap[v.DataTime] = v.Value
- }
- predictEdbInfoData = make([]*data_manage.EdbDataList, 0)
-
-
- for _, predictEdbConf := range predictEdbConfList {
- dataEndTime := endDate
- if predictEdbConf.EndDate.Before(dataEndTime) {
- dataEndTime = predictEdbConf.EndDate
- }
- var tmpMinValue, tmpMaxValue float64
- dayList := getPredictEdbDayList(startDate, dataEndTime, frequency, dataDateType)
- if len(dayList) <= 0 {
- continue
- }
- switch predictEdbConf.RuleType {
- case 1:
- var lastDataValue float64
- tmpAllData := make([]*data_manage.EdbDataList, 0)
- tmpAllData = append(tmpAllData, realPredictEdbInfoData...)
- tmpAllData = append(tmpAllData, predictEdbInfoData...)
- lenTmpAllData := len(tmpAllData)
- if lenTmpAllData > 0 {
- lastDataValue = tmpAllData[lenTmpAllData-1].Value
- }
- predictEdbInfoData = GetChartPredictEdbInfoDataListByRule1(predictEdbConf.PredictEdbInfoId, lastDataValue, dayList, predictEdbInfoData, existMap)
- tmpMaxValue = lastDataValue
- tmpMinValue = lastDataValue
- case 2:
- tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- dataValue, _ := tmpValDecimal.Float64()
- predictEdbInfoData = GetChartPredictEdbInfoDataListByRule1(predictEdbConf.PredictEdbInfoId, dataValue, dayList, predictEdbInfoData, existMap)
- tmpMaxValue = dataValue
- tmpMinValue = dataValue
- case 3:
- tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- tbValue, _ := tmpValDecimal.Float64()
- predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTb(predictEdbConf.PredictEdbInfoId, tbValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
- case 4:
- tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- tcValue, _ := tmpValDecimal.Float64()
- predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTc(predictEdbConf.PredictEdbInfoId, tcValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
- case 5:
- tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- hbValue, _ := tmpValDecimal.Float64()
- predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleHb(predictEdbConf.PredictEdbInfoId, hbValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
- case 6:
- tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- hcValue, _ := tmpValDecimal.Float64()
- predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleHc(predictEdbConf.PredictEdbInfoId, hcValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
- case 7:
- nValue, tmpErr := strconv.Atoi(predictEdbConf.Value)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleNMoveMeanValue(predictEdbConf.PredictEdbInfoId, nValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
- case 8:
- nValue, tmpErr := strconv.Atoi(predictEdbConf.Value)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- if nValue <= 1 {
- errMsg = `N期段线性外推值的N值必须大于1`
- err = errors.New(errMsg)
- return
- }
- predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleNLinearRegression(predictEdbConf.PredictEdbInfoId, nValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
- if err != nil {
- return
- }
- case 9:
-
- hcDataMap := make(map[string]float64)
- if predictEdbConf.PredictEdbInfoId > 0 {
- tmpPredictEdbRuleDataList, tmpErr := data_manage.GetPredictEdbRuleDataList(predictEdbConf.PredictEdbInfoId, predictEdbConf.ConfigId, startDate.Format(utils.FormatDate), endDate.Format(utils.FormatDate))
- if tmpErr != nil {
- err = tmpErr
- return
- }
- for _, v := range tmpPredictEdbRuleDataList {
- hcDataMap[v.DataTime] = v.Value
- }
- } else {
- if len(predictEdbConf.DataList) <= 0 {
- return
- }
- for _, v := range predictEdbConf.DataList {
- currentDate, tmpErr := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
- if tmpErr != nil {
- continue
- }
-
- if currentDate.Before(startDate) || currentDate.After(endDate) {
- continue
- }
- hcDataMap[v.DataTime] = v.Value
- }
- }
- predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTrendsHC(predictEdbConf.PredictEdbInfoId, dayList, realPredictEdbInfoData, predictEdbInfoData, hcDataMap, existMap)
- case 10:
- tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- finalValue, _ := tmpValDecimal.Float64()
- predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleFinalValueHc(predictEdbConf.PredictEdbInfoId, finalValue, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
- case 11:
- predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleSeason(predictEdbConf.PredictEdbInfoId, predictEdbConf.Value, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
- if err != nil {
- return
- }
- case 12:
- var moveAverageConf MoveAverageConf
- tmpErr := json.Unmarshal([]byte(predictEdbConf.Value), &moveAverageConf)
- if tmpErr != nil {
- err = errors.New("季节性配置信息异常:" + tmpErr.Error())
- return
- }
- predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleMoveAverageTb(predictEdbConf.PredictEdbInfoId, moveAverageConf.NValue, moveAverageConf.Year, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
- if err != nil {
- return
- }
- case 13:
- tmpValDecimal, tmpErr := decimal.NewFromString(predictEdbConf.Value)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- tbEndValue, _ := tmpValDecimal.Float64()
- predictEdbInfoData, tmpMinValue, tmpMaxValue = GetChartPredictEdbInfoDataListByRuleTbzscz(predictEdbConf.PredictEdbInfoId, tbEndValue, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
- case 14:
- var ruleConf RuleLineNhConf
- err = json.Unmarshal([]byte(predictEdbConf.Value), &ruleConf)
- if err != nil {
- err = errors.New("一元线性拟合配置信息异常:" + err.Error())
- return
- }
-
- newNhccDataMap := make(map[string]float64)
- if predictEdbConf.PredictEdbInfoId > 0 {
- tmpPredictEdbRuleDataList, tmpErr := data_manage.GetPredictEdbRuleDataList(predictEdbConf.PredictEdbInfoId, predictEdbConf.ConfigId, "", "")
- if tmpErr != nil {
- err = tmpErr
- return
- }
- for _, v := range tmpPredictEdbRuleDataList {
- newNhccDataMap[v.DataTime] = v.Value
- }
- } else {
- newNhccDataMap, err = getCalculateNhccData(append(realPredictEdbInfoData, predictEdbInfoData...), ruleConf)
- if err != nil {
- return
- }
- }
- predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleLineNh(predictEdbConf.PredictEdbInfoId, dayList, realPredictEdbInfoData, predictEdbInfoData, newNhccDataMap, existMap)
- if err != nil {
- return
- }
- case 15:
- predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleNAnnualAverage(predictEdbConf.PredictEdbInfoId, predictEdbConf.Value, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
- if err != nil {
- return
- }
- case 16:
- predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleAnnualValueInversion(predictEdbConf.PredictEdbInfoId, predictEdbConf.Value, dayList, frequency, realPredictEdbInfoData, predictEdbInfoData, existMap)
- if err != nil {
- return
- }
- }
-
- {
- lenPredictEdbInfoData := len(predictEdbInfoData)
- if lenPredictEdbInfoData > 0 {
- tmpDataEndTime, _ := time.ParseInLocation(utils.FormatDate, predictEdbInfoData[lenPredictEdbInfoData-1].DataTime, time.Local)
- if startDate.Before(tmpDataEndTime) {
- startDate = tmpDataEndTime
- }
- }
- }
- if tmpMinValue < minValue {
- minValue = tmpMinValue
- }
- if tmpMaxValue > maxValue {
- maxValue = tmpMaxValue
- }
- }
- return
- }
- func getPredictEdbDayList(startDate, endDate time.Time, frequency, dataDateType string) (dayList []time.Time) {
-
- if dataDateType == `` {
- dataDateType = `交易日`
- }
- switch frequency {
- case "日度":
- for currDate := startDate.AddDate(0, 0, 1); currDate.Before(endDate) || currDate.Equal(endDate); currDate = currDate.AddDate(0, 0, 1) {
-
- if dataDateType == `交易日` && (currDate.Weekday() == time.Sunday || currDate.Weekday() == time.Saturday) {
- continue
- }
- dayList = append(dayList, currDate)
- }
- case "周度":
-
- for currDate := startDate.AddDate(0, 0, 7); currDate.Before(endDate) || currDate.Equal(endDate); currDate = currDate.AddDate(0, 0, 7) {
- dayList = append(dayList, currDate)
- }
- case "旬度":
- for currDate := startDate.AddDate(0, 0, 1); currDate.Before(endDate) || currDate.Equal(endDate); {
- nextDate := currDate.AddDate(0, 0, 1)
-
- if nextDate.Day() == 11 || nextDate.Day() == 21 || nextDate.Day() == 1 {
- dayList = append(dayList, currDate)
- }
- currDate = nextDate
- }
- case "月度":
- for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
- currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
- if !currDate.After(endDate) && !currDate.Equal(startDate) {
- dayList = append(dayList, currDate)
- }
- currDate = currDate.AddDate(0, 0, 1)
- }
- case "季度":
- for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
-
- currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
- if !currDate.After(endDate) && !currDate.Equal(startDate) {
-
- if currDate.Month() == 3 || currDate.Month() == 6 || currDate.Month() == 9 || currDate.Month() == 12 {
- dayList = append(dayList, currDate)
- }
- }
- currDate = currDate.AddDate(0, 0, 1)
- }
- case "半年度":
- for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
-
- currDate = time.Date(currDate.Year(), currDate.Month(), 1, 0, 0, 0, 0, time.Now().Location()).AddDate(0, 1, -1)
- if !currDate.After(endDate) && !currDate.Equal(startDate) {
-
- if currDate.Month() == 6 || currDate.Month() == 12 {
- dayList = append(dayList, currDate)
- }
- }
- currDate = currDate.AddDate(0, 0, 1)
- }
- case "年度":
- for currDate := startDate; currDate.Before(endDate) || currDate.Equal(endDate); {
- currDate = time.Date(currDate.Year()+1, 12, 31, 0, 0, 0, 0, time.Now().Location())
- if !currDate.After(endDate) && !currDate.Equal(startDate) {
- dayList = append(dayList, currDate)
- }
- }
- }
- return
- }
- func GetPredictDataListByPredictEdbInfoId(edbInfoId int, startDate, endDate string, isTimeBetween bool) (edbInfo *data_manage.EdbInfo, dataList []*data_manage.EdbDataList, sourceEdbInfoItem *data_manage.EdbInfo, predictEdbConf *data_manage.PredictEdbConf, err error, errMsg string) {
- edbInfo, err = data_manage.GetEdbInfoById(edbInfoId)
- if err != nil {
- errMsg = `获取预测指标信息失败`
- return
- }
- dataList, sourceEdbInfoItem, predictEdbConf, err, errMsg = GetPredictDataListByPredictEdbInfo(edbInfo, startDate, endDate, isTimeBetween)
- return
- }
- func GetPredictDataListByPredictEdbInfo(edbInfo *data_manage.EdbInfo, startDate, endDate string, isTimeBetween bool) (dataList []*data_manage.EdbDataList, sourceEdbInfoItem *data_manage.EdbInfo, predictEdbConf *data_manage.PredictEdbConf, err error, errMsg string) {
- if !isTimeBetween {
- endDate = ``
- }
- return GetPredictCalculateDataListByPredictEdbInfo(edbInfo, startDate, endDate)
- }
- func GetChartDataList(dataList []*data_manage.EdbDataList, chartType int, calendar, latestDateStr, startDate string) (resultDataList interface{}, err error) {
- startDateReal := startDate
- calendarPreYear := 0
- if calendar == "农历" {
- newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal)
- if err != nil {
- fmt.Println("time.Parse:" + err.Error())
- }
- calendarPreYear = newStartDateReal.Year() - 1
- newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
- startDateReal = newStartDateReal.Format(utils.FormatDate)
- }
-
- latestDate, tmpErr := time.Parse(utils.FormatDate, latestDateStr)
- if tmpErr != nil {
- err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + latestDateStr))
- return
- }
- latestDateYear := latestDate.Year()
-
- if chartType == 1 {
- resultDataList = dataList
- return
- }
- if calendar == "农历" {
- if len(dataList) <= 0 {
- resultDataList = data_manage.EdbDataResult{}
- } else {
- result, tmpErr := data_manage.AddCalculateQuarterV4(dataList)
- if tmpErr != nil {
- err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
- return
- }
-
- for k, edbDataItems := range result.List {
- var cuttingDataTimestamp int64
-
- cuttingDataTimeStr := latestDate.AddDate(0, 0, edbDataItems.BetweenDay).Format(utils.FormatDate)
-
- if edbDataItems.Year >= latestDateYear {
- for _, tmpData := range edbDataItems.Items {
- if tmpData.DataTime == cuttingDataTimeStr {
- cuttingDataTimestamp = tmpData.DataTimestamp
- break
- }
- }
- }
- edbDataItems.CuttingDataTimestamp = cuttingDataTimestamp
- result.List[k] = edbDataItems
- }
- if result.List[0].Year != calendarPreYear {
- itemList := make([]*data_manage.EdbDataList, 0)
- items := new(data_manage.EdbDataItems)
-
- items.Items = itemList
- newResult := new(data_manage.EdbDataResult)
- newResult.List = append(newResult.List, items)
- newResult.List = append(newResult.List, result.List...)
- resultDataList = newResult
- } else {
- resultDataList = result
- }
- }
- } else {
- currentYear := time.Now().Year()
- quarterDataList := make([]*data_manage.QuarterData, 0)
- quarterMap := make(map[int][]*data_manage.EdbDataList)
- var quarterArr []int
- for _, v := range dataList {
- itemDate, tmpErr := time.Parse(utils.FormatDate, v.DataTime)
- if tmpErr != nil {
- err = errors.New("季度指标日期转换,Err:" + tmpErr.Error() + ";DataTime:" + v.DataTime)
- return
- }
- year := itemDate.Year()
- newItemDate := itemDate.AddDate(currentYear-year, 0, 0)
- timestamp := newItemDate.UnixNano() / 1e6
- v.DataTimestamp = timestamp
- if findVal, ok := quarterMap[year]; !ok {
- quarterArr = append(quarterArr, year)
- findVal = append(findVal, v)
- quarterMap[year] = findVal
- } else {
- findVal = append(findVal, v)
- quarterMap[year] = findVal
- }
- }
- for _, v := range quarterArr {
- itemList := quarterMap[v]
- quarterItem := new(data_manage.QuarterData)
- quarterItem.Year = v
- quarterItem.DataList = itemList
-
- if v == latestDateYear {
- var cuttingDataTimestamp int64
- for _, tmpData := range itemList {
- if tmpData.DataTime == latestDateStr {
- cuttingDataTimestamp = tmpData.DataTimestamp
- break
- }
- }
- quarterItem.CuttingDataTimestamp = cuttingDataTimestamp
- } else if v > latestDateYear {
-
- if len(itemList) > 0 {
- quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
- }
- }
- quarterDataList = append(quarterDataList, quarterItem)
- }
- resultDataList = quarterDataList
- }
- return
- }
- func GetPredictCalculateDataListByPredictEdbInfo(edbInfo *data_manage.EdbInfo, startDate, endDate string) (dataList []*data_manage.EdbDataList, sourceEdbInfoItem *data_manage.EdbInfo, predictEdbConf *data_manage.PredictEdbConf, err error, errMsg string) {
- dataList, err = data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, startDate, endDate)
- return
- }
- func ModifyPredictEdbBaseInfoBySourceEdb(sourceEDdbInfo *data_manage.EdbInfo, frequency, unit string) {
- list, err := data_manage.GetGroupPredictEdbBySourceEdbInfoId(sourceEDdbInfo.EdbInfoId)
- if err != nil {
- return
- }
- for _, v := range list {
- v.Frequency = frequency
- v.Unit = unit
- v.Update([]string{"Frequency", "Unit"})
- AddOrEditEdbInfoToEs(v.EdbInfoId)
- }
- }
- func ModifyPredictEdbEnBaseInfoBySourceEdb(sourceEDdbInfo *data_manage.EdbInfo, unitEn string) {
- list, err := data_manage.GetGroupPredictEdbBySourceEdbInfoId(sourceEDdbInfo.EdbInfoId)
- if err != nil {
- return
- }
- for _, v := range list {
- v.UnitEn = unitEn
- v.Update([]string{"UnitEn"})
- AddOrEditEdbInfoToEs(v.EdbInfoId)
- }
- }
- func ModifyPredictEdbUnitBySourceEdbInfoId(sourceEdbInfoId int, frequency, unit string) (err error) {
- list, err := data_manage.GetGroupPredictEdbBySourceEdbInfoId(sourceEdbInfoId)
- if err != nil {
- return
- }
- for _, v := range list {
- v.Frequency = frequency
- v.Unit = unit
- v.Update([]string{"Frequency", "Unit"})
- AddOrEditEdbInfoToEs(v.EdbInfoId)
- }
- return
- }
|