|
|
@@ -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
|
|
|
+}
|