Explorar el Código

查实时敞口期货明细改为查外部头寸

zou.yingbin hace 4 años
padre
commit
90bb8f3837
Se han modificado 1 ficheros con 97 adiciones y 22 borrados
  1. 97 22
      models/ermcpExposure.go

+ 97 - 22
models/ermcpExposure.go

@@ -446,7 +446,7 @@ func (r *ErmcpTradeGoods) GetData() (map[int32]*ErmcpTradeGoods, error) {
 	return mData, err
 }
 
-// 获取用户头寸
+// 获取用户头寸(子账户)
 type ErmcpTradePosition struct {
 	USERID             int32 `json:"userid"  xorm:"'userid'"`                         // 用户id
 	GOODSID            int32 `json:"goodsid"  xorm:"'goodsid'"`                       // 商品id
@@ -457,7 +457,7 @@ type ErmcpTradePosition struct {
 }
 
 func (r *ErmcpTradePosition) buildSql() string {
-	sqlId := "select a.userid," +
+	sqlId := "select a.relateduserid userid," +
 		"       a.goodsid," +
 		"       sum(a.buypositionqty) buypositionqty," +
 		"       sum(a.buycurpositionqty) buycurpositionqty," +
@@ -467,21 +467,78 @@ func (r *ErmcpTradePosition) buildSql() string {
 		"          from tradeposition t" +
 		"         inner join taaccount ta" +
 		"            on t.accountid = ta.accountid" +
-		"         where ta.userid = %v" +
+		"         where ta.relateduserid = %v and ismain=0" +
 		"           ) 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 ErmcpHedgePosition struct {
+	RELATEDUSERID     int32  `json:"relateduserid"  xorm:"'RELATEDUSERID'"`         // 关联用户id
+	ACCOUNTID         int64  `json:"accountid"  xorm:"'ACCOUNTID'"`                 // 资金账号[外部母账户]
+	HEDGEGOODSID      int32  `json:"hedgegoodsid"  xorm:"'HEDGEGOODSID'"`           // 对冲合约ID
+	HEDGEACCOUNTCODE  string `json:"hedgeaccountcode"  xorm:"'HEDGEACCOUNTCODE'"`   // 对冲账号
+	TRADEDATE         string `json:"tradedate"  xorm:"'TRADEDATE'"`                 // 交易日(yyyyMMdd)
+	MARKETID          int32  `json:"marketid"  xorm:"'mARKETID'"`                   // 市场ID
+	YDBUYPOSITION     int32  `json:"ydbuyposition"  xorm:"'YDBUYPOSITION'"`         // 期初买头寸
+	CURBUYPOSITION    int32  `json:"curbuyposition"  xorm:"'CURBUYPOSITION'"`       // 期末买头寸
+	CURYDBUYPOSITION  int32  `json:"curydbuyposition"  xorm:"'CURYDBUYPOSITION'"`   // 期末上日买头寸
+	CURTDBUYPOSITION  int32  `json:"curtdbuyposition"  xorm:"'CURTDBUYPOSITION'"`   // 期末今日买头寸
+	FREYDBUYPOSITION  int32  `json:"freydbuyposition"  xorm:"'FREYDBUYPOSITION'"`   // 冻结上日买头寸
+	FRETDBUYPOSITION  int32  `json:"fretdbuyposition"  xorm:"'FRETDBUYPOSITION'"`   // 冻结今日买头寸
+	YDSELLPOSITION    int32  `json:"ydsellposition"  xorm:"'YDSELLPOSITION'"`       // 期初卖头寸
+	CURSELLPOSITION   int32  `json:"cursellposition"  xorm:"'CURSELLPOSITION'"`     // 期末卖头寸
+	CURYDSELLPOSITION int32  `json:"curydsellposition"  xorm:"'CURYDSELLPOSITION'"` // 期末上日卖头寸
+	CURTDSELLPOSITION int32  `json:"curtdsellposition"  xorm:"'CURTDSELLPOSITION'"` // 期末今日卖头寸
+	FREYDSELLPOSITION int32  `json:"freydsellposition"  xorm:"'FREYDSELLPOSITION'"` // 冻结上日卖头寸
+	FRETDSELLPOSITION int32  `json:"fretdsellposition"  xorm:"'FRETDSELLPOSITION'"` // 冻结今日卖头寸
+}
+
+func (r *ErmcpHedgePosition) buildSql() string {
+	sqlId := "select ta.RELATEDUSERID," +
+		"       t.ACCOUNTID," +
+		"       t.HEDGEGOODSID," +
+		"       t.HEDGEACCOUNTCODE," +
+		"       t.TRADEDATE," +
+		"       t.MARKETID," +
+		"       t.YDBUYPOSITION," +
+		"       t.CURBUYPOSITION," +
+		"       t.CURYDBUYPOSITION," +
+		"       t.CURTDBUYPOSITION," +
+		"       t.FREYDBUYPOSITION," +
+		"       t.FRETDBUYPOSITION," +
+		"       t.YDSELLPOSITION," +
+		"       t.CURSELLPOSITION," +
+		"       t.CURYDSELLPOSITION," +
+		"       t.CURTDSELLPOSITION," +
+		"       t.FREYDSELLPOSITION," +
+		"       t.FRETDSELLPOSITION" +
+		"  from hedge_outtradeposition t" +
+		" inner join taaccount ta" +
+		"    on t.accountid = ta.accountid" +
+		"   and ta.ismain = 1" +
+		"   and ta.relateduserid = %v"
+	sqlId = fmt.Sprintf(sqlId, r.RELATEDUSERID)
+	return sqlId
+}
+
+// 获取对冲头寸(母账号头寸)
+func (r *ErmcpHedgePosition) GetData() ([]ErmcpHedgePosition, error) {
+	sData := make([]ErmcpHedgePosition, 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
@@ -490,8 +547,36 @@ type ErmcpExposurePostion struct {
 	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'"`   // 套保品种变动量
+	DiffQty       int64   `json:"diffqty"  xorm:"'DiffQty'"`             // 持仓变动量=当前持仓-昨日持仓
+	DiffHedgeQty  float64 `json:"diffhedgeqty"  xorm:"'DiffHedgeQty'"`   // 套保品种变动量=持仓变动量*期货合约单位*期货品种系数
+}
+
+// 子账户相关计算(不一定用得到,现在说都是查母账号的)
+func (r *ErmcpExposurePostion) ParseFromPos(val *ErmcpTradeGoods, data *ErmcpTradePosition) {
+	r.AREAUSERID = val.AREAUSERID
+	r.MIDDLEGOODSID = val.MIDDLEGOODSID
+	r.GOODSID = val.MIDDLEGOODSID
+	r.GOODSCODE = val.GOODSCODE
+	r.GOODSNAME = val.GOODSNAME
+	//相关计算
+	r.YdQty = data.BUYPOSITIONQTY - data.SELLPOSITIONQTY
+	r.CurQty = data.BUYCURPOSITIONQTY - data.SELLCURPOSITIONQTY
+	r.DiffQty = r.CurQty - r.YdQty
+	r.DiffHedgeQty = float64(r.DiffQty) * float64(val.AGREEUNIT) * val.CONVERTRATIO
+}
+
+// 母账户相关计算
+func (r *ErmcpExposurePostion) ParseFromHedgePos(val *ErmcpTradeGoods, data *ErmcpHedgePosition) {
+	r.AREAUSERID = val.AREAUSERID
+	r.MIDDLEGOODSID = val.MIDDLEGOODSID
+	r.GOODSID = val.MIDDLEGOODSID
+	r.GOODSCODE = val.GOODSCODE
+	r.GOODSNAME = val.GOODSNAME
+	//相关计算
+	r.YdQty = int64(data.YDBUYPOSITION - data.YDSELLPOSITION)
+	r.CurQty = int64(data.CURBUYPOSITION - data.CURSELLPOSITION)
+	r.DiffQty = r.CurQty - r.YdQty
+	r.DiffHedgeQty = float64(r.DiffQty) * float64(val.AGREEUNIT) * val.CONVERTRATIO
 }
 
 // 获取敞口明细期货头寸
@@ -503,8 +588,8 @@ func (r *ErmcpExposurePostion) GetDataEx() (interface{}, error) {
 		return nil, err
 	}
 
-	// 查询头寸
-	mp := ErmcpTradePosition{USERID: r.AREAUSERID}
+	// 查询头寸(母账号)
+	mp := ErmcpHedgePosition{RELATEDUSERID: r.AREAUSERID}
 	sPostion, err1 := mp.GetData()
 	if err1 != nil || sPostion == nil || len(sPostion) == 0 {
 		return nil, err1
@@ -513,19 +598,9 @@ func (r *ErmcpExposurePostion) GetDataEx() (interface{}, error) {
 	// 合并处理
 	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
+		if val, ok := sGoods[sPostion[i].HEDGEGOODSID]; ok {
+			d := ErmcpExposurePostion{}
+			d.ParseFromHedgePos(val, &sPostion[i])
 			sData = append(sData, d)
 		}
 	}