history.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. package quote
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "mtp2_if/global/app"
  6. "mtp2_if/global/e"
  7. "mtp2_if/logger"
  8. "mtp2_if/models"
  9. "mtp2_if/utils"
  10. "net/http"
  11. "sort"
  12. "time"
  13. "github.com/gin-gonic/gin"
  14. )
  15. // HistoryData 历史数据
  16. type HistoryData struct {
  17. Opened float64 `json:"o"` // 开盘价
  18. Highest float64 `json:"h"` // 最高价
  19. Lowest float64 `json:"l"` // 最低价
  20. Closed float64 `json:"c"` // 收盘价
  21. TotleVolume int `json:"tv"` // 总量
  22. TotleTurnover float64 `json:"tt"` // 总金额
  23. HoldVolume int `json:"hv"` // 持仓量
  24. Settle float64 `json:"s"` // 结算价,日线周期(包括)以上才有
  25. TimeStamp time.Time `json:"ts"` // 时间
  26. IsFill bool `json:"f"` // 是否补充数据
  27. }
  28. // QueryHistoryDatasReq 查询行情历史数据请求参数
  29. type QueryHistoryDatasReq struct {
  30. CycleType int `form:"cycleType" binding:"required"`
  31. GoodsCode string `form:"goodsCode" binding:"required"`
  32. StartTime string `form:"startTime"`
  33. EndTime string `form:"endTime"`
  34. Count int `form:"count"`
  35. IsAsc bool `form:"isAsc"`
  36. }
  37. // QueryHistoryDatas 查询行情历史数据
  38. // @Summary 查询行情历史数据
  39. // @Produce json
  40. // @Security ApiKeyAuth
  41. // @Param cycleType query int true "周期类型, 0-秒 1: 1分钟 2: 5分钟 3: 30分钟 4: 60分钟 120: 2小时 240: 4小时 11: 日线"
  42. // @Param goodsCode query string true "商品代码"
  43. // @Param startTime query string false "开始时间,格式:yyyy-MM-dd HH:mm:ss"
  44. // @Param endTime query string false "结束时间,格式:yyyy-MM-dd HH:mm:ss"
  45. // @Param count query int false "条数"
  46. // @Param isAsc query bool false "是否按时间顺序排序(默认为时间倒序排序)"
  47. // @Success 200 {object} HistoryData
  48. // @Failure 500 {object} app.Response
  49. // @Router /Quote/QueryHistoryDatas [get]
  50. // @Tags 行情服务
  51. func QueryHistoryDatas(c *gin.Context) {
  52. appG := app.Gin{C: c}
  53. // 获取请求参数
  54. var req QueryHistoryDatasReq
  55. if err := appG.C.ShouldBindQuery(&req); err != nil {
  56. logger.GetLogger().Errorf("QueryHistoryDatas failed: %s", err.Error())
  57. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  58. return
  59. }
  60. // 转换时间
  61. timeFormat := "2006-01-02 15:04:05" // go中的时间格式化必须是这个时间
  62. var startTime *time.Time
  63. if len(req.StartTime) > 0 {
  64. st, err := time.ParseInLocation(timeFormat, req.StartTime, time.Local)
  65. if err != nil {
  66. logger.GetLogger().Errorf("QueryHistoryDatas failed: %s", err.Error())
  67. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_TIME_FORMAT_FAIL, nil)
  68. return
  69. }
  70. startTime = &st
  71. }
  72. var endTime *time.Time
  73. if len(req.EndTime) > 0 {
  74. et, err := time.ParseInLocation(timeFormat, req.EndTime, time.Local)
  75. if err != nil {
  76. logger.GetLogger().Errorf("QueryHistoryDatas failed: %s", err.Error())
  77. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_TIME_FORMAT_FAIL, nil)
  78. return
  79. }
  80. endTime = &et
  81. }
  82. // 查询数据
  83. cycleDatas, err := models.GetHistoryCycleDatas(models.CycleType(req.CycleType), req.GoodsCode, startTime, endTime, req.Count, req.IsAsc)
  84. if err != nil {
  85. logger.GetLogger().Errorf("QueryTSData failed: %s", err.Error())
  86. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  87. return
  88. }
  89. // 获取目标商品信息
  90. goods, err := models.GetGoodsByGoodsCode(req.GoodsCode)
  91. if err != nil {
  92. logger.GetLogger().Errorf("QueryTSData failed: %s", err.Error())
  93. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  94. return
  95. }
  96. // 计算最终价格
  97. rst := make([]HistoryData, 0)
  98. for _, v := range cycleDatas {
  99. historyData := HistoryData{
  100. Opened: utils.IntToFloat64(v.Open, int(goods.Decimalplace)),
  101. Highest: utils.IntToFloat64(v.High, int(goods.Decimalplace)),
  102. Lowest: utils.IntToFloat64(v.Low, int(goods.Decimalplace)),
  103. Closed: utils.IntToFloat64(v.Close, int(goods.Decimalplace)),
  104. TotleVolume: v.TV,
  105. TotleTurnover: float64(v.TT),
  106. HoldVolume: v.HV,
  107. Settle: utils.IntToFloat64(v.SP, int(goods.Decimalplace)),
  108. TimeStamp: time.Unix(int64(v.ST), 0),
  109. }
  110. rst = append(rst, historyData)
  111. }
  112. // 查询成功
  113. logger.GetLogger().Debugln("QueryHistoryDatas successed: %v", rst)
  114. appG.Response(http.StatusOK, e.SUCCESS, rst)
  115. }
  116. // HistoryTikData Tik数据
  117. type HistoryTikData struct {
  118. TimeStamp time.Time `json:"TS"` // 行情时间文本
  119. PE float64 `json:"PE"` // 现价
  120. Vol int `json:"Vol"` // 现量
  121. TT float64 `json:"TT"` // 现金额
  122. Bid float64 `json:"Bid"` // 买价
  123. BV int `json:"BV"` // 买量
  124. Ask float64 `json:"Ask"` // 卖价
  125. AV int `json:"AV"` // 卖量
  126. HV int `json:"HV"` // 持仓量
  127. HI int `json:"HI"` // 单笔持仓
  128. TDR int `json:"TDR"` // 交易方向,0:买 1:卖
  129. TK int `json:"TK"` // 交易类型
  130. }
  131. // QueryHistoryTikDatasReq 查询行情Tik数据请求参数
  132. type QueryHistoryTikDatasReq struct {
  133. GoodsCode string `form:"goodsCode" binding:"required"`
  134. StartTime string `form:"startTime"`
  135. EndTime string `form:"endTime"`
  136. Count int `form:"count"`
  137. IsAsc bool `form:"isAsc"`
  138. }
  139. // QueryHistoryTikDatas 查询行情Tik数据
  140. // @Summary 查询行情Tik数据
  141. // @Produce json
  142. // @Security ApiKeyAuth
  143. // @Param goodsCode query string true "商品代码"
  144. // @Param startTime query string false "开始时间,格式:yyyy-MM-dd HH:mm:ss"
  145. // @Param endTime query string false "结束时间,格式:yyyy-MM-dd HH:mm:ss"
  146. // @Param count query int false "条数"
  147. // @Param isAsc query bool false "是否按时间顺序排序(默认为时间倒序排序)"
  148. // @Success 200 {object} HistoryTikData
  149. // @Failure 500 {object} app.Response
  150. // @Router /Quote/QueryHistoryTikDatas [get]
  151. // @Tags 行情服务
  152. func QueryHistoryTikDatas(c *gin.Context) {
  153. appG := app.Gin{C: c}
  154. // 获取请求参数
  155. var req QueryHistoryTikDatasReq
  156. if err := appG.C.ShouldBindQuery(&req); err != nil {
  157. logger.GetLogger().Errorf("QueryHistoryTikDatas failed: %s", err.Error())
  158. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  159. return
  160. }
  161. // 转换时间
  162. timeFormat := "2006-01-02 15:04:05" // go中的时间格式化必须是这个时间
  163. var startTime *time.Time
  164. if len(req.StartTime) > 0 {
  165. st, err := time.ParseInLocation(timeFormat, req.StartTime, time.Local)
  166. if err != nil {
  167. logger.GetLogger().Errorf("QueryHistoryTikDatas failed: %s", err.Error())
  168. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_TIME_FORMAT_FAIL, nil)
  169. return
  170. }
  171. startTime = &st
  172. }
  173. var endTime *time.Time
  174. if len(req.EndTime) > 0 {
  175. et, err := time.ParseInLocation(timeFormat, req.EndTime, time.Local)
  176. if err != nil {
  177. logger.GetLogger().Errorf("QueryHistoryTikDatas failed: %s", err.Error())
  178. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_TIME_FORMAT_FAIL, nil)
  179. return
  180. }
  181. endTime = &et
  182. }
  183. // 查询数据
  184. tikDatas, err := models.GetHistoryTikDatas(req.GoodsCode, startTime, endTime, req.Count, req.IsAsc)
  185. if err != nil {
  186. logger.GetLogger().Errorf("QueryHistoryTikDatas failed: %s", err.Error())
  187. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  188. return
  189. }
  190. // 获取目标商品信息
  191. goods, err := models.GetGoodsByGoodsCode(req.GoodsCode)
  192. if err != nil {
  193. logger.GetLogger().Errorf("QueryHistoryTikDatas failed: %s", err.Error())
  194. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  195. return
  196. }
  197. // 计算最终价格
  198. rst := make([]HistoryTikData, 0)
  199. for _, v := range tikDatas {
  200. // 获取方向
  201. buyOrSell := 0
  202. if v.TDR == 83 { // (Ascii) B-66 S-83
  203. buyOrSell = 1
  204. }
  205. rst = append(rst, HistoryTikData{
  206. TimeStamp: time.Unix(int64(v.AT), 0),
  207. PE: utils.IntToFloat64(v.PE, int(goods.Decimalplace)),
  208. Vol: v.Vol,
  209. TT: float64(v.TT),
  210. Bid: utils.IntToFloat64(v.Bid, int(goods.Decimalplace)),
  211. BV: v.BV,
  212. Ask: utils.IntToFloat64(v.Ask, int(goods.Decimalplace)),
  213. AV: v.AV,
  214. HV: v.HV,
  215. HI: v.HI,
  216. TDR: buyOrSell,
  217. TK: v.TK,
  218. })
  219. }
  220. // 查询成功
  221. logger.GetLogger().Debugln("QueryHistoryTikDatas successed: %v", rst)
  222. appG.Response(http.StatusOK, e.SUCCESS, rst)
  223. }
  224. // QueryTSDataReq 分时图数据查询请求参数
  225. type QueryTSDataReq struct {
  226. GoodsCode string `form:"goodsCode" binding:"required"` // 商品代码
  227. }
  228. // QueryTSDataRsp 分时图数据查询返回模型
  229. type QueryTSDataRsp struct {
  230. GoodsCode string `json:"goodsCode"` // 商品代码
  231. OutGoodsCode string `json:"outGoodsCode"` // 外部商品代码
  232. DecimalPlace int `json:"decimalPlace"` // 小数位
  233. TradeDate string `json:"tradeDate"` // 交易日
  234. StartTime time.Time `json:"startTime"` // 开始时间
  235. EndTime time.Time `json:"endTime"` // 结束时间
  236. PreSettle float64 `json:"preSettle"` // 昨结价
  237. Count int `json:"Count"` // 交易日应有数据个数
  238. HistoryDatas []HistoryData `json:"historyDatas"` // 历史数据
  239. RunSteps []TSDataRunStep `json:"runSteps"` // 交易日开休市计划
  240. }
  241. // TSDataRunStep 分时图交易日开休市计划
  242. type TSDataRunStep struct {
  243. Groupid int32 `json:"groupid"` // 分组ID
  244. Tradeweekday int32 `json:"tradeweekday"` // 交易日归属 - 0:星期天、1:星期一、2:星期二、3:星期三、4:星期四、5:星期五、6:星期六
  245. Sectionid int32 `json:"sectionid"` // 从 1 开始 往下编 [0为系统清盘、结算时间] SectionId = 0时,开始时间=清盘时间 开始周几= 清盘周几, 结束时间=结算时间 结束周几=结算周几
  246. Runstep int32 `json:"runstep"` // 运行阶段 - 2:连续交易
  247. Startweekday int32 `json:"startweekday"` // 起始周几
  248. Starttime string `json:"starttime"` // 起始时间(HH:mm)
  249. Endweekday int32 `json:"endweekday"` // 结束周几
  250. Endtime string `json:"endtime"` // 结束时间(HH:mm)
  251. Startflag int32 `json:"startflag"` // 开始日标识 - (-1:上日 0:当日 1:次日 )
  252. Endflag int32 `json:"endflag"` // 结束日标识 - (-1:上日 0:当日 1:次日 )
  253. Start time.Time `json:"start"` // 真实开始时间
  254. End time.Time `json:"end"` // 真实结束时间
  255. }
  256. // QueryTSData 分时图数据查询
  257. // @Summary 分时图数据查询
  258. // @Produce json
  259. // @Security ApiKeyAuth
  260. // @Param goodsCode query string true "商品代码"
  261. // @Success 200 {object} QueryTSDataRsp
  262. // @Failure 500 {object} app.Response
  263. // @Router /Quote/QueryTSData [get]
  264. // @Tags 行情服务
  265. func QueryTSData(c *gin.Context) {
  266. appG := app.Gin{C: c}
  267. // 获取请求参数
  268. var req QueryTSDataReq
  269. if err := appG.C.ShouldBindQuery(&req); err != nil {
  270. logger.GetLogger().Errorf("QueryTSData failed: %s", err.Error())
  271. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  272. return
  273. }
  274. // FIXME: - 一些不常变化的数据(如市场信息、商品信息等)应缓存到Redis中, 或缓存到服务内存
  275. // 获取商品信息
  276. goods, err := models.GetGoodsByGoodsCode(req.GoodsCode)
  277. if goods == nil {
  278. logger.GetLogger().Errorf("QueryTSData failed: %s", err.Error())
  279. appG.Response(http.StatusBadRequest, e.ERROR_GET_GOODS_FAILED, nil)
  280. return
  281. }
  282. // 获取市场
  283. market, err := models.GetMarketByGoodsCode(req.GoodsCode)
  284. if err != nil {
  285. logger.GetLogger().Errorf("QueryTSData failed: %s", err.Error())
  286. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  287. return
  288. }
  289. if market == nil {
  290. logger.GetLogger().Errorf("QueryTSData failed: %s", err.Error())
  291. appG.Response(http.StatusBadRequest, e.ERROR_GET_MARKET_FAILED, nil)
  292. return
  293. }
  294. // 获取目标品种交易日
  295. // FIXME: - 由于mtp2.0目前未同步外部交易所品种的当前交易日,
  296. // 故通道交易的品种目前只能在交易系统的外部市场中获
  297. // 取统一的交易日,后期应要求服务端同步外部数据
  298. marketRun, err := models.GetMarketRun(int(market.Marketid))
  299. if marketRun == nil {
  300. logger.GetLogger().Errorf("QueryTSData failed: %s", err.Error())
  301. appG.Response(http.StatusBadRequest, e.ERROR_GET_MARKETRUN_FAILED, nil)
  302. return
  303. }
  304. // 获取目标品种的开休市计划
  305. var runSteps []map[string]interface{}
  306. // 通道交易外部市场开休市计划表 - QuoteSourceGroupRunStep; 其它市场的 - MarketRunStepDetail
  307. if market.Trademode == 15 {
  308. // 外部市场
  309. sourceRunSteps, err := models.FindQuoteSourceGroupRunSteps(*goods)
  310. if err != nil {
  311. logger.GetLogger().Errorf("QueryTSData failed: %s", err.Error())
  312. appG.Response(http.StatusBadRequest, e.ERROR_GET_RUNSTEP_FAILED, nil)
  313. return
  314. }
  315. for _, v := range sourceRunSteps {
  316. // struct -> json
  317. if jsonBytes, err := json.Marshal(v); err == nil {
  318. // json -> struct
  319. var runStepMap map[string]interface{}
  320. json.Unmarshal(jsonBytes, &runStepMap)
  321. runSteps = append(runSteps, runStepMap)
  322. }
  323. }
  324. }
  325. // 非外部市场或外部市场没有配置QuoteSourceGroupRunStep表数据的情况下,都从MarketRunStepDetail中获取数据
  326. if len(runSteps) == 0 {
  327. sourceRunSteps, err := models.FindMarketRunStepDetails(int(market.Marketid))
  328. if err != nil {
  329. logger.GetLogger().Errorf("QueryTSData failed: %s", err.Error())
  330. appG.Response(http.StatusBadRequest, e.ERROR_GET_RUNSTEP_FAILED, nil)
  331. return
  332. }
  333. for _, v := range sourceRunSteps {
  334. // struct -> json
  335. if jsonBytes, err := json.Marshal(v); err == nil {
  336. // json -> struct
  337. var runStepMap map[string]interface{}
  338. json.Unmarshal(jsonBytes, &runStepMap)
  339. runSteps = append(runSteps, runStepMap)
  340. }
  341. }
  342. }
  343. // 获取目标商品的盘面信息
  344. quoteDays, err := models.GetQuoteDays("'" + goods.Outgoodscode + "'")
  345. if err != nil {
  346. logger.GetLogger().Errorf("QueryTSData failed: %s", err.Error())
  347. appG.Response(http.StatusBadRequest, e.ERROR_GET_GOODS_FAILED, nil)
  348. return
  349. }
  350. var preSettle float64
  351. var preSettleInt int
  352. if len(quoteDays) > 0 {
  353. if quoteDays[0].Presettle != 0 {
  354. preSettleInt = int(quoteDays[0].Presettle)
  355. preSettle = utils.IntToFloat64(preSettleInt, int(goods.Decimalplace))
  356. }
  357. if preSettle == 0 && quoteDays[0].Preclose != 0 {
  358. preSettleInt = int(quoteDays[0].Preclose)
  359. preSettle = utils.IntToFloat64(preSettleInt, int(goods.Decimalplace))
  360. }
  361. }
  362. // 构建返回数据
  363. // 注意:这里使用的是 marketRun.Tradedate2 行情交易日
  364. queryTSDataRsp := QueryTSDataRsp{
  365. GoodsCode: goods.Goodscode,
  366. OutGoodsCode: goods.Outgoodscode,
  367. TradeDate: marketRun.Tradedate2,
  368. DecimalPlace: int(goods.Decimalplace),
  369. PreSettle: preSettle,
  370. }
  371. // 构建分时图可直接使用的开休市数据
  372. // 这里有一个知识点:TRADEWEEKDAY 与 STARTWEEKDAY,以及 TRADEWEEKDAY 与 ENDWEEKDAY 之间相差最多一天(管理端限制),
  373. // 所以目前并不支持正真的周五夜盘模式。我们在实现时不用做得太复杂。
  374. // 当前交易日(周几)对应的开休市计划
  375. tradeDate, err := time.ParseInLocation("20060102", marketRun.Tradedate2, time.Local)
  376. if err != nil {
  377. logger.GetLogger().Errorf("QueryTSData failed: %s", err.Error())
  378. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  379. return
  380. }
  381. // !!!开休市计划明细
  382. curWeekRunSteps := make([]map[string]interface{}, 0)
  383. for _, v := range runSteps {
  384. tradeWeekDay := int(v["tradeweekday"].(float64))
  385. if tradeWeekDay == int(tradeDate.Weekday()) {
  386. curWeekRunSteps = append(curWeekRunSteps, v)
  387. }
  388. }
  389. // 获取不到可用的开休市计划
  390. if len(curWeekRunSteps) == 0 {
  391. logger.GetLogger().Errorf("QueryTSData failed: %s", err.Error())
  392. appG.Response(http.StatusBadRequest, e.ERROR_GET_RUNSTEP_FAILED, nil)
  393. return
  394. }
  395. // 按 SECTIONID 顺序排序
  396. sort.Slice(curWeekRunSteps, func(i int, j int) bool {
  397. return curWeekRunSteps[i]["sectionid"].(float64) < curWeekRunSteps[j]["sectionid"].(float64)
  398. })
  399. // 把各开休市时间段转化为真实的时间
  400. // 关于开休市计划的时间顺序:管理端会按时间顺序添加开休市计划,所以交易日开始时间为第一条记录的开始时间,结束时间为最后一条记录的结束时间
  401. // 关于目标商品的交易日问题:目前只能从商品所属市场获取当前交易日,这样有两个问题,一是不能按常规显示最后一个有历史数据的交易日;二是目前所有外部商品的开休市计划都是一样的。
  402. timeFormat := "20060102 15:04"
  403. // 开始时间
  404. startInterval := getTradeDay(int(curWeekRunSteps[0]["tradeweekday"].(float64)), int(curWeekRunSteps[0]["startweekday"].(float64)))
  405. queryTSDataRsp.StartTime, _ = time.ParseInLocation(timeFormat, fmt.Sprintf("%s %s", marketRun.Tradedate2, curWeekRunSteps[0]["starttime"].(string)), time.Local)
  406. if startInterval != 0 {
  407. duration, _ := time.ParseDuration(fmt.Sprintf("%dh", startInterval*24))
  408. queryTSDataRsp.StartTime = queryTSDataRsp.StartTime.Add(duration)
  409. }
  410. // 结束时间
  411. index := len(curWeekRunSteps) - 1
  412. endInterval := getTradeDay(int(curWeekRunSteps[index]["tradeweekday"].(float64)), int(curWeekRunSteps[index]["endweekday"].(float64)))
  413. queryTSDataRsp.EndTime, _ = time.ParseInLocation(timeFormat, fmt.Sprintf("%s %s", marketRun.Tradedate2, curWeekRunSteps[index]["endtime"].(string)), time.Local)
  414. if endInterval != 0 {
  415. duration, _ := time.ParseDuration(fmt.Sprintf("%dh", endInterval*24))
  416. queryTSDataRsp.EndTime = queryTSDataRsp.EndTime.Add(duration)
  417. }
  418. // fmt.Printf("开始时间:%s 结束时间:%s\n", queryTSDataRsp.StartTime.Format(timeFormat), queryTSDataRsp.EndTime.Format(timeFormat))
  419. // 获取目标时间段的历史数据(1分钟周期)
  420. // 这里要注意:由于交易库和行情库由于GoodsCode大小写不一定对得上,所以在使用交易库的商品查询行情数据时间,都要使用OutGoodsCode字段
  421. cycleDatas, err := models.GetHistoryCycleDatas(models.CycleTypeMinutes1, queryTSDataRsp.OutGoodsCode, &queryTSDataRsp.StartTime, &queryTSDataRsp.EndTime, 0, true)
  422. if err != nil {
  423. logger.GetLogger().Errorf("QueryTSData failed: %s", err.Error())
  424. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  425. return
  426. }
  427. // ==================== 补数据(补休市数据和缺少的数据)====================
  428. if len(cycleDatas) > 0 {
  429. // 补数据第一步:如果第一数据不是开始时间的,需要补数据
  430. sources := time.Unix(int64(cycleDatas[0].ST), 0)
  431. diff := sources.Sub(queryTSDataRsp.StartTime)
  432. if diff.Minutes() > 0 {
  433. minute := int(diff.Minutes())
  434. for i := 1; i <= minute; i++ {
  435. st := cycleDatas[0].ST - i*60
  436. stt := time.Unix(int64(st), 0).Format("2006-01-02 15:04:05")
  437. cycleDatas = append(cycleDatas, models.CycleData{
  438. GC: cycleDatas[0].GC,
  439. Open: cycleDatas[0].Close,
  440. High: cycleDatas[0].Close,
  441. Low: cycleDatas[0].Close,
  442. Close: cycleDatas[0].Close,
  443. TV: 0,
  444. TT: 0,
  445. HV: 0,
  446. SP: 0,
  447. ST: st,
  448. SST: stt,
  449. FI: true,
  450. })
  451. }
  452. }
  453. // 接时间戳排序
  454. sort.Slice(cycleDatas, func(i int, j int) bool {
  455. return cycleDatas[i].ST < cycleDatas[j].ST
  456. })
  457. // 补数据第二步:按尾部的时间(当前服务器时间或最后休市时间)进行全补
  458. // 获取服务器时间
  459. s, _ := models.GetServerTime()
  460. endTime, err := time.ParseInLocation("2006-01-02T15:04:05Z", *s, time.Local)
  461. if err != nil {
  462. logger.GetLogger().Errorf("QueryTSData failed: %s", err.Error())
  463. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  464. return
  465. }
  466. if endTime.After(queryTSDataRsp.EndTime) {
  467. endTime = queryTSDataRsp.EndTime
  468. }
  469. index := len(cycleDatas) - 1
  470. sources = time.Unix(int64(cycleDatas[index].ST), 0)
  471. diff = endTime.Sub(sources)
  472. if diff.Minutes() > 0 {
  473. minute := int(diff.Minutes())
  474. for i := 1; i <= minute; i++ {
  475. st := cycleDatas[index].ST + i*60
  476. stt := time.Unix(int64(st), 0).Format("2006-01-02 15:04:05")
  477. cycleDatas = append(cycleDatas, models.CycleData{
  478. GC: cycleDatas[index].GC,
  479. Open: cycleDatas[index].Close,
  480. High: cycleDatas[index].Close,
  481. Low: cycleDatas[index].Close,
  482. Close: cycleDatas[index].Close,
  483. TV: 0,
  484. TT: 0,
  485. HV: 0,
  486. SP: 0,
  487. ST: st,
  488. SST: stt,
  489. FI: true,
  490. })
  491. }
  492. }
  493. // 接时间戳排序
  494. sort.Slice(cycleDatas, func(i int, j int) bool {
  495. return cycleDatas[i].ST < cycleDatas[j].ST
  496. })
  497. // 补数据第三步:补中间数据
  498. fillDatas := make([]models.CycleData, 0)
  499. for i := range cycleDatas {
  500. // 第一条记录跳过
  501. if i == 0 {
  502. continue
  503. }
  504. current := time.Unix(int64(cycleDatas[i].ST), 0)
  505. prev := time.Unix(int64(cycleDatas[i-1].ST), 0)
  506. diff := current.Sub(prev)
  507. if diff.Minutes() > 1 {
  508. minute := int(diff.Minutes())
  509. // 判断是否需要补数据,与上一条数据的间距不是一分钟
  510. if minute > 1 {
  511. for j := 1; j <= minute; j++ {
  512. st := cycleDatas[i-1].ST + j*60
  513. stt := time.Unix(int64(st), 0).Format("2006-01-02 15:04:05")
  514. fillDatas = append(fillDatas, models.CycleData{
  515. GC: cycleDatas[i-1].GC,
  516. Open: cycleDatas[i-1].Close,
  517. High: cycleDatas[i-1].Close,
  518. Low: cycleDatas[i-1].Close,
  519. Close: cycleDatas[i-1].Close,
  520. TV: 0,
  521. TT: 0,
  522. HV: 0,
  523. SP: 0,
  524. ST: st,
  525. SST: stt,
  526. FI: true,
  527. })
  528. }
  529. }
  530. }
  531. }
  532. // 加入到源数据
  533. cycleDatas = append(cycleDatas, fillDatas...)
  534. // 接时间戳排序
  535. sort.Slice(cycleDatas, func(i int, j int) bool {
  536. return cycleDatas[i].ST < cycleDatas[j].ST
  537. })
  538. } else {
  539. // TODO: - 下面这块操作需求确认
  540. // 如果查询结果是空数据,则使用昨结价补到服务器时间(或最后休市时间)
  541. // 获取服务器时间
  542. s, _ := models.GetServerTime()
  543. endTime, err := time.ParseInLocation("2006-01-02T15:04:05Z", *s, time.Local)
  544. if err != nil {
  545. logger.GetLogger().Errorf("QueryTSData failed: %s", err.Error())
  546. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  547. return
  548. }
  549. if endTime.After(queryTSDataRsp.EndTime) {
  550. endTime = queryTSDataRsp.EndTime
  551. }
  552. diff := endTime.Sub(queryTSDataRsp.StartTime)
  553. minute := int(diff.Minutes())
  554. for i := 1; i <= minute; i++ {
  555. st := int(queryTSDataRsp.StartTime.Unix()) + i*60
  556. stt := time.Unix(int64(st), 0).Format("2006-01-02 15:04:05")
  557. cycleDatas = append(cycleDatas, models.CycleData{
  558. GC: queryTSDataRsp.GoodsCode,
  559. Open: preSettleInt,
  560. High: preSettleInt,
  561. Low: preSettleInt,
  562. Close: preSettleInt,
  563. TV: 0,
  564. TT: 0,
  565. HV: 0,
  566. SP: 0,
  567. ST: st,
  568. SST: stt,
  569. FI: true,
  570. })
  571. }
  572. // 接时间戳排序
  573. sort.Slice(cycleDatas, func(i int, j int) bool {
  574. return cycleDatas[i].ST < cycleDatas[j].ST
  575. })
  576. }
  577. // 补数据第四步:清除掉开市计划外的数据
  578. // 先计算出每条计划明细的真正开始与结束时间
  579. queryTSDataRsp.RunSteps = make([]TSDataRunStep, 0)
  580. for _, v := range curWeekRunSteps {
  581. // 开始时间
  582. startInterval := getTradeDay(int(v["tradeweekday"].(float64)), int(v["startweekday"].(float64)))
  583. v["start"], _ = time.ParseInLocation(timeFormat, fmt.Sprintf("%s %s", marketRun.Tradedate2, v["starttime"].(string)), time.Local)
  584. if startInterval != 0 {
  585. duration, _ := time.ParseDuration(fmt.Sprintf("%dh", startInterval*24))
  586. v["start"] = v["start"].(time.Time).Add(duration)
  587. }
  588. // 结束时间
  589. endInterval := getTradeDay(int(v["tradeweekday"].(float64)), int(v["endweekday"].(float64)))
  590. v["end"], _ = time.ParseInLocation(timeFormat, fmt.Sprintf("%s %s", marketRun.Tradedate2, v["endtime"].(string)), time.Local)
  591. if endInterval != 0 {
  592. duration, _ := time.ParseDuration(fmt.Sprintf("%dh", endInterval*24))
  593. v["end"] = v["end"].(time.Time).Add(duration)
  594. }
  595. // map -> struct
  596. var tsDataRunStep TSDataRunStep
  597. jsonbody, err := json.Marshal(v)
  598. if err != nil {
  599. continue
  600. }
  601. if err := json.Unmarshal(jsonbody, &tsDataRunStep); err != nil {
  602. continue
  603. }
  604. queryTSDataRsp.RunSteps = append(queryTSDataRsp.RunSteps, tsDataRunStep)
  605. }
  606. // 最终返回的历史数据
  607. historyDatas := make([]HistoryData, 0)
  608. for _, cycleData := range cycleDatas {
  609. needAdd := false
  610. for _, runStep := range curWeekRunSteps {
  611. // 判断是否在开市计划内
  612. if cycleData.ST >= int(runStep["start"].(time.Time).Unix()) && cycleData.ST <= int(runStep["end"].(time.Time).Unix()) {
  613. needAdd = true
  614. break
  615. }
  616. }
  617. if needAdd {
  618. historyDatas = append(historyDatas, HistoryData{
  619. Opened: utils.IntToFloat64(cycleData.Open, int(goods.Decimalplace)),
  620. Highest: utils.IntToFloat64(cycleData.High, int(goods.Decimalplace)),
  621. Lowest: utils.IntToFloat64(cycleData.Low, int(goods.Decimalplace)),
  622. Closed: utils.IntToFloat64(cycleData.Close, int(goods.Decimalplace)),
  623. TotleVolume: cycleData.TV,
  624. TotleTurnover: float64(cycleData.TT),
  625. HoldVolume: cycleData.HV,
  626. Settle: utils.IntToFloat64(cycleData.SP, int(goods.Decimalplace)),
  627. TimeStamp: time.Unix(int64(cycleData.ST), 0),
  628. IsFill: cycleData.FI,
  629. })
  630. }
  631. }
  632. queryTSDataRsp.HistoryDatas = historyDatas
  633. // 计算当前交易日应该有多少个历史数据,主要用于iOS
  634. for _, v := range queryTSDataRsp.RunSteps {
  635. diff := v.End.Sub(v.Start)
  636. queryTSDataRsp.Count += int(diff.Minutes())
  637. }
  638. // 查询成功
  639. logger.GetLogger().Debugln("QueryTSData successed: %v", queryTSDataRsp)
  640. appG.Response(http.StatusOK, e.SUCCESS, queryTSDataRsp)
  641. }
  642. // getTradeDay 获取结算计划中天数间隔的方法
  643. // - tradeDay: 交易日周几
  644. // - weekDay: 开始或结束周几
  645. // - Returns: 天数间隔
  646. func getTradeDay(tradeDay, weekDay int) int {
  647. if tradeDay == weekDay {
  648. return 0
  649. }
  650. if weekDay == 0 {
  651. weekDay = 7
  652. }
  653. betWeekDay := weekDay - tradeDay
  654. if betWeekDay < 0 {
  655. betWeekDay = betWeekDay + 7
  656. }
  657. if betWeekDay >= 4 {
  658. betWeekDay = betWeekDay - 7
  659. }
  660. return betWeekDay
  661. }