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 ThsDsController struct { BaseAuthController } // @Title 新增同花顺指标接口 // @Description 新增同花顺指标接口 // @Success 200 {object} models.AddEdbInfoReq // @router /ds/add [post] func (this *ThsDsController) 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_THS 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 } //期货数据,就默认到今天,特殊处理下 endDate := time.Now().Format(utils.FormatDate) cacheKey = utils.CACHE_EDB_DATA_ADD + strconv.Itoa(source) + "_" + req.StockCode + req.EdbCode if !utils.Rc.IsExist(cacheKey) { utils.Rc.SetNX(cacheKey, 1, 1*time.Minute) dataItem, err := services.GetEdbDataFromThsDs(req.StockCode, req.EdbCode, utils.BASE_START_DATE, endDate, "", req.ExtraPars) if err != nil { br.Msg = "获取指标信息失败!" br.ErrMsg = "获取指标信息失败 GetEdbDataFromThsDs,Err:" + err.Error() return } err = models.AddEdbDataFromThsDs(req.StockCode, req.EdbCode, dataItem) if err != nil { br.Msg = "获取指标信息失败!" br.ErrMsg = "获取指标信息失败 AddEdbDataFromThs,Err:" + err.Error() return } br.Ret = 200 br.Success = true br.Msg = "获取成功" } else { br.Ret = 501 br.Success = true br.Msg = "系统处理中,请稍后重试" } } // @Title 刷新同花顺指标接口 // @Description 刷新同花顺指标接口 // @Success 200 {object} models.RefreshEdbInfoReq // @router /ds/refresh [post] func (this *ThsDsController) Refresh() { br := new(models.BaseResponse).Init() var cacheKey string defer func() { this.Data["json"] = br this.ServeJSON() }() source := utils.DATA_SOURCE_THS 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 } //期货数据,就默认到今天,特殊处理下 endDate := time.Now().Format(utils.FormatDate) // 获取指标详情 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 } utils.Rc.SetNX(cacheKey, 1, 1*time.Minute) defer func() { utils.Rc.Delete(cacheKey) }() var extra models.EdbInfoExtra if edbInfo.Extra != "" { if e := json.Unmarshal([]byte(edbInfo.Extra), &extra); e != nil { br.Msg = "刷新失败" br.ErrMsg = fmt.Sprintf("API额外参数解析失败, %v", e) return } } dataItem, err := services.GetEdbDataFromThsDs(edbInfo.StockCode, edbInfo.IndicatorCode, utils.GetEdbRefreshStartDate(req.StartDate), endDate, edbInfo.TerminalCode, extra.ApiExtraPars) if err != nil { br.Msg = "获取指标信息失败!" br.ErrMsg = "获取指标信息失败 GetEdbDataFromThsDs,Err:" + err.Error() return } err = models.RefreshEdbDataFromThsDs(req.EdbInfoId, req.EdbCode, utils.GetEdbRefreshStartDate(req.StartDate), dataItem) if err != nil && err.Error() != utils.ErrNoRow() { br.Msg = "刷新指标信息失败!" br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromThsDs,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 = "获取成功" } //func init() { // //?EdbCode=s005696248&StartDate=2023-02-03&EndDate=2027-03-23 // //edbCode := `s005696248` // //startDate := `2023-02-03` // //endDate := `2027-03-23` // //EdbCode=S011292460&StartDate=1993-03-23&EndDate=2027-03-23 // edbCode := `S011292460` // startDate := `1993-03-23` // endDate := `2027-03-23` // //edbCode := `@CL0W.NMX` // //startDate := `20221218` // //endDate := `20230118` // list, err := services.GetEdbDataFromThsHttp(edbCode, startDate, endDate, 0) // //list, err := services.GetFutureGoodDataFromThsHttp(edbCode, startDate, endDate) // // //token, err := services.GetAccessToken() // if err != nil { // fmt.Println("err:", err) // return // } // fmt.Println(list) // //fmt.Println(token) //}