erms2.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. package erms2
  2. import (
  3. "fmt"
  4. "mtp2_if/db"
  5. "mtp2_if/global/app"
  6. "mtp2_if/global/e"
  7. "mtp2_if/logger"
  8. "net/http"
  9. "time"
  10. "github.com/gin-gonic/gin"
  11. )
  12. // QueryInnerTradeDetailReq 查询内部成交单信息请求参数
  13. type QueryInnerTradeDetailReq struct {
  14. AccountID int `form:"accountid" binding:"required"`
  15. }
  16. // QueryInnerTradeDetailRsp 内部成交单信息
  17. type QueryInnerTradeDetailRsp struct {
  18. Tradeid string `json:"tradeid" xorm:"'TRADEID'" binding:"required"` // 成交单号(108+Unix秒时间戳(10位)+2位(MarketServiceID)+xxxx)
  19. Buyorsell int64 `json:"buyorsell" xorm:"'BUYORSELL'" binding:"required"` // 方向 - 0:买 1:卖
  20. Orderid string `json:"orderid" xorm:"'ORDERID'"` // 委托单号
  21. Accountid int64 `json:"accountid" xorm:"'ACCOUNTID'"` // 账号ID
  22. Goodsid int64 `json:"goodsid" xorm:"'GOODSID'"` // 商品ID
  23. Marketid int64 `json:"marketid" xorm:"'MARKETID'"` // 市场ID
  24. Tradetime time.Time `json:"tradetime" xorm:"'TRADETIME'"` // 成交时间
  25. Tradeprice float64 `json:"tradeprice" xorm:"'TRADEPRICE'"` // 成交价格
  26. Tradeqty int64 `json:"tradeqty" xorm:"'TRADEQTY'"` // 成交数量
  27. Channelbuildtype int64 `json:"channelbuildtype" xorm:"'CHANNELBUILDTYPE'"` // 委托单据类型 0:无 1:建仓 2:平仓
  28. Closetype int64 `json:"closetype" xorm:"'CLOSETYPE'"` // 平仓方式 - 0:无 1:平今 2:平昨
  29. Relatedouttradeid string `json:"relatedouttradeid" xorm:"'RELATEDOUTTRADEID'"` // 关联外部成交单ID
  30. Asapplyid int64 `json:"asapplyid" xorm:"'ASAPPLYID'" binding:"required"` // 策略申请ID
  31. Detailtype int64 `json:"detailtype" xorm:"'DETAILTYPE'"` // 明细类型 - 1:套利对冲 2:期货换月 3:期货仓位调整
  32. Spotcontractid int64 `json:"spotcontractid" xorm:"'SPOTCONTRACTID'"` // 现货合同ID [1:套利对冲 为合同ID,2:期货换月\3:期货仓位调整时为0]
  33. Asno string `json:"asno" xorm:"'ASNO'"` // 策略编号
  34. Asname string `json:"asname" xorm:"'ASNAME'"` // 策略名称
  35. Spotcontractno string `json:"spotcontractno" xorm:"'SPOTCONTRACTNO'"` // 现货合同编号
  36. Remark string `json:"remark" xorm:"'REMARK'"` // 备注
  37. Updatetime time.Time `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  38. Goodscode string `json:"goodscode" xorm:"'GOODSCODE'" binding:"required"` // 商品代码(合约)
  39. Goodsname string `json:"goodsname" xorm:"'GOODSNAME'" binding:"required"` // 商品名称(合约)
  40. Agreeunit float64 `json:"agreeunit" xorm:"'AGREEUNIT'"` // 合约单位
  41. Goodunit string `json:"goodunit" xorm:"'GOODUNIT'"` // 报价单位
  42. Exexchangecode string `json:"exexchangecode" xorm:"'EXEXCHANGECODE'"` // 外部交易所代码
  43. Exexchangename string `json:"exexchangename" xorm:"'EXEXCHANGENAME'"` // 外部交易所名称
  44. Outgoodscode string `json:"outgoodscode" xorm:"'OUTGOODSCODE'"` // 商品代码(外部)
  45. Decimalplace int64 `json:"decimalplace" xorm:"'DECIMALPLACE'"` // 报价小数位
  46. Goodsgroupid int64 `json:"goodsgroupid" xorm:"'GOODSGROUPID'" binding:"required"` // 商品组ID(品种ID)
  47. Goodsgroupname string `json:"goodsgroupname" xorm:"'GOODSGROUPNAME'" binding:"required"` // 商品组名称(品种)
  48. }
  49. // QueryInnerTradeDetail 查询内部成交单信息
  50. // @Summary 查询内部成交单信息
  51. // @Produce json
  52. // @Security ApiKeyAuth
  53. // @Param accountid query int true "资金账户"
  54. // @Success 200 {object} QueryInnerTradeDetailRsp
  55. // @Failure 500 {object} app.Response
  56. // @Router /Erms2/QueryInnerTradeDetail [get]
  57. // @Tags 风险管理
  58. func QueryInnerTradeDetail(c *gin.Context) {
  59. appG := app.Gin{C: c}
  60. // 获取请求参数
  61. var req QueryInnerTradeDetailReq
  62. if err := appG.C.ShouldBindQuery(&req); err != nil {
  63. logger.GetLogger().Errorf("QueryInnerTradeDetail failed: %s", err.Error())
  64. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  65. return
  66. }
  67. // 查询数据
  68. engine := db.GetEngine()
  69. datas := make([]QueryInnerTradeDetailRsp, 0)
  70. sql := fmt.Sprintf(`select
  71. to_char(t.Tradeid) Tradeid,
  72. t.Buyorsell,
  73. to_char(t.Orderid) Orderid,
  74. t.Accountid,
  75. t.Goodsid,
  76. t.Marketid,
  77. t.Tradetime,
  78. t.Tradeprice,
  79. t.Tradeqty,
  80. t.Channelbuildtype,
  81. t.Closetype,
  82. to_char(t.Relatedouttradeid) Relatedouttradeid,
  83. ast.Asapplyid,
  84. ast.Detailtype,
  85. ast.Spotcontractid,
  86. ast.Remark,
  87. ast.Updatetime,
  88. asd.Asno,
  89. asd.ASName,
  90. sc.contractno Spotcontractno,
  91. g.Goodscode,
  92. g.Goodsname,
  93. g.Agreeunit,
  94. g.Outgoodscode,
  95. g.Decimalplace,
  96. e.enumdicname GoodUnit,
  97. ee.Exexchangecode,
  98. ee.Exexchangename,
  99. gg.Goodsgroupid,
  100. gg.Goodsgroupname
  101. from Hedge_InnerTradeDetail t
  102. left join ERMS2_ASTradeDetails ast on t.relatedouttradeid = OutTradeID
  103. left join ERMS2_ArbitrageStrategy asd on ast.asapplyid = asd.asapplyid
  104. left join ERMS3_SpotContract sc on ast.spotcontractid = sc.spotcontractid
  105. left join goods g on t.goodsid = g.goodsid
  106. left join enumdicitem e on g.goodunitid = e.enumitemname and e.enumdiccode = 'goodsunit'
  107. left join goodsgroup gg on g.goodsgroupid = gg.goodsgroupid
  108. left join ExternalExchange ee on gg.exexchangeid = ee.autoid
  109. where t.Accountid = %d`, req.AccountID)
  110. if err := engine.SQL(sql).Find(&datas); err != nil {
  111. // 查询失败
  112. logger.GetLogger().Errorf("QueryInnerTradeDetail failed: %s", err.Error())
  113. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  114. return
  115. }
  116. // 查询成功
  117. logger.GetLogger().Debugln("QueryInnerTradeDetail successed: %v", datas)
  118. appG.Response(http.StatusOK, e.SUCCESS, datas)
  119. }
  120. // QueryArbitrageStrategyReq 查询期现套利策略表信息请求参数
  121. type QueryArbitrageStrategyReq struct {
  122. // AccountID int `form:"accountid" binding:"required"`
  123. UserID int `form:"userid" binding:"required"`
  124. GoodsGroupID int `form:"goodsgroupid"`
  125. }
  126. // QueryArbitrageStrategyRsp 期现套利策略表信息
  127. type QueryArbitrageStrategyRsp struct {
  128. Asapplyid string `json:"asapplyid" xorm:"'ASAPPLYID'" binding:"required"` // 策略申请ID(702+Unix秒时间戳(10位)+xxxxxx)
  129. Asno string `json:"asno" xorm:"'ASNO'"` // 策略编号
  130. Biztype int64 `json:"biztype" xorm:"'BIZTYPE'"` // 业务类型 - 1:正向套利 -1:反向套利
  131. Userid int64 `json:"userid" xorm:"'USERID'"` // 所属机构
  132. Deliverygoodsid int64 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货品种ID
  133. Goodsgroupid int64 `json:"goodsgroupid" xorm:"'GOODSGROUPID'"` // 期货品种ID
  134. Spotquota float64 `json:"spotquota" xorm:"'SPOTQUOTA'"` // 现货额度
  135. Futurequote float64 `json:"futurequote" xorm:"'FUTUREQUOTE'"` // 期货额度
  136. Applybasis float64 `json:"applybasis" xorm:"'APPLYBASIS'"` // 申请基差
  137. Strategystatus int64 `json:"strategystatus" xorm:"'STRATEGYSTATUS'"` // 策略状态 - 0:未结束 1:已结束
  138. Remark string `json:"remark" xorm:"'REMARK'"` // 备注
  139. Marketid int64 `json:"marketid" xorm:"'MARKETID'"` // 市场ID
  140. Tradedate string `json:"tradedate" xorm:"'TRADEDATE'"` // 交易日(yyyyMMdd)
  141. Closetradedate string `json:"closetradedate" xorm:"'CLOSETRADEDATE'"` // 完结交易日(yyyyMMdd)
  142. Usedquota float64 `json:"usedquota" xorm:"'USEDQUOTA'"` // 已占用资金
  143. Futureqty float64 `json:"futureqty" xorm:"'FUTUREQTY'"` // 期货持仓数量
  144. Futureavgprice float64 `json:"futureavgprice" xorm:"'FUTUREAVGPRICE'"` // 期货建仓均价
  145. Futurepl float64 `json:"futurepl" xorm:"'FUTUREPL'"` // 期货总盈亏[结算更新]
  146. Pricedspotqty float64 `json:"pricedspotqty" xorm:"'PRICEDSPOTQTY'"` // 已定价现货数量
  147. Pricedspotqtynotax float64 `json:"pricedspotqtynotax" xorm:"'PRICEDSPOTQTYNOTAX'"` // 已定价现货不含税数量
  148. Spotavgprice float64 `json:"spotavgprice" xorm:"'SPOTAVGPRICE'"` // 现货均价
  149. Spotpl float64 `json:"spotpl" xorm:"'SPOTPL'"` // 现货总盈亏[结算更新]
  150. Netexposure float64 `json:"netexposure" xorm:"'NETEXPOSURE'"` // 单笔业务头寸净敞口 = 期货持仓数量 + 已定价现货不含税数量
  151. Netexposurerate float64 `json:"netexposurerate" xorm:"'NETEXPOSURERATE'"` // 净敞口比例 - 0:未结束 = (NetExposure/PriceSpotQtyNoTax) ; 已结束为0
  152. Totalpl float64 `json:"totalpl" xorm:"'TOTALPL'"` // 业务合计损益 = FuturePL + SpotPL [结算更新]
  153. Openbasis float64 `json:"openbasis" xorm:"'OPENBASIS'"` // 建仓基差
  154. Curbasis float64 `json:"curbasis" xorm:"'CURBASIS'"` // 当前基差[结算更新]
  155. Basischangepl float64 `json:"basischangepl" xorm:"'BASISCHANGEPL'"` // 基差变动损益[结算更新]
  156. Netexposurepl float64 `json:"netexposurepl" xorm:"'NETEXPOSUREPL'"` // 净敞口损益 = TotalPL - BasisChangePL[结算更新]
  157. Spotusedquota float64 `json:"spotusedquota" xorm:"'SPOTUSEDQUOTA'"` // 现货占用资金
  158. Futureopenqty float64 `json:"futureopenqty" xorm:"'FUTUREOPENQTY'"` // 期货开仓数量
  159. Futureopenamount float64 `json:"futureopenamount" xorm:"'FUTUREOPENAMOUNT'"` // 期货开仓金额
  160. Futurecloseqty float64 `json:"futurecloseqty" xorm:"'FUTURECLOSEQTY'"` // 期货平仓数量
  161. Futurecloseamount float64 `json:"futurecloseamount" xorm:"'FUTURECLOSEAMOUNT'"` // 期货平仓金额
  162. Spotbuyamount float64 `json:"spotbuyamount" xorm:"'SPOTBUYAMOUNT'"` // 现货采购金额
  163. Spotbuyqty float64 `json:"spotbuyqty" xorm:"'SPOTBUYQTY'"` // 现货采购数量
  164. Spotsellamount float64 `json:"spotsellamount" xorm:"'SPOTSELLAMOUNT'"` // 现货销售金额
  165. Spotsellqty float64 `json:"spotsellqty" xorm:"'SPOTSELLQTY'"` // 现货销售数量
  166. Updatetime time.Time `json:"updatetime" xorm:"'UPDATETIME'"` // 更新时间
  167. Asname string `json:"asname" xorm:"'ASNAME'"` // 策略名称
  168. }
  169. // QueryArbitrageStrategy 查询期现套利策略表信息(指定资金账户、未结束的)
  170. // @Summary 查询期现套利策略表信息(指定资金账户、未结束的)
  171. // @Produce json
  172. // @Security ApiKeyAuth
  173. // @Param userid query int true "账户ID"
  174. // @Param goodsgroupid query string false "商品组ID(品种ID)"
  175. // @Success 200 {object} QueryArbitrageStrategyRsp
  176. // @Failure 500 {object} app.Response
  177. // @Router /Erms2/QueryArbitrageStrategy [get]
  178. // @Tags 风险管理
  179. func QueryArbitrageStrategy(c *gin.Context) {
  180. appG := app.Gin{C: c}
  181. // 获取请求参数
  182. var req QueryArbitrageStrategyReq
  183. if err := appG.C.ShouldBindQuery(&req); err != nil {
  184. logger.GetLogger().Errorf("QueryArbitrageStrategy failed: %s", err.Error())
  185. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  186. return
  187. }
  188. // 查询数据
  189. engine := db.GetEngine()
  190. datas := make([]QueryArbitrageStrategyRsp, 0)
  191. sql := fmt.Sprintf(`select
  192. to_char(t.ASApplyID) ASApplyID,
  193. t.ASNo,
  194. t.ASName,
  195. t.BizType,
  196. t.UserID,
  197. t.DeliveryGoodsID,
  198. t.GoodsGroupID,
  199. t.SpotQuota,
  200. t.FutureQuote,
  201. t.ApplyBasis,
  202. t.StrategyStatus,
  203. t.Remark,
  204. t.MarketID,
  205. t.TradeDate,
  206. t.CloseTradeDate,
  207. t.UsedQuota,
  208. t.FutureQty,
  209. t.FutureAvgPrice,
  210. t.FuturePL,
  211. t.PricedSpotQty,
  212. t.PricedSpotQtyNoTax,
  213. t.SpotAvgPrice,
  214. t.SpotPL,
  215. t.NetExposure,
  216. t.NetExposureRate,
  217. t.TotalPL,
  218. t.OpenBasis,
  219. t.CurBasis,
  220. t.BasisChangePL,
  221. t.NetExposurePL,
  222. t.SpotUsedQuota,
  223. t.FutureOpenQty,
  224. t.FutureOpenAmount,
  225. t.FutureCloseQty,
  226. t.FutureCloseAmount,
  227. t.SpotBuyAmount,
  228. t.SpotBuyQty,
  229. t.SpotSellAmount,
  230. t.SpotSellQty,
  231. t.UpdateTime
  232. from ERMS2_ArbitrageStrategy t
  233. left join ERMS2_ASAccount a on t.asapplyid = a.asapplyid and a.taaccounttype = 1
  234. where t.StrategyStatus = 0 and t.userid = %d`, req.UserID)
  235. if req.GoodsGroupID > 0 {
  236. sql += fmt.Sprintf(" and t.GoodsGroupID = %d", req.GoodsGroupID)
  237. }
  238. if err := engine.SQL(sql).Find(&datas); err != nil {
  239. // 查询失败
  240. logger.GetLogger().Errorf("QueryArbitrageStrategy failed: %s", err.Error())
  241. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  242. return
  243. }
  244. // 查询成功
  245. logger.GetLogger().Debugln("QueryArbitrageStrategy successed: %v", datas)
  246. appG.Response(http.StatusOK, e.SUCCESS, datas)
  247. }
  248. // QuerySpotContractReq 查询现货合同表信息请求参数
  249. type QuerySpotContractReq struct {
  250. AsApplyID uint64 `form:"asapplyid" binding:"required"`
  251. SpotContractID uint64 `form:"spotcontractid"`
  252. }
  253. // QuerySpotContractRsp 现货合同表
  254. type QuerySpotContractRsp struct {
  255. Spotcontractid string `json:"spotcontractid" xorm:"'SPOTCONTRACTID'" binding:"required"` // 现货合同ID(701+Unix秒时间戳(10位)+xxxxxx)
  256. Tradedate string `json:"tradedate" xorm:"'TRADEDATE'"` // 交易日(yyyyMMdd)
  257. Contractno string `json:"contractno" xorm:"'CONTRACTNO'"` // 现货合同编号
  258. Contracttype int64 `json:"contracttype" xorm:"'CONTRACTTYPE'"` // 现货合同类型 - 1:采购合同 -1:销售合同
  259. Areauserid int64 `json:"areauserid" xorm:"'AREAUSERID'"` // 所属机构
  260. Userid int64 `json:"userid" xorm:"'USERID'"` // 业务员用户ID
  261. Accountid int64 `json:"accountid" xorm:"'ACCOUNTID'"` // 资金账户ID
  262. Customeruserid int64 `json:"customeruserid" xorm:"'CUSTOMERUSERID'"` // 客户ID
  263. Customeraccountid int64 `json:"customeraccountid" xorm:"'CUSTOMERACCOUNTID'"` // 客户资金账户ID
  264. Signdate time.Time `json:"signdate" xorm:"'SIGNDATE'"` // 签订日期
  265. Lastdate time.Time `json:"lastdate" xorm:"'LASTDATE'"` // 交货时间
  266. Contractattachment string `json:"contractattachment" xorm:"'CONTRACTATTACHMENT'"` // 合同附件
  267. Producttype int64 `json:"producttype" xorm:"'PRODUCTTYPE'"` // 产品类型 - 1:标准仓单 2:等标 3:非标
  268. Deliverygoodsid int64 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 现货品种ID
  269. Deliverygoodsdesc string `json:"deliverygoodsdesc" xorm:"'DELIVERYGOODSDESC'"` // 品种说明
  270. Warehouseid int64 `json:"warehouseid" xorm:"'WAREHOUSEID'"` // 仓库ID
  271. Wrfactortypeid int64 `json:"wrfactortypeid" xorm:"'WRFACTORTYPEID'"` // 仓单要素类型ID
  272. Contractqtychar string `json:"contractqtychar" xorm:"'CONTRACTQTYCHAR'"` // 合同数量\已订价数量 (用于显示)
  273. Spotprice float64 `json:"spotprice" xorm:"'SPOTPRICE'"` // 价格
  274. Contractamount float64 `json:"contractamount" xorm:"'CONTRACTAMOUNT'"` // 合同金额
  275. Marketid int64 `json:"marketid" xorm:"'MARKETID'"` // 市场ID
  276. Remark string `json:"remark" xorm:"'REMARK'"` // 备注
  277. Handlestatus int64 `json:"handlestatus" xorm:"'HANDLESTATUS'"` // 处理状态
  278. Contractqty float64 `json:"contractqty" xorm:"'CONTRACTQTY'"` // 合同数量(数值) (用于计算)
  279. Positionqty int64 `json:"positionqty" xorm:"'POSITIONQTY'"` // 头寸数量 - 合同数量去小数部分
  280. Paystatus int64 `json:"paystatus" xorm:"'PAYSTATUS'"` // 收付款状态 - 0:未支付 1:已收款 2:已付款
  281. Payremark string `json:"payremark" xorm:"'PAYREMARK'"` // 收付款备注
  282. Paydatetime time.Time `json:"paydatetime" xorm:"'PAYDATETIME'"` // 收付款更新时间
  283. Invoicestatus int64 `json:"invoicestatus" xorm:"'INVOICESTATUS'"` // 开收票状态 - 0:未开票 1:已开票
  284. Invoiceremark string `json:"invoiceremark" xorm:"'INVOICEREMARK'"` // 发票备注
  285. Invoiceatt string `json:"invoiceatt" xorm:"'INVOICEATT'"` // 发票附件
  286. Invoicedatetime time.Time `json:"invoicedatetime" xorm:"'INVOICEDATETIME'"` // 开收票更新时间
  287. Spotstatus int64 `json:"spotstatus" xorm:"'SPOTSTATUS'"` // 收发货状态 - 0:未交收 1:已发货 2:已发货
  288. Spotremark string `json:"spotremark" xorm:"'SPOTREMARK'"` // 收发货备注
  289. Spotdatetime time.Time `json:"spotdatetime" xorm:"'SPOTDATETIME'"` // 收发货更新时间
  290. Relatedqty float64 `json:"relatedqty" xorm:"'RELATEDQTY'"` // 已关联数量
  291. Contractstatus int64 `json:"contractstatus" xorm:"'CONTRACTSTATUS'"` // 合同状态 - 0:未结束 1:已结束
  292. Closeremark string `json:"closeremark" xorm:"'CLOSEREMARK'"` // 结束备注
  293. Closetradedate string `json:"closetradedate" xorm:"'CLOSETRADEDATE'"` // 完结交易日(yyyyMMdd)
  294. Relatedstatus int64 `json:"relatedstatus" xorm:"'RELATEDSTATUS'"` // 关联完结状态 - 0:未结束 1:已结束
  295. Wrstandardid int64 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 仓单标准ID(SEQ_WRSTANDARD)
  296. Invoiceopentime time.Time `json:"invoiceopentime" xorm:"'INVOICEOPENTIME'"` // 开票时间
  297. Closetype int64 `json:"closetype" xorm:"'CLOSETYPE'"` // 终止类型 - 1:违约 2:提前终止
  298. Closedate time.Time `json:"closedate" xorm:"'CLOSEDATE'"` // 终止日期
  299. }
  300. // QuerySpotContract 查询现货合同表信息(指定策略ID、未结束的)
  301. // @Summary 查询现货合同表信息(指定策略ID、未结束的)
  302. // @Produce json
  303. // @Security ApiKeyAuth
  304. // @Param asapplyid query uint64 true "策略申请ID"
  305. // @Param spotcontractid query uint64 false "现货合同ID"
  306. // @Success 200 {object} QuerySpotContractRsp
  307. // @Failure 500 {object} app.Response
  308. // @Router /Erms2/QuerySpotContract [get]
  309. // @Tags 风险管理
  310. func QuerySpotContract(c *gin.Context) {
  311. appG := app.Gin{C: c}
  312. // 获取请求参数
  313. var req QuerySpotContractReq
  314. if err := appG.C.ShouldBindQuery(&req); err != nil {
  315. logger.GetLogger().Errorf("QuerySpotContract failed: %s", err.Error())
  316. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  317. return
  318. }
  319. // 查询数据
  320. engine := db.GetEngine()
  321. datas := make([]QuerySpotContractRsp, 0)
  322. sql := fmt.Sprintf(`select
  323. to_char(t.SpotContractID) SpotContractID,
  324. t.TradeDate,
  325. t.ContractNo,
  326. t.ContractType,
  327. t.AreaUserID,
  328. t.UserID,
  329. t.AccountID,
  330. t.CustomerUserID,
  331. t.CustomerAccountID,
  332. t.SignDate,
  333. t.LastDate,
  334. t.ContractAttachment,
  335. t.ProductType,
  336. t.DeliveryGoodsID,
  337. t.DeliveryGoodsDesc,
  338. t.WarehouseID,
  339. t.WRStandardID,
  340. t.WRFactorTypeID,
  341. t.ContractQtyChar,
  342. t.SpotPrice,
  343. t.ContractAmount,
  344. t.MarketID,
  345. t.Remark,
  346. t.HandleStatus,
  347. t.ContractQty,
  348. t.PositionQty,
  349. t.PayStatus,
  350. t.PayRemark,
  351. t.PayDateTime,
  352. t.InvoiceStatus,
  353. t.InvoiceRemark,
  354. t.InvoiceAtt,
  355. t.InvoiceOpenTime,
  356. t.InvoiceDateTime,
  357. t.SpotStatus,
  358. t.SpotRemark,
  359. t.SpotDateTime,
  360. t.RelatedQty,
  361. t.ContractStatus,
  362. t.CloseType,
  363. t.CloseDate,
  364. t.CloseRemark,
  365. t.CloseTradeDate,
  366. t.RelatedStatus
  367. from ERMS2_SpotContract t
  368. left join ERMS2_ASSpotDetail a on t.spotcontractid = a.spotcontractid
  369. where t.ContractStatus = 0
  370. and not exists (
  371. select 1 from ERMS2_ASTradeDetails e where e.spotcontractid=t.spotcontractid
  372. )
  373. and a.asapplyid = %d`, req.AsApplyID)
  374. if req.SpotContractID > 0 {
  375. sql += fmt.Sprintf(" and a.spotcontractid = %d", req.SpotContractID)
  376. }
  377. if err := engine.SQL(sql).Find(&datas); err != nil {
  378. // 查询失败
  379. logger.GetLogger().Errorf("QuerySpotContract failed: %s", err.Error())
  380. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  381. return
  382. }
  383. // 查询成功
  384. logger.GetLogger().Debugln("QuerySpotContract successed: %v", datas)
  385. appG.Response(http.StatusOK, e.SUCCESS, datas)
  386. }