order.go 43 KB

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