order.go 40 KB

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