package erms2 import ( "fmt" "mtp2_if/db" "mtp2_if/global/app" "mtp2_if/global/e" "mtp2_if/logger" "net/http" "time" "github.com/gin-gonic/gin" ) // QueryInnerTradeDetailReq 查询内部成交单信息请求参数 type QueryInnerTradeDetailReq struct { AccountID int `form:"accountid" binding:"required"` } // QueryInnerTradeDetailRsp 内部成交单信息 type QueryInnerTradeDetailRsp struct { Tradeid string `json:"tradeid" xorm:"'TRADEID'" binding:"required"` // 成交单号(108+Unix秒时间戳(10位)+2位(MarketServiceID)+xxxx) Buyorsell int64 `json:"buyorsell" xorm:"'BUYORSELL'" binding:"required"` // 方向 - 0:买 1:卖 Orderid string `json:"orderid" xorm:"'ORDERID'"` // 委托单号 Accountid int64 `json:"accountid" xorm:"'ACCOUNTID'"` // 账号ID Goodsid int64 `json:"goodsid" xorm:"'GOODSID'"` // 商品ID Marketid int64 `json:"marketid" xorm:"'MARKETID'"` // 市场ID Tradetime time.Time `json:"tradetime" xorm:"'TRADETIME'"` // 成交时间 Tradeprice float64 `json:"tradeprice" xorm:"'TRADEPRICE'"` // 成交价格 Tradeqty int64 `json:"tradeqty" xorm:"'TRADEQTY'"` // 成交数量 Channelbuildtype int64 `json:"channelbuildtype" xorm:"'CHANNELBUILDTYPE'"` // 委托单据类型 0:无 1:建仓 2:平仓 Closetype int64 `json:"closetype" xorm:"'CLOSETYPE'"` // 平仓方式 - 0:无 1:平今 2:平昨 Relatedouttradeid string `json:"relatedouttradeid" xorm:"'RELATEDOUTTRADEID'"` // 关联外部成交单ID Asapplyid int64 `json:"asapplyid" xorm:"'ASAPPLYID'" binding:"required"` // 策略申请ID Detailtype int64 `json:"detailtype" xorm:"'DETAILTYPE'"` // 明细类型 - 1:套利对冲 2:期货换月 3:期货仓位调整 Spotcontractid int64 `json:"spotcontractid" xorm:"'SPOTCONTRACTID'"` // 现货合同ID [1:套利对冲 为合同ID,2:期货换月\3:期货仓位调整时为0] Asno string `json:"asno" xorm:"'ASNO'"` // 策略编号 Asname string `json:"asname" xorm:"'ASNAME'"` // 策略名称 Spotcontractno string `json:"spotcontractno" xorm:"'SPOTCONTRACTNO'"` // 现货合同编号 Remark string `json:"remark" xorm:"'REMARK'"` // 备注 Updatetime time.Time `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间 Goodscode string `json:"goodscode" xorm:"'GOODSCODE'" binding:"required"` // 商品代码(合约) Goodsname string `json:"goodsname" xorm:"'GOODSNAME'" binding:"required"` // 商品名称(合约) Agreeunit float64 `json:"agreeunit" xorm:"'AGREEUNIT'"` // 合约单位 Goodunit string `json:"goodunit" xorm:"'GOODUNIT'"` // 报价单位 Exexchangecode string `json:"exexchangecode" xorm:"'EXEXCHANGECODE'"` // 外部交易所代码 Exexchangename string `json:"exexchangename" xorm:"'EXEXCHANGENAME'"` // 外部交易所名称 Outgoodscode string `json:"outgoodscode" xorm:"'OUTGOODSCODE'"` // 商品代码(外部) Decimalplace int64 `json:"decimalplace" xorm:"'DECIMALPLACE'"` // 报价小数位 Goodsgroupid int64 `json:"goodsgroupid" xorm:"'GOODSGROUPID'" binding:"required"` // 商品组ID(品种ID) Goodsgroupname string `json:"goodsgroupname" xorm:"'GOODSGROUPNAME'" binding:"required"` // 商品组名称(品种) } // QueryInnerTradeDetail 查询内部成交单信息 // @Summary 查询内部成交单信息 // @Produce json // @Security ApiKeyAuth // @Param accountid query int true "资金账户" // @Success 200 {object} QueryInnerTradeDetailRsp // @Failure 500 {object} app.Response // @Router /Erms2/QueryInnerTradeDetail [get] // @Tags 风险管理 func QueryInnerTradeDetail(c *gin.Context) { appG := app.Gin{C: c} // 获取请求参数 var req QueryInnerTradeDetailReq if err := appG.C.ShouldBindQuery(&req); err != nil { logger.GetLogger().Errorf("QueryInnerTradeDetail failed: %s", err.Error()) appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil) return } // 查询数据 engine := db.GetEngine() datas := make([]QueryInnerTradeDetailRsp, 0) sql := fmt.Sprintf(`select to_char(t.Tradeid) Tradeid, t.Buyorsell, to_char(t.Orderid) Orderid, t.Accountid, t.Goodsid, t.Marketid, t.Tradetime, t.Tradeprice, t.Tradeqty, t.Channelbuildtype, t.Closetype, to_char(t.Relatedouttradeid) Relatedouttradeid, ast.Asapplyid, ast.Detailtype, ast.Spotcontractid, ast.Remark, ast.Updatetime, asd.Asno, asd.ASName, sc.contractno Spotcontractno, g.Goodscode, g.Goodsname, g.Agreeunit, g.Outgoodscode, g.Decimalplace, e.enumdicname GoodUnit, ee.Exexchangecode, ee.Exexchangename, gg.Goodsgroupid, gg.Goodsgroupname from Hedge_InnerTradeDetail t left join ERMS2_ASTradeDetails ast on t.relatedouttradeid = OutTradeID left join ERMS2_ArbitrageStrategy asd on ast.asapplyid = asd.asapplyid left join ERMS3_SpotContract sc on ast.spotcontractid = sc.spotcontractid left join goods g on t.goodsid = g.goodsid left join enumdicitem e on g.goodunitid = e.enumitemname and e.enumdiccode = 'goodsunit' left join goodsgroup gg on g.goodsgroupid = gg.goodsgroupid left join ExternalExchange ee on gg.exexchangeid = ee.autoid where t.Accountid = %d`, req.AccountID) if err := engine.SQL(sql).Find(&datas); err != nil { // 查询失败 logger.GetLogger().Errorf("QueryInnerTradeDetail failed: %s", err.Error()) appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil) return } // 查询成功 logger.GetLogger().Debugln("QueryInnerTradeDetail successed: %v", datas) appG.Response(http.StatusOK, e.SUCCESS, datas) } // QueryArbitrageStrategyReq 查询期现套利策略表信息请求参数 type QueryArbitrageStrategyReq struct { // AccountID int `form:"accountid" binding:"required"` UserID int `form:"userid" binding:"required"` GoodsGroupID int `form:"goodsgroupid"` } // QueryArbitrageStrategyRsp 期现套利策略表信息 type QueryArbitrageStrategyRsp struct { Asapplyid string `json:"asapplyid" xorm:"'ASAPPLYID'" binding:"required"` // 策略申请ID(702+Unix秒时间戳(10位)+xxxxxx) Asno string `json:"asno" xorm:"'ASNO'"` // 策略编号 Biztype int64 `json:"biztype" xorm:"'BIZTYPE'"` // 业务类型 - 1:正向套利 -1:反向套利 Userid int64 `json:"userid" xorm:"'USERID'"` // 所属机构 Deliverygoodsid int64 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货品种ID Goodsgroupid int64 `json:"goodsgroupid" xorm:"'GOODSGROUPID'"` // 期货品种ID Spotquota float64 `json:"spotquota" xorm:"'SPOTQUOTA'"` // 现货额度 Futurequote float64 `json:"futurequote" xorm:"'FUTUREQUOTE'"` // 期货额度 Applybasis float64 `json:"applybasis" xorm:"'APPLYBASIS'"` // 申请基差 Strategystatus int64 `json:"strategystatus" xorm:"'STRATEGYSTATUS'"` // 策略状态 - 0:未结束 1:已结束 Remark string `json:"remark" xorm:"'REMARK'"` // 备注 Marketid int64 `json:"marketid" xorm:"'MARKETID'"` // 市场ID Tradedate string `json:"tradedate" xorm:"'TRADEDATE'"` // 交易日(yyyyMMdd) Closetradedate string `json:"closetradedate" xorm:"'CLOSETRADEDATE'"` // 完结交易日(yyyyMMdd) Usedquota float64 `json:"usedquota" xorm:"'USEDQUOTA'"` // 已占用资金 Futureqty float64 `json:"futureqty" xorm:"'FUTUREQTY'"` // 期货持仓数量 Futureavgprice float64 `json:"futureavgprice" xorm:"'FUTUREAVGPRICE'"` // 期货建仓均价 Futurepl float64 `json:"futurepl" xorm:"'FUTUREPL'"` // 期货总盈亏[结算更新] Pricedspotqty float64 `json:"pricedspotqty" xorm:"'PRICEDSPOTQTY'"` // 已定价现货数量 Pricedspotqtynotax float64 `json:"pricedspotqtynotax" xorm:"'PRICEDSPOTQTYNOTAX'"` // 已定价现货不含税数量 Spotavgprice float64 `json:"spotavgprice" xorm:"'SPOTAVGPRICE'"` // 现货均价 Spotpl float64 `json:"spotpl" xorm:"'SPOTPL'"` // 现货总盈亏[结算更新] Netexposure float64 `json:"netexposure" xorm:"'NETEXPOSURE'"` // 单笔业务头寸净敞口 = 期货持仓数量 + 已定价现货不含税数量 Netexposurerate float64 `json:"netexposurerate" xorm:"'NETEXPOSURERATE'"` // 净敞口比例 - 0:未结束 = (NetExposure/PriceSpotQtyNoTax) ; 已结束为0 Totalpl float64 `json:"totalpl" xorm:"'TOTALPL'"` // 业务合计损益 = FuturePL + SpotPL [结算更新] Openbasis float64 `json:"openbasis" xorm:"'OPENBASIS'"` // 建仓基差 Curbasis float64 `json:"curbasis" xorm:"'CURBASIS'"` // 当前基差[结算更新] Basischangepl float64 `json:"basischangepl" xorm:"'BASISCHANGEPL'"` // 基差变动损益[结算更新] Netexposurepl float64 `json:"netexposurepl" xorm:"'NETEXPOSUREPL'"` // 净敞口损益 = TotalPL - BasisChangePL[结算更新] Spotusedquota float64 `json:"spotusedquota" xorm:"'SPOTUSEDQUOTA'"` // 现货占用资金 Futureopenqty float64 `json:"futureopenqty" xorm:"'FUTUREOPENQTY'"` // 期货开仓数量 Futureopenamount float64 `json:"futureopenamount" xorm:"'FUTUREOPENAMOUNT'"` // 期货开仓金额 Futurecloseqty float64 `json:"futurecloseqty" xorm:"'FUTURECLOSEQTY'"` // 期货平仓数量 Futurecloseamount float64 `json:"futurecloseamount" xorm:"'FUTURECLOSEAMOUNT'"` // 期货平仓金额 Spotbuyamount float64 `json:"spotbuyamount" xorm:"'SPOTBUYAMOUNT'"` // 现货采购金额 Spotbuyqty float64 `json:"spotbuyqty" xorm:"'SPOTBUYQTY'"` // 现货采购数量 Spotsellamount float64 `json:"spotsellamount" xorm:"'SPOTSELLAMOUNT'"` // 现货销售金额 Spotsellqty float64 `json:"spotsellqty" xorm:"'SPOTSELLQTY'"` // 现货销售数量 Updatetime time.Time `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间 Asname string `json:"asname" xorm:"'ASNAME'"` // 策略名称 } // QueryArbitrageStrategy 查询期现套利策略表信息(指定资金账户、未结束的) // @Summary 查询期现套利策略表信息(指定资金账户、未结束的) // @Produce json // @Security ApiKeyAuth // @Param userid query int true "账户ID" // @Param goodsgroupid query string false "商品组ID(品种ID)" // @Success 200 {object} QueryArbitrageStrategyRsp // @Failure 500 {object} app.Response // @Router /Erms2/QueryArbitrageStrategy [get] // @Tags 风险管理 func QueryArbitrageStrategy(c *gin.Context) { appG := app.Gin{C: c} // 获取请求参数 var req QueryArbitrageStrategyReq if err := appG.C.ShouldBindQuery(&req); err != nil { logger.GetLogger().Errorf("QueryArbitrageStrategy failed: %s", err.Error()) appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil) return } // 查询数据 engine := db.GetEngine() datas := make([]QueryArbitrageStrategyRsp, 0) sql := fmt.Sprintf(`select to_char(t.ASApplyID) ASApplyID, t.ASNo, t.ASName, t.BizType, t.UserID, t.DeliveryGoodsID, t.GoodsGroupID, t.SpotQuota, t.FutureQuote, t.ApplyBasis, t.StrategyStatus, t.Remark, t.MarketID, t.TradeDate, t.CloseTradeDate, t.UsedQuota, t.FutureQty, t.FutureAvgPrice, t.FuturePL, t.PricedSpotQty, t.PricedSpotQtyNoTax, t.SpotAvgPrice, t.SpotPL, t.NetExposure, t.NetExposureRate, t.TotalPL, t.OpenBasis, t.CurBasis, t.BasisChangePL, t.NetExposurePL, t.SpotUsedQuota, t.FutureOpenQty, t.FutureOpenAmount, t.FutureCloseQty, t.FutureCloseAmount, t.SpotBuyAmount, t.SpotBuyQty, t.SpotSellAmount, t.SpotSellQty, t.UpdateTime from ERMS2_ArbitrageStrategy t left join ERMS2_ASAccount a on t.asapplyid = a.asapplyid and a.taaccounttype = 1 where t.StrategyStatus = 0 and t.userid = %d`, req.UserID) if req.GoodsGroupID > 0 { sql += fmt.Sprintf(" and t.GoodsGroupID = %d", req.GoodsGroupID) } if err := engine.SQL(sql).Find(&datas); err != nil { // 查询失败 logger.GetLogger().Errorf("QueryArbitrageStrategy failed: %s", err.Error()) appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil) return } // 查询成功 logger.GetLogger().Debugln("QueryArbitrageStrategy successed: %v", datas) appG.Response(http.StatusOK, e.SUCCESS, datas) } // QuerySpotContractReq 查询现货合同表信息请求参数 type QuerySpotContractReq struct { AsApplyID uint64 `form:"asapplyid" binding:"required"` SpotContractID uint64 `form:"spotcontractid"` } // QuerySpotContractRsp 现货合同表 type QuerySpotContractRsp struct { Spotcontractid string `json:"spotcontractid" xorm:"'SPOTCONTRACTID'" binding:"required"` // 现货合同ID(701+Unix秒时间戳(10位)+xxxxxx) Tradedate string `json:"tradedate" xorm:"'TRADEDATE'"` // 交易日(yyyyMMdd) Contractno string `json:"contractno" xorm:"'CONTRACTNO'"` // 现货合同编号 Contracttype int64 `json:"contracttype" xorm:"'CONTRACTTYPE'"` // 现货合同类型 - 1:采购合同 -1:销售合同 Areauserid int64 `json:"areauserid" xorm:"'AREAUSERID'"` // 所属机构 Userid int64 `json:"userid" xorm:"'USERID'"` // 业务员用户ID Accountid int64 `json:"accountid" xorm:"'ACCOUNTID'"` // 资金账户ID Customeruserid int64 `json:"customeruserid" xorm:"'CUSTOMERUSERID'"` // 客户ID Customeraccountid int64 `json:"customeraccountid" xorm:"'CUSTOMERACCOUNTID'"` // 客户资金账户ID Signdate time.Time `json:"signdate" xorm:"'SIGNDATE'"` // 签订日期 Lastdate time.Time `json:"lastdate" xorm:"'LASTDATE'"` // 交货时间 Contractattachment string `json:"contractattachment" xorm:"'CONTRACTATTACHMENT'"` // 合同附件 Producttype int64 `json:"producttype" xorm:"'PRODUCTTYPE'"` // 产品类型 - 1:标准仓单 2:等标 3:非标 Deliverygoodsid int64 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货品种ID Deliverygoodsdesc string `json:"deliverygoodsdesc" xorm:"'DELIVERYGOODSDESC'"` // 品种说明 Warehouseid int64 `json:"warehouseid" xorm:"'WAREHOUSEID'"` // 仓库ID Wrfactortypeid int64 `json:"wrfactortypeid" xorm:"'WRFACTORTYPEID'"` // 仓单要素类型ID Contractqtychar string `json:"contractqtychar" xorm:"'CONTRACTQTYCHAR'"` // 合同数量\已订价数量 (用于显示) Spotprice float64 `json:"spotprice" xorm:"'SPOTPRICE'"` // 价格 Contractamount float64 `json:"contractamount" xorm:"'CONTRACTAMOUNT'"` // 合同金额 Marketid int64 `json:"marketid" xorm:"'MARKETID'"` // 市场ID Remark string `json:"remark" xorm:"'REMARK'"` // 备注 Handlestatus int64 `json:"handlestatus" xorm:"'HANDLESTATUS'"` // 处理状态 Contractqty float64 `json:"contractqty" xorm:"'CONTRACTQTY'"` // 合同数量(数值) (用于计算) Positionqty int64 `json:"positionqty" xorm:"'POSITIONQTY'"` // 头寸数量 - 合同数量去小数部分 Paystatus int64 `json:"paystatus" xorm:"'PAYSTATUS'"` // 收付款状态 - 0:未支付 1:已收款 2:已付款 Payremark string `json:"payremark" xorm:"'PAYREMARK'"` // 收付款备注 Paydatetime time.Time `json:"paydatetime" xorm:"'PAYDATETIME'"` // 收付款更新时间 Invoicestatus int64 `json:"invoicestatus" xorm:"'INVOICESTATUS'"` // 开收票状态 - 0:未开票 1:已开票 Invoiceremark string `json:"invoiceremark" xorm:"'INVOICEREMARK'"` // 发票备注 Invoiceatt string `json:"invoiceatt" xorm:"'INVOICEATT'"` // 发票附件 Invoicedatetime time.Time `json:"invoicedatetime" xorm:"'INVOICEDATETIME'"` // 开收票更新时间 Spotstatus int64 `json:"spotstatus" xorm:"'SPOTSTATUS'"` // 收发货状态 - 0:未交收 1:已发货 2:已发货 Spotremark string `json:"spotremark" xorm:"'SPOTREMARK'"` // 收发货备注 Spotdatetime time.Time `json:"spotdatetime" xorm:"'SPOTDATETIME'"` // 收发货更新时间 Relatedqty float64 `json:"relatedqty" xorm:"'RELATEDQTY'"` // 已关联数量 Contractstatus int64 `json:"contractstatus" xorm:"'CONTRACTSTATUS'"` // 合同状态 - 0:未结束 1:已结束 Closeremark string `json:"closeremark" xorm:"'CLOSEREMARK'"` // 结束备注 Closetradedate string `json:"closetradedate" xorm:"'CLOSETRADEDATE'"` // 完结交易日(yyyyMMdd) Relatedstatus int64 `json:"relatedstatus" xorm:"'RELATEDSTATUS'"` // 关联完结状态 - 0:未结束 1:已结束 Wrstandardid int64 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 仓单标准ID(SEQ_WRSTANDARD) Invoiceopentime time.Time `json:"invoiceopentime" xorm:"'INVOICEOPENTIME'"` // 开票时间 Closetype int64 `json:"closetype" xorm:"'CLOSETYPE'"` // 终止类型 - 1:违约 2:提前终止 Closedate time.Time `json:"closedate" xorm:"'CLOSEDATE'"` // 终止日期 } // QuerySpotContract 查询现货合同表信息(指定策略ID、未结束的) // @Summary 查询现货合同表信息(指定策略ID、未结束的) // @Produce json // @Security ApiKeyAuth // @Param asapplyid query uint64 true "策略申请ID" // @Param spotcontractid query uint64 false "现货合同ID" // @Success 200 {object} QuerySpotContractRsp // @Failure 500 {object} app.Response // @Router /Erms2/QuerySpotContract [get] // @Tags 风险管理 func QuerySpotContract(c *gin.Context) { appG := app.Gin{C: c} // 获取请求参数 var req QuerySpotContractReq if err := appG.C.ShouldBindQuery(&req); err != nil { logger.GetLogger().Errorf("QuerySpotContract failed: %s", err.Error()) appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil) return } // 查询数据 engine := db.GetEngine() datas := make([]QuerySpotContractRsp, 0) sql := fmt.Sprintf(`select to_char(t.SpotContractID) SpotContractID, t.TradeDate, t.ContractNo, t.ContractType, t.AreaUserID, t.UserID, t.AccountID, t.CustomerUserID, t.CustomerAccountID, t.SignDate, t.LastDate, t.ContractAttachment, t.ProductType, t.DeliveryGoodsID, t.DeliveryGoodsDesc, t.WarehouseID, t.WRStandardID, t.WRFactorTypeID, t.ContractQtyChar, t.SpotPrice, t.ContractAmount, t.MarketID, t.Remark, t.HandleStatus, t.ContractQty, t.PositionQty, t.PayStatus, t.PayRemark, t.PayDateTime, t.InvoiceStatus, t.InvoiceRemark, t.InvoiceAtt, t.InvoiceOpenTime, t.InvoiceDateTime, t.SpotStatus, t.SpotRemark, t.SpotDateTime, t.RelatedQty, t.ContractStatus, t.CloseType, t.CloseDate, t.CloseRemark, t.CloseTradeDate, t.RelatedStatus from ERMS2_SpotContract t left join ERMS2_ASSpotDetail a on t.spotcontractid = a.spotcontractid where t.ContractStatus = 0 and not exists ( select 1 from ERMS2_ASTradeDetails e where e.spotcontractid=t.spotcontractid ) and a.asapplyid = %d`, req.AsApplyID) if req.SpotContractID > 0 { sql += fmt.Sprintf(" and a.spotcontractid = %d", req.SpotContractID) } if err := engine.SQL(sql).Find(&datas); err != nil { // 查询失败 logger.GetLogger().Errorf("QuerySpotContract failed: %s", err.Error()) appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil) return } // 查询成功 logger.GetLogger().Debugln("QuerySpotContract successed: %v", datas) appG.Response(http.StatusOK, e.SUCCESS, datas) }