order.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. package order
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "mtp2_if/db"
  6. "mtp2_if/global/app"
  7. "mtp2_if/global/e"
  8. "mtp2_if/logger"
  9. "mtp2_if/models"
  10. "mtp2_if/utils"
  11. "net/http"
  12. "strconv"
  13. "time"
  14. "github.com/gin-gonic/gin"
  15. )
  16. // QueryTradePositionReq 持仓汇总查询请求参数(合约市场)
  17. type QueryTradePositionReq struct {
  18. AccountID string `form:"accountID" binding:"required"`
  19. TradeMode string `form:"tradeMode"`
  20. }
  21. // QueryTradePositionRsp 持仓汇总查询返回模型(合约市场)
  22. type QueryTradePositionRsp struct {
  23. AccountID int64 `json:"accountid" xorm:"'ACCOUNTID'"` // 资金账户
  24. BuyOrSell int64 `json:"buyorsell" xorm:"'BUYORSELL'" ` // 方向 - 0:买 1:卖
  25. Goodsid int32 `json:"goodsid" xorm:"'GOODSID'" binding:"required"` // 商品Id
  26. GoodsCode string `json:"goodscode" xorm:"GOODSCODE"` // 商品代码
  27. GoodsName string `json:"goodsname" xorm:"GOODSNAME"` // 商品名称
  28. AgreeUnit float64 `json:"agreeunit" xorm:"'AGREEUNIT'"` // 合约单位
  29. CurrencyID int64 `json:"currencyid" xorm:"'CURRENCYID'"` // 报价货币ID
  30. GoodUnitID int64 `json:"goodunitid" xorm:"'GOODUNITID'"` // 报价单位ID
  31. Goodunit string `json:"goodunit" xorm:"'GOODUNIT'"` // 报价单位
  32. DecimalPlace int64 `json:"decimalplace" xorm:"'DECIMALPLACE'"` // 报价小数位
  33. MarketID int64 `json:"marketid" xorm:"'MARKETID'"` // 所属市场ID
  34. TradeMode int32 `json:"trademode" xorm:"'TRADEMODE'"` // 交易模式
  35. PositionQTY int64 `json:"positionqty" xorm:"'POSITIONQTY'"` // 期初持仓数量
  36. HolderAmount float64 `json:"holderamount" xorm:"'HOLDERAMOUNT'"` // 期初持仓总金额[商品币种]
  37. CurPositionQTY int64 `json:"curpositionqty" xorm:"'CURPOSITIONQTY'"` // 当前持仓总数量
  38. CurHolderAmount float64 `json:"curholderamount" xorm:"'CURHOLDERAMOUNT'"` // 当前持仓总金额[商品币种]
  39. FrozenQTY int64 `json:"frozenqty" xorm:"'FROZENQTY'"` // 持仓冻结数量
  40. OtherFrozenQTY int64 `json:"otherfrozenqty" xorm:"'OTHERFROZENQTY'"` // 持仓其他冻结数量(交割冻结)
  41. OpenReqQTY int64 `json:"openreqqty" xorm:"'OPENREQQTY'"` // 开仓申请数量(用于比较最大持仓数量)
  42. OpenTotalQTY int64 `json:"opentotalqty" xorm:"'OPENTOTALQTY'"` // 开仓总数量
  43. CloseTotalQTY int64 `json:"closetotalqty" xorm:"'CLOSETOTALQTY'"` // 平仓总数量
  44. TNQTY int64 `json:"tnqty" xorm:"'TNQTY'"` // T+N冻结总量
  45. TNUsedQTY int64 `json:"tnusedqty" xorm:"'TNUSEDQTY'"` // T+N使用量(可以使用T+N的冻结数量)
  46. CurTDPosition int64 `json:"curtdposition" xorm:"'CURTDPOSITION'"` // 期末今日头寸
  47. FreTDPosition int64 `json:"fretdposition" xorm:"'FRETDPOSITION'"` // 冻结今日头寸
  48. EnableQTY int64 `json:"enableqty" xorm:"'ENABLEQTY'"` // 可用量
  49. AveragePrice float64 `json:"averageprice" xorm:"AVERAGEPRICE"` // 持仓均价
  50. Usedmargin float64 `json:"usedmargin" xorm:"'USEDMARGIN'"` // 占用保证金[商品币种]
  51. }
  52. // QueryTradePosition 持仓汇总查询(合约市场)
  53. // @Summary 持仓汇总查询(合约市场)
  54. // @Produce json
  55. // @Security ApiKeyAuth
  56. // @Param accountID query string true "资金账户 - 格式:1,2,3"
  57. // @Param tradeMode query string false "交易模式 - 格式:1,2,3"
  58. // @Success 200 {object} QueryTradePositionRsp
  59. // @Failure 500 {object} app.Response
  60. // @Router /Order/QueryTradePosition [get]
  61. // @Tags 通用单据
  62. // 参考通用查询:SearchTradePositionDetail
  63. func QueryTradePosition(c *gin.Context) {
  64. appG := app.Gin{C: c}
  65. // 获取请求参数
  66. var req QueryTradePositionReq
  67. if err := appG.C.ShouldBindQuery(&req); err != nil {
  68. logger.GetLogger().Errorf("QueryTradePosition failed: %s", err.Error())
  69. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  70. return
  71. }
  72. // 查询数据
  73. type tradePosition struct {
  74. models.Tradeposition `xorm:"extends"`
  75. Goodscode string `json:"goodscode" xorm:"'GOODSCODE'"` // 商品代码(内部)
  76. Goodsname string `json:"goodsname" xorm:"'GOODSNAME'"` // 商品名称
  77. Currencyid int64 `json:"currencyid" xorm:"'CURRENCYID'"` // 报价货币ID
  78. Goodunitid int64 `json:"goodunitid" xorm:"'GOODUNITID'"` // 报价单位ID
  79. Goodunit string `json:"goodunit" xorm:"'GOODUNIT'"` // 报价单位
  80. Agreeunit float64 `json:"agreeunit" xorm:"'AGREEUNIT'"` // 合约单位
  81. Decimalplace int64 `json:"decimalplace" xorm:"'DECIMALPLACE'"` // 报价小数位
  82. Marketid int32 `json:"marketid" xorm:"'MARKETID'"` // 市场ID
  83. Trademode int32 `json:"trademode" xorm:"'TRADEMODE'"` // 交易模式
  84. DECIMALPLACE int32 `json:"decimalplace" xorm:"'DECIMALPLACE'"` // 商品报价小数位
  85. }
  86. datas := make([]tradePosition, 0)
  87. engine := db.GetEngine()
  88. // ORACLE好像在JOIN里不支持别名功能(在XORM中)
  89. s := engine.Table("TRADEPOSITION").
  90. Join("LEFT", "GOODS", "TRADEPOSITION.GOODSID = GOODS.GOODSID").
  91. Join("LEFT", "MARKET", "GOODS.MARKETID = MARKET.MARKETID").
  92. Join("LEFT", "ENUMDICITEM", "GOODS.GOODUNITID = ENUMDICITEM.ENUMITEMNAME and ENUMDICITEM.ENUMDICCODE = 'goodsunit'").
  93. Select("TRADEPOSITION.*, GOODS.GOODSCODE, GOODS.GOODSNAME, GOODS.CURRENCYID, GOODS.GOODUNITID,GOODS.DECIMALPLACE, ENUMDICITEM.ENUMDICNAME as GOODUNIT, GOODS.AGREEUNIT, GOODS.DECIMALPLACE, MARKET.MARKETID, MARKET.TRADEMODE").
  94. Where(fmt.Sprintf(`TRADEPOSITION.ACCOUNTID in (%s)`, req.AccountID))
  95. if len(req.TradeMode) > 0 {
  96. s = s.And(fmt.Sprintf(`MARKET.TRADEMODE in (%s)`, req.TradeMode))
  97. }
  98. if err := s.Find(&datas); err != nil {
  99. // 查询失败
  100. logger.GetLogger().Errorf("QueryTradePosition failed: %s", err.Error())
  101. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  102. return
  103. }
  104. // 构建返回数据
  105. rst := make([]QueryTradePositionRsp, 0)
  106. for _, v := range datas {
  107. // 构建买方向持仓汇总
  108. if v.Buycurpositionqty > 0 {
  109. var tradePosition QueryTradePositionRsp
  110. // 反射数据
  111. // struct -> json
  112. if jsonBytes, err := json.Marshal(v); err == nil {
  113. // json -> struct
  114. json.Unmarshal(jsonBytes, &tradePosition)
  115. tradePosition.BuyOrSell = 0
  116. tradePosition.PositionQTY = v.Buypositionqty
  117. tradePosition.HolderAmount = v.Buyholderamount
  118. tradePosition.CurPositionQTY = v.Buycurpositionqty
  119. tradePosition.CurHolderAmount = v.Buycurholderamount
  120. tradePosition.FrozenQTY = v.Buyfrozenqty
  121. tradePosition.OtherFrozenQTY = v.Buyotherfrozenqty
  122. tradePosition.OpenReqQTY = v.Buyopenreqqty
  123. tradePosition.OpenTotalQTY = v.Buyopentotalqty
  124. tradePosition.CloseTotalQTY = v.Buyclosetotalqty
  125. tradePosition.TNQTY = v.Buytnqty
  126. tradePosition.TNUsedQTY = v.Buytnusedqty
  127. tradePosition.CurTDPosition = v.Buycurtdposition
  128. tradePosition.FreTDPosition = v.Buyfretdposition
  129. tradePosition.EnableQTY = v.Buycurpositionqty - v.Buyfrozenqty - v.Buyotherfrozenqty
  130. // 计算持仓均价
  131. averagePrice := tradePosition.CurHolderAmount / float64(tradePosition.CurPositionQTY) / tradePosition.AgreeUnit
  132. tradePosition.AveragePrice, _ = strconv.ParseFloat(utils.FormatFloat(averagePrice, int(v.Decimalplace)), 64)
  133. rst = append(rst, tradePosition)
  134. }
  135. }
  136. // 构建卖方向持仓汇总
  137. if v.Tradeproperty != 2 && v.Sellcurpositionqty > 0 {
  138. var tradePosition QueryTradePositionRsp
  139. // 反射数据
  140. // struct -> json
  141. if jsonBytes, err := json.Marshal(v); err == nil {
  142. // json -> struct
  143. json.Unmarshal(jsonBytes, &tradePosition)
  144. tradePosition.BuyOrSell = 1
  145. tradePosition.PositionQTY = v.Sellpositionqty
  146. tradePosition.HolderAmount = v.Sellholderamount
  147. tradePosition.CurPositionQTY = v.Sellcurpositionqty
  148. tradePosition.CurHolderAmount = v.Sellcurholderamount
  149. tradePosition.FrozenQTY = v.Sellfrozenqty
  150. tradePosition.OtherFrozenQTY = v.Sellotherfrozenqty
  151. tradePosition.OpenReqQTY = v.Sellopenreqqty
  152. tradePosition.OpenTotalQTY = v.Sellopentotalqty
  153. tradePosition.CloseTotalQTY = v.Sellclosetotalqty
  154. tradePosition.TNQTY = v.Selltnqty
  155. tradePosition.TNUsedQTY = v.Selltnusedqty
  156. tradePosition.CurTDPosition = v.Sellcurtdposition
  157. tradePosition.FreTDPosition = v.Sellfretdposition
  158. tradePosition.EnableQTY = v.Sellcurpositionqty - v.Sellfrozenqty - v.Sellotherfrozenqty
  159. // 计算持仓均价
  160. averagePrice := tradePosition.CurHolderAmount / float64(tradePosition.CurPositionQTY) / tradePosition.AgreeUnit
  161. tradePosition.AveragePrice, _ = strconv.ParseFloat(utils.FormatFloat(averagePrice, int(v.Decimalplace)), 64)
  162. rst = append(rst, tradePosition)
  163. }
  164. }
  165. }
  166. // 查询成功
  167. logger.GetLogger().Debugln("QueryTradePosition successed: %v", rst)
  168. appG.Response(http.StatusOK, e.SUCCESS, rst)
  169. }
  170. // QueryTradeOrderDetailReq 委托单查询请求参数(合约市场)
  171. type QueryTradeOrderDetailReq struct {
  172. AccountID string `form:"accountID" binding:"required"`
  173. OrderStatus string `form:"orderStatus"`
  174. TradeMode string `form:"tradeMode"`
  175. OrderID int `form:"orderID"`
  176. }
  177. // QueryTradeOrderDetailRsp 委托单查询返回模型(合约市场)
  178. type QueryTradeOrderDetailRsp struct {
  179. Orderid string `json:"orderid" xorm:"'ORDERID'"` // 委托单号(100+Unix秒时间戳(10位)+2位(MarketServiceID)+xxxx)
  180. Tradedate string `json:"tradedate" xorm:"'TRADEDATE'" binding:"required"` // 交易日(yyyyMMdd)
  181. Buildtype int64 `json:"buildtype" xorm:"'BUILDTYPE'" binding:"required"` // 委托单据类型 - 1:建仓 2:平仓 3:先平后建
  182. Preorderid string `json:"preorderid" xorm:"'PREORDERID'"` // 关联预埋单号(止盈止损单时填写)
  183. Cancelorderid string `json:"cancelorderid" xorm:"'CANCELORDERID'"` // 撤单单号(撤单时填写)
  184. Relatedid string `json:"relatedid" xorm:"'RELATEDID'"` // 关联单号(交割单)
  185. Marketid int64 `json:"marketid" xorm:"'MARKETID'" binding:"required"` // 市场ID
  186. Goodsid int64 `json:"goodsid" xorm:"'GOODSID'" binding:"required"` // 商品ID
  187. Accountid int64 `json:"accountid" xorm:"'ACCOUNTID'" binding:"required"` // 账户ID[报价币种]
  188. Buyorsell int64 `json:"buyorsell" xorm:"'BUYORSELL'" binding:"required"` // 买卖 - 0:买 1:卖
  189. Pricemode int64 `json:"pricemode" xorm:"'PRICEMODE'" binding:"required"` // 取价方式 - 1:市价 2: 限价
  190. Orderprice float64 `json:"orderprice" xorm:"'ORDERPRICE'"` // 委托价格
  191. Orderqty int64 `json:"orderqty" xorm:"'ORDERQTY'" binding:"required"` // 委托数量
  192. Tradeqty int64 `json:"tradeqty" xorm:"'TRADEQTY'"` // 成交数量
  193. Cancelqty int64 `json:"cancelqty" xorm:"'CANCELQTY'"` // 撤单数量
  194. Openqty int64 `json:"openqty" xorm:"'OPENQTY'"` // 开仓数量(先建后平操作,需要记录)
  195. Closeqty int64 `json:"closeqty" xorm:"'CLOSEQTY'"` // 平仓数量(先建后平操作 需要记录)
  196. Opentradeqty int64 `json:"opentradeqty" xorm:"'OPENTRADEQTY'"` // 开仓成交数量(先建后平操作,需要记录)
  197. Closetradeqty int64 `json:"closetradeqty" xorm:"'CLOSETRADEQTY'"` // 平仓成交数量(先建后平操作,需要记录)
  198. Freezemargin float64 `json:"freezemargin" xorm:"'FREEZEMARGIN'"` // 冻结保证金(冻结交易金额)
  199. Unfreezemargin float64 `json:"unfreezemargin" xorm:"'UNFREEZEMARGIN'"` // 解冻保证金
  200. Freezecharge float64 `json:"freezecharge" xorm:"'FREEZECHARGE'"` // 冻结手续费
  201. Unfreezecharge float64 `json:"unfreezecharge" xorm:"'UNFREEZECHARGE'"` // 解冻手续费
  202. Openfreezecharge float64 `json:"openfreezecharge" xorm:"'OPENFREEZECHARGE'"` // 开仓冻结手续费(先建后平操作,需要记录)
  203. Closefreezecharge float64 `json:"closefreezecharge" xorm:"'CLOSEFREEZECHARGE'"` // 平仓冻结手续费(先建后平操作,需要记录)
  204. Openunfreezecharge float64 `json:"openunfreezecharge" xorm:"'OPENUNFREEZECHARGE'"` // 开仓解冻手续费(先建后平操作,需要记录)
  205. Closeunfreezecharge float64 `json:"closeunfreezecharge" xorm:"'CLOSEUNFREEZECHARGE'"` // 平仓解冻手续费(先建后平操作,需要记录)
  206. Validtype int64 `json:"validtype" xorm:"'VALIDTYPE'" binding:"required"` // 有效类型 - 1当日有效 2本周有效 3指定日期有效 4一直有效 5指定时间有效
  207. Validtime time.Time `json:"validtime" xorm:"'VALIDTIME'"` // 有效期限
  208. Volumetype int64 `json:"volumetype" xorm:"'VOLUMETYPE'"` // 当时间有效类型为 “立即执行否则取消 IOC” 时,需要此项 - 0:任意量 1:最小量(暂时不支持) 2:全部量
  209. Operatetype int64 `json:"operatetype" xorm:"'OPERATETYPE'" binding:"required"` // 操作类型 - 1:正常下单 2:斩仓 3:转单 4:结算撤单 5:系统卖出(适用于先平后建的卖出) 6:行情源报价 7:(结算)到期强平 8:(结算)协议转让 9:系统对冲单 10:(结算)到期无效 11:交割协议转让 12:交割协议平仓 13:交割成交(所有权) 14:管理端强行平仓 15:管理端协议转让
  210. Ordertime time.Time `json:"ordertime" xorm:"'ORDERTIME'" binding:"required"` // 委托时间
  211. Orderstatus int64 `json:"orderstatus" xorm:"'ORDERSTATUS'"` // 委托状态 - 1: 委托请求 2:待冻结 3:委托成功 4: 委托失败 5:配对成功 6: 已撤销 7:部分成交 8:已成交 9:部成部撤 10:成交失败 11:已拒绝 12:经过摘牌(先摘后挂专用-先摘后挂已摘过) 13:冻结成功(通道交易专用) 14:通道已撤 15:通道部成部撤 16:成交失败违约(荷兰式竞拍专用)
  212. Listingselecttype int64 `json:"listingselecttype" xorm:"'LISTINGSELECTTYPE'"` // 挂牌点选类型 - 1:挂牌 2:摘牌 3:先摘后挂
  213. Delistingtype int64 `json:"delistingtype" xorm:"'DELISTINGTYPE'"` // 摘牌类型 - 1:价格最优 2:点选成交
  214. Ordersrc int64 `json:"ordersrc" xorm:"'ORDERSRC'"` // 委托来源 - 1:客户端 2:管理端 3:风控服务 4:交割服务 5:交易服务 6:交易日结 7:商品强平 8:管理端商品退市强平 9:交易接口 10:交割服务商被动(受托竞价) 11:预埋触发
  215. Clienttype int64 `json:"clienttype" xorm:"'CLIENTTYPE'"` // 客户端类型 - 0:保留为未填终端类型 1:PC管理端 2:PC交易端 3:手机客户端_安卓 4:网页客户端 5:微信客户端 6:手机客户端_苹果 7:网上开户客户端 8:无效终端编号 9:报价终端(中江)
  216. Operatorid int64 `json:"operatorid" xorm:"'OPERATORID'"` // 登录账号(LoginID)
  217. GoodsCode string `json:"goodscode" xorm:"GOODSCODE"` // 商品代码
  218. GoodsName string `json:"goodsname" xorm:"GOODSNAME"` // 商品名称
  219. DECIMALPLACE int32 `json:"decimalplace" xorm:"'DECIMALPLACE'"` // 商品报价小数位
  220. Marketname string `json:"marketname" xorm:"'MARKETNAME'"` // 市场名称
  221. TradeMode int32 `json:"trademode" xorm:"'TRADEMODE'"` // 交易模式
  222. Enableqty int64 `json:"enableqty" xorm:"ENABLEQTY"` // 可用数量 = 委托数量 - 成交数量 - 撤单数量
  223. }
  224. // QueryTradeOrderDetail 委托单查询请求(合约市场)
  225. // @Summary 委托单查询请求(合约市场)
  226. // @Produce json
  227. // @Security ApiKeyAuth
  228. // @Param accountID query string true "资金账户 - 格式:1,2,3"
  229. // @Param tradeMode query string false "交易模式 - 格式:1,2,3"
  230. // @Param orderStatus query string false "委托状态 - 格式:1,2,3"
  231. // @Param orderID query int false "委托单号"
  232. // @Success 200 {object} QueryTradeOrderDetailRsp
  233. // @Failure 500 {object} app.Response
  234. // @Router /Order/QueryTradeOrderDetail [get]
  235. // @Tags 通用单据
  236. // 参考通用查询:SearchTradeOrderDetail
  237. func QueryTradeOrderDetail(c *gin.Context) {
  238. appG := app.Gin{C: c}
  239. // 获取请求参数
  240. var req QueryTradeOrderDetailReq
  241. if err := appG.C.ShouldBindQuery(&req); err != nil {
  242. logger.GetLogger().Errorf("QueryTradeOrderDetail failed: %s", err.Error())
  243. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  244. return
  245. }
  246. datas := make([]QueryTradeOrderDetailRsp, 0)
  247. engine := db.GetEngine()
  248. // 由于int64类型数据(单号)过长是获取会有问题(可能是oci8组件问题),所以这里将可能会出问题的单号都用to_char来输出
  249. s := engine.Table("TRADE_ORDERDETAIL").
  250. Join("LEFT", "GOODS", "GOODS.GOODSID = TRADE_ORDERDETAIL.GOODSID").
  251. Join("LEFT", "MARKET", "MARKET.MARKETID = TRADE_ORDERDETAIL.MARKETID").
  252. Select(`to_char(TRADE_ORDERDETAIL.ORDERID) as ORDERID, to_char(TRADE_ORDERDETAIL.PREORDERID) as PREORDERID, to_char(TRADE_ORDERDETAIL.CANCELORDERID) as CANCELORDERID, to_char(TRADE_ORDERDETAIL.RELATEDID) as RELATEDID,
  253. TRADE_ORDERDETAIL.*, TRADE_ORDERDETAIL.ORDERQTY - TRADE_ORDERDETAIL.TRADEQTY - TRADE_ORDERDETAIL.CANCELQTY as ENABLEQTY,
  254. GOODS.GOODSCODE, GOODS.GOODSNAME,GOODS.DECIMALPLACE, MARKET.MARKETNAME, MARKET.TRADEMODE`).
  255. Where(fmt.Sprintf(`TRADE_ORDERDETAIL.ORDERSRC != 10 and TRADE_ORDERDETAIL.ACCOUNTID in (%s)`, req.AccountID)).
  256. Desc("TRADE_ORDERDETAIL.ORDERTIME")
  257. if len(req.OrderStatus) > 0 {
  258. s = s.And(fmt.Sprintf(`TRADE_ORDERDETAIL.ORDERSTATUS in (%s)`, req.OrderStatus))
  259. }
  260. if len(req.TradeMode) > 0 {
  261. s = s.And(fmt.Sprintf(`MARKET.TRADEMODE in (%s)`, req.TradeMode))
  262. }
  263. if req.OrderID > 0 {
  264. s = s.And("TRADE_ORDERDETAIL.ORDERID = ?", req.OrderID)
  265. }
  266. if err := s.Find(&datas); err != nil {
  267. // 查询失败
  268. logger.GetLogger().Errorf("QueryTradeOrderDetail failed: %s", err.Error())
  269. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  270. return
  271. }
  272. // 查询成功
  273. logger.GetLogger().Debugln("QueryTradeOrderDetail successed: %v", datas)
  274. appG.Response(http.StatusOK, e.SUCCESS, datas)
  275. }
  276. // QueryHisTradeOrderDetailReq 历史委托单查询请求参数(合约市场)
  277. type QueryHisTradeOrderDetailReq struct {
  278. AccountID string `form:"accountID" binding:"required"` // 资金账户 - 格式:1,2,3
  279. OrderStatus string `form:"orderStatus"` // 委托状态 - 格式:1,2,3
  280. TradeMode string `form:"tradeMode"` // 交易模式 - 格式:1,2,3
  281. OrderID int `form:"orderID"` // 委托单号
  282. StartDate string `form:"startDate"` // 开始时间
  283. EndDate string `form:"endDate"` // 结束时间
  284. }
  285. // QueryHisTradeOrderDetailRsp 历史委托单查询返回模型(合约市场)
  286. type QueryHisTradeOrderDetailRsp struct {
  287. models.Histradeorderdetail `xorm:"extends"`
  288. GoodsCode string `json:"goodscode" xorm:"GOODSCODE"` // 商品代码
  289. GoodsName string `json:"goodsname" xorm:"GOODSNAME"` // 商品名称
  290. DECIMALPLACE int32 `json:"decimalplace" xorm:"'DECIMALPLACE'"` // 商品报价小数位
  291. Marketname string `json:"marketname" xorm:"'MARKETNAME'"` // 市场名称
  292. TradeMode int32 `json:"trademode" xorm:"'TRADEMODE'"` // 交易模式
  293. }
  294. // QueryHisTradeOrderDetail 历史委托单查询请求(合约市场)
  295. // @Summary 历史委托单查询请求(合约市场)
  296. // @Produce json
  297. // @Security ApiKeyAuth
  298. // @Param accountID query string true "资金账户 - 格式:1,2,3"
  299. // @Param tradeMode query string false "交易模式 - 格式:1,2,3"
  300. // @Param orderStatus query string false "委托状态 - 格式:1,2,3"
  301. // @Param orderID query int false "委托单号"
  302. // @Param startDate query string false "开始时间 - 闭区间,格式:yyyy-MM-dd"
  303. // @Param endDate query string false "结束时间 - 闭区间,格式:yyyy-MM-dd"
  304. // @Success 200 {object} QueryHisTradeOrderDetailRsp
  305. // @Failure 500 {object} app.Response
  306. // @Router /Order/QueryHisTradeOrderDetail [get]
  307. // @Tags 通用单据
  308. // 参考通用查询:Client_QueryHis_trade_orderdetail
  309. func QueryHisTradeOrderDetail(c *gin.Context) {
  310. appG := app.Gin{C: c}
  311. // 获取请求参数
  312. var req QueryHisTradeOrderDetailReq
  313. if err := appG.C.ShouldBindQuery(&req); err != nil {
  314. logger.GetLogger().Errorf("QueryHisTradeOrderDetail failed: %s", err.Error())
  315. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  316. return
  317. }
  318. datas := make([]QueryHisTradeOrderDetailRsp, 0)
  319. engine := db.GetEngine()
  320. s := engine.Table("HIS_TRADE_ORDERDETAIL").
  321. Join("LEFT", "GOODS", "GOODS.GOODSID = HIS_TRADE_ORDERDETAIL.GOODSID").
  322. Join("LEFT", "MARKET", "MARKET.MARKETID = HIS_TRADE_ORDERDETAIL.MARKETID").
  323. Select(`to_char(HIS_TRADE_ORDERDETAIL.ORDERID) as ORDERID, to_char(HIS_TRADE_ORDERDETAIL.PREORDERID) as PREORDERID, to_char(HIS_TRADE_ORDERDETAIL.CANCELORDERID) as CANCELORDERID, to_char(HIS_TRADE_ORDERDETAIL.RELATEDID) as RELATEDID,
  324. HIS_TRADE_ORDERDETAIL.*,
  325. GOODS.GOODSCODE, GOODS.GOODSNAME,GOODS.DECIMALPLACE, MARKET.MARKETNAME, MARKET.TRADEMODE`).
  326. Where(fmt.Sprintf(`HIS_TRADE_ORDERDETAIL.ORDERSRC != 10 and HIS_TRADE_ORDERDETAIL.ISVALIDDATA = 1 and HIS_TRADE_ORDERDETAIL.ACCOUNTID in (%s)`, req.AccountID)).
  327. Desc("HIS_TRADE_ORDERDETAIL.ORDERTIME")
  328. if len(req.OrderStatus) > 0 {
  329. s = s.And(fmt.Sprintf(`HIS_TRADE_ORDERDETAIL.ORDERSTATUS in (%s)`, req.OrderStatus))
  330. }
  331. if len(req.TradeMode) > 0 {
  332. s = s.And(fmt.Sprintf(`MARKET.TRADEMODE in (%s)`, req.TradeMode))
  333. }
  334. if req.OrderID > 0 {
  335. s = s.And("HIS_TRADE_ORDERDETAIL.ORDERID = ?", req.OrderID)
  336. }
  337. if len(req.StartDate) > 0 {
  338. s = s.And(fmt.Sprintf("to_date(HIS_TRADE_ORDERDETAIL.HISTRADEDATE,'yyyyMMdd') >= to_date('%s','yyyy-MM-dd')", req.StartDate))
  339. }
  340. if len(req.EndDate) > 0 {
  341. s = s.And(fmt.Sprintf("to_date(HIS_TRADE_ORDERDETAIL.HISTRADEDATE,'yyyyMMdd') <= to_date('%s','yyyy-MM-dd')", req.EndDate))
  342. }
  343. if err := s.Find(&datas); err != nil {
  344. // 查询失败
  345. logger.GetLogger().Errorf("QueryHisTradeOrderDetail failed: %s", err.Error())
  346. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  347. return
  348. }
  349. // 查询成功
  350. logger.GetLogger().Debugln("QueryHisTradeOrderDetail successed: %v", datas)
  351. appG.Response(http.StatusOK, e.SUCCESS, datas)
  352. }
  353. // QueryTradeDetailReq 成交单查询请求参数
  354. type QueryTradeDetailReq struct {
  355. AccountID string `form:"accountID" binding:"required"`
  356. TradeID int `form:"tradeID"`
  357. OrderID int `form:"orderID"`
  358. TradeMode string `form:"tradeMode"`
  359. BuildType int `form:"buildType"`
  360. TradeType string `form:"tradeType"`
  361. GoodsID int `form:"goodsID"`
  362. }
  363. // QueryTradeDetailRsp 成交单查询返回模型
  364. type QueryTradeDetailRsp struct {
  365. models.Tradetradedetail `xorm:"extends"`
  366. GoodsCode string `json:"goodscode" xorm:"GOODSCODE"` // 商品代码
  367. GoodsName string `json:"goodsname" xorm:"GOODSNAME"` // 商品名称
  368. DECIMALPLACE int32 `json:"decimalplace" xorm:"'DECIMALPLACE'"` // 商品报价小数位
  369. Marketname string `json:"marketname" xorm:"'MARKETNAME'"` // 市场名称
  370. TradeMode int32 `json:"trademode" xorm:"'TRADEMODE'"` // 交易模式
  371. ListingSelectType int32 `json:"listingselecttype" xorm:"'LISTINGSELECTTYPE'"` // 关联委托单挂牌点选类型 - 1:挂牌 2:摘牌 3:先摘后挂
  372. Charge float64 `json:"charge" xorm:"CHARGE"` // 手续费
  373. }
  374. // QueryTradeDetail 成交单查询(合约市场)
  375. // @Summary 成交单查询(合约市场)
  376. // @Produce json
  377. // @Security ApiKeyAuth
  378. // @Param accountID query string true "资金账户 - 格式:1,2,3"
  379. // @Param tradeID query int false "成交单号"
  380. // @Param orderID query int false "委托单号"
  381. // @Param tradeMode query string false "交易模式 - 格式:1,2,3"
  382. // @Param buildType query int false "委托单据类型"
  383. // @Param tradeType query string false "成交类别 - 格式:1,2,3"
  384. // @Param goodsID query int false "商品ID"
  385. // @Success 200 {object} QueryTradeDetailRsp
  386. // @Failure 500 {object} app.Response
  387. // @Router /Order/QueryTradeDetail [get]
  388. // @Tags 通用单据
  389. // 参考通用查询:SearchAllTransactionDetailOrder
  390. func QueryTradeDetail(c *gin.Context) {
  391. appG := app.Gin{C: c}
  392. // 获取请求参数
  393. var req QueryTradeDetailReq
  394. if err := appG.C.ShouldBindQuery(&req); err != nil {
  395. logger.GetLogger().Errorf("QueryTradeDetail failed: %s", err.Error())
  396. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  397. return
  398. }
  399. datas := make([]QueryTradeDetailRsp, 0)
  400. engine := db.GetEngine()
  401. s := engine.Table("TRADE_TRADEDETAIL").
  402. Join("LEFT", "GOODS", "GOODS.GOODSID = TRADE_TRADEDETAIL.GOODSID").
  403. Join("LEFT", "MARKET", "MARKET.MARKETID = TRADE_TRADEDETAIL.MARKETID").
  404. Join("LEFT", "TRADE_ORDERDETAIL", "TRADE_ORDERDETAIL.ORDERID = TRADE_TRADEDETAIL.ORDERID").
  405. Select(`to_char(TRADE_TRADEDETAIL.TRADEID) as TRADEID, to_char(TRADE_TRADEDETAIL.ORDERID) as ORDERID,
  406. TRADE_TRADEDETAIL.*, CASE TRADE_TRADEDETAIL.BUILDTYPE WHEN 1 THEN TRADE_TRADEDETAIL.OPENCHARGE ELSE TRADE_TRADEDETAIL.CLOSECHARGE END as CHARGE,
  407. GOODS.GOODSCODE, GOODS.GOODSNAME, GOODS.DECIMALPLACE, MARKET.MARKETNAME, MARKET.TRADEMODE,
  408. TRADE_ORDERDETAIL.LISTINGSELECTTYPE`).
  409. Where(fmt.Sprintf("TRADE_TRADEDETAIL.TRADETYPE <= 13 and TRADE_TRADEDETAIL.ACCOUNTID in (%s)", req.AccountID)).
  410. Desc("TRADE_TRADEDETAIL.TRADETIME")
  411. if req.TradeID > 0 {
  412. s = s.And("TRADE_TRADEDETAIL.TRADEID = ?", req.TradeID)
  413. }
  414. if req.OrderID > 0 {
  415. s = s.And("TRADE_TRADEDETAIL.ORDERID = ?", req.OrderID)
  416. }
  417. if req.GoodsID > 0 {
  418. s = s.And("TRADE_TRADEDETAIL.GOODSID = ?", req.GoodsID)
  419. }
  420. if len(req.TradeMode) > 0 {
  421. s = s.And(fmt.Sprintf("MARKET.TRADEMODE in (%s)", req.TradeMode))
  422. }
  423. if req.BuildType > 0 {
  424. s = s.And("TRADE_TRADEDETAIL.BUILDTYPE = ?", req.BuildType)
  425. }
  426. if len(req.TradeType) > 0 {
  427. s = s.And(fmt.Sprintf("TRADE_TRADEDETAIL.TRADETYPE in (%s)", req.TradeType))
  428. }
  429. if err := s.Find(&datas); err != nil {
  430. // 查询失败
  431. logger.GetLogger().Errorf("QueryTradeDetail failed: %s", err.Error())
  432. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  433. return
  434. }
  435. // 查询成功
  436. logger.GetLogger().Debugln("QueryTradeDetail successed: %v", datas)
  437. appG.Response(http.StatusOK, e.SUCCESS, datas)
  438. }
  439. // QueryHisTradeDetailReq 历史成交单查询请求参数
  440. type QueryHisTradeDetailReq struct {
  441. AccountID string `form:"accountID" binding:"required"`
  442. TradeID int `form:"tradeID"`
  443. OrderID int `form:"orderID"`
  444. GoodsID int `form:"goodsID"`
  445. TradeMode string `form:"tradeMode"`
  446. BuildType int `form:"buildType"`
  447. TradeType string `form:"tradeType"`
  448. StartDate string `form:"startDate"` // 开始时间
  449. EndDate string `form:"endDate"` // 结束时间
  450. }
  451. // QueryHisTradeDetailRsp 历史成交单查询返回模型
  452. type QueryHisTradeDetailRsp struct {
  453. models.Histradetradedetail `xorm:"extends"`
  454. GoodsCode string `json:"goodscode" xorm:"GOODSCODE"` // 商品代码
  455. GoodsName string `json:"goodsname" xorm:"GOODSNAME"` // 商品名称
  456. DECIMALPLACE int32 `json:"decimalplace" xorm:"'DECIMALPLACE'"` // 商品报价小数位
  457. Marketname string `json:"marketname" xorm:"'MARKETNAME'"` // 市场名称
  458. TradeMode int32 `json:"trademode" xorm:"'TRADEMODE'"` // 交易模式
  459. ListingSelectType int32 `json:"listingselecttype" xorm:"'LISTINGSELECTTYPE'"` // 关联委托单挂牌点选类型 - 1:挂牌 2:摘牌 3:先摘后挂
  460. Charge float64 `json:"charge" xorm:"CHARGE"` // 手续费
  461. }
  462. // QueryHisTradeDetail 历史成交单查询(合约市场)
  463. // @Summary 历史成交单查询(合约市场)
  464. // @Produce json
  465. // @Security ApiKeyAuth
  466. // @Param accountID query string true "资金账户 - 格式:1,2,3"
  467. // @Param tradeID query int false "成交单号"
  468. // @Param orderID query int false "委托单号"
  469. // @Param goodsID query int false "商品ID"
  470. // @Param tradeMode query string false "交易模式 - 格式:1,2,3"
  471. // @Param buildType query int false "委托单据类型"
  472. // @Param tradeType query string false "成交类别 - 格式:1,2,3"
  473. // @Param startDate query string false "开始时间 - 闭区间,格式:yyyy-MM-dd"
  474. // @Param endDate query string false "结束时间 - 闭区间,格式:yyyy-MM-dd"
  475. // @Success 200 {object} QueryHisTradeDetailRsp
  476. // @Failure 500 {object} app.Response
  477. // @Router /Order/QueryHisTradeDetail [get]
  478. // @Tags 通用单据
  479. // 参考通用查询:Client_QueryHis_trade_transactiondetail
  480. func QueryHisTradeDetail(c *gin.Context) {
  481. appG := app.Gin{C: c}
  482. // 获取请求参数
  483. var req QueryHisTradeDetailReq
  484. if err := appG.C.ShouldBindQuery(&req); err != nil {
  485. logger.GetLogger().Errorf("QueryHisTradeDetail failed: %s", err.Error())
  486. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  487. return
  488. }
  489. datas := make([]QueryHisTradeDetailRsp, 0)
  490. engine := db.GetEngine()
  491. s := engine.Table("HIS_TRADE_TRADEDETAIL").
  492. Join("LEFT", "GOODS", "GOODS.GOODSID = HIS_TRADE_TRADEDETAIL.GOODSID").
  493. Join("LEFT", "MARKET", "MARKET.MARKETID = HIS_TRADE_TRADEDETAIL.MARKETID").
  494. Join("LEFT", "HIS_TRADE_ORDERDETAIL", "HIS_TRADE_ORDERDETAIL.ORDERID = HIS_TRADE_TRADEDETAIL.ORDERID").
  495. Select(`to_char(HIS_TRADE_TRADEDETAIL.TRADEID) as TRADEID, to_char(HIS_TRADE_TRADEDETAIL.ORDERID) as ORDERID,
  496. HIS_TRADE_TRADEDETAIL.*, CASE HIS_TRADE_TRADEDETAIL.BUILDTYPE WHEN 1 THEN HIS_TRADE_TRADEDETAIL.OPENCHARGE ELSE HIS_TRADE_TRADEDETAIL.CLOSECHARGE END as CHARGE,
  497. GOODS.GOODSCODE, GOODS.GOODSNAME,GOODS.DECIMALPLACE, MARKET.MARKETNAME, MARKET.TRADEMODE,
  498. HIS_TRADE_ORDERDETAIL.LISTINGSELECTTYPE`).
  499. Where(fmt.Sprintf("HIS_TRADE_TRADEDETAIL.TRADETYPE <= 13 and HIS_TRADE_TRADEDETAIL.ISVALIDDATA = 1 and HIS_TRADE_TRADEDETAIL.ACCOUNTID in (%s)", req.AccountID)).
  500. Desc("HIS_TRADE_TRADEDETAIL.TRADETIME")
  501. if req.TradeID > 0 {
  502. s = s.And("HIS_TRADE_TRADEDETAIL.TRADEID = ?", req.TradeID)
  503. }
  504. if req.OrderID > 0 {
  505. s = s.And("HIS_TRADE_TRADEDETAIL.ORDERID = ?", req.OrderID)
  506. }
  507. if req.GoodsID > 0 {
  508. s = s.And("TRADE_TRADEDETAIL.GOODSID = ?", req.GoodsID)
  509. }
  510. if len(req.TradeMode) > 0 {
  511. s = s.And(fmt.Sprintf("MARKET.TRADEMODE in (%s)", req.TradeMode))
  512. }
  513. if req.BuildType > 0 {
  514. s = s.And("HIS_TRADE_TRADEDETAIL.BUILDTYPE = ?", req.BuildType)
  515. }
  516. if len(req.TradeType) > 0 {
  517. s = s.And(fmt.Sprintf("HIS_TRADE_TRADEDETAIL.TRADETYPE in (%s)", req.TradeType))
  518. }
  519. if len(req.StartDate) > 0 {
  520. s = s.And(fmt.Sprintf("to_date(HIS_TRADE_TRADEDETAIL.HISTRADEDATE,'yyyyMMdd') >= to_date('%s','yyyy-MM-dd')", req.StartDate))
  521. }
  522. if len(req.EndDate) > 0 {
  523. s = s.And(fmt.Sprintf("to_date(HIS_TRADE_TRADEDETAIL.HISTRADEDATE,'yyyyMMdd') <= to_date('%s','yyyy-MM-dd')", req.EndDate))
  524. }
  525. if err := s.Find(&datas); err != nil {
  526. // 查询失败
  527. logger.GetLogger().Errorf("QueryHisTradeDetail failed: %s", err.Error())
  528. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  529. return
  530. }
  531. // 查询成功
  532. logger.GetLogger().Debugln("QueryHisTradeDetail successed: %v", datas)
  533. appG.Response(http.StatusOK, e.SUCCESS, datas)
  534. }