Parcourir la source

增加接口:查询 敞口/实时敞口/期货明细
fix bug:#93057 、可选的期货品种去重处理

zou.yingbin il y a 4 ans
Parent
commit
343da0385d

+ 27 - 0
controllers/ermcp/qryExposure.go

@@ -186,3 +186,30 @@ func QueryHisExposure(c *gin.Context) {
 		appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
 	}
 }
+
+
+// 实时敞口/期货明细 请求
+type RealtimeExposurePositionReq struct {
+	AreaUserId    int32 `form:"areaUserId" json:"areaUserId"`       // 所属机构ID
+	MiddleGoodsId int32  `form:"middleGoodsId" json:"middlegoodsid"` // 套保商品
+}
+
+// 实时敞口/期货明细 应答
+type RealtimeExposurePositionRsp models.ErmcpExposurePostion
+
+// @Summary 查询实时敞口期货头寸明细(菜单:实时敞口/期货明细)
+// @Produce json
+// @Security ApiKeyAuth
+// @Param areaUserId query int true "所属机构ID"
+// @Param middleGoodsId query int true "套保商品ID"
+// @Success 200 {array} RealtimeExposurePositionRsp
+// @Failure 500 {object} app.Response
+// @Router /Ermcp/QueryRealtimeExposurePosition [get]
+// @Tags 企业风险管理(app)
+func QueryRealtimeExposurePosition(c *gin.Context)  {
+	appG := app.GinUtils{Gin:app.Gin{C: c}}
+	var req RealtimeExposurePositionReq
+	appG.DoBindReq(&req)
+	m := models.ErmcpExposurePostion{AREAUSERID: req.AreaUserId, MIDDLEGOODSID: req.MiddleGoodsId}
+	appG.DoGetDataEx(&m)
+}

+ 93 - 0
docs/docs.go

