123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362 |
- package data
- import (
- "encoding/json"
- "errors"
- "fmt"
- "github.com/shopspring/decimal"
- "github.com/yidane/formula"
- "hongze/hz_crm_api/models/data_manage"
- "hongze/hz_crm_api/models/data_manage/request"
- "hongze/hz_crm_api/models/system"
- "hongze/hz_crm_api/utils"
- "strconv"
- "strings"
- "time"
- )
- // AddPredictEdbInfo 新增预测指标
- 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)
- // 判断该来源指标是否已经被引用了
- {
- //predictEdbConf, tmpErr := data_manage.GetPredictEdbConfBySourceEdbInfoId(sourceEdbInfoId)
- //if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- // errMsg = "新增失败"
- // err = tmpErr
- // return
- //}
- // 如果该来源指标已经被引用了,那么不允许再次使用
- //if predictEdbConf != nil {
- // //获取预测指标详情
- // predictEdbInfo, tmpErr := data_manage.GetEdbInfoById(predictEdbConf.PredictEdbInfoId)
- // if tmpErr != nil {
- // errMsg = "新增失败"
- // err = tmpErr
- // return
- // }
- // //获取预测指标的分类
- // edbClassifyInfo, tmpErr := data_manage.GetEdbClassifyById(predictEdbInfo.ClassifyId)
- // if tmpErr != nil {
- // errMsg = "新增失败"
- // err = tmpErr
- // return
- // }
- // errMsg = "该指标已存在数据库,目录为:" + edbClassifyInfo.ClassifyName + ",请重新选择指标"
- // err = errors.New(errMsg)
- // return
- //}
- }
- //判断指标名称是否存在
- 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{
- //EdbInfoId: 0,
- 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,
- //Sort: sourceEdbInfo.,
- 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{
- //EdbInfoCalculateMappingId: 0,
- //EdbInfoId: 0,
- Source: edbInfo.Source,
- SourceName: edbInfo.SourceName,
- EdbCode: edbInfo.EdbCode,
- FromEdbInfoId: sourceEdbInfo.EdbInfoId,
- FromEdbCode: sourceEdbInfo.EdbCode,
- FromEdbName: sourceEdbInfo.EdbName,
- FromSource: sourceEdbInfo.Source,
- FromSourceName: sourceEdbInfo.SourceName,
- //FromTag: "",
- 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 3: //3:同比
- // v.Value = "0.1"
- //case 4: //4:同差
- // v.Value = "10"
- //case 5: //5:环比
- // v.Value = "0.1"
- //case 6: //6:环差
- // v.Value = "0.1"
- //case 7: //7:N期移动均值
- // v.Value = "5"
- //case 8: //8:N期段线性外推值
- // v.Value = "5"
- //}
- switch v.RuleType {
- case 8: //N期段线性外推值
- valInt, tmpErr := strconv.Atoi(v.Value)
- if tmpErr != nil {
- errMsg = "N期段线性外推值的N值异常"
- return
- }
- if valInt <= 1 {
- errMsg = "N期段线性外推值的N值必须大于1"
- return
- }
- case 9: //9:动态环差
- for _, v := range v.EdbInfoIdArr {
- fromEdbMap[v.EdbInfoId] = v.EdbInfoId
- }
- }
- tmpPredictEdbConf := &data_manage.PredictEdbConf{
- PredictEdbInfoId: 0,
- SourceEdbInfoId: sourceEdbInfoId,
- RuleType: v.RuleType,
- //FixedValue: v.Value,
- 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)
- }
- //添加es
- AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
- return
- }
- // EditPredictEdbInfo 编辑预测指标
- 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: //N期段线性外推值
- valInt, tmpErr := strconv.Atoi(v.Value)
- if tmpErr != nil {
- errMsg = "N期段线性外推值的N值异常"
- return
- }
- if valInt <= 1 {
- errMsg = "N期段线性外推值的N值必须大于1"
- return
- }
- case 9: //9:动态环差
- }
- tmpPredictEdbConf := &data_manage.PredictEdbConf{
- PredictEdbInfoId: edbInfoId,
- SourceEdbInfoId: predictEdbConf.SourceEdbInfoId,
- RuleType: v.RuleType,
- //FixedValue: v.Value,
- 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)
- }
- //添加es
- AddOrEditEdbInfoToEs(edbInfoId)
- // 刷新关联指标
- go EdbInfoRefreshAllFromBaseV2(edbInfo.EdbInfoId, true)
- return
- }
- // RefreshPredictEdbInfo 刷新预测指标
- func RefreshPredictEdbInfo(edbInfoId int, refreshAll bool) (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
- }
- }
- err = EdbInfoRefreshAllFromBaseV2(edbInfo.EdbInfoId, refreshAll)
- return
- }
- // MovePredictEdbInfo 移动预测指标
- 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
- }
- // 移动权限校验
- button := GetEdbOpButton(sysUser, edbInfo.SysUserId, edbInfo.EdbType, edbInfo.EdbInfoType)
- if !button.MoveButton {
- errMsg = "无权限操作"
- err = nil
- return
- 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)
- //如果有传入 上一个兄弟节点分类id
- 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
- }
- //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
- 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
- }
- //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
- 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
- }
- //新增操作日志
- {
- 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
- }
- // GetPredictEdbInfoDataList 获取预测指标的未来数据
- func GetPredictEdbInfoDataList(predictEdbConf data_manage.PredictEdbConf, latestDateStr string, lastDataValue float64, endDateStr, frequency string) (predictEdbInfoData []*data_manage.EdbData, err error) {
- 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
- }
- dataValue := lastDataValue
- if predictEdbConf.RuleType == 2 {
- dataValue = predictEdbConf.FixedValue
- }
- //获取后面的预测数据
- dayList := getPredictEdbDayList(latestDate, endDate, frequency)
- predictEdbInfoData = make([]*data_manage.EdbData, 0)
- lenDayList := len(dayList)
- if lenDayList > 0 {
- for i := lenDayList - 1; i >= 0; i-- {
- v := dayList[i]
- predictEdbInfoData = append(predictEdbInfoData, &data_manage.EdbData{
- EdbDataId: predictEdbConf.PredictEdbInfoId + 100000 + i,
- EdbInfoId: predictEdbConf.PredictEdbInfoId,
- DataTime: v.Format(utils.FormatDate),
- Value: dataValue,
- })
- }
- }
- return
- }
- // GetChartPredictEdbInfoDataList 获取图表的预测指标的未来数据
- func GetChartPredictEdbInfoDataList(predictEdbConf data_manage.PredictEdbConf, filtrateStartDateStr, latestDateStr string, lastDataValue float64, endDateStr, frequency string) (predictEdbInfoData []*data_manage.EdbDataList, err error) {
- 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)
- }
- }
- dataValue := lastDataValue
- if predictEdbConf.RuleType == 2 {
- dataValue = predictEdbConf.FixedValue
- }
- //获取后面的预测数据
- dayList := getPredictEdbDayList(startDate, endDate, frequency)
- predictEdbInfoData = make([]*data_manage.EdbDataList, 0)
- for k, v := range dayList {
- predictEdbInfoData = append(predictEdbInfoData, &data_manage.EdbDataList{
- EdbDataId: predictEdbConf.PredictEdbInfoId + 100000 + k,
- EdbInfoId: predictEdbConf.PredictEdbInfoId,
- DataTime: v.Format(utils.FormatDate),
- Value: dataValue,
- DataTimestamp: v.UnixNano() / 1e6,
- })
- }
- return
- }
- // GetChartPredictEdbInfoDataListByConfList 获取图表的预测指标的未来数据
- func GetChartPredictEdbInfoDataListByConfList(predictEdbConfList []data_manage.PredictEdbConfAndData, filtrateStartDateStr, latestDateStr, endDateStr, frequency 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)
- }
- }
- //var dateArr []string
- // 对应日期的值
- existMap := make(map[string]float64)
- for _, v := range realPredictEdbInfoData {
- //dateArr = append(dateArr, v.DataTime)
- existMap[v.DataTime] = v.Value
- }
- predictEdbInfoData = make([]*data_manage.EdbDataList, 0)
- //dataValue := lastDataValue
- //预测规则,1:最新,2:固定值,3:同比,4:同差,5:环比,6:环差,7:N期移动均值,8:N期段线性外推值
- for _, predictEdbConf := range predictEdbConfList {
- dataEndTime := endDate
- if predictEdbConf.EndDate.Before(dataEndTime) {
- dataEndTime = predictEdbConf.EndDate
- }
- var tmpMinValue, tmpMaxValue float64 // 当前预测结果中的最大/最小值
- dayList := getPredictEdbDayList(startDate, dataEndTime, frequency)
- if len(dayList) <= 0 { // 如果未来没有日期的话,那么就退出当前循环,进入下一个循环
- continue
- }
- switch predictEdbConf.RuleType {
- case 1: //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: //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: //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: //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: //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: //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: //7:N期移动均值
- 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: //8:N期段线性外推值
- 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: //9:动态环差”预测规则;
- //规则计算的环差值map
- 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: //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: //11:根据 季节性 规则获取预测数据
- predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleSeason(predictEdbConf.PredictEdbInfoId, predictEdbConf.Value, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
- if err != nil {
- return
- }
- case 12: //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: //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: //14:根据 一元线性拟合 规则获取预测数据
- var ruleConf RuleLineNhConf
- err = json.Unmarshal([]byte(predictEdbConf.Value), &ruleConf)
- if err != nil {
- err = errors.New("一元线性拟合配置信息异常:" + err.Error())
- return
- }
- // 规则计算的拟合残差值map
- 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: //15:N年均值:过去N年同期均值。过去N年可以连续或者不连续,指标数据均用线性插值补全为日度数据后计算;
- predictEdbInfoData, tmpMinValue, tmpMaxValue, err = GetChartPredictEdbInfoDataListByRuleNAnnualAverage(predictEdbConf.PredictEdbInfoId, predictEdbConf.Value, dayList, realPredictEdbInfoData, predictEdbInfoData, existMap)
- if err != nil {
- return
- }
- case 16: //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
- }
- // GetPredictEdbDayList 获取预测指标日期列表
- func getPredictEdbDayList(startDate, endDate time.Time, frequency string) (dayList []time.Time) {
- //if !utils.InArrayByStr([]string{"日度", "周度", "月度"}, frequency)
- switch frequency {
- case "日度":
- for currDate := startDate.AddDate(0, 0, 1); currDate.Before(endDate) || currDate.Equal(endDate); currDate = currDate.AddDate(0, 0, 1) {
- //周六、日排除
- if currDate.Weekday() == time.Sunday || currDate.Weekday() == time.Saturday {
- continue
- }
- dayList = append(dayList, currDate)
- }
- case "周度":
- //nextDate := startDate.AddDate(0, 0, 7)
- 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)
- //每个月的10号、20号、最后一天,那么就写入
- 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
- }
- // GetPredictDataListByPredictEdbInfoId 根据预测指标id获取预测指标的数据
- 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
- }
- // GetPredictDataListByPredictEdbInfo 根据预测指标信息获取预测指标的数据
- 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 edbInfo.EdbType != 1 {
- if !isTimeBetween { //如果不是区间数据,那么就结束日期为空
- endDate = ``
- }
- return GetPredictCalculateDataListByPredictEdbInfo(edbInfo, startDate, endDate)
- }
- // 查找该预测指标配置
- predictEdbConfList, err := data_manage.GetPredictEdbConfListById(edbInfo.EdbInfoId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- errMsg = "获取预测指标配置信息失败"
- return
- }
- if len(predictEdbConfList) == 0 {
- errMsg = "获取预测指标配置信息失败"
- err = errors.New(errMsg)
- return
- }
- predictEdbConf = predictEdbConfList[0]
- // 来源指标
- sourceEdbInfoItem, err = data_manage.GetEdbInfoById(predictEdbConf.SourceEdbInfoId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "找不到来源指标信息"
- err = errors.New(errMsg)
- }
- return
- }
- allDataList := make([]*data_manage.EdbDataList, 0)
- //获取指标数据(实际已生成)
- dataList, err = data_manage.GetEdbDataList(sourceEdbInfoItem.Source, sourceEdbInfoItem.EdbInfoId, startDate, endDate)
- if err != nil {
- return
- }
- // 如果选择了日期,那么需要筛选所有的数据,用于未来指标的生成
- if startDate != `` {
- allDataList, err = data_manage.GetEdbDataList(sourceEdbInfoItem.Source, sourceEdbInfoItem.EdbInfoId, "", "")
- if err != nil {
- return
- }
- } else {
- allDataList = dataList
- }
- // 获取预测指标未来的数据
- predictDataList := make([]*data_manage.EdbDataList, 0)
- endDateStr := edbInfo.EndDate //预测指标的结束日期
- if isTimeBetween { //如果是时间区间,那么
- reqEndDateTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
- endDateTime, _ := time.ParseInLocation(utils.FormatDate, edbInfo.EndDate, time.Local)
- // 如果选择的时间区间结束日期 晚于 当天,那么预测数据截止到当天
- if reqEndDateTime.Before(endDateTime) {
- endDateStr = endDate
- }
- }
- //predictDataList, err = GetChartPredictEdbInfoDataList(*predictEdbConf, startDate, sourceEdbInfoItem.LatestDate, sourceEdbInfoItem.LatestValue, endDateStr, edbInfo.Frequency)
- predictEdbConfDataList := make([]data_manage.PredictEdbConfAndData, 0)
- for _, v := range predictEdbConfList {
- predictEdbConfDataList = append(predictEdbConfDataList, data_manage.PredictEdbConfAndData{
- ConfigId: v.ConfigId,
- PredictEdbInfoId: v.PredictEdbInfoId,
- SourceEdbInfoId: v.SourceEdbInfoId,
- RuleType: v.RuleType,
- FixedValue: v.FixedValue,
- Value: v.Value,
- EndDate: v.EndDate,
- ModifyTime: v.ModifyTime,
- CreateTime: v.CreateTime,
- DataList: make([]*data_manage.EdbDataList, 0),
- })
- }
- //var predictMinValue, predictMaxValue float64
- predictDataList, _, _, err, _ = GetChartPredictEdbInfoDataListByConfList(predictEdbConfDataList, startDate, sourceEdbInfoItem.LatestDate, endDateStr, edbInfo.Frequency, allDataList)
- if err != nil {
- return
- }
- dataList = append(dataList, predictDataList...)
- //if len(predictDataList) > 0 {
- // // 如果最小值 大于 预测值,那么将预测值作为最小值数据返回
- // if edbInfo.MinValue > predictMinValue {
- // edbInfo.MinValue = predictMinValue
- // }
- //
- // // 如果最大值 小于 预测值,那么将预测值作为最大值数据返回
- // if edbInfo.MaxValue < predictMaxValue {
- // edbInfo.MaxValue = predictMaxValue
- // }
- //}
- return
- }
- // GetChartDataList 通过完整的预测数据 进行 季节性图、公历、农历处理
- 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)
- }
- // 曲线图
- if chartType == 1 {
- resultDataList = dataList
- return
- }
- //实际数据的截止日期
- 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 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.Year = calendarPreYear
- 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
- }
- // GetPredictCalculateDataListByPredictEdbInfo 根据预测运算指标信息获取预测指标的数据
- 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.EdbInfoId, startDate, endDate)
- return
- }
- // GetCalculateByRuleByNineParams 获取预测规则9的计算参数
- func GetCalculateByRuleByNineParams(req request.RuleConfig) (formula string, edbInfoList []*data_manage.EdbInfo, edbInfoIdBytes []string, err error, errMsg string) {
- formula = req.Value
- formula = strings.Replace(formula, "(", "(", -1)
- formula = strings.Replace(formula, ")", ")", -1)
- formula = strings.Replace(formula, ",", ",", -1)
- formula = strings.Replace(formula, "。", ".", -1)
- formula = strings.Replace(formula, "%", "*0.01", -1)
- //检验公式
- var checkFormulaStr string
- for _, tmpEdbInfoId := range req.EdbInfoIdArr {
- checkFormulaStr += tmpEdbInfoId.FromTag + ","
- edbInfoIdBytes = append(edbInfoIdBytes, tmpEdbInfoId.FromTag)
- }
- formulaMap := CheckFormula(formula)
- for _, tmpFormula := range formulaMap {
- if !strings.Contains(checkFormulaStr, tmpFormula) {
- errMsg = "公式错误,请重新填写"
- return
- }
- }
- //关联的指标信息
- edbInfoList = make([]*data_manage.EdbInfo, 0)
- for _, tmpEdbInfoId := range req.EdbInfoIdArr {
- fromEdbInfo, tmpErr := data_manage.GetEdbInfoById(tmpEdbInfoId.EdbInfoId)
- if tmpErr != nil {
- if tmpErr.Error() == utils.ErrNoRow() {
- err = errors.New("指标 " + strconv.Itoa(tmpEdbInfoId.EdbInfoId) + " 不存在")
- } else {
- err = errors.New("获取指标失败:Err:" + tmpErr.Error())
- }
- errMsg = "数据计算失败"
- return
- }
- edbInfoList = append(edbInfoList, fromEdbInfo)
- }
- ok, _ := CheckFormula2(edbInfoList, formulaMap, formula, edbInfoIdBytes)
- if !ok {
- errMsg = "生成计算指标失败,请使用正确的计算公式"
- err = errors.New(errMsg)
- }
- return
- }
- // CalculateByRuleByNine 动态环差规则计算入库
- func CalculateByRuleByNine(formulaStr string, edbInfoList []*data_manage.EdbInfo, edbInfoIdBytes []string) (dataList []*data_manage.EdbDataList, err error) {
- realSaveDataMap := make(map[string]map[int]float64)
- saveDataMap := make(map[string]map[int]float64)
- dateList := make([]string, 0) //日期
- formulaStr = strings.ToUpper(formulaStr)
- // 获取关联指标数据
- for edbInfoIndex, v := range edbInfoList {
- sourceDataList, _, _, tmpErr, _ := GetPredictDataListByPredictEdbInfo(v, "", "", false)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- dataMap := make(map[string]float64)
- for _, dv := range sourceDataList {
- // 实际数据
- 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)
- }
- }
- item := new(CalculateItems)
- item.EdbInfoId = v.EdbInfoId
- item.DataMap = dataMap
- }
- //数据处理,将日期内不全的数据做补全
- handleDateSaveDataMap(dateList, realSaveDataMap, saveDataMap, edbInfoList)
- // 添加数据
- dataList = make([]*data_manage.EdbDataList, 0)
- // 计算规则
- formulaMap := CheckFormula(formulaStr)
- existDataMap := make(map[string]string)
- for k, date := range dateList {
- sv := saveDataMap[date]
- //fmt.Println(date, sv)
- formulaFormStr := ReplaceFormula(edbInfoList, sv, formulaMap, formulaStr, edbInfoIdBytes)
- if formulaFormStr == `` {
- //计算公式异常,那么就移除该指标
- continue
- }
- //fmt.Println(fmt.Sprintf("formulaFormStr:%s", formulaFormStr))
- expression := formula.NewExpression(formulaFormStr)
- calResult, tmpErr := expression.Evaluate()
- if tmpErr != nil {
- // 分母为0的报错
- if strings.Contains(tmpErr.Error(), "divide by zero") {
- continue
- }
- err = errors.New("计算失败:Err:" + tmpErr.Error() + ";formulaStr:" + formulaFormStr)
- return
- }
- calVal, tmpErr := calResult.Float64()
- if tmpErr != nil {
- err = errors.New("计算失败:获取计算值失败 Err:" + tmpErr.Error() + ";formulaStr:" + formulaFormStr)
- fmt.Println(err)
- return
- }
- saveValue, _ := decimal.NewFromFloat(calVal).RoundCeil(4).Float64() //utils.SubFloatToString(calVal, 4)
- dataTime, _ := time.Parse(utils.FormatDate, date)
- timestamp := dataTime.UnixNano() / 1e6
- if _, existOk := existDataMap[date]; !existOk {
- tmpPredictEdbRuleData := &data_manage.EdbDataList{
- EdbDataId: k,
- EdbInfoId: 0,
- DataTime: date,
- DataTimestamp: timestamp,
- Value: saveValue,
- }
- dataList = append(dataList, tmpPredictEdbRuleData)
- }
- existDataMap[date] = date
- }
- return
- }
|