123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395 |
- package controllers
- import (
- "encoding/json"
- "eta/eta_index_lib/logic"
- "eta/eta_index_lib/models"
- "eta/eta_index_lib/services"
- "eta/eta_index_lib/utils"
- "fmt"
- "strconv"
- "strings"
- "time"
- )
- // PredictCalculateController 预测计算指标
- type PredictCalculateController struct {
- BaseAuthController
- }
- // Save
- // @Title 新增/编辑预测指标运算接口
- // @Description 新增预测指标运算接口
- // @Success 200 {object} models.EdbInfoCalculateSaveReq
- // @router /save [post]
- func (this *PredictCalculateController) Save() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req models.EdbInfoCalculateSaveReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.EdbInfoId <= 0 {
- addPredictCalculate(br, req, this.Lang)
- } else {
- editPredictCalculate(br, req, this.Lang)
- }
- }
- // AddPredictCalculate 添加预测指标运算
- func addPredictCalculate(br *models.BaseResponse, req models.EdbInfoCalculateSaveReq, lang string) {
- req.EdbName = strings.Trim(req.EdbName, " ")
- if req.EdbName == "" {
- br.Msg = "指标名称不能为空"
- return
- }
- if req.Frequency == "" {
- br.Msg = "频率不能为空"
- return
- }
- if req.Unit == "" {
- br.Msg = "单位不能为空"
- return
- }
- if req.ClassifyId <= 0 {
- br.Msg = "请选择分类"
- return
- }
- if len(req.EdbInfoIdArr) <= 0 {
- br.Msg = "请选择指标"
- return
- }
- if req.CalculateFormula == "" {
- br.Msg = "请填写指标"
- return
- }
- //加入缓存机制,避免创建同一个名称的指标 start
- redisKey := fmt.Sprint("predict_edb_info:calculate:batch:save:", utils.DATA_SOURCE_PREDICT_CALCULATE, ":", req.EdbName)
- isExist := utils.Rc.IsExist(redisKey)
- if isExist {
- br.Msg = "指标正在处理,请勿重复提交"
- return
- } else {
- //设置3分钟缓存
- utils.Rc.SetNX(redisKey, 1, time.Second*300)
- defer func() {
- _ = utils.Rc.Delete(redisKey)
- }()
- }
- calculateFormula := req.CalculateFormula
- calculateFormula = strings.Replace(calculateFormula, "(", "(", -1)
- calculateFormula = strings.Replace(calculateFormula, ")", ")", -1)
- calculateFormula = strings.Replace(calculateFormula, ",", ",", -1)
- calculateFormula = strings.Replace(calculateFormula, "。", ".", -1)
- if strings.Contains(req.CalculateFormula, "%") {
- calculateFormula = strings.Replace(calculateFormula, "%", "*0.01", -1)
- }
- req.CalculateFormula = calculateFormula
- //检验公式
- var formulaStr string
- var edbInfoIdBytes []string
- for _, v := range req.EdbInfoIdArr {
- formulaStr += v.FromTag + ","
- edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
- }
- formulaSlice, err := utils.CheckFormulaJson(req.CalculateFormula)
- if err != nil {
- br.Msg = "公式格式错误,请重新填写"
- return
- }
- for _, formula := range formulaSlice {
- formulaMap, e := utils.CheckFormula(formula)
- if e != nil {
- br.Msg = "公式错误,请重新填写"
- return
- }
- for _, v := range formulaMap {
- if !strings.Contains(formulaStr, v) {
- br.Msg = "公式错误,请重新填写"
- return
- }
- }
- }
- // 根据指标名称和指标ID校验库中是否还存在其他同名指标
- existEdbName, err := logic.CheckExistByEdbNameAndEdbInfoId(utils.PREDICT_EDB_INFO_TYPE, 0, req.EdbName, lang)
- if err != nil {
- br.Msg = "判断指标名称是否存在失败"
- br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
- return
- }
- if existEdbName {
- br.Msg = "指标名称已存在,请重新填写"
- br.ErrMsg = "指标名称已存在,请重新填写"
- return
- }
- // 关联指标信息
- edbInfoList := make([]*models.EdbInfo, 0)
- calculateMappingList := make([]*models.EdbInfoCalculateMapping, 0)
- edbInfoTag := make(map[string]int)
- for k, v := range req.EdbInfoIdArr {
- edbInfoTag[v.FromTag] = v.EdbInfoId
- fromEdbInfo, err := models.GetEdbInfoById(v.EdbInfoId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "生成计算指标失败"
- br.Msg = "指标 " + strconv.Itoa(v.EdbInfoId) + " 不存在"
- return
- }
- br.Msg = "生成计算指标失败"
- br.Msg = "获取指标失败:Err:" + err.Error()
- return
- }
- edbInfoList = append(edbInfoList, fromEdbInfo)
- //关联关系表
- {
- calculateMappingItem := &models.EdbInfoCalculateMapping{
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- Sort: k + 1,
- //EdbCode: edbCode,
- //EdbInfoId: int(edbInfoId),
- FromEdbInfoId: fromEdbInfo.EdbInfoId,
- FromEdbCode: fromEdbInfo.EdbCode,
- FromEdbName: fromEdbInfo.EdbName,
- FromSource: fromEdbInfo.Source,
- FromSourceName: fromEdbInfo.SourceName,
- FromTag: v.FromTag,
- Source: utils.DATA_SOURCE_PREDICT_CALCULATE,
- SourceName: "预测指标运算",
- }
- calculateMappingList = append(calculateMappingList, calculateMappingItem)
- }
- }
- for _, v := range formulaSlice {
- formulaMap, e := utils.CheckFormula(v)
- if e != nil {
- br.Msg = "公式错误,请重新填写"
- return
- }
- //预先计算,判断公式是否正常
- ok, _ := models.CheckFormula2(edbInfoList, formulaMap, v, edbInfoIdBytes)
- if !ok {
- br.Msg = "生成计算指标失败,请使用正确的计算公式"
- return
- }
- }
- // 指标入库
- //生成指标编码
- edbCode, err := utils.GenerateEdbCode(1, "2")
- if err != nil {
- br.Msg = "指标生成失败"
- br.ErrMsg = "指标编码生成失败,Err:" + err.Error()
- return
- }
- timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
- uniqueCode := utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
- edbInfo := &models.EdbInfo{
- EdbInfoType: 1,
- Source: utils.DATA_SOURCE_PREDICT_CALCULATE,
- SourceName: "预测指标运算",
- EdbCode: edbCode,
- EdbName: req.EdbName,
- EdbNameSource: req.EdbName,
- Frequency: req.Frequency,
- Unit: req.Unit,
- ClassifyId: req.ClassifyId,
- SysUserId: req.AdminId,
- SysUserRealName: req.AdminName,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- UniqueCode: uniqueCode,
- CalculateFormula: req.CalculateFormula,
- EdbType: 2,
- EmptyType: req.EmptyType,
- MaxEmptyType: req.MaxEmptyType,
- Extra: req.Extra,
- EdbNameEn: req.EdbName,
- UnitEn: req.Unit,
- Sort: models.GetAddEdbMaxSortByClassifyId(req.ClassifyId, utils.PREDICT_EDB_INFO_TYPE),
- }
- edbInfoId, err := models.AddEdbInfo(edbInfo)
- if err != nil {
- br.Msg = "生成计算指标失败"
- br.Msg = "生成计算指标失败,AddEdbInfo Err:" + err.Error()
- return
- }
- edbInfo.EdbInfoId = int(edbInfoId)
- //处理同名指标
- {
- edbNameList, err := models.GetEdbInfoByName(req.EdbName)
- if err != nil {
- br.Msg = "保存失败"
- br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
- return
- }
- if len(edbNameList) >= 2 {
- for _, v := range edbNameList {
- edbName := v.EdbName + "(" + v.SourceName + ")"
- err = models.ModifyEdbInfoNameSource(edbName, v.EdbInfoId)
- if err != nil {
- br.Msg = "保存失败"
- br.ErrMsg = "修改指标名称失败,Err:" + err.Error()
- return
- }
- }
- }
- }
- //关联关系表入库
- {
- if len(calculateMappingList) > 0 {
- for k, v := range calculateMappingList {
- v.EdbCode = edbInfo.EdbCode
- v.EdbInfoId = edbInfo.EdbInfoId
- calculateMappingList[k] = v
- }
- go models.AddEdbInfoCalculateMappingMulti(calculateMappingList)
- }
- }
- // 开始添加预测指标
- latestDateStr, latestValue, err := models.AddPredictCalculateData(edbInfoList, edbInfoTag, edbInfo, edbCode, req.CalculateFormula, edbInfoIdBytes)
- if err != nil {
- br.Msg = "生成计算指标失败"
- br.Msg = "生成计算指标失败,Calculate Err:" + err.Error()
- return
- }
- // 更新指标最大最小值
- err, errMsg := models.UnifiedModifyPredictEdbInfoMaxAndMinInfo(edbInfo, latestDateStr, latestValue)
- if err != nil {
- br.Msg = errMsg
- br.ErrMsg = err.Error()
- return
- }
- resp := models.AddEdbInfoResp{
- EdbInfoId: int(edbInfoId),
- UniqueCode: uniqueCode,
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "保存成功"
- br.Data = resp
- br.IsAddLog = true
- }
- // editPredictCalculate 编辑预测指标运算
- func editPredictCalculate(br *models.BaseResponse, req models.EdbInfoCalculateSaveReq, lang string) {
- req.EdbName = strings.Trim(req.EdbName, " ")
- if req.EdbName == "" {
- br.Msg = "指标名称不能为空"
- return
- }
- if req.Frequency == "" {
- br.Msg = "频率不能为空"
- return
- }
- if req.Unit == "" {
- br.Msg = "单位不能为空"
- return
- }
- if req.ClassifyId <= 0 {
- br.Msg = "请选择分类"
- return
- }
- if len(req.EdbInfoIdArr) <= 0 {
- br.Msg = "请选择指标"
- return
- }
- if req.CalculateFormula == "" {
- br.Msg = "请填写指标"
- return
- }
- //加入缓存机制,避免创建同一个名称的指标 start
- redisKey := fmt.Sprint("predict_edb_info:calculate:batch:save:", utils.DATA_SOURCE_PREDICT_CALCULATE, ":", req.EdbName)
- isExist := utils.Rc.IsExist(redisKey)
- if isExist {
- br.Msg = "指标正在处理,请勿重复提交"
- // return
- } else {
- //设置3分钟缓存
- utils.Rc.SetNX(redisKey, 1, time.Second*300)
- defer func() {
- _ = utils.Rc.Delete(redisKey)
- }()
- }
- calculateFormula := req.CalculateFormula
- calculateFormula = strings.Replace(calculateFormula, "(", "(", -1)
- calculateFormula = strings.Replace(calculateFormula, ")", ")", -1)
- calculateFormula = strings.Replace(calculateFormula, ",", ",", -1)
- calculateFormula = strings.Replace(calculateFormula, "。", ".", -1)
- if strings.Contains(req.CalculateFormula, "%") {
- calculateFormula = strings.Replace(calculateFormula, "%", "*0.01", -1)
- }
- req.CalculateFormula = calculateFormula
- //检验公式
- var formulaStr string
- var edbInfoIdBytes []string
- for _, v := range req.EdbInfoIdArr {
- formulaStr += v.FromTag + ","
- edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
- }
- formulaSlice, err := utils.CheckFormulaJson(req.CalculateFormula)
- if err != nil {
- br.Msg = "公式格式错误,请重新填写"
- return
- }
- for _, formula := range formulaSlice {
- formulaMap, e := utils.CheckFormula(formula)
- if e != nil {
- br.Msg = "公式错误,请重新填写"
- return
- }
- for _, v := range formulaMap {
- if !strings.Contains(formulaStr, v) {
- br.Msg = "公式错误,请重新填写"
- return
- }
- }
- }
- // 获取计算指标信息
- edbInfo, err := models.GetEdbInfoById(req.EdbInfoId)
- if err != nil {
- br.Msg = "获取指标信息失败"
- br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
- return
- }
- // 记录旧的指标信息
- oldEdbName := edbInfo.EdbName
- oldFrequency := edbInfo.Frequency
- oldUnit := edbInfo.Unit
- // 根据指标名称和指标ID校验库中是否还存在其他同名指标
- existEdbName, err := logic.CheckExistByEdbNameAndEdbInfoId(edbInfo.EdbInfoType, edbInfo.EdbInfoId, req.EdbName, lang)
- if err != nil {
- br.Msg = "判断指标名称是否存在失败"
- br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
- return
- }
- if existEdbName {
- br.Msg = "指标名称已存在,请重新填写"
- br.ErrMsg = "指标名称已存在,请重新填写"
- return
- }
- // 关联指标信息
- edbInfoList := make([]*models.EdbInfo, 0)
- calculateMappingList := make([]*models.EdbInfoCalculateMapping, 0)
- edbInfoTag := make(map[string]int)
- for k, v := range req.EdbInfoIdArr {
- edbInfoTag[v.FromTag] = v.EdbInfoId
- fromEdbInfo, err := models.GetEdbInfoById(v.EdbInfoId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "生成计算指标失败"
- br.Msg = "指标 " + strconv.Itoa(v.EdbInfoId) + " 不存在"
- return
- }
- br.Msg = "生成计算指标失败"
- br.Msg = "获取指标失败:Err:" + err.Error()
- return
- }
- edbInfoList = append(edbInfoList, fromEdbInfo)
- //关联关系表
- {
- calculateMappingItem := &models.EdbInfoCalculateMapping{
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- Sort: k + 1,
- EdbCode: edbInfo.EdbCode,
- EdbInfoId: edbInfo.EdbInfoId,
- FromEdbInfoId: fromEdbInfo.EdbInfoId,
- FromEdbCode: fromEdbInfo.EdbCode,
- FromEdbName: fromEdbInfo.EdbName,
- FromSource: fromEdbInfo.Source,
- FromSourceName: fromEdbInfo.SourceName,
- FromTag: v.FromTag,
- Source: utils.DATA_SOURCE_PREDICT_CALCULATE,
- SourceName: "预测指标运算",
- }
- calculateMappingList = append(calculateMappingList, calculateMappingItem)
- }
- }
- for _, v := range formulaSlice {
- formulaMap, e := utils.CheckFormula(v)
- if e != nil {
- br.Msg = "公式错误,请重新填写"
- return
- }
- //预先计算,判断公式是否正常
- ok, _ := models.CheckFormula2(edbInfoList, formulaMap, v, edbInfoIdBytes)
- if !ok {
- br.Msg = "生成计算指标失败,请使用正确的计算公式"
- return
- }
- }
- //处理同名指标
- {
- edbNameList, err := models.GetEdbInfoByName(req.EdbName)
- if err != nil {
- br.Msg = "保存失败"
- br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
- return
- }
- if len(edbNameList) >= 2 {
- for _, v := range edbNameList {
- edbName := v.EdbName + "(" + v.SourceName + ")"
- err = models.ModifyEdbInfoNameSource(edbName, v.EdbInfoId)
- if err != nil {
- br.Msg = "保存失败"
- br.ErrMsg = "修改指标名称失败,Err:" + err.Error()
- return
- }
- }
- }
- }
- switch lang {
- case utils.EnLangVersion:
- edbInfo.EdbNameEn = req.EdbName
- edbInfo.UnitEn = req.Unit
- default:
- edbInfo.EdbName = req.EdbName
- edbInfo.Unit = req.Unit
- }
- edbInfo.Frequency = req.Frequency
- edbInfo.ClassifyId = req.ClassifyId
- edbInfo.CalculateFormula = req.CalculateFormula
- edbInfo.EmptyType = req.EmptyType
- edbInfo.MaxEmptyType = req.MaxEmptyType
- edbInfo.Extra = req.Extra
- err = models.EditPredictCalculate(edbInfo, calculateMappingList)
- if err != nil {
- br.Msg = "修改计算指标失败"
- br.Msg = "修改计算指标失败,Calculate Err:" + err.Error()
- return
- }
- // 开始添加预测指标
- latestDateStr, latestValue, err := models.AddPredictCalculateData(edbInfoList, edbInfoTag, edbInfo, edbInfo.EdbCode, req.CalculateFormula, edbInfoIdBytes)
- if err != nil {
- br.Msg = "生成计算指标失败"
- br.Msg = "生成计算指标失败,Calculate Err:" + err.Error()
- return
- }
- // 更新指标最大最小值
- err, errMsg := models.UnifiedModifyPredictEdbInfoMaxAndMinInfo(edbInfo, latestDateStr, latestValue)
- if err != nil {
- br.Msg = errMsg
- br.ErrMsg = err.Error()
- return
- }
- // 记录基础信息变更日志
- oldEdbInfo := new(models.EdbInfo)
- oldEdbInfo.EdbInfoId = req.EdbInfoId
- oldEdbInfo.EdbName = oldEdbName
- oldEdbInfo.Frequency = oldFrequency
- oldEdbInfo.Unit = oldUnit
- newEdbInfoEditRecord := new(models.EdbInfoEditRecord)
- newEdbInfoEditRecord.EdbName = req.EdbName
- newEdbInfoEditRecord.Frequency = req.Frequency
- newEdbInfoEditRecord.Unit = req.Unit
- newEdbInfoEditRecord.OperateUserId = req.AdminId
- newEdbInfoEditRecord.OperateUserRealName = req.AdminName
- err = services.AddEditEdbInfoRcord(oldEdbInfo, newEdbInfoEditRecord)
- if err != nil {
- br.Msg = "记录基础信息变更日志失败"
- br.ErrMsg = err.Error()
- return
- }
- resp := models.AddEdbInfoResp{
- EdbInfoId: edbInfo.EdbInfoId,
- UniqueCode: edbInfo.UniqueCode,
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "保存成功"
- br.Data = resp
- br.IsAddLog = true
- }
- // CalculateBatchSave
- // @Title 累计值转月-同比值-同差等计算新增
- // @Description 累计值转月-同比值-同差等计算新增接口
- // @Param request body models.EdbInfoCalculateBatchSaveReq true "type json string"
- // @Success Ret=200 返回指标id
- // @router /batch/save [post]
- func (this *PredictCalculateController) CalculateBatchSave() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req models.EdbInfoCalculateBatchSaveReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- req.EdbName = strings.Trim(req.EdbName, " ")
- if req.EdbName == "" {
- br.Msg = "指标名称不能为空"
- return
- }
- if req.Frequency == "" {
- br.Msg = "频率不能为空"
- return
- }
- if req.Unit == "" {
- br.Msg = "单位不能为空"
- return
- }
- if req.ClassifyId <= 0 {
- br.Msg = "请选择分类"
- return
- }
- // 基础指标id
- fromEdbInfoId := req.FromEdbInfoId
- notNeedFromEdbSourceList := []int{utils.DATA_SOURCE_PREDICT_CALCULATE_KSZS, utils.DATA_SOURCE_PREDICT_CALCULATE_NHCC} // 不需要传入来源指标id的 指标类型
- if fromEdbInfoId <= 0 && !utils.InArrayByInt(notNeedFromEdbSourceList, req.Source) {
- br.Msg = "请选择指标"
- return
- }
- //加入缓存机制,避免创建同一个名称的指标 start
- redisKey := fmt.Sprint("edb_lib:predict_calculate:batch:save:", req.Source, ":", req.EdbName)
- isExist := utils.Rc.IsExist(redisKey)
- if isExist {
- br.Msg = "指标正在处理,请勿重复提交"
- return
- } else {
- //设置3分钟缓存
- utils.Rc.SetNX(redisKey, 1, time.Second*300)
- defer func() {
- _ = utils.Rc.Delete(redisKey)
- }()
- }
- //加入缓存机制,避免创建同一个名称的指标 end
- // 根据指标名称和指标ID校验库中是否还存在其他同名指标
- existEdbName, err := logic.CheckExistByEdbNameAndEdbInfoId(utils.PREDICT_EDB_INFO_TYPE, req.EdbInfoId, req.EdbName, this.Lang)
- if err != nil {
- br.Msg = "判断指标名称是否存在失败"
- br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
- return
- }
- if existEdbName {
- br.Msg = "指标名称已存在,请重新填写"
- br.ErrMsg = "指标名称已存在,请重新填写"
- return
- }
- //生成指标编码
- edbCode, err := utils.GenerateEdbCode(1, "2")
- if err != nil {
- br.Msg = "指标生成失败"
- br.ErrMsg = "指标编码生成失败,Err:" + err.Error()
- return
- }
- timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
- uniqueCode := utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
- adminId := req.AdminId
- adminName := req.AdminName
- var sourName string
- var errMsg string
- var edbInfo *models.EdbInfo
- var latestDateStr string // 最近实际数据的日期
- var latestValue float64 // 最近实际数据的值
- var formulaInt int
- var nhccDate models.NhccDate // 拟合残差的日期
- switch req.Source {
- case utils.DATA_SOURCE_PREDICT_CALCULATE_NSZYDPJJS, utils.DATA_SOURCE_PREDICT_CALCULATE_HBZ, utils.DATA_SOURCE_PREDICT_CALCULATE_HCZ, utils.DATA_SOURCE_PREDICT_CALCULATE_TIME_SHIFT, utils.DATA_SOURCE_PREDICT_CALCULATE_CJJX:
- if req.Formula == "" {
- br.Msg = "请填写N值"
- return
- }
- formulaInt, _ = strconv.Atoi(req.Formula)
- if formulaInt <= 0 {
- br.Msg = "N值输入错误,请重新输入"
- return
- }
- /*case utils.DATA_SOURCE_PREDICT_CALCULATE_ZJPJ:
- //直接拼接指标
- //校验时间格式
- _, err = time.ParseInLocation(utils.FormatDate, req.Formula, time.Local)
- if err != nil {
- br.Msg = "拼接日期有误,请重新输入"
- return
- }*/
- case utils.DATA_SOURCE_PREDICT_CALCULATE_NHCC: //拟合残差指标
- //指标校验
- if len(req.EdbInfoIdArr) != 2 {
- br.Msg = "选择的指标异常,请重新选择"
- return
- }
- fromEdbInfoId = req.EdbInfoIdArr[0].EdbInfoId
- //校验时间格式
- //数据格式:2022-11-01,2022-11-10
- timeList := strings.Split(req.Formula, ",")
- if len(timeList) != 2 {
- br.Msg = "选择时间有误,请重新输入"
- return
- }
- startDate, err := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
- if err != nil {
- br.Msg = "开始日期有误,请重新输入"
- return
- }
- endDate, err := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
- if err != nil {
- br.Msg = "结束日期有误,请重新输入"
- return
- }
- if utils.GetTimeSubDay(startDate, endDate) < 2 {
- br.Msg = "日期间隔不得少于两天"
- return
- }
- nhccDate.StartDate = startDate
- nhccDate.EndDate = endDate
- case utils.DATA_SOURCE_CALCULATE_ZSXY: // 指数修匀
- if req.Formula == "" {
- br.Msg = "请填写alpha值"
- return
- }
- a, e := strconv.ParseFloat(req.Formula, 64)
- if e != nil {
- br.Msg = "alpha值输入错误, 请重新输入"
- return
- }
- if a <= 0 || a >= 1 {
- br.Msg = "alpha值输入错误, 请重新输入"
- return
- }
- }
- if err != nil {
- br.Msg = "获取指标信息失败"
- br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
- return
- }
- // 来源预测指标信息
- var fromEdbInfo *models.EdbInfo
- if fromEdbInfoId > 0 {
- fromEdbInfo, err = models.GetEdbInfoById(fromEdbInfoId)
- if err != nil {
- br.Msg = "获取指标信息失败"
- br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
- return
- }
- if fromEdbInfo.EdbInfoType != 1 {
- br.Msg = "来源指标不是预测指标"
- br.ErrMsg = "来源指标不是预测指标"
- return
- }
- }
- var baseEdbInfoModel models.BasePredictEdbInfoInterface
- saveParams := models.BatchSaveCalculateBatchParams{
- Req: req,
- FromEdbInfo: fromEdbInfo,
- EdbCode: edbCode,
- UniqueCode: uniqueCode,
- SysUserId: req.AdminId,
- SysUserRealName: req.AdminName,
- Lang: this.Lang,
- }
- if req.Source == utils.DATA_SOURCE_PREDICT_CALCULATE_TBZ {
- if req.EdbInfoId <= 0 {
- if req.Frequency != fromEdbInfo.Frequency {
- br.Msg = "当前频度和原指标频度不一致"
- return
- }
- if req.Unit != "无" {
- br.Msg = "单位只允许为无,禁止选择其他单位"
- return
- }
- }
- }
- if req.Source == utils.DATA_SOURCE_PREDICT_CALCULATE_TCZ {
- if req.EdbInfoId <= 0 {
- if req.Frequency != fromEdbInfo.Frequency {
- br.Msg = "当前频度和原指标频度不一致"
- return
- }
- if req.Unit != fromEdbInfo.Unit {
- br.Msg = "当前单位和原指标单位不一致"
- return
- }
- }
- sourName = "预测同差"
- edbInfo, latestDateStr, latestValue, err, errMsg = models.SavePredictCalculateTcz(req.EdbInfoId, req.ClassifyId, req.EdbName, req.Frequency, req.Unit, req.Formula, fromEdbInfo, edbCode, uniqueCode, adminId, adminName, this.Lang)
- } else if req.Source == utils.DATA_SOURCE_PREDICT_CALCULATE_NSZYDPJJS {
- sourName = "预测N数值移动平均计算"
- edbInfo, latestDateStr, latestValue, err, errMsg = models.SavePredictCalculateNszydpjjs(req.EdbInfoId, req.ClassifyId, req.EdbName, req.Frequency, req.Unit, req.Formula, fromEdbInfo, edbCode, uniqueCode, adminId, adminName, this.Lang)
- } else if req.Source == utils.DATA_SOURCE_PREDICT_CALCULATE_BP {
- sourName = "预测变频"
- edbInfo, latestDateStr, latestValue, err = models.SavePredictCalculateBp(&req, fromEdbInfo, edbCode, uniqueCode, adminId, adminName, this.Lang)
- } else if req.Source == utils.DATA_SOURCE_PREDICT_CALCULATE_HBZ {
- checkDataList, tmpErr := models.GetPredictEdbDataListAllByStartDate(fromEdbInfo, 0, "")
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- br.Msg = "判断环比值是否可计算失败"
- br.ErrMsg = "判断环比值是否可计算失败,Err:" + tmpErr.Error()
- return
- }
- for _, v := range checkDataList {
- if v.Value <= 0 {
- br.Msg = "原始数据中存在0或负数,该指标不能进行环比运算"
- br.ErrMsg = "原始数据中出现0和负值时,提示该指标不能进行环比运算"
- return
- }
- }
- sourName = "预测环比值"
- edbInfo, latestDateStr, latestValue, err = models.SavePredictCalculateHbz(&req, fromEdbInfo, edbCode, uniqueCode, adminId, adminName, formulaInt, this.Lang)
- } else if req.Source == utils.DATA_SOURCE_PREDICT_CALCULATE_HCZ {
- sourName = "预测环差值"
- edbInfo, latestDateStr, latestValue, err = models.SavePredictCalculateHcz(&req, fromEdbInfo, edbCode, uniqueCode, adminId, adminName, formulaInt, this.Lang)
- } else if req.Source == utils.DATA_SOURCE_PREDICT_CALCULATE_LJZZY {
- sourName = "预测累计值转月值"
- if fromEdbInfo.Frequency != "月度" {
- br.Msg = "请选择月度指标"
- return
- }
- edbInfo, latestDateStr, latestValue, err = models.SavePredictCalculateLjzzy(&req, fromEdbInfo, edbCode, uniqueCode, adminId, adminName, this.Lang)
- } else if req.Source == utils.DATA_SOURCE_PREDICT_CALCULATE_TIME_SHIFT {
- sourName = "预测时间移位"
- edbInfo, latestDateStr, latestValue, err = models.SavePredictCalculateTimeShift(&req, fromEdbInfo, edbCode, uniqueCode, adminId, adminName, this.Lang)
- } else if req.Source == utils.DATA_SOURCE_PREDICT_CALCULATE_PHASE_SHIFT {
- sourName = "预测期数移位"
- edbInfo, latestDateStr, latestValue, err = models.SavePredictCalculatePhaseShift(&req, fromEdbInfo, edbCode, uniqueCode, adminId, adminName, this.Lang)
- } else if req.Source == utils.DATA_SOURCE_PREDICT_CALCULATE_CJJX {
- sourName = "预测超季节性"
- edbInfo, latestDateStr, latestValue, err = models.SavePredictCalculateCjjx(&req, fromEdbInfo, edbCode, uniqueCode, adminId, adminName, formulaInt, this.Lang)
- } else if req.Source == utils.DATA_SOURCE_PREDICT_CALCULATE_NHCC {
- sourName = "预测拟合残差"
- secondEdbInfoReq := req.EdbInfoIdArr[1]
- secondEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
- if tmpErr != nil {
- br.Msg = "获取因变量的指标信息失败"
- br.ErrMsg = "获取因变量的指标信息失败:Err:" + tmpErr.Error()
- return
- }
- if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
- br.Msg = "两个指标不允许为同一个"
- br.ErrMsg = "两个指标不允许为同一个"
- return
- }
- edbInfo, latestDateStr, latestValue, err = models.SavePredictCalculateNhcc(&req, fromEdbInfo, secondEdbInfo, edbCode, uniqueCode, nhccDate, adminId, adminName, this.Lang)
- } else if req.Source == utils.DATA_SOURCE_PREDICT_CALCULATE_JP {
- sourName = utils.DATA_SOURCE_NAME_PREDICT_CALCULATE_JP
- edbInfo, latestDateStr, latestValue, err, errMsg = models.SavePredictCalculateJp(req.EdbInfoId, req.ClassifyId, req.EdbName, req.Frequency, req.Unit, req.Formula, fromEdbInfo, edbCode, uniqueCode, adminId, adminName, this.Lang)
- } else if req.Source == utils.DATA_SOURCE_PREDICT_CALCULATE_NH {
- sourName = utils.DATA_SOURCE_NAME_PREDICT_CALCULATE_NH
- edbInfo, latestDateStr, latestValue, err, errMsg = models.SavePredictCalculateNh(req.EdbInfoId, req.ClassifyId, req.EdbName, req.Frequency, req.Unit, req.Formula, fromEdbInfo, edbCode, uniqueCode, adminId, adminName, this.Lang)
- } else if req.Source == utils.DATA_SOURCE_PREDICT_CALCULATE_KSZS {
- sourName = utils.DATA_SOURCE_NAME_PREDICT_CALCULATE_KSZS
- edbInfo, latestDateStr, latestValue, err, errMsg = models.SavePredictCalculateKszs(req.EdbInfoId, req.ClassifyId, req.EdbName, req.Frequency, req.Unit, req.Formula, req.EdbInfoIdArr, edbCode, uniqueCode, adminId, adminName, this.Lang)
- } else {
- // 获取通用的数据源处理服务
- baseEdbInfoModel = models.GetBasePredictEdbInfoModel(req.Source)
- // 没有找到的话,那么就直接返回报错吧
- if baseEdbInfoModel == nil {
- br.Msg = "无效计算方式"
- br.ErrMsg = "无效计算方式,source:" + strconv.Itoa(req.Source)
- return
- }
- sourName = baseEdbInfoModel.GetSourceName()
- if req.EdbInfoId <= 0 {
- edbInfo, latestDateStr, latestValue, err, errMsg = baseEdbInfoModel.Add(saveParams)
- } else {
- edbInfo, err = models.GetEdbInfoById(req.EdbInfoId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "指标已被删除,请刷新页面"
- br.ErrMsg = "指标已被删除,请刷新页面:Err:" + err.Error()
- return
- }
- br.Msg = "获取指标信息失败"
- br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
- return
- }
- saveParams.EdbInfo = edbInfo
- latestDateStr, latestValue, err, errMsg = baseEdbInfoModel.Edit(saveParams)
- }
- }
- if err != nil {
- br.Msg = "生成" + sourName + "失败"
- if errMsg != `` {
- br.Msg = errMsg
- }
- br.Msg = "生成" + sourName + "失败 Err:" + err.Error()
- return
- }
- if edbInfo == nil {
- br.Msg = "生成" + sourName + "失败"
- br.ErrMsg = "生成" + sourName + "失败,edbInfo is nil"
- return
- }
- //处理同名指标
- {
- edbNameList, err := models.GetEdbInfoByName(req.EdbName)
- if err != nil {
- br.Msg = "保存失败"
- br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
- return
- }
- if len(edbNameList) >= 2 {
- for _, v := range edbNameList {
- edbName := v.EdbName + "(" + v.SourceName + ")"
- err = models.ModifyEdbInfoNameSource(edbName, v.EdbInfoId)
- if err != nil {
- br.Msg = "保存失败"
- br.ErrMsg = "修改指标名称失败,Err:" + err.Error()
- return
- }
- }
- }
- }
- // 更新指标最大最小值
- err, errMsg = models.UnifiedModifyPredictEdbInfoMaxAndMinInfo(edbInfo, latestDateStr, latestValue)
- if err != nil {
- br.Msg = errMsg
- br.ErrMsg = err.Error()
- return
- }
- if req.EdbInfoId > 0 {
- oldEdbInfo, err := models.GetEdbInfoById(req.EdbInfoId)
- if err != nil {
- br.Msg = "获取指标信息失败"
- br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
- return
- }
- // 记录操作变更记录
- newEdbInfo := new(models.EdbInfoEditRecord)
- newEdbInfo.EdbName = req.EdbName
- newEdbInfo.Frequency = req.Frequency
- newEdbInfo.Unit = req.Unit
- newEdbInfo.OperateUserId = req.AdminId
- newEdbInfo.OperateUserRealName = req.AdminName
- err = services.AddEditEdbInfoRcord(oldEdbInfo, newEdbInfo)
- if err != nil {
- br.Msg = "保存失败"
- br.ErrMsg = "保存失败,Err:" + err.Error()
- return
- }
- }
- resp := models.AddEdbInfoResp{
- EdbInfoId: edbInfo.EdbInfoId,
- UniqueCode: uniqueCode,
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "保存成功"
- br.Data = resp
- br.IsAddLog = true
- }
- // Refresh
- // @Title 刷新计算指标接口
- // @Description 刷新计算指标接口
- // @Success 200 {object} models.RefreshEdbInfoReq
- // @router /refresh [post]
- func (this *PredictCalculateController) Refresh() {
- br := new(models.BaseResponse).Init()
- var cacheKey string
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req models.RefreshEdbInfoReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.EdbCode == "" {
- br.Msg = "请输入指标编码!"
- br.ErrMsg = "请输入指标编码,指标编码为空"
- return
- }
- if req.EdbInfoId <= 0 {
- br.Msg = "请输入指标ID!"
- br.ErrMsg = "请输入指标ID"
- return
- }
- edbInfo, err := models.GetEdbInfoById(req.EdbInfoId)
- if err != nil {
- br.Msg = "指标不存在!"
- br.ErrMsg = "指标不存在"
- return
- }
- if edbInfo.IsStaticData == 1 {
- //静态数据直接返回
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- return
- }
- cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(edbInfo.Source) + "_" + req.EdbCode
- if utils.Rc.IsExist(cacheKey) {
- br.Ret = 501
- br.Success = true
- br.Msg = "系统处理中,请稍后重试"
- br.IsSendEmail = false
- return
- }
- utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
- defer func() {
- _ = utils.Rc.Delete(cacheKey)
- }()
- startDate := req.StartDate
- var errMsg string
- endDate := time.Now().Format(utils.FormatDate)
- edbInfoId := edbInfo.EdbInfoId
- source := edbInfo.Source
- var latestDateStr string // 最近实际数据的日期
- var latestValue float64 // 最近实际数据的值
- var baseEdbInfoModel models.BasePredictEdbInfoInterface
- refreshParams := models.RefreshParams{
- EdbInfo: edbInfo,
- }
- switch source {
- case utils.DATA_SOURCE_PREDICT_CALCULATE_TBZ: //刷新同比值
- calculateTbz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
- if err != nil {
- errMsg = "GetEdbInfoCalculateTbzDetail Err:" + err.Error()
- break
- }
- fromEdbInfo, err := models.GetEdbInfoById(calculateTbz.FromEdbInfoId)
- if err != nil {
- errMsg = "GetEdbInfoById Err:" + err.Error()
- break
- }
- refreshParams.FromEdbInfo = fromEdbInfo
- refreshParams.StartDate = edbInfo.StartDate
- }
- switch source {
- case utils.DATA_SOURCE_PREDICT_CALCULATE:
- //startDate = edbInfo.StartDate
- //sTime, err := time.ParseInLocation(utils.FormatDate, edbInfo.EndDate, time.Local)
- //if err != nil {
- // return
- //}
- //startDate = sTime.Format(utils.FormatDate)
- startDate = ""
- var edbInfoIdBytes []string
- calculateMap, err := models.GetEdbInfoCalculateDetailList(edbInfo.EdbInfoId)
- if err != nil {
- errMsg = "GetEdbInfoCalculateDetail Err:" + err.Error()
- break
- }
- var formulaStr string
- edbInfoList := make([]*models.EdbInfo, 0)
- edbInfoTag := make(map[string]int)
- for _, v := range calculateMap {
- edbInfoTag[v.FromTag] = v.FromEdbInfoId
- formulaStr += v.FromTag + ","
- edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
- edbInfo, _ := models.GetEdbInfoById(v.FromEdbInfoId)
- edbInfoList = append(edbInfoList, edbInfo)
- }
- latestDateStr, latestValue, err = models.RefreshAllPredictCalculate(edbInfoList, edbInfoTag, edbInfo.EdbInfoId, source, edbInfo.SubSource, edbInfo.EdbCode, edbInfo.CalculateFormula, startDate, edbInfoIdBytes, edbInfo.EmptyType, edbInfo.MaxEmptyType, edbInfo.Extra)
- if err != nil && err.Error() != utils.ErrNoRow() {
- errMsg = "RefreshCalculate Err:" + err.Error()
- break
- }
- case utils.DATA_SOURCE_PREDICT_CALCULATE_TCZ: //同差值
- calculateTcz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
- if err != nil {
- errMsg = "GetEdbInfoCalculateTczDetail Err:" + err.Error()
- break
- }
- fromEdbInfo, err := models.GetEdbInfoById(calculateTcz.FromEdbInfoId)
- if err != nil {
- errMsg = "GetEdbInfoById Err:" + err.Error()
- break
- }
- startDate = edbInfo.StartDate
- latestDateStr, latestValue, err = models.RefreshAllPredictCalculateTcz(edbInfoId, source, edbInfo.SubSource, fromEdbInfo, calculateTcz.EdbCode, startDate)
- if err != nil && err.Error() != utils.ErrNoRow() {
- errMsg = "RefreshCalculateTcz Err:" + err.Error()
- break
- }
- case utils.DATA_SOURCE_PREDICT_CALCULATE_NSZYDPJJS: //N数值移动平均计算
- calculateInfo, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
- if err != nil {
- errMsg = "GetEdbInfoCalculateTczDetail Err:" + err.Error()
- break
- }
- fromEdbInfo, err := models.GetEdbInfoById(calculateInfo.FromEdbInfoId)
- if err != nil {
- errMsg = "GetEdbInfoById Err:" + err.Error()
- break
- }
- var formulaInt int
- formulaInt, _ = strconv.Atoi(calculateInfo.CalculateFormula)
- if formulaInt <= 0 {
- errMsg = "N值输入错误,请重新输入"
- break
- }
- startDate = edbInfo.StartDate
- latestDateStr, latestValue, err = models.RefreshAllPredictCalculateNszydpjjs(edbInfoId, source, edbInfo.SubSource, formulaInt, fromEdbInfo, calculateInfo.EdbCode, startDate)
- if err != nil && err.Error() != utils.ErrNoRow() {
- errMsg = "RefreshAllPredictCalculateNszydpjjs Err:" + err.Error()
- break
- }
- case utils.DATA_SOURCE_PREDICT_CALCULATE_BP: //刷新变频
- calculateTbz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
- if err != nil {
- errMsg = "GetEdbInfoCalculateTbzDetail Err:" + err.Error()
- break
- }
- fromEdbInfo, err := models.GetEdbInfoById(calculateTbz.FromEdbInfoId)
- if err != nil {
- errMsg = "GetEdbInfoById Err:" + err.Error()
- break
- }
- endDate = time.Now().Format(utils.FormatDate)
- latestDateStr, latestValue, err = models.RefreshAllPredictCalculateBp(edbInfoId, source, edbInfo.SubSource, fromEdbInfo, calculateTbz.EdbCode, startDate, endDate, edbInfo.EmptyType)
- if err != nil && err.Error() != utils.ErrNoRow() {
- errMsg = "RefreshAllPredictCalculateBp Err:" + err.Error()
- break
- }
- case utils.DATA_SOURCE_PREDICT_CALCULATE_HBZ: //刷新环比值
- calculateTbz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
- if err != nil {
- errMsg = "GetEdbInfoCalculateHbzDetail Err:" + err.Error()
- break
- }
- fromEdbInfo, err := models.GetEdbInfoById(calculateTbz.FromEdbInfoId)
- if err != nil {
- errMsg = "GetEdbInfoById Err:" + err.Error()
- break
- }
- startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
- endDate = time.Now().Format(utils.FormatDate)
- formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
- latestDateStr, latestValue, err = models.RefreshAllPredictCalculateHbz(edbInfoId, source, edbInfo.SubSource, fromEdbInfo, calculateTbz.EdbCode, startDate, endDate, formulaInt)
- if err != nil && err.Error() != utils.ErrNoRow() {
- errMsg = "RefreshAllPredictCalculateHbz Err:" + err.Error()
- break
- }
- case utils.DATA_SOURCE_PREDICT_CALCULATE_HCZ: //刷新环差值
- calculateTbz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
- if err != nil {
- errMsg = "GetEdbInfoCalculateHczDetail Err:" + err.Error()
- break
- }
- fromEdbInfo, err := models.GetEdbInfoById(calculateTbz.FromEdbInfoId)
- if err != nil {
- errMsg = "GetEdbInfoById Err:" + err.Error()
- break
- }
- startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
- endDate = time.Now().Format(utils.FormatDate)
- formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
- latestDateStr, latestValue, err = models.RefreshAllPredictCalculateHcz(edbInfoId, source, edbInfo.SubSource, fromEdbInfo, calculateTbz.EdbCode, startDate, endDate, formulaInt)
- if err != nil && err.Error() != utils.ErrNoRow() {
- errMsg = "RefreshAllPredictCalculateHcz Err:" + err.Error()
- break
- }
- case utils.DATA_SOURCE_PREDICT_CALCULATE_LJZZY: //刷新累计值转月值
- calculateLjzzy, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
- if err != nil {
- errMsg = "GetEdbInfoCalculateLjzzyDetail Err:" + err.Error()
- break
- }
- fromEdbInfo, err := models.GetEdbInfoById(calculateLjzzy.FromEdbInfoId)
- if err != nil {
- errMsg = "GetEdbInfoById Err:" + err.Error()
- break
- }
- endDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
- startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
- latestDateStr, latestValue, err = models.RefreshAllPredictCalculateLjzzy(edbInfoId, source, edbInfo.SubSource, fromEdbInfo, calculateLjzzy.EdbCode, startDate, endDate)
- if err != nil && err.Error() != utils.ErrNoRow() {
- errMsg = "RefreshAllPredictCalculateLjzzy Err:" + err.Error()
- break
- }
- case utils.DATA_SOURCE_PREDICT_CALCULATE_TIME_SHIFT:
- calculate, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
- if err != nil {
- errMsg = "GetEdbInfoCalculateTbzDetail Err:" + err.Error()
- break
- }
- fromEdbInfo, err := models.GetEdbInfoById(calculate.FromEdbInfoId)
- if err != nil {
- errMsg = "GetEdbInfoById Err:" + err.Error()
- break
- }
- //startDate = edbInfo.StartDate
- startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
- endDate = time.Now().Format(utils.FormatDate)
- formulaInt, _ := strconv.Atoi(calculate.CalculateFormula)
- latestDateStr, latestValue, err = models.RefreshAllPredictCalculateTimeShift(edbInfoId, source, edbInfo.SubSource, formulaInt, calculate.MoveType, fromEdbInfo, calculate.EdbCode, startDate, endDate, calculate.MoveFrequency)
- if err != nil && err.Error() != utils.ErrNoRow() {
- errMsg = "RefreshAllCalculateTimeShift Err:" + err.Error()
- }
- case utils.DATA_SOURCE_PREDICT_CALCULATE_LJZTBPJ: //刷新累计值同比拼接
- latestDateStr, latestValue, err = models.RefreshAllPredictCalculateLjztbpj(edbInfo)
- if err != nil && err.Error() != utils.ErrNoRow() {
- errMsg = "RefreshAllPredictCalculateLjztbpj Err:" + err.Error()
- break
- }
- case utils.DATA_SOURCE_PREDICT_CALCULATE_ZJPJ: //刷新直接拼接
- latestDateStr, latestValue, err = models.RefreshAllPredictCalculateZjpj(edbInfo)
- if err != nil && err.Error() != utils.ErrNoRow() {
- errMsg = "RefreshAllPredictCalculateZjpj Err:" + err.Error()
- break
- }
- case utils.DATA_SOURCE_PREDICT_CALCULATE_CJJX: //超季节性
- calculateCjjx, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
- if err != nil {
- errMsg = "GetEdbInfoCalculateNszydpjjsDetail Err:" + err.Error()
- break
- }
- fromEdbInfo, err := models.GetEdbInfoById(calculateCjjx.FromEdbInfoId)
- if err != nil {
- errMsg = "GetEdbInfoById Err:" + err.Error()
- break
- }
- formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
- startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
- latestDateStr, latestValue, err = models.RefreshAllPredictCalculateCjjx(edbInfoId, edbInfo.Source, edbInfo.SubSource, fromEdbInfo, calculateCjjx.EdbCode, startDate, "", edbInfo.Calendar, formulaInt)
- if err != nil && err.Error() != utils.ErrNoRow() {
- errMsg = "RefreshAllPredictCalculateCjjx Err:" + err.Error()
- break
- }
- case utils.DATA_SOURCE_PREDICT_CALCULATE_NHCC: //nhcc
- latestDateStr, latestValue, err = models.RefreshAllPredictCalculateNhcc(edbInfo)
- if err != nil && err.Error() != utils.ErrNoRow() {
- errMsg = "RefreshAllPredictCalculateNhcc Err:" + err.Error()
- break
- }
- case utils.DATA_SOURCE_PREDICT_CALCULATE_JP: //降频
- calculateInfo, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
- if err != nil {
- errMsg = "GetEdbInfoCalculateTczDetail Err:" + err.Error()
- break
- }
- fromEdbInfo, err := models.GetEdbInfoById(calculateInfo.FromEdbInfoId)
- if err != nil {
- errMsg = "GetEdbInfoById Err:" + err.Error()
- break
- }
- latestDateStr, latestValue, err = models.RefreshAllPredictCalculateJp(edbInfoId, source, edbInfo.SubSource, fromEdbInfo, calculateInfo.EdbCode, edbInfo.Frequency, edbInfo.CalculateFormula)
- if err != nil && err.Error() != utils.ErrNoRow() {
- errMsg = "RefreshAllPredictCalculateJp Err:" + err.Error()
- break
- }
- case utils.DATA_SOURCE_PREDICT_CALCULATE_NH: //年化
- calculateInfo, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
- if err != nil {
- errMsg = "GetEdbInfoCalculateTczDetail Err:" + err.Error()
- break
- }
- fromEdbInfo, err := models.GetEdbInfoById(calculateInfo.FromEdbInfoId)
- if err != nil {
- errMsg = "GetEdbInfoById Err:" + err.Error()
- break
- }
- latestDateStr, latestValue, err = models.RefreshAllPredictCalculateNh(edbInfoId, source, edbInfo.SubSource, fromEdbInfo, calculateInfo.EdbCode, edbInfo.Frequency, edbInfo.CalculateFormula)
- if err != nil && err.Error() != utils.ErrNoRow() {
- errMsg = "RefreshAllPredictCalculateNh Err:" + err.Error()
- break
- }
- case utils.DATA_SOURCE_PREDICT_CALCULATE_KSZS: //扩散指数
- latestDateStr, latestValue, err = models.RefreshAllPredictCalculateKszs(edbInfo)
- if err != nil && err.Error() != utils.ErrNoRow() {
- errMsg = "RefreshAllPredictCalculateKszs Err:" + err.Error()
- break
- }
- default:
- // 获取通用的数据源处理服务
- baseEdbInfoModel = models.GetBasePredictEdbInfoModel(source)
- // 没有找到的话,那么就直接返回报错吧
- if baseEdbInfoModel == nil {
- br.Msg = "来源异常,请联系相关开发!"
- br.ErrMsg = "来源异常,请联系相关开发"
- return
- }
- latestDateStr, latestValue, err, errMsg = baseEdbInfoModel.Refresh(refreshParams)
- }
- if errMsg != `` {
- br.Msg = "刷新指标失败!"
- br.ErrMsg = "刷新指标失败,err:" + errMsg
- return
- }
- // 更新指标最大最小值
- err, errMsg = models.UnifiedModifyPredictEdbInfoMaxAndMinInfo(edbInfo, latestDateStr, latestValue)
- if err != nil {
- br.Msg = errMsg
- br.ErrMsg = err.Error()
- return
- }
- // 更新ES
- go logic.UpdateEs(edbInfo.EdbInfoId)
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- }
- // CalculateComputeCorrelation
- // @Title 拟合残差计算相关性接口
- // @Description 拟合残差计算相关性接口
- // @Param request body models.EdbInfoCalculateBatchSaveReq true "type json string"
- // @Success Ret=200 返回指标id
- // @router /compute_correlation [post]
- func (this *PredictCalculateController) CalculateComputeCorrelation() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req models.EdbInfoCalculateBatchSaveReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if len(req.EdbInfoIdArr) != 2 {
- br.Msg = "选择的指标异常,请重新选择"
- return
- }
- //校验时间格式
- //数据格式:2022-11-01,2022-11-10
- timeList := strings.Split(req.Formula, ",")
- if len(timeList) != 2 {
- br.Msg = "选择时间有误,请重新输入"
- return
- }
- startDate, err := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
- if err != nil {
- br.Msg = "开始日期有误,请重新输入"
- return
- }
- endDate, err := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
- if err != nil {
- br.Msg = "结束日期有误,请重新输入"
- return
- }
- if utils.GetTimeSubDay(startDate, endDate) < 2 {
- br.Msg = "日期间隔不得少于两天"
- return
- }
- var nhccDate models.NhccDate // 拟合残差的日期
- nhccDate.StartDate = startDate
- nhccDate.EndDate = endDate
- // 来源预测指标信息
- fromEdbInfoId := req.EdbInfoIdArr[0].EdbInfoId
- if fromEdbInfoId <= 0 {
- br.Msg = "请选择指标"
- return
- }
- // 来源指标(自变量指标)
- fromEdbInfo, err := models.GetEdbInfoById(fromEdbInfoId)
- if err != nil {
- br.Msg = "获取指标信息失败"
- br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
- return
- }
- if fromEdbInfo.EdbInfoType != 1 {
- br.Msg = "来源指标不是预测指标"
- br.ErrMsg = "来源指标不是预测指标"
- return
- }
- // 因变量指标
- secondEdbInfoReq := req.EdbInfoIdArr[1]
- secondEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
- if tmpErr != nil {
- br.Msg = "获取因变量的指标信息失败"
- br.ErrMsg = "获取因变量的指标信息失败:Err:" + tmpErr.Error()
- return
- }
- if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
- br.Msg = "两个指标不允许为同一个"
- br.ErrMsg = "两个指标不允许为同一个"
- return
- }
- rVal, err := models.CalculatePredictComputeCorrelation(&req, fromEdbInfo, secondEdbInfo, nhccDate)
- if err != nil {
- br.Msg = "计算失败"
- br.ErrMsg = "计算失败,ERR:" + err.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "计算成功"
- br.Data = rVal
- br.IsAddLog = true
- }
|