delivery.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. ENUMDICNAME string `json:"enumdicname" xorm:"'ENUMDICNAME'"` // 品类单位名称
  48. PGOODSCODE string `json:"pgoodscode" xorm:"'PGOODSCODE'"` // p合约代码
  49. }
  50. // QueryDeliveryRelation 查询商品交割关系表
  51. // @Summary 查询商品交割关系表
  52. // @Produce json
  53. // @Security ApiKeyAuth
  54. // @Param goodsid query int false "商品ID"
  55. // @Param deliverygoodsid query int false "品种ID"
  56. // @Param marketid query int false "市场ID"
  57. // @Success 200 {object} QueryDeliveryRelationRsp
  58. // @Failure 500 {object} app.Response
  59. // @Router /Delivery/QueryDeliveryRelation [get]
  60. // @Tags 交割服务
  61. func QueryDeliveryRelation(c *gin.Context) {
  62. appG := app.Gin{C: c}
  63. // 获取请求参数
  64. var req QueryDeliveryRelationReq
  65. if err := appG.C.ShouldBindQuery(&req); err != nil {
  66. logger.GetLogger().Errorf("QueryDeliveryRelation failed: %s", err.Error())
  67. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  68. return
  69. }
  70. // 查询数据
  71. engine := db.GetEngine()
  72. datas := make([]QueryDeliveryRelationRsp, 0)
  73. sql := `select t.GOODSID,
  74. t.DELIVERYMODE,
  75. t.WRSTANDARDID,
  76. t.DELIVERYGOODSID,
  77. t.MINDELIVERYQTY,
  78. t.RRATIO1,
  79. t.RRATIO2,
  80. t.DELIVERYPRICERULE,
  81. t.BEGINDATE,
  82. t.ENDDATE,
  83. t.BUYTEMPLATEID,
  84. t.SELLTEMPLATEID,
  85. t.DELIVERYTYPE,
  86. t.XDELIVERYRATIO,
  87. t.PPRICEMODE,
  88. t.PDELIVERYPRICE,
  89. t.PDELIVERYRATIO,
  90. t.PGOODSID,
  91. t.P2PRICEMODE,
  92. t.P2DELIVERYPRICE,
  93. t.P2DELIVERYRATIO,
  94. t.P2GOODSID,
  95. t.RRATIO,
  96. g.goodscode,
  97. g.goodsname,
  98. g.marketid,
  99. g2.goodscode pgoodscode,
  100. g2.goodsname pgoodsname,
  101. dg.deliverygoodscode,
  102. dg.deliverygoodsname,
  103. e.enumdicname
  104. from DELIVERYRELATION t
  105. left join goods g
  106. on t.goodsid = g.goodsid
  107. left join goods g2 on t.pgoodsid=g2.goodsid
  108. left join deliverygoods dg
  109. on t.deliverygoodsid = dg.deliverygoodsid
  110. left join wrstandard wd on t.wrstandardid=wd.wrstandardid
  111. left join enumdicitem e on wd.unitid=e.enumitemname and e.enumdiccode='goodsunit'
  112. where 1 = 1`
  113. if req.GoodsID > 0 {
  114. sql += fmt.Sprintf(` and t.GoodsID = %d`, req.GoodsID)
  115. }
  116. if req.DeliveryGoodsID > 0 {
  117. sql += fmt.Sprintf(` and t.DeliveryGoodsID = %d`, req.DeliveryGoodsID)
  118. }
  119. // FIXME: 之前写成了 and t.MarketID = %d ,但返回时err == nil ,需要判断原因
  120. if req.MarketID > 0 {
  121. sql += fmt.Sprintf(` and g.MarketID = %d`, req.MarketID)
  122. }
  123. if err := engine.SQL(sql).Find(&datas); err != nil {
  124. // 查询失败
  125. logger.GetLogger().Errorf("QueryDeliveryRelation failed: %s", err.Error())
  126. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  127. return
  128. }
  129. // 查询成功
  130. logger.GetLogger().Debugln("QueryDeliveryRelation successed: %v", datas)
  131. appG.Response(http.StatusOK, e.SUCCESS, datas)
  132. }