@@ -1517,6 +1517,55 @@ var doc = `{
                 }
             }
         },
+        "/Ermcp/QueryRealtimeExposurePosition": {
+            "get": {
+                "security": [
+                    {
+                        "ApiKeyAuth": []
+                    }
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "企业风险管理(app)"
+                ],
+                "summary": "查询实时敞口期货头寸明细(菜单:实时敞口/期货明细)",
+                "parameters": [
+                    {
+                        "type": "integer",
+                        "description": "所属机构ID",
+                        "name": "areaUserId",
+                        "in": "query",
+                        "required": true
+                    },
+                    {
+                        "type": "integer",
+                        "description": "套保商品ID",
+                        "name": "middleGoodsId",
+                        "in": "query",
+                        "required": true
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "OK",
+                        "schema": {
+                            "type": "array",
+                            "items": {
+                                "$ref": "#/definitions/ermcp.RealtimeExposurePositionRsp"
+                            }
+                        }
+                    },
+                    "500": {
+                        "description": "Internal Server Error",
+                        "schema": {
+                            "$ref": "#/definitions/app.Response"
+                        }
+                    }
+                }
+            }
+        },
         "/Ermcp/QuerySpotContract": {
             "get": {
                 "security": [
@@ -6644,6 +6693,9 @@ var doc = `{
         "ermcp.QryWrStandardRsp": {
             "$ref": "#/definitions/models.ErmcpWrstandard"
         },
+        "ermcp.RealtimeExposurePositionRsp": {
+            "$ref": "#/definitions/models.ErmcpExposurePostion"
+        },
         "ermcp.RealtimeExposureRsp": {
             "$ref": "#/definitions/models.ErmcpRealExposureModel"
         },
@@ -8561,6 +8613,47 @@ var doc = `{
                 }
             }
         },
+        "models.ErmcpExposurePostion": {
+            "type": "object",
+            "properties": {
+                "areauserid": {
+                    "description": "所属机构id",
+                    "type": "integer"
+                },
+                "curqty": {
+                    "description": "当前持仓",
+                    "type": "integer"
+                },
+                "diffhedgeqty": {
+                    "description": "套保品种变动量",
+                    "type": "number"
+                },
+                "diffqty": {
+                    "description": "持仓变动量",
+                    "type": "integer"
+                },
+                "goodscode": {
+                    "description": "商品代码",
+                    "type": "string"
+                },
+                "goodsid": {
+                    "description": "商品id",
+                    "type": "integer"
+                },
+                "goodsname": {
+                    "description": "商品名称",
+                    "type": "string"
+                },
+                "middlegoodsid": {
+                    "description": "套保商品id",
+                    "type": "integer"
+                },
+                "ydqty": {
+                    "description": "昨日持仓",
+                    "type": "integer"
+                }
+            }
+        },
         "models.ErmcpGGConvertCfg": {
             "type": "object",
             "properties": {

+ 93 - 0
docs/swagger.json

@@ -1501,6 +1501,55 @@
                 }
             }
         },
+        "/Ermcp/QueryRealtimeExposurePosition": {
+            "get": {
+                "security": [
+                    {
+                        "ApiKeyAuth": []
+                    }
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "企业风险管理(app)"
+                ],
+                "summary": "查询实时敞口期货头寸明细(菜单:实时敞口/期货明细)",
+                "parameters": [
+                    {
+                        "type": "integer",
+                        "description": "所属机构ID",
+                        "name": "areaUserId",
+                        "in": "query",
+                        "required": true
+                    },
+                    {
+                        "type": "integer",
+                        "description": "套保商品ID",
+                        "name": "middleGoodsId",
+                        "in": "query",
+                        "required": true
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "OK",
+                        "schema": {
+                            "type": "array",
+                            "items": {
+                                "$ref": "#/definitions/ermcp.RealtimeExposurePositionRsp"
+                            }
+                        }
+                    },
+                    "500": {
+                        "description": "Internal Server Error",
+                        "schema": {
+                            "$ref": "#/definitions/app.Response"
+                        }
+                    }
+                }
+            }
+        },
         "/Ermcp/QuerySpotContract": {
             "get": {
                 "security": [
@@ -6628,6 +6677,9 @@
         "ermcp.QryWrStandardRsp": {
             "$ref": "#/definitions/models.ErmcpWrstandard"
         },
+        "ermcp.RealtimeExposurePositionRsp": {
+            "$ref": "#/definitions/models.ErmcpExposurePostion"
+        },
         "ermcp.RealtimeExposureRsp": {
             "$ref": "#/definitions/models.ErmcpRealExposureModel"
         },
@@ -8545,6 +8597,47 @@
                 }
             }
         },
+        "models.ErmcpExposurePostion": {
+            "type": "object",
+            "properties": {
+                "areauserid": {
+                    "description": "所属机构id",
+                    "type": "integer"
+                },
+                "curqty": {
+                    "description": "当前持仓",
+                    "type": "integer"
+                },
+                "diffhedgeqty": {
+                    "description": "套保品种变动量",
+                    "type": "number"
+                },
+                "diffqty": {
+                    "description": "持仓变动量",
+                    "type": "integer"
+                },
+                "goodscode": {
+                    "description": "商品代码",
+                    "type": "string"
+                },
+                "goodsid": {
+                    "description": "商品id",
+                    "type": "integer"
+                },
+                "goodsname": {
+                    "description": "商品名称",
+                    "type": "string"
+                },
+                "middlegoodsid": {
+                    "description": "套保商品id",
+                    "type": "integer"
+                },
+                "ydqty": {
+                    "description": "昨日持仓",
+                    "type": "integer"
+                }
+            }
+        },
         "models.ErmcpGGConvertCfg": {
             "type": "object",
             "properties": {

+ 63 - 0
docs/swagger.yaml

@@ -1164,6 +1164,8 @@ definitions:
     $ref: '#/definitions/models.ErmcpWrstandDetail'
   ermcp.QryWrStandardRsp:
     $ref: '#/definitions/models.ErmcpWrstandard'
+  ermcp.RealtimeExposurePositionRsp:
+    $ref: '#/definitions/models.ErmcpExposurePostion'
   ermcp.RealtimeExposureRsp:
     $ref: '#/definitions/models.ErmcpRealExposureModel'
   erms2.QueryArbitrageStrategyRsp:
@@ -2583,6 +2585,36 @@ definitions:
         description: 现货商品名称
         type: string
     type: object
+  models.ErmcpExposurePostion:
+    properties:
+      areauserid:
+        description: 所属机构id
+        type: integer
+      curqty:
+        description: 当前持仓
+        type: integer
+      diffhedgeqty:
+        description: 套保品种变动量
+        type: number
+      diffqty:
+        description: 持仓变动量
+        type: integer
+      goodscode:
+        description: 商品代码
+        type: string
+      goodsid:
+        description: 商品id
+        type: integer
+      goodsname:
+        description: 商品名称
+        type: string
+      middlegoodsid:
+        description: 套保商品id
+        type: integer
+      ydqty:
+        description: 昨日持仓
+        type: integer
+    type: object
   models.ErmcpGGConvertCfg:
     properties:
       convertratio:
@@ -8030,6 +8062,37 @@ paths:
       summary: 查询实时敞口
       tags:
       - 企业风险管理(app)
+  /Ermcp/QueryRealtimeExposurePosition:
+    get:
+      parameters:
+      - description: 所属机构ID
+        in: query
+        name: areaUserId
+        required: true
+        type: integer
+      - description: 套保商品ID
+        in: query
+        name: middleGoodsId
+        required: true
+        type: integer
+      produces:
+      - application/json
+      responses:
+        "200":
+          description: OK
+          schema:
+            items:
+              $ref: '#/definitions/ermcp.RealtimeExposurePositionRsp'
+            type: array
+        "500":
+          description: Internal Server Error
+          schema:
+            $ref: '#/definitions/app.Response'
+      security:
+      - ApiKeyAuth: []
+      summary: 查询实时敞口期货头寸明细(菜单:实时敞口/期货明细)
+      tags:
+      - 企业风险管理(app)
   /Ermcp/QuerySpotContract:
     get:
       parameters:

+ 1 - 1
models/ermcp.go

@@ -100,7 +100,7 @@ func (r *ErmcpSpotContractModel) buildSql(nQueryType int32) string {
 	case 1:
 		status = "0"
 	case 2:
-		status = "1"
+		status = "1,4,6"
 	case 3:
 		status = "2"
 	case 4:

+ 135 - 0
models/ermcpExposure.go

@@ -392,3 +392,138 @@ func (r *ErmcpHisExposure) GetData() ([]ErmcpHisExposure, error) {
 	}
 	return sData, nil
 }
+
+// 套保品种关联交易商品
+type ErmcpTradeGoods struct {
+	MIDDLEGOODSID    int32   `json:"middlegoodsid"  xorm:"'MIDDLEGOODSID'"`    // 套保商品id
+	MIDDLEGOODSNAME  string  `json:"middlegoodsname"  xorm:"'MIDDLEGOODSNAME'"`   // 套保商品名称
+	MIDDLEGOODSCODE  string  `json:"middlegoodscode"  xorm:"'MIDDLEGOODSCODE'"`   // 套保商品代码
+	SRCGOODSGROUPID  int32   `json:"srcgoodsgroupid"  xorm:"'SRCGOODSGROUPID'"`   // 源期货品种id
+	DESTGOODSGROUPID int32   `json:"destgoodsgroupid"  xorm:"'DESTGOODSGROUPID'"` // 目标期货品种id
+	CONVERTRATIO     float64 `json:"convertratio"  xorm:"'CONVERTRATIO'"`         // 折算系数
+	GOODSID          int32   `json:"goodsid"  xorm:"pk 'GOODSID'"`                   // 商品id
+	GOODSCODE        string  `json:"goodscode"  xorm:"'GOODSCODE'"`               // 商品代码
+	GOODSNAME        string  `json:"goodsname"  xorm:"'GOODSNAME'"`               // 商品名称
+	GOODSGROUPID     int32   `json:"goodsgroupid"  xorm:"'GOODSGROUPID'"`         // 商品组id
+	GOODSUNITID      int32   `json:"goodsunitid"  xorm:"'GOODSUNITID'"`           // 套保品种单位id
+	AREAUSERID       int32   `json:"areauserid"  xorm:"'AREAUSERID'"`             // 所属机构id
+	AGREEUNIT        float64 `json:"agreeunit"  xorm:"'agreeunit'"`               // 合约单位
+}
+
+func (r *ErmcpTradeGoods) buildSq() string {
+	sqlId := "SELECT t.MIDDLEGOODSID," +
+		"       t.MIDDLEGOODSNAME," +
+		"       t.MIDDLEGOODSCODE," +
+		"       t.GOODSUNITID," +
+		"       t.AREAUSERID," +
+		"       c.SRCGOODSGROUPID," +
+		"       c.DESTGOODSGROUPID," +
+		"       c.CONVERTRATIO," +
+		"       g.GOODSID," +
+		"       g.GOODSCODE," +
+		"       g.GOODSNAME," +
+		"       g.GOODSGROUPID," +
+		"       g.AGREEUNIT" +
+		"  FROM ERMS_MIDDLEGOODS t" +
+		" INNER JOIN ERMCP_GGCONVERTCONFIG c" +
+		"    ON t.GOODSGROUPID = c.DESTGOODSGROUPID" +
+		" INNER JOIN GOODS g" +
+		"    ON c.SRCGOODSGROUPID = g.GOODSGROUPID" +
+		" WHERE t.AREAUSERID = %v" +
+		"   AND t.MIDDLEGOODSID = %v"
+	sqlId = fmt.Sprintf(sqlId, r.AREAUSERID, r.MIDDLEGOODSID)
+	return sqlId
+}
+
+// 获取套保商品关联的交易商品
+func (r *ErmcpTradeGoods) GetData() (map[int32]*ErmcpTradeGoods, error) {
+	mData := make(map[int32]*ErmcpTradeGoods, 0)
+	err := db.GetEngine().SQL(r.buildSq()).Find(&mData)
+	return mData, err
+}
+
+// 获取用户头寸
+type ErmcpTradePosition struct {
+	USERID             int32 `json:"userid"  xorm:"'userid'"`                         // 用户id
+	GOODSID            int32 `json:"goodsid"  xorm:"'goodsid'"`                       // 商品id
+	BUYPOSITIONQTY     int64 `json:"buypositionqty"  xorm:"'Buypositionqty'"`         // 买期初持仓
+	BUYCURPOSITIONQTY  int64 `json:"buycurpositionqty"  xorm:"'Buycurpositionqty'"`   // 买当前持仓
+	SELLPOSITIONQTY    int64 `json:"sellpositionqty"  xorm:"'Sellpositionqty'"`       // 卖期初持仓
+	SELLCURPOSITIONQTY int64 `json:"sellcurpositionqty"  xorm:"'Sellcurpositionqty'"` // 卖当前持仓
+}
+
+func (r *ErmcpTradePosition) buildSql() string {
+	sqlId := "select a.userid," +
+		"       a.goodsid," +
+		"       sum(a.buypositionqty) buypositionqty," +
+		"       sum(a.buycurpositionqty) buycurpositionqty," +
+		"       sum(a.sellpositionqty) sellpositionqty," +
+		"       sum(a.sellcurpositionqty) sellcurpositionqty" +
+		"  from (select ta.userid, t.*" +
+		"          from tradeposition t" +
+		"         inner join taaccount ta" +
+		"            on t.accountid = ta.accountid" +
+		"         where ta.userid = %v" +
+		"           ) a" +
+		" group by a.userid, a.goodsid"
+	sqlId = fmt.Sprintf(sqlId, r.USERID)
+	return sqlId
+}
+
+// 获取用户持仓头寸
+func (r *ErmcpTradePosition) GetData() ([]ErmcpTradePosition, error) {
+	sData := make([]ErmcpTradePosition, 0)
+	err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
+	return sData, err
+}
+
+// 实时敞口\现货明细(头寸)
+type ErmcpExposurePostion struct {
+	AREAUSERID    int32   `json:"areauserid"  xorm:"'AREAUSERID'"`       // 所属机构id
+	MIDDLEGOODSID int32   `json:"middlegoodsid"  xorm:"'MIDDLEGOODSID'"` // 套保商品id
+	GOODSID       int32   `json:"goodsid"  xorm:"'GOODSID'"`             // 商品id
+	GOODSCODE     string  `json:"goodscode"  xorm:"'GOODSCODE'"`         // 商品代码
+	GOODSNAME     string  `json:"goodsname"  xorm:"'GOODSNAME'"`         // 商品名称
+	YdQty         int64   `json:"ydqty"  xorm:"'YdQty'"`                 // 昨日持仓
+	CurQty        int64   `json:"curqty"  xorm:"'CurQty'"`               // 当前持仓
+	DiffQty       int64   `json:"diffqty"  xorm:"'DiffQty'"`             // 持仓变动量
+	DiffHedgeQty  float64 `json:"diffhedgeqty"  xorm:"'DiffHedgeQty'"`   // 套保品种变动量
+}
+
+// 获取敞口明细期货头寸
+func (r *ErmcpExposurePostion) GetDataEx() (interface{}, error) {
+	// 查询交易商品
+	mg := ErmcpTradeGoods{AREAUSERID: r.AREAUSERID, MIDDLEGOODSID: r.MIDDLEGOODSID}
+	sGoods, err := mg.GetData()
+	if err != nil || sGoods == nil || len(sGoods) == 0 {
+		return nil, err
+	}
+
+	// 查询头寸
+	mp := ErmcpTradePosition{USERID: r.AREAUSERID}
+	sPostion, err1 := mp.GetData()
+	if err1 != nil || sPostion == nil || len(sPostion) == 0 {
+		return nil, err1
+	}
+
+	// 合并处理
+	sData := make([]ErmcpExposurePostion, 0)
+	for i := range sPostion {
+		if val, ok := sGoods[sPostion[i].GOODSID]; ok {
+			d := ErmcpExposurePostion{
+				AREAUSERID:    val.AREAUSERID,
+				MIDDLEGOODSID: val.MIDDLEGOODSID,
+				GOODSID:       val.MIDDLEGOODSID,
+				GOODSCODE:     val.GOODSCODE,
+				GOODSNAME:     val.GOODSNAME,
+			}
+			//相关计算
+			d.YdQty = sPostion[i].BUYPOSITIONQTY - sPostion[i].SELLPOSITIONQTY
+			d.CurQty = sPostion[i].BUYCURPOSITIONQTY - sPostion[i].SELLCURPOSITIONQTY
+			d.DiffQty = d.CurQty - d.YdQty
+			d.DiffHedgeQty = float64(d.DiffQty) * float64(val.AGREEUNIT) * val.CONVERTRATIO
+			sData = append(sData, d)
+		}
+	}
+	return sData, nil
+}

+ 1 - 1
models/ermcpGGConvertconfig.go

@@ -88,7 +88,7 @@ func (r *ErmcpAvalidGoodsGroupModel) GetDataEx() (interface{}, error) {
 }
 
 func (r *ErmcpAvalidGoodsGroupModel) buildSql() string {
-	sqlId := "select g.groupcategroyid," +
+	sqlId := "select distinct g.groupcategroyid," +
 		"       g.goodsgroupid," +
 		"       g.goodsgroupname," +
 		"       g.outergroupcode," +

+ 1 - 0
routers/router.go

@@ -329,6 +329,7 @@ func InitRouter() *gin.Engine {
 		ermcpR.GET("/QueryGGConvertConfig", ermcp.QueryGGConvertConfig)
 		ermcpR.GET("/QueryMiddleGoodsChangeLog", ermcp.QueryMiddleGoodsChangeLog)
 		ermcpR.GET("/QueryAvaildGoodsGroup", ermcp.QueryAvaildGoodsGroup)
+		ermcpR.GET("/QueryRealtimeExposurePosition", ermcp.QueryRealtimeExposurePosition)
 	}
 
 	return r

+ 18 - 0
utils/sqlUtils.go

@@ -0,0 +1,18 @@
+/**
+* @Author: zou.yingbin
+* @Create  : 2021/1/29 9:30
+* @Modify  : 2021/1/29 9:30
+ */
+
+package utils
+
+import "fmt"
+
+// sql辅助构建工具
+
+type SQLVal string
+
+// 增加and 条件
+func (r *SQLVal) And(fieldName string, val interface{}) {
+	*r = *r + SQLVal(fmt.Sprintf(" and %v = %v", fieldName, val))
+}