123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- package future_good
- import (
- "encoding/json"
- "eta/eta_index_lib/controllers"
- "eta/eta_index_lib/logic"
- "eta/eta_index_lib/models"
- "eta/eta_index_lib/models/future_good"
- "eta/eta_index_lib/services"
- "eta/eta_index_lib/utils"
- "strconv"
- "time"
- )
- // FutureGoodEdbInfoController 期货数据
- type FutureGoodEdbInfoController struct {
- controllers.BaseAuthController
- }
- // Add
- // @Title 新增wind商品指标接口
- // @Description 新增wind指标接口
- // @Success 200 {object} models.AddEdbInfoReq
- // @router /add [post]
- func (this *FutureGoodEdbInfoController) Add() {
- br := new(models.BaseResponse).Init()
- var cacheKey string
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- 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
- }
- req.EdbCode = utils.TrimStr(req.EdbCode)
- cacheKey = utils.CACHE_EDB_DATA_ADD + "_futuregood_" + req.EdbCode
- if true {
- utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
- defer func() {
- utils.Rc.Delete(cacheKey)
- }()
- //期货数据,就默认到今天,特殊处理下
- futureGoodEdbInfo, err := future_good.GetFutureGoodEdbInfoByCode(req.EdbCode)
- if err != nil {
- if err.Error() != utils.ErrNoRow() {
- br.Msg = "系统内找不到该指标"
- } else {
- br.Msg = "添加失败"
- br.ErrMsg = "添加失败,ERR:" + err.Error()
- }
- return
- }
- //endDate := utils.BASE_END_DATE
- endDateTime := time.Now()
- //每天下午三点之前刷新数据的话,截止日期就到昨天,三点之后才刷新到今天的日期
- if endDateTime.Hour() < 15 {
- endDateTime = endDateTime.AddDate(0, 0, -1)
- }
- endDate := time.Now().Format(utils.FormatDate)
- dataItem, err := services.GetFutureGoodDataFromThs(req.EdbCode, utils.BASE_START_DATE, endDate, "")
- if err != nil {
- br.Msg = "获取指标信息失败!"
- br.ErrMsg = "获取指标信息失败 GetFutureGoodDataFromThs,Err:" + err.Error()
- return
- }
- err = future_good.AddEdbDataFromThs(futureGoodEdbInfo.FutureGoodEdbInfoId, req.EdbCode, dataItem)
- if err != nil {
- br.Msg = "获取指标信息失败!"
- br.ErrMsg = "获取指标信息失败 AddEdbDataFromWind,Err:" + err.Error()
- return
- }
- // 更新指标最大最小值
- err, errMsg := future_good.UnifiedModifyEdbInfoMaxAndMinInfo(futureGoodEdbInfo)
- if err != nil {
- br.Msg = errMsg
- br.ErrMsg = err.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- } else {
- br.Ret = 501
- br.Success = true
- br.Msg = "系统处理中,请稍后重试"
- }
- }
- // Refresh
- // @Title 刷新wind期货数据指标接口
- // @Description 刷新wind指标接口
- // @Success 200 {object} models.RefreshEdbInfoReq
- // @router /refresh [post]
- func (this *FutureGoodEdbInfoController) Refresh() {
- br := new(models.BaseResponse).Init()
- var cacheKey string
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req future_good.RefreshFutureEdbEdbInfoReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.FutureGoodEdbCode == "" {
- br.Msg = "请输入指标编码!"
- br.ErrMsg = "请输入指标编码,指标编码为空"
- return
- }
- if req.FutureGoodEdbInfoId <= 0 {
- br.Msg = "请输入指标ID!"
- br.ErrMsg = "请输入指标ID"
- return
- }
- cacheKey = utils.CACHE_EDB_DATA_REFRESH + "_futuregood_" + req.FutureGoodEdbCode
- 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)
- }()
- //获取指标信息
- futureGoodEdbInfo, err := future_good.GetFutureGoodEdbInfoByCode(req.FutureGoodEdbCode)
- if err != nil {
- if err.Error() != utils.ErrNoRow() {
- br.Msg = "系统内找不到该指标"
- } else {
- br.Msg = "添加失败"
- br.ErrMsg = "添加失败,ERR:" + err.Error()
- }
- return
- }
- //期货数据,就默认到今天,特殊处理下
- endDateTime := time.Now()
- //每天下午三点之前刷新数据的话,截止日期就到昨天,三点之后才刷新到今天的日期
- if endDateTime.Hour() < 15 {
- endDateTime = endDateTime.AddDate(0, 0, -1)
- }
- endDate := endDateTime.Format(utils.FormatDate)
- // 数据开始日期
- startDate := req.StartDate
- if startDate == `` { // 如果传入的日期为空的话,那么就默认兼容一周的数据吧
- startDate = endDateTime.AddDate(0, 0, -7).Format(utils.FormatDate)
- }
- dataItem, err := services.GetFutureGoodDataFromThs(req.FutureGoodEdbCode, req.StartDate, endDate, futureGoodEdbInfo.TerminalCode)
- if err != nil {
- br.Msg = "获取指标信息失败!"
- br.ErrMsg = "获取指标信息失败 GetFutureGoodDataFromThs,Err:" + err.Error()
- return
- }
- err = future_good.RefreshFutureGoodEdbDataFromThs(req.FutureGoodEdbInfoId, req.FutureGoodEdbCode, req.StartDate, dataItem)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "刷新指标信息失败!"
- br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromWind,Err:" + err.Error()
- return
- }
- // 更新指标最大最小值
- err, errMsg := future_good.UnifiedModifyEdbInfoMaxAndMinInfo(futureGoodEdbInfo)
- if err != nil {
- br.Msg = errMsg
- br.ErrMsg = err.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- }
- // RefreshRelation
- // @Title 刷新商品期货指标相关的接口
- // @Description 刷新商品期货指标相关的接口
- // @Success 200 {object} models.RefreshEdbInfoReq
- // @router /relation/refresh [post]
- func (this *FutureGoodEdbInfoController) RefreshRelation() {
- br := new(models.BaseResponse).Init()
- var cacheKey string
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req future_good.RefreshFutureChartInfoReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.ChartInfoId <= 0 {
- br.Msg = "请输入图表ID!"
- br.ErrMsg = "请输入图表ID"
- return
- }
- cacheKey = utils.CACHE_EDB_DATA_REFRESH + "_futuregood_relation_chart" + strconv.Itoa(req.ChartInfoId)
- if utils.Rc.IsExist(cacheKey) {
- br.Ret = 501
- br.Success = true
- br.Msg = "系统处理中,请稍后重试"
- return
- }
- utils.Rc.SetNX(cacheKey, 1, 10*time.Minute)
- defer func() {
- utils.Rc.Delete(cacheKey)
- }()
- err, errMsg := logic.RefreshByChartId(req.ChartInfoId)
- if err != nil {
- br.Msg = "利润曲线图表刷新失败"
- br.ErrMsg = "利润曲线图表刷新失败,Err:" + errMsg
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "刷新成功"
- }
- //func init() {
- //
- // list, err := future_good.GetFutureGoodEdbInfoList(" AND start_date = '0000-00-00' ", []interface{}{})
- // if err != nil {
- // fmt.Println("list err:", err)
- // return
- // }
- //
- // fmt.Println("start")
- // for _, futureGoodEdbInfo := range list {
- // // 更新指标最大最小值
- // err, errMsg := future_good.UnifiedModifyEdbInfoMaxAndMinInfo(futureGoodEdbInfo)
- // if err != nil {
- // fmt.Println(futureGoodEdbInfo.FutureGoodEdbCode, "更新失败:", errMsg, ";err:", err)
- // return
- // }
- // }
- // fmt.Println("end")
- //}
|