delivery.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package delivery
  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. "github.com/gin-gonic/gin"
  10. )
  11. // QueryDeliveryRelationReq 查询商品交割关系表请求参数
  12. type QueryDeliveryRelationReq struct {
  13. GoodsID int `form:"goodsid"`
  14. DeliveryGoodsID int `form:"deliverygoodsid"`
  15. MarketID int `form:"marketid"`
  16. }
  17. // QueryDeliveryRelationRsp 商品交割关系表
  18. type QueryDeliveryRelationRsp struct {
  19. Goodsid int64 `json:"goodsid" xorm:"'GOODSID'" binding:"required"` // 交易合约ID
  20. Deliverymode int64 `json:"deliverymode" xorm:"'DELIVERYMODE'"` // 交割方式 - 1:点选式 2:申报式
  21. Wrstandardid int64 `json:"wrstandardid" xorm:"'WRSTANDARDID'"` // 仓单标准ID
  22. Deliverygoodsid int64 `json:"deliverygoodsid" xorm:"'DELIVERYGOODSID'"` // 交割商品
  23. Mindeliveryqty int64 `json:"mindeliveryqty" xorm:"'MINDELIVERYQTY'" binding:"required"` // 最小交割系数(K)
  24. Rratio1 int64 `json:"rratio1" xorm:"'RRATIO1'"` // 兑换系数(交易合约)(R1)
  25. Rratio2 int64 `json:"rratio2" xorm:"'RRATIO2'"` // 兑换系数(仓单标准)(R2)
  26. Deliverypricerule int64 `json:"deliverypricerule" xorm:"'DELIVERYPRICERULE'"` // 交割价规则- 1:行情价 2:建仓价
  27. Begindate string `json:"begindate" xorm:"'BEGINDATE'" binding:"required"` // 起始日期(yyyyMMdd)
  28. Enddate string `json:"enddate" xorm:"'ENDDATE'" binding:"required"` // 结束日期(yyyyMMdd)
  29. Buytemplateid int64 `json:"buytemplateid" xorm:"'BUYTEMPLATEID'"` // 买履约计划模板ID
  30. Selltemplateid int64 `json:"selltemplateid" xorm:"'SELLTEMPLATEID'"` // 卖履约计划模板ID
  31. Deliverytype int64 `json:"deliverytype" xorm:"'DELIVERYTYPE'"` // 交割模式 - 1:X交割 2:X+P交割 3:X+C交割 4:X+P+C交割
  32. Xdeliveryratio int64 `json:"xdeliveryratio" xorm:"'XDELIVERYRATIO'" binding:"required"` // 交易合约系数(m)
  33. Ppricemode int64 `json:"ppricemode" xorm:"'PPRICEMODE'"` // P合约价格方式 - 1:商品价 2:固定值
  34. Pdeliveryprice float64 `json:"pdeliveryprice" xorm:"'PDELIVERYPRICE'"` // P合约价格(商品价时填写0,固定值时填写固定值)
  35. Pdeliveryratio int64 `json:"pdeliveryratio" xorm:"'PDELIVERYRATIO'"` // P合约系数(n)
  36. Pgoodsid int64 `json:"pgoodsid" xorm:"'PGOODSID'"` // P合约ID
  37. P2pricemode int64 `json:"p2pricemode" xorm:"'P2PRICEMODE'"` // P2合约价格方式 - 1:商品价 2:固定值
  38. P2deliveryprice float64 `json:"p2deliveryprice" xorm:"'P2DELIVERYPRICE'"` // P2合约价格(商品价时填写0,固定值时填写固定值)
  39. P2deliveryratio int64 `json:"p2deliveryratio" xorm:"'P2DELIVERYRATIO'"` // P2合约系数(p)
  40. P2goodsid int64 `json:"p2goodsid" xorm:"'P2GOODSID'"` // P2合约ID
  41. Rratio int64 `json:"rratio" xorm:"'RRATIO'"` // 兑换系数(R)
  42. Goodscode string `json:"goodscode" xorm:"'GOODSCODE'"` // 商品代码
  43. Goodsname string `json:"goodsname" xorm:"'GOODSNAME'"` // 商品名称
  44. Marketid int64 `json:"marketid" xorm:"'MARKETID'"` // 市场ID
  45. Deliverygoodscode string `json:"deliverygoodscode" xorm:"'DELIVERYGOODSCODE'"` // 品种代码
  46. Deliverygoodsname string `json:"deliverygoodsname" xorm:"'DELIVERYGOODSNAME'"` // 品种名称
  47. }
  48. // QueryDeliveryRelation 查询商品交割关系表
  49. // @Summary 查询商品交割关系表
  50. // @Produce json
  51. // @Security ApiKeyAuth
  52. // @Param goodsid query int false "商品ID"
  53. // @Param deliverygoodsid query int false "品种ID"
  54. // @Param marketid query int false "市场ID"
  55. // @Success 200 {object} QueryDeliveryRelationRsp
  56. // @Failure 500 {object} app.Response
  57. // @Router /Delivery/QueryDeliveryRelation [get]
  58. // @Tags 交割服务
  59. func QueryDeliveryRelation(c *gin.Context) {
  60. appG := app.Gin{C: c}
  61. // 获取请求参数
  62. var req QueryDeliveryRelationReq
  63. if err := appG.C.ShouldBindQuery(&req); err != nil {
  64. logger.GetLogger().Errorf("QueryDeliveryRelation failed: %s", err.Error())
  65. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  66. return
  67. }
  68. // 查询数据
  69. engine := db.GetEngine()
  70. datas := make([]QueryDeliveryRelationRsp, 0)
  71. sql := `select
  72. t.GOODSID,
  73. t.DELIVERYMODE,
  74. t.WRSTANDARDID,
  75. t.DELIVERYGOODSID,
  76. t.MINDELIVERYQTY,
  77. t.RRATIO1,
  78. t.RRATIO2,
  79. t.DELIVERYPRICERULE,
  80. t.BEGINDATE,
  81. t.ENDDATE,
  82. t.BUYTEMPLATEID,
  83. t.SELLTEMPLATEID,
  84. t.DELIVERYTYPE,
  85. t.XDELIVERYRATIO,
  86. t.PPRICEMODE,
  87. t.PDELIVERYPRICE,
  88. t.PDELIVERYRATIO,
  89. t.PGOODSID,
  90. t.P2PRICEMODE,
  91. t.P2DELIVERYPRICE,
  92. t.P2DELIVERYRATIO,
  93. t.P2GOODSID,
  94. t.RRATIO,
  95. g.goodscode,
  96. g.goodsname,
  97. g.marketid,
  98. dg.deliverygoodscode,
  99. dg.deliverygoodsname
  100. from DELIVERYRELATION t
  101. left join goods g on t.goodsid = g.goodsid
  102. left join deliverygoods dg on t.deliverygoodsid = dg.deliverygoodsid
  103. where 1=1`
  104. if req.GoodsID > 0 {
  105. sql += fmt.Sprintf(` and t.GoodsID = %d`, req.GoodsID)
  106. }
  107. if req.DeliveryGoodsID > 0 {
  108. sql += fmt.Sprintf(` and t.DeliveryGoodsID = %d`, req.DeliveryGoodsID)
  109. }
  110. // FIXME: 之前写成了 and t.MarketID = %d ,但返回时err == nil ,需要判断原因
  111. if req.MarketID > 0 {
  112. sql += fmt.Sprintf(` and g.MarketID = %d`, req.MarketID)
  113. }
  114. if err := engine.SQL(sql).Find(&datas); err != nil {
  115. // 查询失败
  116. logger.GetLogger().Errorf("QueryDeliveryRelation failed: %s", err.Error())
  117. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  118. return
  119. }
  120. // 查询成功
  121. logger.GetLogger().Infof("QueryDeliveryRelation successed: %v", datas)
  122. appG.Response(http.StatusOK, e.SUCCESS, datas)
  123. }