order.go 36 KB

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