12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400 |
- 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.AddEditEdbInfoRecord(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 oldEdbInfo *models.EdbInfo
- if req.EdbInfoId > 0 {
- oldEdbInfo, err = models.GetEdbInfoById(req.EdbInfoId)
- 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 {
- // 记录操作变更记录
- 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.AddEditEdbInfoRecord(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
- }
|