Parcourir la source

增加“持仓汇总查询(合约市场)”接口

Simon Zhou il y a 5 ans
Parent
commit
f64038986d
6 fichiers modifiés avec 587 ajouts et 0 suppressions
  1. 164 0
      controllers/order/order.go
  2. 146 0
      docs/docs.go
  3. 146 0
      docs/swagger.json
  4. 103 0
      docs/swagger.yaml
  5. 20 0
      models/commonModels.go
  6. 8 0
      routers/router.go

+ 164 - 0
controllers/order/order.go

@@ -0,0 +1,164 @@
+package order
+
+import (
+	"encoding/json"
+	"mtp2_if/db"
+	"mtp2_if/global/app"
+	"mtp2_if/global/e"
+	"mtp2_if/logger"
+	"mtp2_if/models"
+	"net/http"
+
+	"github.com/gin-gonic/gin"
+)
+
+// QueryTradePositionReq 持仓汇总查询请求参数
+type QueryTradePositionReq struct {
+	AccountID int `form:"accountID" binding:"required"`
+	TradeMode int `form:"tradeMode"`
+}
+
+// QueryTradePositionRsp 持仓汇总查询返回模型
+type QueryTradePositionRsp struct {
+	BuyOrSell       int64   `json:"buyorsell"  xorm:"'BUYORSELL'" `            // 方向 - 0:买 1:卖
+	GoodsCode       string  `json:"goodscode" xorm:"GOODSCODE"`                // 商品代码
+	GoodsName       string  `json:"goodsname" xorm:"GOODSNAME"`                // 商品名称
+	Agreeunit       float64 `json:"agreeunit"  xorm:"'AGREEUNIT'"`             // 合约单位
+	CurrencyID      int64   `json:"currencyid"  xorm:"'CURRENCYID'"`           // 报价货币ID
+	GoodUnitID      int64   `json:"goodunitid"  xorm:"'GOODUNITID'"`           // 报价单位ID
+	Goodunit        string  `json:"goodunit" xorm:"'GOODUNIT'"`                // 报价单位
+	DecimalPlace    int64   `json:"decimalplace"  xorm:"'DECIMALPLACE'"`       // 报价小数位
+	MarketID        int64   `json:"marketid"  xorm:"'MARKETID'"`               // 所属市场ID
+	TradeMode       uint32  `json:"trademode"  xorm:"'TRADEMODE'"`             // 交易模式
+	PositionQTY     uint64  `json:"positionqty"  xorm:"'POSITIONQTY'"`         // 期初持仓数量
+	HolderAmount    float64 `json:"holderamount"  xorm:"'HOLDERAMOUNT'"`       // 期初持仓总金额[商品币种]
+	CurPositionQTY  uint64  `json:"curpositionqty"  xorm:"'CURPOSITIONQTY'"`   // 当前持仓总数量
+	CurHolderAmount float64 `json:"curholderamount"  xorm:"'CURHOLDERAMOUNT'"` // 当前持仓总金额[商品币种]
+	FrozenQTY       uint64  `json:"frozenqty"  xorm:"'FROZENQTY'"`             // 持仓冻结数量
+	OtherFrozenQTY  uint64  `json:"otherfrozenqty"  xorm:"'OTHERFROZENQTY'"`   // 持仓其他冻结数量(交割冻结)
+	OpenReqQTY      uint64  `json:"openreqqty"  xorm:"'OPENREQQTY'"`           // 开仓申请数量(用于比较最大持仓数量)
+	OpenTotalQTY    uint64  `json:"opentotalqty"  xorm:"'OPENTOTALQTY'"`       // 开仓总数量
+	CloseTotalQTY   uint64  `json:"closetotalqty"  xorm:"'CLOSETOTALQTY'"`     // 平仓总数量
+	TNQTY           uint64  `json:"tnqty"  xorm:"'TNQTY'"`                     // T+N冻结总量
+	TNUsedQTY       uint64  `json:"tnusedqty"  xorm:"'TNUSEDQTY'"`             // T+N使用量(可以使用T+N的冻结数量)
+	CurTDPosition   uint64  `json:"curtdposition"  xorm:"'CURTDPOSITION'"`     // 期末今日头寸
+	FreTDPosition   uint64  `json:"fretdposition"  xorm:"'FRETDPOSITION'"`     // 冻结今日头寸
+	EnableQTY       uint64  `json:"enableqty"  xorm:"'ENABLEQTY'"`             // 可用量
+}
+
+// QueryTradePosition 持仓汇总查询(合约市场)
+// @Summary 持仓汇总查询(合约市场)
+// @Produce json
+// @Security ApiKeyAuth
+// @Param accountID query int true "资金账户"
+// @Param tradeMode query int false "交易模式"
+// @Success 200 {object} QueryTradePositionRsp
+// @Failure 500 {object} app.Response
+// @Router /Order/QueryTradePosition [get]
+// @Tags 通用单据
+func QueryTradePosition(c *gin.Context) {
+	appG := app.Gin{C: c}
+
+	// 获取请求参数
+	var req QueryTradePositionReq
+	if err := appG.C.ShouldBindQuery(&req); err != nil {
+		logger.GetLogger().Errorf("QueryTradePosition failed: %s", err.Error())
+		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
+		return
+	}
+
+	// 查询数据
+	type tradePosition struct {
+		models.Tradeposition `xorm:"extends"`
+		Goodscode            string  `json:"goodscode"  xorm:"'GOODSCODE'"`       // 商品代码(内部)
+		Goodsname            string  `json:"goodsname"  xorm:"'GOODSNAME'"`       // 商品名称
+		Currencyid           int64   `json:"currencyid"  xorm:"'CURRENCYID'"`     // 报价货币ID
+		Goodunitid           int64   `json:"goodunitid"  xorm:"'GOODUNITID'"`     // 报价单位ID
+		Goodunit             string  `json:"goodunit" xorm:"'GOODUNIT'"`          // 报价单位
+		Agreeunit            float64 `json:"agreeunit"  xorm:"'AGREEUNIT'"`       // 合约单位
+		Decimalplace         int64   `json:"decimalplace"  xorm:"'DECIMALPLACE'"` // 报价小数位
+		Marketid             uint32  `json:"marketid"  xorm:"'MARKETID'"`         // 市场ID
+		Trademode            uint32  `json:"trademode"  xorm:"'TRADEMODE'"`       // 交易模式
+	}
+	datas := make([]tradePosition, 0)
+	engine := db.GetEngine()
+	// ORACLE好像在JOIN里不支持别名功能(在XORM中)
+	s := engine.Table("TRADEPOSITION").
+		Join("LEFT", "GOODS", "TRADEPOSITION.GOODSID = GOODS.GOODSID").
+		Join("LEFT", "MARKET", "GOODS.MARKETID = MARKET.MARKETID").
+		Join("LEFT", "ENUMDICITEM", "GOODS.GOODUNITID = ENUMDICITEM.ENUMITEMNAME and ENUMDICITEM.ENUMDICCODE = 'goodsunit'").
+		Select("TRADEPOSITION.*, GOODS.GOODSCODE, GOODS.GOODSNAME, GOODS.CURRENCYID, GOODS.GOODUNITID, ENUMDICITEM.ENUMDICNAME as GOODUNIT, GOODS.AGREEUNIT, GOODS.DECIMALPLACE, MARKET.MARKETID, MARKET.TRADEMODE").
+		Where("TRADEPOSITION.ACCOUNTID = ?", req.AccountID)
+	if req.TradeMode != 0 {
+		s = s.And("MARKET.TRADEMODE = ?", req.TradeMode)
+	}
+	if err := s.Find(&datas); err != nil {
+		// 查询失败
+		logger.GetLogger().Errorf("QueryTradePosition failed: %s", err.Error())
+		appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
+		return
+	}
+	// 构建返回数据
+	rst := make([]QueryTradePositionRsp, 0)
+	for _, v := range datas {
+		// 构建买方向持仓汇总
+		if v.Buycurpositionqty > 0 {
+			var tradePosition QueryTradePositionRsp
+			// 反射数据
+			// struct -> json
+			if jsonBytes, err := json.Marshal(v); err == nil {
+				// json -> struct
+				json.Unmarshal(jsonBytes, &tradePosition)
+				tradePosition.BuyOrSell = 0
+				tradePosition.PositionQTY = v.Buypositionqty
+				tradePosition.HolderAmount = v.Buyholderamount
+				tradePosition.CurPositionQTY = v.Buycurpositionqty
+				tradePosition.CurHolderAmount = v.Buycurholderamount
+				tradePosition.FrozenQTY = v.Buyfrozenqty
+				tradePosition.OtherFrozenQTY = v.Buyotherfrozenqty
+				tradePosition.OpenReqQTY = v.Buyopenreqqty
+				tradePosition.OpenTotalQTY = v.Buyopentotalqty
+				tradePosition.CloseTotalQTY = v.Buyclosetotalqty
+				tradePosition.TNQTY = v.Buytnqty
+				tradePosition.TNUsedQTY = v.Buytnusedqty
+				tradePosition.CurTDPosition = v.Buycurtdposition
+				tradePosition.FreTDPosition = v.Buyfretdposition
+				tradePosition.EnableQTY = v.Buycurpositionqty - v.Buyfrozenqty - v.Buyotherfrozenqty
+
+				rst = append(rst, tradePosition)
+			}
+		}
+
+		// 构建卖方向持仓汇总
+		if v.Tradeproperty != 2 && v.Sellcurpositionqty > 0 {
+			var tradePosition QueryTradePositionRsp
+			// 反射数据
+			// struct -> json
+			if jsonBytes, err := json.Marshal(v); err == nil {
+				// json -> struct
+				json.Unmarshal(jsonBytes, &tradePosition)
+				tradePosition.BuyOrSell = 1
+				tradePosition.PositionQTY = v.Sellpositionqty
+				tradePosition.HolderAmount = v.Sellholderamount
+				tradePosition.CurPositionQTY = v.Sellcurpositionqty
+				tradePosition.CurHolderAmount = v.Sellcurholderamount
+				tradePosition.FrozenQTY = v.Sellfrozenqty
+				tradePosition.OtherFrozenQTY = v.Sellotherfrozenqty
+				tradePosition.OpenReqQTY = v.Sellopenreqqty
+				tradePosition.OpenTotalQTY = v.Sellopentotalqty
+				tradePosition.CloseTotalQTY = v.Sellclosetotalqty
+				tradePosition.TNQTY = v.Selltnqty
+				tradePosition.TNUsedQTY = v.Selltnusedqty
+				tradePosition.CurTDPosition = v.Sellcurtdposition
+				tradePosition.FreTDPosition = v.Sellfretdposition
+				tradePosition.EnableQTY = v.Sellcurpositionqty - v.Sellfrozenqty - v.Sellotherfrozenqty
+
+				rst = append(rst, tradePosition)
+			}
+		}
+	}
+
+	// 查询成功
+	logger.GetLogger().Infof("QueryTradePosition successed: %v", rst)
+	appG.Response(http.StatusOK, e.SUCCESS, rst)
+}

