future_good_edb_info.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. package future_good
  2. import (
  3. "encoding/json"
  4. "eta/eta_index_lib/controllers"
  5. "eta/eta_index_lib/logic"
  6. "eta/eta_index_lib/models"
  7. "eta/eta_index_lib/models/future_good"
  8. "eta/eta_index_lib/services"
  9. "eta/eta_index_lib/utils"
  10. "fmt"
  11. "time"
  12. )
  13. // FutureGoodEdbInfoController 期货数据
  14. type FutureGoodEdbInfoController struct {
  15. controllers.BaseAuthController
  16. }
  17. // Add
  18. // @Title 新增wind商品指标接口
  19. // @Description 新增wind指标接口
  20. // @Success 200 {object} models.AddEdbInfoReq
  21. // @router /add [post]
  22. func (this *FutureGoodEdbInfoController) Add() {
  23. br := new(models.BaseResponse).Init()
  24. var cacheKey string
  25. defer func() {
  26. this.Data["json"] = br
  27. this.ServeJSON()
  28. }()
  29. var req models.AddEdbInfoReq
  30. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  31. if err != nil {
  32. br.Msg = "参数解析异常!"
  33. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  34. return
  35. }
  36. if req.EdbCode == "" {
  37. br.Msg = "请输入指标编码!"
  38. br.ErrMsg = "请输入指标编码,指标编码为空"
  39. return
  40. }
  41. req.EdbCode = utils.TrimStr(req.EdbCode)
  42. cacheKey = utils.CACHE_EDB_DATA_ADD + "_futuregood_" + req.EdbCode
  43. if true {
  44. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  45. defer func() {
  46. utils.Rc.Delete(cacheKey)
  47. }()
  48. //期货数据,就默认到今天,特殊处理下
  49. futureGoodEdbInfo, err := future_good.GetFutureGoodEdbInfoByCode(req.EdbCode)
  50. if err != nil {
  51. if err.Error() != utils.ErrNoRow() {
  52. br.Msg = "系统内找不到该指标"
  53. } else {
  54. br.Msg = "添加失败"
  55. br.ErrMsg = "添加失败,ERR:" + err.Error()
  56. }
  57. return
  58. }
  59. //endDate := utils.BASE_END_DATE
  60. endDateTime := time.Now()
  61. //每天下午三点之前刷新数据的话,截止日期就到昨天,三点之后才刷新到今天的日期
  62. if endDateTime.Hour() < 15 {
  63. endDateTime = endDateTime.AddDate(0, 0, -1)
  64. }
  65. endDate := time.Now().Format(utils.FormatDate)
  66. dataItem, err := services.GetFutureGoodDataFromThs(req.EdbCode, utils.BASE_START_DATE, endDate)
  67. if err != nil {
  68. br.Msg = "获取指标信息失败!"
  69. br.ErrMsg = "获取指标信息失败 GetFutureGoodDataFromThs,Err:" + err.Error()
  70. return
  71. }
  72. err = future_good.AddEdbDataFromThs(futureGoodEdbInfo.FutureGoodEdbInfoId, req.EdbCode, dataItem)
  73. if err != nil {
  74. br.Msg = "获取指标信息失败!"
  75. br.ErrMsg = "获取指标信息失败 AddEdbDataFromWind,Err:" + err.Error()
  76. return
  77. }
  78. // 更新指标最大最小值
  79. err, errMsg := future_good.UnifiedModifyEdbInfoMaxAndMinInfo(futureGoodEdbInfo)
  80. if err != nil {
  81. br.Msg = errMsg
  82. br.ErrMsg = err.Error()
  83. return
  84. }
  85. br.Ret = 200
  86. br.Success = true
  87. br.Msg = "获取成功"
  88. } else {
  89. br.Ret = 501
  90. br.Success = true
  91. br.Msg = "系统处理中,请稍后重试"
  92. }
  93. }
  94. // Refresh
  95. // @Title 刷新wind期货数据指标接口
  96. // @Description 刷新wind指标接口
  97. // @Success 200 {object} models.RefreshEdbInfoReq
  98. // @router /refresh [post]
  99. func (this *FutureGoodEdbInfoController) Refresh() {
  100. br := new(models.BaseResponse).Init()
  101. var cacheKey string
  102. defer func() {
  103. this.Data["json"] = br
  104. this.ServeJSON()
  105. }()
  106. var req future_good.RefreshFutureEdbEdbInfoReq
  107. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  108. if err != nil {
  109. br.Msg = "参数解析异常!"
  110. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  111. return
  112. }
  113. if req.FutureGoodEdbCode == "" {
  114. br.Msg = "请输入指标编码!"
  115. br.ErrMsg = "请输入指标编码,指标编码为空"
  116. return
  117. }
  118. if req.FutureGoodEdbInfoId <= 0 {
  119. br.Msg = "请输入指标ID!"
  120. br.ErrMsg = "请输入指标ID"
  121. return
  122. }
  123. cacheKey = utils.CACHE_EDB_DATA_REFRESH + "_futuregood_" + req.FutureGoodEdbCode
  124. if utils.Rc.IsExist(cacheKey) {
  125. br.Ret = 501
  126. br.Success = true
  127. br.Msg = "系统处理中,请稍后重试"
  128. return
  129. }
  130. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  131. defer func() {
  132. utils.Rc.Delete(cacheKey)
  133. }()
  134. //获取指标信息
  135. futureGoodEdbInfo, err := future_good.GetFutureGoodEdbInfoByCode(req.FutureGoodEdbCode)
  136. if err != nil {
  137. if err.Error() != utils.ErrNoRow() {
  138. br.Msg = "系统内找不到该指标"
  139. } else {
  140. br.Msg = "添加失败"
  141. br.ErrMsg = "添加失败,ERR:" + err.Error()
  142. }
  143. return
  144. }
  145. //期货数据,就默认到今天,特殊处理下
  146. endDateTime := time.Now()
  147. //每天下午三点之前刷新数据的话,截止日期就到昨天,三点之后才刷新到今天的日期
  148. if endDateTime.Hour() < 15 {
  149. endDateTime = endDateTime.AddDate(0, 0, -1)
  150. }
  151. endDate := endDateTime.Format(utils.FormatDate)
  152. // 数据开始日期
  153. startDate := req.StartDate
  154. if startDate == `` { // 如果传入的日期为空的话,那么就默认兼容一周的数据吧
  155. startDate = endDateTime.AddDate(0, 0, -7).Format(utils.FormatDate)
  156. }
  157. dataItem, err := services.GetFutureGoodDataFromThs(req.FutureGoodEdbCode, req.StartDate, endDate)
  158. if err != nil {
  159. br.Msg = "获取指标信息失败!"
  160. br.ErrMsg = "获取指标信息失败 GetFutureGoodDataFromThs,Err:" + err.Error()
  161. return
  162. }
  163. err = future_good.RefreshFutureGoodEdbDataFromThs(req.FutureGoodEdbInfoId, req.FutureGoodEdbCode, req.StartDate, dataItem)
  164. if err != nil && err.Error() != utils.ErrNoRow() {
  165. br.Msg = "刷新指标信息失败!"
  166. br.ErrMsg = "刷新指标信息失败 RefreshEdbDataFromWind,Err:" + err.Error()
  167. return
  168. }
  169. // 更新指标最大最小值
  170. err, errMsg := future_good.UnifiedModifyEdbInfoMaxAndMinInfo(futureGoodEdbInfo)
  171. if err != nil {
  172. br.Msg = errMsg
  173. br.ErrMsg = err.Error()
  174. return
  175. }
  176. br.Ret = 200
  177. br.Success = true
  178. br.Msg = "获取成功"
  179. }
  180. // RefreshRelation
  181. // @Title 刷新商品期货指标相关的接口
  182. // @Description 刷新商品期货指标相关的接口
  183. // @Success 200 {object} models.RefreshEdbInfoReq
  184. // @router /relation/refresh [post]
  185. func (this *FutureGoodEdbInfoController) RefreshRelation() {
  186. br := new(models.BaseResponse).Init()
  187. var cacheKey string
  188. defer func() {
  189. this.Data["json"] = br
  190. this.ServeJSON()
  191. }()
  192. var req future_good.RefreshFutureEdbEdbInfoReq
  193. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  194. if err != nil {
  195. br.Msg = "参数解析异常!"
  196. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  197. return
  198. }
  199. if req.FutureGoodEdbInfoId <= 0 {
  200. br.Msg = "请输入指标ID!"
  201. br.ErrMsg = "请输入指标ID"
  202. return
  203. }
  204. cacheKey = utils.CACHE_EDB_DATA_REFRESH + "_futuregood_relation_" + req.FutureGoodEdbCode
  205. if utils.Rc.IsExist(cacheKey) {
  206. br.Ret = 501
  207. br.Success = true
  208. br.Msg = "系统处理中,请稍后重试"
  209. return
  210. }
  211. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  212. defer func() {
  213. utils.Rc.Delete(cacheKey)
  214. }()
  215. //获取指标信息
  216. futureGoodEdbInfo, err := future_good.GetFutureGoodEdbInfo(req.FutureGoodEdbInfoId)
  217. if err != nil {
  218. if err.Error() != utils.ErrNoRow() {
  219. br.Msg = "系统内找不到该指标"
  220. } else {
  221. br.Msg = "刷新失败"
  222. br.ErrMsg = "添加失败,ERR:" + err.Error()
  223. }
  224. return
  225. }
  226. // 获取相关图表
  227. list, err := models.GetGroupChartEdbMappingListByEdbInfoId(futureGoodEdbInfo.FutureGoodEdbInfoId, 2)
  228. if err != nil {
  229. br.Msg = "查找相关图表id失败"
  230. br.ErrMsg = "添加失败,ERR:" + err.Error()
  231. return
  232. }
  233. go func() {
  234. errMsgList := make([]string, 0)
  235. for _, v := range list {
  236. err, errMsg := logic.RefreshByChartId(v.ChartInfoId)
  237. if err != nil {
  238. errMsgList = append(errMsgList, fmt.Sprint(v.ChartInfoId, "更新失败,"+errMsg))
  239. }
  240. }
  241. if len(errMsgList) > 0 {
  242. //br.Msg = "部分刷新失败"
  243. //br.ErrMsg = "部分刷新失败,Err:" + strings.Join(errMsgList, ";")
  244. return
  245. }
  246. }()
  247. br.Ret = 200
  248. br.Success = true
  249. br.Msg = "刷新成功"
  250. }
  251. //func init() {
  252. //
  253. // list, err := future_good.GetFutureGoodEdbInfoList(" AND start_date = '0000-00-00' ", []interface{}{})
  254. // if err != nil {
  255. // fmt.Println("list err:", err)
  256. // return
  257. // }
  258. //
  259. // fmt.Println("start")
  260. // for _, futureGoodEdbInfo := range list {
  261. // // 更新指标最大最小值
  262. // err, errMsg := future_good.UnifiedModifyEdbInfoMaxAndMinInfo(futureGoodEdbInfo)
  263. // if err != nil {
  264. // fmt.Println(futureGoodEdbInfo.FutureGoodEdbCode, "更新失败:", errMsg, ";err:", err)
  265. // return
  266. // }
  267. // }
  268. // fmt.Println("end")
  269. //}