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" "strconv" "strings" "time" ) // JiaYueController 嘉悦物产数据源 type JiaYueController struct { BaseAuthController } // Add // @Title 嘉悦物产-新增指标接口 // @Description 新增指标接口 // @Success 200 {object} models.AddEdbInfoReq // @router /add [post] func (this *JiaYueController) Add() { 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() }() // TODO:桥接 source := utils.DATA_SOURCE_WIND 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) { br.Ret = 501 br.Success = true br.Msg = "系统处理中,请稍后重试" return } utils.Rc.SetNX(cacheKey, 1, 1*time.Minute) // 期货数据,就默认到今天,特殊处理下 endDate := utils.BASE_END_DATE if strings.Contains(req.EdbCode, ".") { endDate = time.Now().Format(utils.FormatDate) } // 走桥接服务 windUrl, _ := services.GetWindBridgeUrl() if windUrl == "" { br.Msg = "wind服务地址有误" br.ErrMsg = "wind服务地址有误" return } var dataList []*models.EdbDataFromWindSimple var errMsg string dataList, err, errMsg = services.GetEdbDataFromBridge(windUrl, req, endDate) if err != nil { br.Msg = "获取指标信息失败!" br.ErrMsg = "获取桥接服务指标信息失败,Err:" + errMsg + err.Error() return } err = models.AddEdbDataFromWind(req.EdbCode, dataList) if err != nil { br.Msg = "获取指标信息失败!" br.ErrMsg = "获取指标信息失败 AddEdbDataFromWind,Err:" + err.Error() return } br.Ret = 200 br.Success = true br.Msg = "获取成功" } // Refresh // @Title 嘉悦物产-刷新指标接口 // @Description 刷新指标接口 // @Success 200 {object} models.RefreshEdbInfoReq // @router /refresh [post] func (this *JiaYueController) Refresh() { br := new(models.BaseResponse).Init() var cacheKey string defer func() { if br.ErrMsg == "" { br.IsSendEmail = false } this.Data["json"] = br this.ServeJSON() }() source := utils.DATA_SOURCE_WIND 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 } 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 } utils.Rc.SetNX(cacheKey, 1, 1*time.Minute) defer func() { utils.Rc.Delete(cacheKey) }() //获取指标信息 edbInfo, err := models.GetEdbInfoById(req.EdbInfoId) if err != nil { br.Msg = "查询指标信息失败!" br.ErrMsg = "查询指标信息失败 Err:" + err.Error() return } //期货数据,就默认到今天,特殊处理下 endDate := utils.BASE_END_DATE if strings.Contains(req.EdbCode, ".") { endDate = time.Now().Format(utils.FormatDate) } // 判断是否调用桥接服务 windBridgeUrl, _ := services.GetWindBridgeJYUrl() var dataList []*models.EdbDataFromWindSimple if windBridgeUrl != "" && windBridgeUrl == edbInfo.ServerUrl { var errMsg string dataList, err, errMsg = services.RefreshEdbDataFromWindBridge(edbInfo.ServerUrl, req, edbInfo, endDate) if err != nil { br.Msg = "获取指标信息失败!" br.ErrMsg = "获取桥接服务指标信息失败,Err:" + errMsg + err.Error() return } } else { dataItem, errCode, err := services.GetEdbDataFromWindUrl(edbInfo.ServerUrl, req.EdbCode, req.StartDate, endDate) if errCode == 421 { //指标超限 br.Ret = 421 br.Msg = "指标超限!" return } // 忽略掉指标下架的错误, 并更新指标为停止更新 if errCode == services.WindNoAuthCode { edbInfo.NoUpdate = 1 edbInfo.ModifyTime = time.Now().Local() if e := edbInfo.Update([]string{"NoUpdate", "ModifyTime"}); e != nil { br.Msg = "刷新失败" br.ErrMsg = "更新wind指标停更失败, Err: " + e.Error() return } br.Ret = 200 br.Msg = "操作成功" return } if err != nil { br.Msg = "获取指标信息失败!" br.ErrMsg = "获取指标信息失败 GetEdbDataFromWind,Err:" + err.Error() return } dataList = models.DealWindData(dataItem) } err = models.RefreshEdbDataFromWind(req.EdbInfoId, req.EdbCode, req.StartDate, dataList) if err != nil && err.Error() != utils.ErrNoRow() { br.Msg = "刷新指标信息失败!" br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromWind,Err:" + err.Error() return } // 更新指标最大最小值 err, errMsg := models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo) 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 = "获取成功" }