瀏覽代碼

QueryHistoryDatas接口当cycleType >= 11时会自动加上当前盘面周期

zhou.xiaoning 3 年之前
父節點
當前提交
5bc53ecc32
共有 4 個文件被更改,包括 180 次插入39 次删除
  1. 143 1
      controllers/quote/history.go
  2. 16 20
      docs/docs.go
  3. 11 9
      docs/swagger.json
  4. 10 9
      docs/swagger.yaml

+ 143 - 1
controllers/quote/history.go

@@ -45,7 +45,7 @@ type QueryHistoryDatasReq struct {
 // @Summary 查询行情历史数据
 // @Produce json
 // @Security ApiKeyAuth
-// @Param cycleType query int true "周期类型, 0-秒 1: 1分钟 2: 5分钟 3: 30分钟 4: 60分钟 120: 2小时 240: 4小时 11: 日线"
+// @Param cycleType query int true "周期类型, 0-秒 1: 1分钟 2: 5分钟 3: 30分钟 4: 60分钟 120: 2小时 240: 4小时 11: 日线 12:周线 13:月线 14:年线"
 // @Param goodsCode query string true "商品代码"
 // @Param startTime query string false "开始时间,格式:yyyy-MM-dd HH:mm:ss"
 // @Param endTime query string false "结束时间,格式:yyyy-MM-dd HH:mm:ss"
@@ -120,6 +120,113 @@ func QueryHistoryDatas(c *gin.Context) {
 		rst = append(rst, historyData)
 	}
 
+	// 包括日线以上的周期,如果没有带当前周期则通过盘面数据生成当前周期数据
+	if req.CycleType >= 11 {
+		// 获取商品信息
+		goods, err := models.GetGoodsByGoodsCode(req.GoodsCode)
+		if goods == nil {
+			logger.GetLogger().Errorf("QueryHistoryDatas failed: %s", err.Error())
+			appG.Response(http.StatusBadRequest, e.ERROR_GET_GOODS_FAILED, nil)
+			return
+		}
+		// 获取市场
+		market, err := models.GetMarketByGoodsCode(req.GoodsCode)
+		if err != nil {
+			logger.GetLogger().Errorf("QueryHistoryDatas failed: %s", err.Error())
+			appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
+			return
+		}
+		if market == nil {
+			logger.GetLogger().Errorf("QueryHistoryDatas failed: %s", err.Error())
+			appG.Response(http.StatusBadRequest, e.ERROR_GET_MARKET_FAILED, nil)
+			return
+		}
+
+		// 获取目标品种交易日
+		// FIXME: - 由于mtp2.0目前未同步外部交易所品种的当前交易日,
+		//          故通道交易的品种目前只能在交易系统的外部市场中获
+		//          取统一的交易日,后期应要求服务端同步外部数据
+		// 2021.06.30 业务更新:15、97和99市场的行情交易日要从商品盘面中获取
+		quoteTradeDate := ""
+		if market.Trademode == 15 || market.Trademode == 97 || market.Trademode == 99 {
+			if quoteDays, err := models.GetQuoteDays("'" + req.GoodsCode + "'"); err == nil {
+				if len(quoteDays) > 0 {
+					quoteTradeDate = time.Unix(quoteDays[0].Exchangedate, 0).Format("20060102")
+				}
+			}
+		} else {
+			if marketRun, err := models.GetMarketRun(int(market.Marketid)); err == nil {
+				quoteTradeDate = marketRun.Tradedate2
+			}
+		}
+		if quoteTradeDate != "" {
+			if tradeDate, err := time.ParseInLocation("20060102", quoteTradeDate, time.Local); err == nil {
+				var fisrtDate time.Time
+				switch req.CycleType {
+				case 12: // 周
+					fisrtDate = getFirstDateOfWeek(tradeDate)
+				case 13: // 月
+					fisrtDate = getFirstDateOfMonth(tradeDate)
+				case 14: // 年
+					fisrtDate = getFirstDateOfYear(tradeDate)
+				default: // 日
+					fisrtDate = tradeDate
+				}
+
+				// 获取盘面数据
+				if quoteDays, err := models.GetQuoteDays(req.GoodsCode); err == nil && len(quoteDays) > 0 {
+					v := quoteDays[0]
+					if len(rst) == 0 {
+						// 历史数据当前没数据则直接加当前盘面数据
+						historyData := HistoryData{
+							Opened:        utils.IntToFloat64(int(v.Last), dcplace),
+							Highest:       utils.IntToFloat64(int(v.Last), dcplace),
+							Lowest:        utils.IntToFloat64(int(v.Last), dcplace),
+							Closed:        utils.IntToFloat64(int(v.Last), dcplace),
+							TotleVolume:   int(v.Totalvolume),
+							TotleTurnover: float64(v.Totalturnover),
+							HoldVolume:    int(v.Holdvolume),
+							Settle:        utils.IntToFloat64(int(v.Settle), dcplace),
+							TimeStamp:     fisrtDate,
+						}
+
+						if req.IsAsc {
+							rst = append(rst, historyData)
+						} else {
+							// 插入第一条
+							rear := append([]HistoryData{}, rst[0:]...)
+							rst = append(append(rst[:0], historyData), rear...)
+						}
+					} else {
+						// 判断最后周期是否已经存在盘面周期
+						hd := rst[len(rst)-1]
+						if hd.TimeStamp.Before(fisrtDate) {
+							historyData := HistoryData{
+								Opened:        utils.IntToFloat64(int(v.Last), dcplace),
+								Highest:       utils.IntToFloat64(int(v.Last), dcplace),
+								Lowest:        utils.IntToFloat64(int(v.Last), dcplace),
+								Closed:        utils.IntToFloat64(int(v.Last), dcplace),
+								TotleVolume:   int(v.Totalvolume),
+								TotleTurnover: float64(v.Totalturnover),
+								HoldVolume:    int(v.Holdvolume),
+								Settle:        utils.IntToFloat64(int(v.Settle), dcplace),
+								TimeStamp:     fisrtDate,
+							}
+
+							if req.IsAsc {
+								rst = append(rst, historyData)
+							} else {
+								// 插入第一条
+								rear := append([]HistoryData{}, rst[0:]...)
+								rst = append(append(rst[:0], historyData), rear...)
+							}
+						}
+					}
+				}
+			}
+		}
+	}
+
 	// 查询成功
 	logger.GetLogger().Debugln("QueryHistoryDatas successed, rows:%v", len(rst))
 	appG.Response(http.StatusOK, e.SUCCESS, rst)
@@ -718,3 +825,38 @@ func getTradeDay(tradeDay, weekDay int) int {
 
 	return betWeekDay
 }
+
+/**
+获取某日所在周周一的日期
+*/
+func getFirstDateOfWeek(d time.Time) time.Time {
+	offset := int(time.Monday - d.Weekday())
+	if offset > 0 {
+		offset = -6
+	}
+
+	weekStartDate := time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
+	return weekStartDate
+}
+
+//获取传入的时间所在月份的第一天,即某月第一天的0点。如传入time.Now(), 返回当前月份的第一天0点时间。
+func getFirstDateOfMonth(d time.Time) time.Time {
+	d = d.AddDate(0, 0, -d.Day()+1)
+	return getZeroTime(d)
+}
+
+//获取传入的时间所在年的第一天,即某年第一天的0点。如传入time.Now(), 返回当前年的第一天0点时间。
+func getFirstDateOfYear(d time.Time) time.Time {
+	d = d.AddDate(0, -d.Year()+1, 0)
+	return getZeroTime(d)
+}
+
+//获取传入的时间所在月份的最后一天,即某月最后一天的0点。如传入time.Now(), 返回当前月份的最后一天0点时间。
+func getLastDateOfMonth(d time.Time) time.Time {
+	return getFirstDateOfMonth(d).AddDate(0, 1, -1)
+}
+
+//获取某一天的0点时间
+func getZeroTime(d time.Time) time.Time {
+	return time.Date(d.Year(), d.Month(), d.Day(), 0, 0, 0, 0, d.Location())
+}

+ 16 - 20
docs/docs.go

@@ -1,13 +1,14 @@
-// Package docs GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
+// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT
 // This file was generated by swaggo/swag
+
 package docs
 
 import (
 	"bytes"
 	"encoding/json"
 	"strings"
-	"text/template"
 
+	"github.com/alecthomas/template"
 	"github.com/swaggo/swag"
 )
 
@@ -15,7 +16,7 @@ var doc = `{
     "schemes": {{ marshal .Schemes }},
     "swagger": "2.0",
     "info": {
-        "description": "{{escape .Description}}",
+        "description": "{{.Description}}",
         "title": "{{.Title}}",
         "termsOfService": "http://www.muchinfo.cn",
         "contact": {},
@@ -10430,7 +10431,7 @@ var doc = `{
                 "parameters": [
                     {
                         "type": "integer",
-                        "description": "周期类型, 0-秒 1: 1分钟 2: 5分钟 3: 30分钟 4: 60分钟 120: 2小时 240: 4小时 11: 日线",
+                        "description": "周期类型, 0-秒 1: 1分钟 2: 5分钟 3: 30分钟 4: 60分钟 120: 2小时 240: 4小时 11: 日线 12:周线 13:月线 14:年线",
                         "name": "cycleType",
                         "in": "query",
                         "required": true
@@ -14332,7 +14333,9 @@ var doc = `{
                 "code": {
                     "type": "integer"
                 },
-                "data": {},
+                "data": {
+                    "type": "object"
+                },
                 "msg": {
                     "type": "string"
                 },
@@ -17412,31 +17415,31 @@ var doc = `{
                     "type": "number"
                 },
                 "diffArbitrageQty": {
-                    "description": "期现数量差 = 套保计划量 - 今日期货关联成交量",
+                    "description": "套利变动量",
                     "type": "number"
                 },
                 "diffExposoureQty": {
-                    "description": "期现数量差 = 套保计划量 - 今日期货关联成交量",
+                    "description": "变动量(套保敞口)",
                     "type": "number"
                 },
                 "diffFutuQty": {
-                    "description": "期现数量差 = 套保计划量 - 今日期货关联成交量",
+                    "description": "变动量(期货总量) 平安:保值净持仓量今日变动",
                     "type": "number"
                 },
                 "diffHedgeQty": {
-                    "description": "期现数量差 = 套保计划量 - 今日期货关联成交量",
+                    "description": "套保变动量",
                     "type": "number"
                 },
                 "diffQty": {
-                    "description": "期现数量差 = 套保计划量 - 今日期货关联成交量",
+                    "description": "变动量(总敞口) 平安:净敞口今日变动",
                     "type": "number"
                 },
                 "diffSpotHedgeQty": {
-                    "description": "期现数量差 = 套保计划量 - 今日期货关联成交量",
+                    "description": "变动量(现货应套保总量) 平安:应套保量今日变动",
                     "type": "number"
                 },
                 "diffSpotQty": {
-                    "description": "期现数量差 = 套保计划量 - 今日期货关联成交量",
+                    "description": "变动量(现货总量) = 现货数量 - 期初现货数量 平安:采销定价净值今日变动",
                     "type": "number"
                 },
                 "enumdicname": {
@@ -40451,13 +40454,6 @@ func (s *s) ReadDoc() string {
 			a, _ := json.Marshal(v)
 			return string(a)
 		},
-		"escape": func(v interface{}) string {
-			// escape tabs
-			str := strings.Replace(v.(string), "\t", "\\t", -1)
-			// replace " with \", and if that results in \\", replace that with \\\"
-			str = strings.Replace(str, "\"", "\\\"", -1)
-			return strings.Replace(str, "\\\\\"", "\\\\\\\"", -1)
-		},
 	}).Parse(doc)
 	if err != nil {
 		return doc
@@ -40472,5 +40468,5 @@ func (s *s) ReadDoc() string {
 }
 
 func init() {
-	swag.Register("swagger", &s{})
+	swag.Register(swag.Name, &s{})
 }

+ 11 - 9
docs/swagger.json

@@ -10415,7 +10415,7 @@
                 "parameters": [
                     {
                         "type": "integer",
-                        "description": "周期类型, 0-秒 1: 1分钟 2: 5分钟 3: 30分钟 4: 60分钟 120: 2小时 240: 4小时 11: 日线",
+                        "description": "周期类型, 0-秒 1: 1分钟 2: 5分钟 3: 30分钟 4: 60分钟 120: 2小时 240: 4小时 11: 日线 12:周线 13:月线 14:年线",
                         "name": "cycleType",
                         "in": "query",
                         "required": true
@@ -14317,7 +14317,9 @@
                 "code": {
                     "type": "integer"
                 },
-                "data": {},
+                "data": {
+                    "type": "object"
+                },
                 "msg": {
                     "type": "string"
                 },
@@ -17397,31 +17399,31 @@
                     "type": "number"
                 },
                 "diffArbitrageQty": {
-                    "description": "期现数量差 = 套保计划量 - 今日期货关联成交量",
+                    "description": "套利变动量",
                     "type": "number"
                 },
                 "diffExposoureQty": {
-                    "description": "期现数量差 = 套保计划量 - 今日期货关联成交量",
+                    "description": "变动量(套保敞口)",
                     "type": "number"
                 },
                 "diffFutuQty": {
-                    "description": "期现数量差 = 套保计划量 - 今日期货关联成交量",
+                    "description": "变动量(期货总量) 平安:保值净持仓量今日变动",
                     "type": "number"
                 },
                 "diffHedgeQty": {
-                    "description": "期现数量差 = 套保计划量 - 今日期货关联成交量",
+                    "description": "套保变动量",
                     "type": "number"
                 },
                 "diffQty": {
-                    "description": "期现数量差 = 套保计划量 - 今日期货关联成交量",
+                    "description": "变动量(总敞口) 平安:净敞口今日变动",
                     "type": "number"
                 },
                 "diffSpotHedgeQty": {
-                    "description": "期现数量差 = 套保计划量 - 今日期货关联成交量",
+                    "description": "变动量(现货应套保总量) 平安:应套保量今日变动",
                     "type": "number"
                 },
                 "diffSpotQty": {
-                    "description": "期现数量差 = 套保计划量 - 今日期货关联成交量",
+                    "description": "变动量(现货总量) = 现货数量 - 期初现货数量 平安:采销定价净值今日变动",
                     "type": "number"
                 },
                 "enumdicname": {

+ 10 - 9
docs/swagger.yaml

@@ -4,7 +4,8 @@ definitions:
     properties:
       code:
         type: integer
-      data: {}
+      data:
+        type: object
       msg:
         type: string
       page:
@@ -2295,25 +2296,25 @@ definitions:
         description: 套利量
         type: number
       diffArbitrageQty:
-        description: 期现数量差 = 套保计划量 - 今日期货关联成交
+        description: 套利变动
         type: number
       diffExposoureQty:
-        description: 期现数量差 = 套保计划量 - 今日期货关联成交量
+        description: 变动量(套保敞口)
         type: number
       diffFutuQty:
-        description: 期现数量差 = 套保计划量 - 今日期货关联成交量
+        description: 变动量(期货总量) 平安:保值净持仓量今日变动
         type: number
       diffHedgeQty:
-        description: 期现数量差 = 套保计划量 - 今日期货关联成交
+        description: 套保变动
         type: number
       diffQty:
-        description: 期现数量差 = 套保计划量 - 今日期货关联成交量
+        description: 变动量(总敞口) 平安:净敞口今日变动
         type: number
       diffSpotHedgeQty:
-        description: 期现数量差 = 套保计划量 - 今日期货关联成交量
+        description: 变动量(现货应套保总量) 平安:应套保量今日变动
         type: number
       diffSpotQty:
-        description: 期现数量差 = 套保计划量 - 今日期货关联成交量
+        description: 变动量(现货总量) = 现货数量 - 期初现货数量 平安:采销定价净值今日变动
         type: number
       enumdicname:
         description: 单位名称
@@ -26074,7 +26075,7 @@ paths:
     get:
       parameters:
       - description: '周期类型, 0-秒 1: 1分钟 2: 5分钟 3: 30分钟 4: 60分钟 120: 2小时 240: 4小时 11:
-          日线'
+          日线 12:周线 13:月线 14:年线'
         in: query
         name: cycleType
         required: true