order.go 45 KB

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