order.go 41 KB

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