business.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. package erms3
  2. import (
  3. "mtp2_if/global/app"
  4. "mtp2_if/global/e"
  5. "mtp2_if/logger"
  6. "mtp2_if/models"
  7. "mtp2_if/pb"
  8. "mtp2_if/rediscli"
  9. "net/http"
  10. "strconv"
  11. "strings"
  12. "github.com/gin-gonic/gin"
  13. "github.com/golang/protobuf/proto"
  14. )
  15. // QueryBusinessInfoReq 查询业务请求.
  16. type QueryBusinessInfoReq struct {
  17. Accountids string `form:"accountids" binding:"required"` // 资金账号ID列表,逗号分隔.
  18. Status int32 `form:"status"` // 合同状态,0-未结束 1-已结束.
  19. }
  20. // QueryBusinessInfoRsp 查询业务响应.
  21. type QueryBusinessInfoRsp struct {
  22. BusinessID int64 `json:"businessid"` // 业务ID.
  23. Type int32 `json:"type"` // 业务类型,1-期现套利,2-仓单回购,3-现货贸易.
  24. GoodsID string `json:"goodsid"` // 商品名称/商品代码.
  25. Buyqty string `json:"buyqty"` // 采购量.
  26. BuyAmount float64 `json:"buyamount"` // 采购额.
  27. Sellqty string `json:"sellqty"` // 销售量.
  28. SellAmount float64 `json:"sellamount"` // 销售额.
  29. Spotqty string `json:"spotqty"` // 现货量.
  30. SpotMarketValue float64 `json:"spotmarketvalue"` // 现货市值.
  31. Hedgingqty string `json:"hedgingqty"` // 套保量.
  32. Spotpl float64 `json:"spotpl"` // 浮动权益.
  33. Futureqty string `json:"futureqty"` // 期货敞口.
  34. Futurepl float64 `json:"futurepl"` // 期货盈亏.
  35. Totalqty string `json:"totalqty"` // 总敞口.
  36. Totalpl float64 `json:"totalpl"` // 总盈亏.
  37. Status int32 `json:"statu"` // 状态,0-未结束 1-已结束.
  38. }
  39. // QueryBusinessInfo 查询业务数据
  40. // @Summary 查询业务表单数据
  41. // @Produce json
  42. // @Security ApiKeyAuth
  43. // @Param accountids query string true "资金账号ID列表,用逗号分隔"
  44. // @Param status query int true "状态,0为未结束 1为已结束"
  45. // @Success 200 {array} QueryBusinessInfoRsp
  46. // @Failure 500 {object} app.Response
  47. // @Router /Erms3/QueryBusinessInfo [get]
  48. // @Tags 风险管理v3
  49. func QueryBusinessInfo(c *gin.Context) {
  50. appG := app.Gin{C: c}
  51. // 获取请求参数
  52. var req QueryBusinessInfoReq
  53. err := appG.C.ShouldBindQuery(&req)
  54. if err != nil {
  55. logger.GetLogger().Errorf("QueryBusinessInfo failed: %s", err.Error())
  56. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  57. return
  58. }
  59. // 获取商品信息.
  60. goods, err := models.GetDeliverGoods()
  61. if err != nil {
  62. logger.GetLogger().Errorf("query deliverygoods failed: %s", err.Error())
  63. appG.Response(http.StatusBadRequest, e.ERROR_GET_GOODS_FAILED, nil)
  64. return
  65. }
  66. // 转换格式.
  67. id2goods := make(map[int32]models.Deliverygoods, len(goods))
  68. for i := range goods {
  69. id2goods[goods[i].Deliverygoodsid] = goods[i]
  70. }
  71. // 获取枚举信息.
  72. enuminfo, err := models.GetEnumDicItem("goodsunit", 0)
  73. if err != nil {
  74. logger.GetLogger().Errorf("query enumifno failed: %s", err.Error())
  75. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  76. return
  77. }
  78. // 转换格式.
  79. id2enum := make(map[int64]models.Enumdicitem)
  80. for i := range enuminfo {
  81. id2enum[enuminfo[i].Enumitemname] = enuminfo[i]
  82. }
  83. // 查询期现套利业务.
  84. var asa models.Erms2Arbitragestrategy
  85. asainfo, err := asa.GetByAccountIDSAndStatus(req.Accountids, req.Status)
  86. if err != nil {
  87. logger.GetLogger().Errorf("query asainfo failed: %s", err.Error())
  88. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  89. return
  90. }
  91. redisClient := rediscli.GetRedisClient()
  92. preKey := "Erms2_ArbitrageStrategy:"
  93. rsp := make([]QueryBusinessInfoRsp, 0, len(asainfo))
  94. for i := range asainfo {
  95. key := preKey + asainfo[i].Asapplyid
  96. redisMsg := pb.Erms2ArbitrageStrategy{}
  97. redisRsp, err := redisClient.Get(key).Result()
  98. if err != nil {
  99. logger.GetLogger().Errorf("redis query arbitragestrategy failed: %s, key: %s", err.Error(), key)
  100. } else {
  101. err = proto.Unmarshal([]byte(redisRsp), &redisMsg)
  102. if err != nil {
  103. logger.GetLogger().Errorf("unmarshal arbitragestrategy failed: %s, key: %s", err.Error(), key)
  104. }
  105. }
  106. goodsinfo := id2goods[int32(asainfo[i].Deliverygoodsid)]
  107. goodsunit := id2enum[int64(goodsinfo.Goodsunitid)]
  108. businessid, err := strconv.ParseInt(asainfo[i].Asapplyid, 10, 64)
  109. if err != nil {
  110. logger.GetLogger().Errorf("parse string[%s] to int failed: %s", asainfo[i].Asapplyid, err.Error())
  111. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  112. return
  113. }
  114. business := QueryBusinessInfoRsp{
  115. BusinessID: businessid,
  116. Type: 1,
  117. GoodsID: strings.Join([]string{goodsinfo.Deliverygoodsname, goodsinfo.Deliverygoodscode}, "/"),
  118. Buyqty: strconv.FormatFloat(asainfo[i].Buyqty, 'f', 2, 64) + goodsunit.Enumdicname,
  119. BuyAmount: asainfo[i].Buyamount,
  120. Sellqty: strconv.FormatFloat(asainfo[i].Sellqty, 'f', 2, 64) + goodsunit.Enumdicname,
  121. SellAmount: asainfo[i].Sellamount,
  122. Spotqty: strconv.FormatFloat(asainfo[i].Pricedspotqty, 'f', 2, 64) + goodsunit.Enumdicname,
  123. SpotMarketValue: 0.0,
  124. Hedgingqty: strconv.FormatFloat(asainfo[i].Futureqty, 'f', 2, 64) + goodsunit.Enumdicname,
  125. Spotpl: redisMsg.GetSpotPL(),
  126. Futureqty: strconv.FormatFloat(asainfo[i].Futureqty, 'f', 2, 64) + goodsunit.Enumdicname,
  127. Futurepl: redisMsg.GetFuturePL(),
  128. Totalqty: strconv.FormatFloat(asainfo[i].Pricedspotqty-asainfo[i].Futureqty, 'f', 2, 64) + goodsunit.Enumdicname,
  129. Totalpl: redisMsg.GetTotalPL(),
  130. Status: asainfo[i].Strategystatus,
  131. }
  132. rsp = append(rsp, business)
  133. }
  134. // 查询仓单回购业务.
  135. var wr models.Erms2Wrrcontract
  136. wrrcontracts, err := wr.GetByAccountIDSAndStatus(req.Accountids, req.Status)
  137. if err != nil {
  138. logger.GetLogger().Errorf("query wrrcontract failed: %s", err.Error())
  139. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  140. return
  141. }
  142. for i := range wrrcontracts {
  143. goodsinfo := id2goods[int32(wrrcontracts[i].Deliverygoodsid)]
  144. goodsunit := id2enum[int64(goodsinfo.Goodsunitid)]
  145. businessid, err := strconv.ParseInt(wrrcontracts[i].Wrrcontractid, 10, 64)
  146. if err != nil {
  147. logger.GetLogger().Errorf("parse string[%s] to int failed: %s", asainfo[i].Asapplyid, err.Error())
  148. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  149. return
  150. }
  151. business := QueryBusinessInfoRsp{
  152. BusinessID: businessid,
  153. Type: 2,
  154. GoodsID: strings.Join([]string{goodsinfo.Deliverygoodsname, goodsinfo.Deliverygoodscode}, "/"),
  155. Buyqty: strconv.FormatFloat(wrrcontracts[i].Buyqty, 'f', 2, 64) + goodsunit.Enumdicname,
  156. BuyAmount: wrrcontracts[i].Buyamount,
  157. Sellqty: strconv.FormatFloat(wrrcontracts[i].Sellqty, 'f', 2, 64) + goodsunit.Enumdicname,
  158. SellAmount: wrrcontracts[i].Sellamount,
  159. Spotqty: strconv.FormatFloat(wrrcontracts[i].Contractqty, 'f', 2, 64) + goodsunit.Enumdicname,
  160. SpotMarketValue: 0,
  161. Hedgingqty: "-",
  162. Spotpl: 0,
  163. Futureqty: "-",
  164. Futurepl: 0,
  165. Totalqty: "-",
  166. Totalpl: 0,
  167. Status: wrrcontracts[i].Contractstatus,
  168. }
  169. rsp = append(rsp, business)
  170. }
  171. // 查询现货贸易业务.
  172. var sbt models.Erms2Spottradebiz
  173. spotTradeBiz, err := sbt.GetByAccountIDSAndStatus(req.Accountids, req.Status)
  174. if err != nil {
  175. logger.GetLogger().Errorf("query spottradebiz failed: %s", err.Error())
  176. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  177. return
  178. }
  179. for i := range spotTradeBiz {
  180. goodsinfo := id2goods[int32(spotTradeBiz[i].Deliverygoodsid)]
  181. goodsunit := id2enum[int64(goodsinfo.Goodsunitid)]
  182. businessid, err := strconv.ParseInt(spotTradeBiz[i].Spottradeid, 10, 64)
  183. if err != nil {
  184. logger.GetLogger().Errorf("parse string[%s] to int failed: %s", asainfo[i].Asapplyid, err.Error())
  185. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  186. return
  187. }
  188. business := QueryBusinessInfoRsp{
  189. BusinessID: businessid,
  190. Type: 3,
  191. GoodsID: strings.Join([]string{goodsinfo.Deliverygoodsname, goodsinfo.Deliverygoodscode}, "/"),
  192. Buyqty: strconv.FormatFloat(spotTradeBiz[i].TradeBuyqty, 'f', 2, 64) + goodsunit.Enumdicname,
  193. BuyAmount: spotTradeBiz[i].TradeBuyamount,
  194. Sellqty: strconv.FormatFloat(spotTradeBiz[i].TradeSellqty, 'f', 2, 64) + goodsunit.Enumdicname,
  195. SellAmount: spotTradeBiz[i].TradeSellamount,
  196. Spotqty: strconv.FormatFloat(spotTradeBiz[i].Buyqty+spotTradeBiz[i].Sellqty, 'f', 2, 64) + goodsunit.Enumdicname,
  197. SpotMarketValue: 0,
  198. Hedgingqty: "-",
  199. Spotpl: 0,
  200. Futureqty: "-",
  201. Futurepl: 0,
  202. Totalqty: "-",
  203. Totalpl: 0,
  204. Status: spotTradeBiz[i].Closestatus,
  205. }
  206. rsp = append(rsp, business)
  207. }
  208. // 查询成功
  209. logger.GetLogger().Debugf("QuerySpotContractDetail successed: %v", rsp)
  210. appG.Response(http.StatusOK, e.SUCCESS, rsp)
  211. }
  212. // AddErms2ASApplyReq 新增期现套利业务申请请求参数
  213. type AddErms2ASApplyReq struct {
  214. models.Erms2asapply
  215. }
  216. // AddErms2ASApply 新增期现套利业务申请
  217. // @Summary 新增期现套利业务申请
  218. // @Produce json
  219. // @Security ApiKeyAuth
  220. // @Param jsonBody body AddErms2ASApplyReq true "申请参数"
  221. // @Success 200 {object} app.Response
  222. // @Failure 500 {object} app.Response
  223. // @Router /Erms3/AddErms2ASApply [post]
  224. // @Tags 风险管理v3
  225. func AddErms2ASApply(c *gin.Context) {
  226. appG := app.Gin{C: c}
  227. // 获取请求参数
  228. var req AddErms2ASApplyReq
  229. err := appG.C.ShouldBindJSON(&req)
  230. if err != nil {
  231. logger.GetLogger().Errorf("AddErms2ASApply failed: %s", err.Error())
  232. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  233. return
  234. }
  235. // 插入数据
  236. if err := models.InsertErms2AsApply(req.Erms2asapply); err != nil {
  237. logger.GetLogger().Errorf("AddErms2ASApply failed: %s", err.Error())
  238. appG.Response(http.StatusBadRequest, e.ERROR_OPERATION_FAILED, nil)
  239. return
  240. }
  241. // 执行成功
  242. logger.GetLogger().Debugf("AddErms2ASApply successed: ok")
  243. appG.Response(http.StatusOK, e.SUCCESS, "ok")
  244. }
  245. // AddErms2SpotTradeApplyReq 新增期现套利业务申请请求参数
  246. type AddErms2SpotTradeApplyReq struct {
  247. models.Erms2spottradeapply
  248. }
  249. // AddErms2SpotTradeApply 新增现货贸易业务申请
  250. // @Summary 新增现货贸易业务申请
  251. // @Produce json
  252. // @Security ApiKeyAuth
  253. // @Param jsonBody body AddErms2SpotTradeApplyReq true "申请参数"
  254. // @Success 200 {object} app.Response
  255. // @Failure 500 {object} app.Response
  256. // @Router /Erms3/AddErms2SpotTradeApply [post]
  257. // @Tags 风险管理v3
  258. func AddErms2SpotTradeApply(c *gin.Context) {
  259. appG := app.Gin{C: c}
  260. // 获取请求参数
  261. var req AddErms2SpotTradeApplyReq
  262. err := appG.C.ShouldBindJSON(&req)
  263. if err != nil {
  264. logger.GetLogger().Errorf("AddErms2SpotTradeApply failed: %s", err.Error())
  265. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  266. return
  267. }
  268. // 插入数据
  269. if err := models.InsertErms2SpotTradeApply(req.Erms2spottradeapply); err != nil {
  270. logger.GetLogger().Errorf("AddErms2SpotTradeApply failed: %s", err.Error())
  271. appG.Response(http.StatusBadRequest, e.ERROR_OPERATION_FAILED, nil)
  272. return
  273. }
  274. // 执行成功
  275. logger.GetLogger().Debugf("AddErms2SpotTradeApply successed: ok")
  276. appG.Response(http.StatusOK, e.SUCCESS, "ok")
  277. }