瀏覽代碼

增加“查询行情历史数据”接口

zhou.xiaoning 5 年之前
父節點
當前提交
ce6530113f
共有 8 個文件被更改,包括 511 次插入109 次删除
  1. 73 2
      controllers/quote/history.go
  2. 149 34
      docs/docs.go
  3. 149 34
      docs/swagger.json
  4. 101 22
      docs/swagger.yaml
  5. 1 0
      global/e/code.go
  6. 1 0
      global/e/msg.go
  7. 29 17
      models/quote.go
  8. 8 0
      routers/router.go

+ 73 - 2
controllers/quote/history.go

@@ -26,6 +26,77 @@ type HistoryData struct {
 	TimeStamp     time.Time `json:"timeStamp"`     // 开盘时间
 }
 
+// QueryHistoryDatasReq 查询行情历史数据请求参数
+type QueryHistoryDatasReq struct {
+	CycleType int    `form:"cycleType" binding:"required"`
+	GoodsCode string `form:"goodsCode"  binding:"required"`
+	StartTime string `form:"startTime"`
+	EndTime   string `form:"endTime"`
+	Count     int    `form:"count"`
+	IsAsc     bool   `form:"isAsc"`
+}
+
+// QueryHistoryDatas 查询行情历史数据
+// @Summary 查询行情历史数据
+// @Produce json
+// @Security ApiKeyAuth
+// @Param cycleType query int true "周期类型, 0-秒 1: 1分钟 2: 5分钟 3: 30分钟 4: 60分钟 120: 2小时 240: 4小时 11: 日线 10: Tik"
+// @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"
+// @Param count query int false "条数"
+// @Param IsAsc query bool false "是否按时间顺序排序(默认为时间倒序排序)"
+// @Success 200 {object} models.CycleData
+// @Failure 500 {object} app.Response
+// @Router /Quote/QueryHistoryDatas [get]
+// @Tags 行情服务
+func QueryHistoryDatas(c *gin.Context) {
+	appG := app.Gin{C: c}
+
+	// 获取请求参数
+	var req QueryHistoryDatasReq
+	if err := appG.C.ShouldBindQuery(&req); err != nil {
+		logger.GetLogger().Errorf("QueryHistoryDatas failed: %s", err.Error())
+		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
+		return
+	}
+
+	// 转换时间
+	timeFormat := "2006-01-02 15:04:05" // go中的时间格式化必须是这个时间
+	var startTime *time.Time
+	if len(req.StartTime) > 0 {
+		st, err := time.ParseInLocation(timeFormat, req.StartTime, time.Local)
+		if err != nil {
+			logger.GetLogger().Errorf("QueryHistoryDatas failed: %s", err.Error())
+			appG.Response(http.StatusBadRequest, e.ERROR_QUERY_TIME_FORMAT_FAIL, nil)
+			return
+		}
+		startTime = &st
+	}
+	var endTime *time.Time
+	if len(req.EndTime) > 0 {
+		et, err := time.ParseInLocation(timeFormat, req.EndTime, time.Local)
+		if err != nil {
+			logger.GetLogger().Errorf("QueryHistoryDatas failed: %s", err.Error())
+			appG.Response(http.StatusBadRequest, e.ERROR_QUERY_TIME_FORMAT_FAIL, nil)
+			return
+		}
+		endTime = &et
+	}
+
+	// 查询数据
+	cycleData, err := models.GetHistoryCycleDatas(models.CycleType(req.CycleType), req.GoodsCode, startTime, endTime, req.Count, req.IsAsc)
+	if err != nil {
+		logger.GetLogger().Errorf("QueryTSData failed: %s", err.Error())
+		appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
+		return
+	}
+
+	// 查询成功
+	logger.GetLogger().Debugln("QueryHistoryDatas successed: %v", cycleData)
+	appG.Response(http.StatusOK, e.SUCCESS, cycleData)
+}
+
 // QueryTSDataReq 分时图数据查询请求参数
 type QueryTSDataReq struct {
 	GoodsCode string `form:"goodsCode" binding:"required"` // 商品代码
@@ -48,8 +119,8 @@ type QueryTSDataRsp struct {
 // @Param GoodsCode query string true "商品代码"
 // @Success 200 {object} QueryTSDataRsp
 // @Failure 500 {object} app.Response
-// @Router /History/QueryTSData [get]
-// @Tags 通用服务
+// @Router /Quote/QueryTSData [get]
+// @Tags 行情服务
 func QueryTSData(c *gin.Context) {
 	appG := app.Gin{C: c}
 

+ 149 - 34
docs/docs.go

@@ -1208,40 +1208,6 @@ var doc = `{
                 }
             }
         },
-        "/History/QueryTSData": {
-            "get": {
-                "produces": [
-                    "application/json"
-                ],
-                "tags": [
-                    "通用服务"
-                ],
-                "summary": "分时图数据查询",
-                "parameters": [
-                    {
-                        "type": "string",
-                        "description": "商品代码",
-                        "name": "GoodsCode",
-                        "in": "query",
-                        "required": true
-                    }
-                ],
-                "responses": {
-                    "200": {
-                        "description": "OK",
-                        "schema": {
-                            "$ref": "#/definitions/quote.QueryTSDataRsp"
-                        }
-                    },
-                    "500": {
-                        "description": "Internal Server Error",
-                        "schema": {
-                            "$ref": "#/definitions/app.Response"
-                        }
-                    }
-                }
-            }
-        },
         "/Order/QueryHisTradeDetail": {
             "get": {
                 "security": [
@@ -1563,6 +1529,110 @@ var doc = `{
                 }
             }
         },
+        "/Quote/QueryHistoryDatas": {
+            "get": {
+                "security": [
+                    {
+                        "ApiKeyAuth": []
+                    }
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "行情服务"
+                ],
+                "summary": "查询行情历史数据",
+                "parameters": [
+                    {
+                        "type": "integer",
+                        "description": "周期类型, 0: 秒 1: 1分钟 2: 5分钟 3: 30分钟 4: 60分钟 120: 2小时 240: 4小时 11: 日线 10: Tik",
+                        "name": "cycleType",
+                        "in": "query",
+                        "required": true
+                    },
+                    {
+                        "type": "string",
+                        "description": "商品代码",
+                        "name": "goodsCode",
+                        "in": "query",
+                        "required": true
+                    },
+                    {
+                        "type": "string",
+                        "description": "开始时间,格式:yyyy-MM-dd HH:mm:ss",
+                        "name": "startTime",
+                        "in": "query"
+                    },
+                    {
+                        "type": "string",
+                        "description": "结束时间,格式:yyyy-MM-dd HH:mm:ss",
+                        "name": "endTime",
+                        "in": "query"
+                    },
+                    {
+                        "type": "integer",
+                        "description": "条数",
+                        "name": "count",
+                        "in": "query"
+                    },
+                    {
+                        "type": "boolean",
+                        "description": "是否按时间顺序排序(默认为时间倒序排序)",
+                        "name": "IsAsc",
+                        "in": "query"
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "OK",
+                        "schema": {
+                            "$ref": "#/definitions/models.CycleData"
+                        }
+                    },
+                    "500": {
+                        "description": "Internal Server Error",
+                        "schema": {
+                            "$ref": "#/definitions/app.Response"
+                        }
+                    }
+                }
+            }
+        },
+        "/Quote/QueryTSData": {
+            "get": {
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "行情服务"
+                ],
+                "summary": "分时图数据查询",
+                "parameters": [
+                    {
+                        "type": "string",
+                        "description": "商品代码",
+                        "name": "GoodsCode",
+                        "in": "query",
+                        "required": true
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "OK",
+                        "schema": {
+                            "$ref": "#/definitions/quote.QueryTSDataRsp"
+                        }
+                    },
+                    "500": {
+                        "description": "Internal Server Error",
+                        "schema": {
+                            "$ref": "#/definitions/app.Response"
+                        }
+                    }
+                }
+            }
+        },
         "/SZDZ/QueryConvertConfig": {
             "get": {
                 "security": [
@@ -3766,6 +3836,51 @@ var doc = `{
                 }
             }
         },
+        "models.CycleData": {
+            "type": "object",
+            "properties": {
+                "close": {
+                    "description": "收盘价",
+                    "type": "integer"
+                },
+                "gc": {
+                    "description": "商品代码",
+                    "type": "string"
+                },
+                "high": {
+                    "description": "最高价",
+                    "type": "integer"
+                },
+                "hv": {
+                    "description": "持仓量",
+                    "type": "integer"
+                },
+                "low": {
+                    "description": "最低价",
+                    "type": "integer"
+                },
+                "open": {
+                    "description": "开盘价",
+                    "type": "integer"
+                },
+                "sp": {
+                    "description": "结算价,日线周期(包括)以上才有",
+                    "type": "integer"
+                },
+                "sst": {
+                    "description": "时间文本",
+                    "type": "string"
+                },
+                "tt": {
+                    "description": "总金额",
+                    "type": "integer"
+                },
+                "tv": {
+                    "description": "总量",
+                    "type": "integer"
+                }
+            }
+        },
         "models.Division": {
             "type": "object",
             "required": [

+ 149 - 34
docs/swagger.json

@@ -1192,40 +1192,6 @@
                 }
             }
         },
-        "/History/QueryTSData": {
-            "get": {
-                "produces": [
-                    "application/json"
-                ],
-                "tags": [
-                    "通用服务"
-                ],
-                "summary": "分时图数据查询",
-                "parameters": [
-                    {
-                        "type": "string",
-                        "description": "商品代码",
-                        "name": "GoodsCode",
-                        "in": "query",
-                        "required": true
-                    }
-                ],
-                "responses": {
-                    "200": {
-                        "description": "OK",
-                        "schema": {
-                            "$ref": "#/definitions/quote.QueryTSDataRsp"
-                        }
-                    },
-                    "500": {
-                        "description": "Internal Server Error",
-                        "schema": {
-                            "$ref": "#/definitions/app.Response"
-                        }
-                    }
-                }
-            }
-        },
         "/Order/QueryHisTradeDetail": {
             "get": {
                 "security": [
@@ -1547,6 +1513,110 @@
                 }
             }
         },
+        "/Quote/QueryHistoryDatas": {
+            "get": {
+                "security": [
+                    {
+                        "ApiKeyAuth": []
+                    }
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "行情服务"
+                ],
+                "summary": "查询行情历史数据",
+                "parameters": [
+                    {
+                        "type": "integer",
+                        "description": "周期类型, 0: 秒 1: 1分钟 2: 5分钟 3: 30分钟 4: 60分钟 120: 2小时 240: 4小时 11: 日线 10: Tik",
+                        "name": "cycleType",
+                        "in": "query",
+                        "required": true
+                    },
+                    {
+                        "type": "string",
+                        "description": "商品代码",
+                        "name": "goodsCode",
+                        "in": "query",
+                        "required": true
+                    },
+                    {
+                        "type": "string",
+                        "description": "开始时间,格式:yyyy-MM-dd HH:mm:ss",
+                        "name": "startTime",
+                        "in": "query"
+                    },
+                    {
+                        "type": "string",
+                        "description": "结束时间,格式:yyyy-MM-dd HH:mm:ss",
+                        "name": "endTime",
+                        "in": "query"
+                    },
+                    {
+                        "type": "integer",
+                        "description": "条数",
+                        "name": "count",
+                        "in": "query"
+                    },
+                    {
+                        "type": "boolean",
+                        "description": "是否按时间顺序排序(默认为时间倒序排序)",
+                        "name": "IsAsc",
+                        "in": "query"
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "OK",
+                        "schema": {
+                            "$ref": "#/definitions/models.CycleData"
+                        }
+                    },
+                    "500": {
+                        "description": "Internal Server Error",
+                        "schema": {
+                            "$ref": "#/definitions/app.Response"
+                        }
+                    }
+                }
+            }
+        },
+        "/Quote/QueryTSData": {
+            "get": {
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "行情服务"
+                ],
+                "summary": "分时图数据查询",
+                "parameters": [
+                    {
+                        "type": "string",
+                        "description": "商品代码",
+                        "name": "GoodsCode",
+                        "in": "query",
+                        "required": true
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "OK",
+                        "schema": {
+                            "$ref": "#/definitions/quote.QueryTSDataRsp"
+                        }
+                    },
+                    "500": {
+                        "description": "Internal Server Error",
+                        "schema": {
+                            "$ref": "#/definitions/app.Response"
+                        }
+                    }
+                }
+            }
+        },
         "/SZDZ/QueryConvertConfig": {
             "get": {
                 "security": [
@@ -3750,6 +3820,51 @@
                 }
             }
         },
+        "models.CycleData": {
+            "type": "object",
+            "properties": {
+                "close": {
+                    "description": "收盘价",
+                    "type": "integer"
+                },
+                "gc": {
+                    "description": "商品代码",
+                    "type": "string"
+                },
+                "high": {
+                    "description": "最高价",
+                    "type": "integer"
+                },
+                "hv": {
+                    "description": "持仓量",
+                    "type": "integer"
+                },
+                "low": {
+                    "description": "最低价",
+                    "type": "integer"
+                },
+                "open": {
+                    "description": "开盘价",
+                    "type": "integer"
+                },
+                "sp": {
+                    "description": "结算价,日线周期(包括)以上才有",
+                    "type": "integer"
+                },
+                "sst": {
+                    "description": "时间文本",
+                    "type": "string"
+                },
+                "tt": {
+                    "description": "总金额",
+                    "type": "integer"
+                },
+                "tv": {
+                    "description": "总量",
+                    "type": "integer"
+                }
+            }
+        },
         "models.Division": {
             "type": "object",
             "required": [

+ 101 - 22
docs/swagger.yaml

@@ -1200,6 +1200,39 @@ definitions:
     required:
     - spotcontractid
     type: object
+  models.CycleData:
+    properties:
+      close:
+        description: 收盘价
+        type: integer
+      gc:
+        description: 商品代码
+        type: string
+      high:
+        description: 最高价
+        type: integer
+      hv:
+        description: 持仓量
+        type: integer
+      low:
+        description: 最低价
+        type: integer
+      open:
+        description: 开盘价
+        type: integer
+      sp:
+        description: 结算价,日线周期(包括)以上才有
+        type: integer
+      sst:
+        description: 时间文本
+        type: string
+      tt:
+        description: 总金额
+        type: integer
+      tv:
+        description: 总量
+        type: integer
+    type: object
   models.Division:
     properties:
       autoid:
@@ -4289,28 +4322,6 @@ paths:
       summary: 查询热卖商品列表(二级市场挂牌点选)
       tags:
       - 定制【海商报业】
-  /History/QueryTSData:
-    get:
-      parameters:
-      - description: 商品代码
-        in: query
-        name: GoodsCode
-        required: true
-        type: string
-      produces:
-      - application/json
-      responses:
-        "200":
-          description: OK
-          schema:
-            $ref: '#/definitions/quote.QueryTSDataRsp'
-        "500":
-          description: Internal Server Error
-          schema:
-            $ref: '#/definitions/app.Response'
-      summary: 分时图数据查询
-      tags:
-      - 通用服务
   /Order/QueryHisTradeDetail:
     get:
       parameters:
@@ -4515,6 +4526,74 @@ paths:
       summary: 持仓汇总查询(合约市场)
       tags:
       - 通用单据
+  /Quote/QueryHistoryDatas:
+    get:
+      parameters:
+      - description: '周期类型, 0: 秒 1: 1分钟 2: 5分钟 3: 30分钟 4: 60分钟 120: 2小时 240: 4小时 11:
+          日线 10: Tik'
+        in: query
+        name: cycleType
+        required: true
+        type: integer
+      - description: 商品代码
+        in: query
+        name: goodsCode
+        required: true
+        type: string
+      - description: 开始时间,格式:yyyy-MM-dd HH:mm:ss
+        in: query
+        name: startTime
+        type: string
+      - description: 结束时间,格式:yyyy-MM-dd HH:mm:ss
+        in: query
+        name: endTime
+        type: string
+      - description: 条数
+        in: query
+        name: count
+        type: integer
+      - description: 是否按时间顺序排序(默认为时间倒序排序)
+        in: query
+        name: IsAsc
+        type: boolean
+      produces:
+      - application/json
+      responses:
+        "200":
+          description: OK
+          schema:
+            $ref: '#/definitions/models.CycleData'
+        "500":
+          description: Internal Server Error
+          schema:
+            $ref: '#/definitions/app.Response'
+      security:
+      - ApiKeyAuth: []
+      summary: 查询行情历史数据
+      tags:
+      - 行情服务
+  /Quote/QueryTSData:
+    get:
+      parameters:
+      - description: 商品代码
+        in: query
+        name: GoodsCode
+        required: true
+        type: string
+      produces:
+      - application/json
+      responses:
+        "200":
+          description: OK
+          schema:
+            $ref: '#/definitions/quote.QueryTSDataRsp'
+        "500":
+          description: Internal Server Error
+          schema:
+            $ref: '#/definitions/app.Response'
+      summary: 分时图数据查询
+      tags:
+      - 行情服务
   /SZDZ/QueryConvertConfig:
     get:
       parameters:

+ 1 - 0
global/e/code.go

@@ -16,6 +16,7 @@ const (
 	ERROR_GET_MARKET_FAILED        = 30012
 	ERROR_GET_MARKETRUN_FAILED     = 30013
 	ERROR_GET_RUNSTEP_FAILED       = 30014
+	ERROR_QUERY_TIME_FORMAT_FAIL   = 30015
 
 	ERROR_UPLOAD_SAVE_IMAGE_FAIL    = 40001
 	ERROR_UPLOAD_CHECK_IMAGE_FAIL   = 40002

+ 1 - 0
global/e/msg.go

@@ -16,6 +16,7 @@ var MsgFlags = map[int]string{
 	ERROR_GET_MARKET_FAILED:        "获取市场信息失败",
 	ERROR_GET_MARKETRUN_FAILED:     "获取市场运行参数失败",
 	ERROR_GET_RUNSTEP_FAILED:       "获取市场开休市计划失败",
+	ERROR_QUERY_TIME_FORMAT_FAIL:   "错误的时间格式",
 
 	ERROR_UPLOAD_SAVE_IMAGE_FAIL:    "保存图片失败",
 	ERROR_UPLOAD_CHECK_IMAGE_FAIL:   "检查图片失败",

+ 29 - 17
models/quote.go

@@ -35,18 +35,18 @@ const (
 
 // CycleData MongoDB中历史数据模型
 type CycleData struct {
-	ID    bson.ObjectId `bson:"_id"`   // id
-	GC    string        `bson:"GC"`    // 商品代码
-	ST    int           `bson:"ST"`    // 时间戳
-	SST   string        `bson:"SST"`   // 时间文本
-	Open  int           `bson:"Open"`  // 开盘价
-	High  int           `bson:"High"`  // 最高价
-	Low   int           `bson:"Low"`   // 最低价
-	Close int           `bson:"Close"` // 收盘价
-	TV    int           `bson:"TV"`    // 总量
-	TT    int           `bson:"TT"`    // 总金额
-	HV    int           `bson:"HV"`    // 持仓量
-	SP    int           `bson:"SP"`    // 结算价,日线周期(包括)以上才有
+	ID    bson.ObjectId `json:"-" bson:"_id"` // id
+	GC    string        `bson:"GC"`           // 商品代码
+	ST    int           `json:"-" bson:"ST"`  // 时间戳
+	SST   string        `bson:"SST"`          // 时间文本
+	Open  int           `bson:"Open"`         // 开盘价
+	High  int           `bson:"High"`         // 最高价
+	Low   int           `bson:"Low"`          // 最低价
+	Close int           `bson:"Close"`        // 收盘价
+	TV    int           `bson:"TV"`           // 总量
+	TT    int           `bson:"TT"`           // 总金额
+	HV    int           `bson:"HV"`           // 持仓量
+	SP    int           `bson:"SP"`           // 结算价,日线周期(包括)以上才有
 }
 
 // Quoteday 行情盘面
@@ -188,9 +188,10 @@ type Quoteday struct {
 // 参数 startTime time.Time 开始时间(闭区间)
 // 参数 endTime time.Time 结束时间(闭区间)
 // 参数 count int 条数
+// 参数 isAscForST bool 是否按时间顺序排序,默认为时间倒序排序
 // 返回值 []CycleData 历史数据
 // 返回值 error 错误
-func GetHistoryCycleDatas(cycleType CycleType, goodsCode string, startTime, endTime time.Time, count int, isAscForST bool) ([]CycleData, error) {
+func GetHistoryCycleDatas(cycleType CycleType, goodsCode string, startTime, endTime *time.Time, count int, isAscForST bool) ([]CycleData, error) {
 	db := db.GetMongoDB()
 
 	// 获取目标Collection
@@ -204,6 +205,8 @@ func GetHistoryCycleDatas(cycleType CycleType, goodsCode string, startTime, endT
 		collection = "min5cycle"
 	case CycleTypeMinutes30:
 		collection = "min30cycle"
+	case CycleTypeMinutesDay:
+		collection = "daycycle"
 	default:
 		return nil, errors.New("不支持的周期类型")
 	}
@@ -217,10 +220,19 @@ func GetHistoryCycleDatas(cycleType CycleType, goodsCode string, startTime, endT
 
 	// 查询数据
 	var cycleDatas []CycleData
-	if err := c.Find(bson.M{
-		"GC": goodsCode,
-		"ST": bson.M{"$gte": startTime.Unix(), "$lte": endTime.Unix()},
-	}).Limit(count).Sort(sort).All(&cycleDatas); err != nil {
+	m := bson.M{"GC": goodsCode}
+	if startTime != nil && endTime == nil {
+		m["ST"] = bson.M{"$gte": startTime.Unix()}
+	} else if startTime == nil && endTime != nil {
+		m["ST"] = bson.M{"$lte": endTime.Unix()}
+	} else if startTime != nil && endTime != nil {
+		m["ST"] = bson.M{"$gte": startTime.Unix(), "$lte": endTime.Unix()}
+	}
+	query := c.Find(m)
+	if count > 0 {
+		query = query.Limit(count)
+	}
+	if err := query.Sort(sort).All(&cycleDatas); err != nil {
 		return nil, err
 	}
 

+ 8 - 0
routers/router.go

@@ -9,6 +9,7 @@ import (
 	"mtp2_if/controllers/erms2"
 	"mtp2_if/controllers/hsby"
 	"mtp2_if/controllers/order"
+	"mtp2_if/controllers/quote"
 	"mtp2_if/controllers/szdz"
 	"mtp2_if/controllers/taaccount"
 	"mtp2_if/controllers/user"
@@ -83,6 +84,13 @@ func InitRouter() *gin.Engine {
 		// 通知公告设置已读请求
 		commonR.Use(token.Auth()).POST("/NoticeReaded", common.NoticeReaded)
 	}
+	// ************************ 行情服务 ************************
+	quoteR := apiR.Group("Quote")
+	quoteR.Use(token.Auth())
+	{
+		// 查询行情历史数据
+		quoteR.GET("/QueryHistoryDatas", quote.QueryHistoryDatas)
+	}
 	// ************************ 通用单据 ************************
 	orderR := apiR.Group("Order")
 	orderR.Use(token.Auth())