+ 146 - 0
docs/docs.go

@@ -615,6 +615,51 @@ var doc = `{
                 }
             }
         },
+        "/Order/QueryTradePosition": {
+            "get": {
+                "security": [
+                    {
+                        "ApiKeyAuth": []
+                    }
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "通用单据"
+                ],
+                "summary": "持仓汇总查询(合约市场)",
+                "parameters": [
+                    {
+                        "type": "integer",
+                        "description": "资金账户",
+                        "name": "accountID",
+                        "in": "query",
+                        "required": true
+                    },
+                    {
+                        "type": "integer",
+                        "description": "交易模式",
+                        "name": "tradeMode",
+                        "in": "query"
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "OK",
+                        "schema": {
+                            "$ref": "#/definitions/order.QueryTradePositionRsp"
+                        }
+                    },
+                    "500": {
+                        "description": "Internal Server Error",
+                        "schema": {
+                            "$ref": "#/definitions/app.Response"
+                        }
+                    }
+                }
+            }
+        },
         "/User/GetLoginID": {
             "get": {
                 "produces": [
@@ -2368,6 +2413,107 @@ var doc = `{
                     "type": "string"
                 }
             }
+        },
+        "order.QueryTradePositionRsp": {
+            "type": "object",
+            "properties": {
+                "agreeunit": {
+                    "description": "合约单位",
+                    "type": "number"
+                },
+                "buyorsell": {
+                    "description": "方向 - 0:买 1:卖",
+                    "type": "integer"
+                },
+                "closetotalqty": {
+                    "description": "平仓总数量",
+                    "type": "integer"
+                },
+                "curholderamount": {
+                    "description": "当前持仓总金额[商品币种]",
+                    "type": "number"
+                },
+                "curpositionqty": {
+                    "description": "当前持仓总数量",
+                    "type": "integer"
+                },
+                "currencyid": {
+                    "description": "报价货币ID",
+                    "type": "integer"
+                },
+                "curtdposition": {
+                    "description": "期末今日头寸",
+                    "type": "integer"
+                },
+                "decimalplace": {
+                    "description": "报价小数位",
+                    "type": "integer"
+                },
+                "enableqty": {
+                    "description": "可用量",
+                    "type": "integer"
+                },
+                "fretdposition": {
+                    "description": "冻结今日头寸",
+                    "type": "integer"
+                },
+                "frozenqty": {
+                    "description": "持仓冻结数量",
+                    "type": "integer"
+                },
+                "goodscode": {
+                    "description": "商品代码",
+                    "type": "string"
+                },
+                "goodsname": {
+                    "description": "商品名称",
+                    "type": "string"
+                },
+                "goodunit": {
+                    "description": "报价单位",
+                    "type": "string"
+                },
+                "goodunitid": {
+                    "description": "报价单位ID",
+                    "type": "integer"
+                },
+                "holderamount": {
+                    "description": "期初持仓总金额[商品币种]",
+                    "type": "number"
+                },
+                "marketid": {
+                    "description": "所属市场ID",
+                    "type": "integer"
+                },
+                "openreqqty": {
+                    "description": "开仓申请数量(用于比较最大持仓数量)",
+                    "type": "integer"
+                },
+                "opentotalqty": {
+                    "description": "开仓总数量",
+                    "type": "integer"
+                },
+                "otherfrozenqty": {
+                    "description": "持仓其他冻结数量(交割冻结)",
+                    "type": "integer"
+                },
+                "positionqty": {
+                    "description": "期初持仓数量",
+                    "type": "integer"
+                },
+                "tnqty": {
+                    "description": "T+N冻结总量",
+                    "type": "integer"
+                },
+                "tnusedqty": {
+                    "description": "T+N使用量(可以使用T+N的冻结数量)",
+                    "type": "integer"
+                },
+                "trademode": {
+                    "description": "交易模式",
+                    "type": "integer"
+                }
+            }
         }
     },
     "securityDefinitions": {

+ 146 - 0
docs/swagger.json

@@ -599,6 +599,51 @@
                 }
             }
         },
