// @Author gmy 2024/8/13 16:01:00 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" "time" ) type BaseFromRzdController struct { BaseAuthController } // Add // @Title 新增睿姿得指标 // @Description 新增粮油商务网指标 // @router /add [post] func (this *BaseFromRzdController) Add() { br := new(models.BaseResponse).Init() var cacheKey string defer func() { utils.Rc.Delete(cacheKey) this.Data["json"] = br this.ServeJSON() }() source := utils.DATA_SOURCE_RZD var req models.AddEdbInfoReq 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 } cacheKey = utils.CACHE_EDB_DATA_ADD + strconv.Itoa(source) + "_" + req.EdbCode if !utils.Rc.IsExist(cacheKey) { utils.Rc.SetNX(cacheKey, 1, 1*time.Minute) err = models.AddEdbDataFromRzd(req.EdbCode) if err != nil { br.Msg = "获取指标信息失败!" br.ErrMsg = "获取指标信息失败 AddEdbDataFromSci99,Err:" + err.Error() return } br.Ret = 200 br.Success = true br.Msg = "获取成功" } else { br.Ret = 501 br.Success = true br.Msg = "系统处理中,请稍后重试" } } // Refresh // @Title 刷新粮油商务网指标接口 // @Description 刷新粮油商务网指标接口 // @Success 200 {object} models.RefreshEdbInfoReq // @router /refresh [post] func (this *BaseFromRzdController) Refresh() { br := new(models.BaseResponse).Init() var cacheKey string defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } _ = utils.Rc.Delete(cacheKey) this.Data["json"] = br this.ServeJSON() }() source := utils.DATA_SOURCE_RZD 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.GetEdbInfoByEdbCode(source, req.EdbCode) if err != nil { br.Msg = "指标不存在!" br.ErrMsg = "指标不存在" return } cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(source) + "_" + req.EdbCode if utils.Rc.IsExist(cacheKey) { br.Ret = 501 br.Success = true br.Msg = "系统处理中,请稍后重试" return } dataUpdateTime := time.Now().Format(utils.FormatDateTime) utils.Rc.SetNX(cacheKey, 1, 1*time.Minute) err = models.RefreshEdbDataFromRzd(req.EdbInfoId, req.EdbCode, req.StartDate) if err != nil && err.Error() != utils.ErrNoRow() { br.Msg = "刷新指标信息失败!" br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromBloomberg,Err:" + err.Error() return } // 更新指标最大最小值 erDataUpdateDate, err, errMsg := models.UnifiedModifyEdbInfoMaxAndMinInfoDataUpdate(edbInfo, dataUpdateTime) if err != nil { br.Msg = errMsg br.ErrMsg = err.Error() return } // 添加指标刷新成功日志 if erDataUpdateDate != "" { _ = services.AddEdbInfoUpdateLog(edbInfo.EdbInfoId, 1, "", 1, "", 0, 0) } else { _ = services.AddEdbInfoUpdateLog(edbInfo.EdbInfoId, 1, "", 2, "未刷新到数据", 0, 0) } // 更新ES go logic.UpdateEs(edbInfo.EdbInfoId) br.Ret = 200 br.Success = true br.Msg = "获取成功" } // AddRzdClassify // @Title 新增分类 // @Description 获取分类 // @Success 200 {object} models.BaseFromRzdClassify // @router /add/rzd/classify [post] func (this *BaseFromRzdController) AddRzdClassify() { br := new(models.BaseResponse).Init() var cacheKey string defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } _ = utils.Rc.Delete(cacheKey) this.Data["json"] = br this.ServeJSON() }() var reqData struct { ClassifyName string `json:"ClassifyName"` ParentId int `json:"parentId"` } err := json.Unmarshal(this.Ctx.Input.RequestBody, &reqData) if err != nil { br.ErrMsg = "无法解析请求体" return } categoryName := reqData.ClassifyName if categoryName == "" { br.Msg = "请输入分类!" br.ErrMsg = "请输入分类" return } rzdClassify := models.BaseFromRzdClassify{ ClassifyName: categoryName, ParentId: reqData.ParentId, CreateTime: utils.GetCurrentTime(), ModifyTime: utils.GetCurrentTime(), } lyClassify, err := models.AddRzdClassify(&rzdClassify) if err != nil { return } br.Ret = 200 br.Success = true br.Data = lyClassify br.Msg = "获取成功" } type RzdClassify struct { ClassifyName string `json:"ClassifyName"` ParentId int `json:"ParentId"` Sort int `json:"Sort"` ClassifyNameEn string `json:"ClassifyNameEn"` } type BatchAddRzdClassifyReq struct { List []RzdClassify `json:"List"` } // BatchAddRzdClassify // @Title 新增分类 // @Description 获取分类 // @Success 200 {object} models.BaseFromRzdClassify // @router /add/rzd/batch_add_classify [post] func (this *BaseFromRzdController) BatchAddRzdClassify() { br := new(models.BaseResponse).Init() var cacheKey string defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } _ = utils.Rc.Delete(cacheKey) this.Data["json"] = br this.ServeJSON() }() var reqData BatchAddRzdClassifyReq err := json.Unmarshal(this.Ctx.Input.RequestBody, &reqData) if err != nil { br.ErrMsg = "无法解析请求体" return } var categoryList []*models.BaseFromRzdClassify for _, classify := range reqData.List { if classify.ClassifyName == "" { br.Msg = "请输入分类!" br.ErrMsg = "请输入分类" return } categoryList = append(categoryList, &models.BaseFromRzdClassify{ ClassifyName: classify.ClassifyName, ParentId: classify.ParentId, Sort: classify.Sort, CreateTime: utils.GetCurrentTime(), ModifyTime: utils.GetCurrentTime(), ClassifyNameEn: classify.ClassifyNameEn, }) } err = models.BatchAddRzdClassify(categoryList) if err != nil { return } br.Ret = 200 br.Success = true br.Data = categoryList br.Msg = "获取成功" } // GetRzdClassifyByName // @Title 获取分类 // @Description 获取分类 // @Success 200 {object} models.BaseFromRzdClassify // @router /get/rzd/classify/by/name [post] func (this *BaseFromRzdController) GetRzdClassifyByName() { br := new(models.BaseResponse).Init() var cacheKey string defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } _ = utils.Rc.Delete(cacheKey) this.Data["json"] = br this.ServeJSON() }() var reqData struct { ClassifyName string `json:"ClassifyName"` } err := json.Unmarshal(this.Ctx.Input.RequestBody, &reqData) if err != nil { br.ErrMsg = "无法解析请求体" return } categoryName := reqData.ClassifyName if categoryName == "" { br.Msg = "请输入分类!" br.ErrMsg = "请输入分类" return } lyClassify, err := models.GetRzdClassifyByName(categoryName) if err != nil { return } br.Ret = 200 br.Success = true br.Data = lyClassify br.Msg = "获取成功" } // GetRzdClassifyList // @Title 获取分类 // @Description 获取分类 // @Success 200 {object} models.BaseFromRzdClassify // @router /get/rzd/classify [post] func (this *BaseFromRzdController) GetRzdClassifyList() { br := new(models.BaseResponse).Init() defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() classifyList, err := models.GetRzdClassifyList() if err != nil { br.Msg = "获取睿咨得分类失败" br.ErrMsg = "获取睿咨得分类失败,err:" + err.Error() return } br.Ret = 200 br.Success = true br.Data = classifyList br.Msg = "获取成功" } // AddBatchRzdData // @Title 新增数据源指标数据 // @Description 新增数据源指标数据 // @Success 200 string "处理成功" // @router /add/batch/rzd/data [post] func (this *BaseFromRzdController) AddBatchRzdData() { br := new(models.BaseResponse).Init() var cacheKey string defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } _ = utils.Rc.Delete(cacheKey) this.Data["json"] = br this.ServeJSON() }() var req models.IndexInfo err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } //先添加指标 rzdIndex, err := models.GetRzdIndexByCode(req.IndexCode) if err != nil { if !utils.IsErrNoRow(err) { br.Msg = "查询指标失败" br.ErrMsg = "查询指标失败,Err:" + err.Error() return } rzdIndex = &models.BaseFromRzdIndex{ CreateTime: time.Now(), // 创建时间 BaseFromRzdClassifyId: req.ClassifyId, // 指标编码 IndexName: req.IndexName, // 指标名称 IndexCode: req.IndexCode, Frequency: req.Frequency, // 频度 Unit: req.Unit, // 单位 StartDate: req.StartDate, // 数据开始日期 EndDate: req.EndDate, // 数据结束日期 LatestValue: req.LatestValue.InexactFloat64(), } var dataList []*models.BaseFromRzdData for _, data := range req.DataList { rzdData := &models.BaseFromRzdData{ CreateTime: time.Now().Format(utils.FormatDateTime), IndexCode: req.IndexCode, DataTime: data.DataTime.Format(utils.FormatDate), Value: data.Value.InexactFloat64(), } dataList = append(dataList, rzdData) } err = models.AddRzdIndexWithDataList(rzdIndex, dataList) if err != nil { br.Msg = "查询指标失败" br.ErrMsg = "查询指标失败,Err:" + err.Error() return } } else { rzdIndex.BaseFromRzdClassifyId = req.ClassifyId if rzdIndex.StartDate.After(req.StartDate) { rzdIndex.StartDate = req.StartDate } if rzdIndex.EndDate.Before(req.EndDate) { rzdIndex.EndDate = req.EndDate rzdIndex.LatestValue = req.LatestValue.InexactFloat64() } var dataList []*models.BaseFromRzdData for _, data := range req.DataList { rzdData := &models.BaseFromRzdData{ BaseFromRzdIndexId: rzdIndex.BaseFromRzdIndexId, CreateTime: time.Now().Format(utils.FormatDateTime), IndexCode: req.IndexCode, DataTime: data.DataTime.Format(utils.FormatDate), Value: data.Value.InexactFloat64(), } dataList = append(dataList, rzdData) } err = models.UpdateIndexAndAddRzdDataList(rzdIndex, dataList) if err != nil { br.Msg = "新增指标数据失败!" br.ErrMsg = "新增指标数据失败,Err:" + err.Error() return } //更新指标库信息 // 获取指标详情 var edbInfo *models.EdbInfo edbInfo, err = models.GetEdbInfoByEdbCode(utils.DATA_SOURCE_RZD, rzdIndex.IndexCode) if err != nil { if err.Error() != utils.ErrNoRow() { utils.FileLog.Error(fmt.Sprint("刷新ETA指标异常,指标编码:", rzdIndex.IndexCode, err.Error())) return } else { err = nil } } // 已经加入到指标库的话,那么就去更新ETA指标库吧 if edbInfo != nil { go logic.RefreshBaseEdbInfo(edbInfo, ``) } } br.Ret = 200 br.Success = true br.Msg = "处理成功" } // AddRzdIndex // @Title 新增指标 // @Description 新增指标 // @Success 200 string "处理成功" // @router /add/rzd/index [post] func (this *BaseFromRzdController) AddRzdIndex() { br := new(models.BaseResponse).Init() var cacheKey string defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } _ = utils.Rc.Delete(cacheKey) this.Data["json"] = br this.ServeJSON() }() var req models.BaseFromRzdIndex err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } indexId, err := models.AddRzdIndex(&req) if err != nil { return } br.Ret = 200 br.Success = true br.Data = indexId br.Msg = "处理成功" } // GetRzdIndexDataByIndexIdAndDataTime // @Title 根据指标code和时间获取指标数据 // @Description 根据指标code和时间获取指标数据 // @Success 200 {object} models.BaseFromLyData // @router /get/rzd/index/data/by/code/and/time [post] func (this *BaseFromRzdController) GetRzdIndexDataByIndexIdAndDataTime() { br := new(models.BaseResponse).Init() var cacheKey string defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } _ = utils.Rc.Delete(cacheKey) this.Data["json"] = br this.ServeJSON() }() var reqData struct { IndexCode string `json:"IndexCode"` DataTime string `json:"DataTime"` } err := json.Unmarshal(this.Ctx.Input.RequestBody, &reqData) if err != nil { br.ErrMsg = "无法解析请求体" return } indexCode := reqData.IndexCode if indexCode == "" { br.Msg = "请输入指标code!" br.ErrMsg = "请输入指标code" return } dataTime := reqData.DataTime if dataTime == "" { br.Msg = "请输入时间!" br.ErrMsg = "请输入时间" return } rzdData, err := models.GetRzdDataByIndexCodeAndDataTime(indexCode, dataTime) if err != nil { return } br.Ret = 200 br.Success = true br.Data = rzdData br.Msg = "获取成功" } // UpdateRzdDataById // @Title 更新数据源指标数据 // @Description 更新数据源指标数据 // @Success 200 string "处理成功" // @router /update/rzd/data/by/id [post] func (this *BaseFromRzdController) UpdateRzdDataById() { br := new(models.BaseResponse).Init() var cacheKey string defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } _ = utils.Rc.Delete(cacheKey) this.Data["json"] = br this.ServeJSON() }() var reqData struct { Id int `json:"Id"` Value float64 `json:"Value"` } err := json.Unmarshal(this.Ctx.Input.RequestBody, &reqData) if err != nil { br.ErrMsg = "无法解析请求体" return } id := reqData.Id if id == 0 { br.Msg = "请输入id!" br.ErrMsg = "请输入id" return } value := reqData.Value if value == 0 { br.Msg = "请输入值!" br.ErrMsg = "请输入值" return } err = models.UpdateRzdDataById(id, value) if err != nil { return } br.Ret = 200 br.Success = true br.Msg = "处理成功" } // GetRzdEdbDataByIndexCodeAndDataTime // @Title 根据指标编码和精确日期获取指标库数据 // @Description 根据指标编码和精确日期获取指标库数据 // @Success 200 {object} []models.EdbDataRzd // @router /get/edb/rzd/data/by/code/and/time [post] func (this *BaseFromRzdController) GetRzdEdbDataByIndexCodeAndDataTime() { br := new(models.BaseResponse).Init() var cacheKey string defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } _ = utils.Rc.Delete(cacheKey) this.Data["json"] = br this.ServeJSON() }() var reqData struct { IndexCode string `json:"IndexCode"` DataTime string `json:"DataTime"` } err := json.Unmarshal(this.Ctx.Input.RequestBody, &reqData) if err != nil { br.ErrMsg = "无法解析请求体" return } indexCode := reqData.IndexCode if indexCode == "" { br.Msg = "请输入指标id!" br.ErrMsg = "请输入指标id" return } dataTime := reqData.DataTime if dataTime == "" { br.Msg = "请输入时间!" br.ErrMsg = "请输入时间" return } lyEdbData, err := models.GetLyEdbDataByIndexCodeAndExactDataTime(indexCode, dataTime) if err != nil { return } br.Ret = 200 br.Success = true br.Data = lyEdbData br.Msg = "获取成功" } // GetRzdEdbInfoByIndexCode // @Title 根据指标编码获取指标库指标 // @Description 根据指标编码获取指标库指标 // @Success 200 {object} []models.EdbDataRzd // @router /get/rzd/edb/info/by/code [post] func (this *BaseFromRzdController) GetRzdEdbInfoByIndexCode() { br := new(models.BaseResponse).Init() var cacheKey string defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } _ = utils.Rc.Delete(cacheKey) this.Data["json"] = br this.ServeJSON() }() var reqData struct { IndexCode string `json:"IndexCode"` Source int `json:"Source"` } err := json.Unmarshal(this.Ctx.Input.RequestBody, &reqData) if err != nil { br.ErrMsg = "无法解析请求体" return } indexCode := reqData.IndexCode if indexCode == "" { br.Msg = "请输入指标id!" br.ErrMsg = "请输入指标id" return } source := reqData.Source if source == 0 { br.Msg = "请输入来源!" br.ErrMsg = "请输入来源" return } lyEdbData, err := models.GetEdbInfoByEdbCode(source, indexCode) if err != nil { return } br.Ret = 200 br.Success = true br.Data = lyEdbData br.Msg = "获取成功" } // UpdateRzdEdbDataById // @Title 更新指标库数据 须根据指标编码和日期更新 仅适合月度数据 // @Description 更新指标库数据 须根据指标编码和日期更新 仅适合月度数据 // @Success 200 string "处理成功" // @router /update/rzd/edb/data/by/id [post] func (this *BaseFromRzdController) UpdateRzdEdbDataById() { br := new(models.BaseResponse).Init() var cacheKey string defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } _ = utils.Rc.Delete(cacheKey) this.Data["json"] = br this.ServeJSON() }() var reqData struct { Id int `json:"Id"` Value float64 `json:"Value"` } err := json.Unmarshal(this.Ctx.Input.RequestBody, &reqData) if err != nil { br.ErrMsg = "无法解析请求体" return } id := reqData.Id if id == 0 { br.Msg = "请输入id!" br.ErrMsg = "请输入id" return } value := reqData.Value if value == 0 { br.Msg = "请输入值!" br.ErrMsg = "请输入值" return } err = models.UpdateRzdEdbDataById(id, value) if err != nil { return } br.Ret = 200 br.Success = true br.Msg = "处理成功" } // GetRzdIndexByCode // @Title 查询指标编码是否存在 // @Description 查询指标编码是否存在 // @Success 200 {object} models.BaseFromLyIndex // @router /get/rzd/index/by/code [post] func (this *BaseFromRzdController) GetRzdIndexByCode() { br := new(models.BaseResponse).Init() var cacheKey string defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } _ = utils.Rc.Delete(cacheKey) this.Data["json"] = br this.ServeJSON() }() var reqData struct { IndexCode string `json:"IndexCode"` } err := json.Unmarshal(this.Ctx.Input.RequestBody, &reqData) if err != nil { br.ErrMsg = "无法解析请求体" return } indexCode := reqData.IndexCode if indexCode == "" { br.Msg = "请输入指标id!" br.ErrMsg = "请输入指标id" return } rzdIndex, err := models.GetRzdIndexByCode(indexCode) if err != nil { return } br.Ret = 200 br.Success = true br.Data = rzdIndex br.Msg = "获取成功" } // AddBatchRzdEdbData // @Title 批量增加睿咨得指标库数据 // @Description 批量增加睿咨得指标库数据 // @Success 200 string "处理成功" // @router /add/batch/rzd/edb/data [post] func (this *BaseFromRzdController) AddBatchRzdEdbData() { br := new(models.BaseResponse).Init() var cacheKey string defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } _ = utils.Rc.Delete(cacheKey) this.Data["json"] = br this.ServeJSON() }() var req []models.EdbDataRzd err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } err = models.AddRzdEdbDataList(req) if err != nil { return } br.Ret = 200 br.Success = true br.Msg = "处理成功" } // UpdateRzdEdbData // @Title 修改指标数据 // @Description 修改指标数据 // @Success 200 string "处理成功" // @router /update/rzd/edb/data [post] func (this *BaseFromRzdController) UpdateRzdEdbData() { br := new(models.BaseResponse).Init() var cacheKey string defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } _ = utils.Rc.Delete(cacheKey) this.Data["json"] = br this.ServeJSON() }() var req models.BaseFromRzdData err := json.Unmarshal(this.Ctx.Input.RequestBody, &req) if err != nil { br.Msg = "参数解析异常!" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } err = models.UpdateRzdData(&req) if err != nil { return } // 同步更新指标库数据 edbInfo, err := models.GetEdbInfoByEdbCode(utils.DATA_SOURCE_RZD, req.IndexCode) if err != nil { br.Msg = "指标不存在!" br.ErrMsg = "指标不存在" return } if edbInfo.EdbInfoId > 0 { err = models.UpdateRzdEdbDataByIndexCodeAndDataTime(req.BaseFromRzdIndexId, req.DataTime, req.Value) if err != nil { return } } br.Ret = 200 br.Success = true br.Msg = "处理成功" }