+        "/Order/QueryTradePosition": {
+            "get": {
+                "security": [
+                    {
+                        "ApiKeyAuth": []
+                    }
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "通用单据"
+                ],
+                "summary": "持仓汇总查询(合约市场)",
+                "parameters": [
+                    {
+                        "type": "integer",
+                        "description": "资金账户",
+                        "name": "accountID",
+                        "in": "query",
+                        "required": true
+                    },
+                    {
+                        "type": "integer",
+                        "description": "交易模式",
+                        "name": "tradeMode",
+                        "in": "query"
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "OK",
+                        "schema": {
+                            "$ref": "#/definitions/order.QueryTradePositionRsp"
+                        }
+                    },
+                    "500": {
+                        "description": "Internal Server Error",
+                        "schema": {
+                            "$ref": "#/definitions/app.Response"
+                        }
+                    }
+                }
+            }
+        },
         "/User/GetLoginID": {
             "get": {
                 "produces": [
@@ -2352,6 +2397,107 @@
                     "type": "string"
                 }
             }
+        },
+        "order.QueryTradePositionRsp": {
+            "type": "object",
+            "properties": {
+                "agreeunit": {
+                    "description": "合约单位",
+                    "type": "number"
+                },
+                "buyorsell": {
+                    "description": "方向 - 0:买 1:卖",
+                    "type": "integer"
+                },
+                "closetotalqty": {
+                    "description": "平仓总数量",
+                    "type": "integer"
+                },
+                "curholderamount": {
+                    "description": "当前持仓总金额[商品币种]",
+                    "type": "number"
+                },
+                "curpositionqty": {
+                    "description": "当前持仓总数量",
+                    "type": "integer"
+                },
+                "currencyid": {
+                    "description": "报价货币ID",
+                    "type": "integer"
+                },
+                "curtdposition": {
+                    "description": "期末今日头寸",
+                    "type": "integer"
+                },
+                "decimalplace": {
+                    "description": "报价小数位",
+                    "type": "integer"
+                },
+                "enableqty": {
+                    "description": "可用量",
+                    "type": "integer"
+                },
+                "fretdposition": {
+                    "description": "冻结今日头寸",
+                    "type": "integer"
+                },
+                "frozenqty": {
+                    "description": "持仓冻结数量",
+                    "type": "integer"
+                },
+                "goodscode": {
+                    "description": "商品代码",
+                    "type": "string"
+                },
+                "goodsname": {
+                    "description": "商品名称",
+                    "type": "string"
+                },
+                "goodunit": {
+                    "description": "报价单位",
+                    "type": "string"
+                },
+                "goodunitid": {
+                    "description": "报价单位ID",
+                    "type": "integer"
+                },
+                "holderamount": {
+                    "description": "期初持仓总金额[商品币种]",
+                    "type": "number"
+                },
+                "marketid": {
+                    "description": "所属市场ID",
+                    "type": "integer"
+                },
+                "openreqqty": {
+                    "description": "开仓申请数量(用于比较最大持仓数量)",
+                    "type": "integer"
+                },
+                "opentotalqty": {
+                    "description": "开仓总数量",
+                    "type": "integer"
+                },
+                "otherfrozenqty": {
+                    "description": "持仓其他冻结数量(交割冻结)",
+                    "type": "integer"
+                },
+                "positionqty": {
+                    "description": "期初持仓数量",
+                    "type": "integer"
+                },
+                "tnqty": {
+                    "description": "T+N冻结总量",
+                    "type": "integer"
+                },
+                "tnusedqty": {
+                    "description": "T+N使用量(可以使用T+N的冻结数量)",
+                    "type": "integer"
+                },
+                "trademode": {
+                    "description": "交易模式",
+                    "type": "integer"
+                }
+            }
         }
     },
     "securityDefinitions": {

+ 103 - 0
docs/swagger.yaml

@@ -1226,6 +1226,81 @@ definitions:
     required:
     - autoid
     type: object
+  order.QueryTradePositionRsp:
+    properties:
+      agreeunit:
+        description: 合约单位
+        type: number
+      buyorsell:
+        description: 方向 - 0:买 1:卖
+        type: integer
+      closetotalqty:
+        description: 平仓总数量
+        type: integer
+      curholderamount:
+        description: 当前持仓总金额[商品币种]
+        type: number
+      curpositionqty:
+        description: 当前持仓总数量
+        type: integer
+      currencyid:
+        description: 报价货币ID
+        type: integer
+      curtdposition:
+        description: 期末今日头寸
+        type: integer
+      decimalplace:
+        description: 报价小数位
+        type: integer
+      enableqty:
+        description: 可用量
+        type: integer
+      fretdposition:
+        description: 冻结今日头寸
+        type: integer
+      frozenqty:
+        description: 持仓冻结数量
+        type: integer
+      goodscode:
+        description: 商品代码
+        type: string
+      goodsname:
+        description: 商品名称
+        type: string
+      goodunit:
+        description: 报价单位
+        type: string
+      goodunitid:
+        description: 报价单位ID
+        type: integer
+      holderamount:
+        description: 期初持仓总金额[商品币种]
+        type: number
+      marketid:
+        description: 所属市场ID
+        type: integer
+      openreqqty:
+        description: 开仓申请数量(用于比较最大持仓数量)
+        type: integer
+      opentotalqty:
+        description: 开仓总数量
+        type: integer
+      otherfrozenqty:
+        description: 持仓其他冻结数量(交割冻结)
+        type: integer
+      positionqty:
+        description: 期初持仓数量
+        type: integer
+      tnqty:
+        description: T+N冻结总量
+        type: integer
+      tnusedqty:
+        description: T+N使用量(可以使用T+N的冻结数量)
+        type: integer
+      trademode:
+        description: 交易模式
+        type: integer
+    type: object
 info:
   contact: {}
   description: 新的查询服务,替代原通用查询服务。
@@ -1602,6 +1677,34 @@ paths:
       summary: 查询现货合同表信息(指定策略ID、未结束的)
       tags:
       - 风险管理
+  /Order/QueryTradePosition:
+    get:
+      parameters:
+      - description: 资金账户
+        in: query
+        name: accountID
+        required: true
+        type: integer
+      - description: 交易模式
+        in: query
+        name: tradeMode
+        type: integer
+      produces:
+      - application/json
+      responses:
+        "200":
+          description: OK
+          schema:
+            $ref: '#/definitions/order.QueryTradePositionRsp'
+        "500":
+          description: Internal Server Error
+          schema:
+            $ref: '#/definitions/app.Response'
+      security:
+      - ApiKeyAuth: []
+      summary: 持仓汇总查询(合约市场)
+      tags:
+      - 通用单据
   /User/GetLoginID:
     get:
       parameters:

+ 20 - 0
models/commonModels.go

@@ -65,3 +65,23 @@ type Tablecolumnconfig struct {
 func (Tablecolumnconfig) TableName() string {
 	return "TABLECOLUMNCONFIG"
 }
+
+// Enumdicitem 枚举项字典表
+type Enumdicitem struct {
+	Autoid          uint64 `json:"autoid"  xorm:"'AUTOID'" binding:"required"`             // 自增ID
+	Enumdicid       uint32 `json:"enumdicid"  xorm:"'ENUMDICID'" binding:"required"`       // 所属枚举ID
+	Enumdiccode     string `json:"enumdiccode"  xorm:"'ENUMDICCODE'" binding:"required"`   // 所属枚举代码
+	Enumdicname     string `json:"enumdicname"  xorm:"'ENUMDICNAME'"`                      // 枚举项名称
+	Enumitemname    uint64 `json:"enumitemname"  xorm:"'ENUMITEMNAME'" binding:"required"` // 枚举项值
+	Enumitemstatus  uint32 `json:"enumitemstatus"  xorm:"'ENUMITEMSTATUS'"`                // 枚举项状态 - 1.启用 2.不启用
+	Bankmappedvalue string `json:"bankmappedvalue"  xorm:"'BANKMAPPEDVALUE'"`              // 银行服务对应值
+	Remark          string `json:"remark"  xorm:"'REMARK'"`                                // 备注
+	Enumitemvalue   string `json:"enumitemvalue"  xorm:"'ENUMITEMVALUE'"`                  // 通用值 - [币种通用简写]
+	Param1          string `json:"param1"  xorm:"'PARAM1'"`                                // 参数1[币种:币种小数位]
+	Param2          string `json:"param2"  xorm:"'PARAM2'"`                                // 参数1[币种:币种显示单位]
+}
+
+// TableName is ENUMDICITEM
+func (Enumdicitem) TableName() string {
+	return "ENUMDICITEM"
+}

+ 8 - 0
routers/router.go

@@ -6,6 +6,7 @@ import (
 	"mtp2_if/controllers/cptrade"
 	"mtp2_if/controllers/delivery"
 	"mtp2_if/controllers/erms2"
+	"mtp2_if/controllers/order"
 	"mtp2_if/controllers/user"
 	"mtp2_if/controllers/wrtrade"
 	"mtp2_if/logger"
@@ -55,6 +56,13 @@ func InitRouter() *gin.Engine {
 		// 查询交易端列表头信息
 		commonR.GET("/QueryTableColumnConfig", common.QueryTableColumnConfig)
 	}
+	// ************************ 通用单据 ************************
+	orderR := apiR.Group("Order")
+	orderR.Use(token.Auth())
+	{
+		// 持仓汇总查询(合约市场)
+		orderR.GET("/QueryTradePosition", order.QueryTradePosition)
+	}
 	// ************************ 仓单贸易 ************************
 	wrTradeR := apiR.Group("WRTrade")
 	wrTradeR.Use(token.Auth())