business.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. strids := strings.Split(strings.TrimSpace(req.Accountids), ",")
  60. accountids := make([]int64, len(strids))
  61. for i := range strids {
  62. accountids[i], err = strconv.ParseInt(strids[i], 10, 64)
  63. if err != nil {
  64. logger.GetLogger().Errorf("ParseInt failed: %s", err.Error())
  65. appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
  66. return
  67. }
  68. }
  69. // 获取商品信息.
  70. goods, err := models.GetDeliverGoods()
  71. if err != nil {
  72. logger.GetLogger().Errorf("query deliverygoods failed: %s", err.Error())
  73. appG.Response(http.StatusBadRequest, e.ERROR_GET_GOODS_FAILED, nil)
  74. return
  75. }
  76. // 转换格式.
  77. id2goods := make(map[int32]models.Deliverygoods, len(goods))
  78. for i := range goods {
  79. id2goods[goods[i].Deliverygoodsid] = goods[i]
  80. }
  81. // 获取枚举信息.
  82. enuminfo, err := models.GetEnumDicItem("goodsunit", 0)
  83. if err != nil {
  84. logger.GetLogger().Errorf("query enumifno failed: %s", err.Error())
  85. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  86. return
  87. }
  88. // 转换格式.
  89. id2enum := make(map[int64]models.Enumdicitem)
  90. for i := range enuminfo {
  91. id2enum[enuminfo[i].Enumitemname] = enuminfo[i]
  92. }
  93. // 查询期现套利业务,首先查询业务ID集合.
  94. applyid, err := models.QueryASApplyIDSByAccountID(accountids)
  95. if err != nil {
  96. logger.GetLogger().Errorf("query applyid failed: %s", err.Error())
  97. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  98. return
  99. }
  100. // 查询业务对应单据详细信息.
  101. tradeinfo, err := models.QueryBizTradeInfo(applyid)
  102. if err != nil {
  103. logger.GetLogger().Errorf("query biztradedetail failed: %s", err.Error())
  104. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  105. return
  106. }
  107. asainfo, err := models.QueryArbitragestrategy(applyid, req.Status)
  108. if err != nil {
  109. logger.GetLogger().Errorf("query arbitragestrategy failed: %s", err.Error())
  110. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  111. return
  112. }
  113. redisClient := rediscli.GetRedisClient()
  114. preKey := "Erms2_ArbitrageStrategy:"
  115. rsp := make([]QueryBusinessInfoRsp, 0, len(asainfo))
  116. for i := range asainfo {
  117. key := preKey + strconv.FormatInt(asainfo[i].Asapplyid, 10)
  118. redisMsg := pb.Erms2ArbitrageStrategy{}
  119. redisRsp, err := redisClient.Get(key).Result()
  120. if err != nil {
  121. logger.GetLogger().Errorf("redis query arbitragestrategy failed: %s, key: %s", err.Error(), key)
  122. } else {
  123. err = proto.Unmarshal([]byte(redisRsp), &redisMsg)
  124. if err != nil {
  125. logger.GetLogger().Errorf("unmarshal arbitragestrategy failed: %s, key: %s", err.Error(), key)
  126. }
  127. }
  128. goodsinfo := id2goods[int32(asainfo[i].Deliverygoodsid)]
  129. goodsunit := id2enum[int64(goodsinfo.Goodsunitid)]
  130. detail := tradeinfo[asainfo[i].Asapplyid]
  131. business := QueryBusinessInfoRsp{
  132. BusinessID: asainfo[i].Asapplyid,
  133. Type: 1,
  134. GoodsID: strings.Join([]string{goodsinfo.Deliverygoodsname, goodsinfo.Deliverygoodscode}, "/"),
  135. Buyqty: strconv.FormatFloat(detail.Buyqty, 'f', 2, 64) + goodsunit.Enumdicname,
  136. BuyAmount: detail.Buyamount,
  137. Sellqty: strconv.FormatFloat(detail.Sellqty, 'f', 2, 64) + goodsunit.Enumdicname,
  138. SellAmount: detail.Sellamount,
  139. Spotqty: strconv.FormatFloat(asainfo[i].Pricedspotqty, 'f', 2, 64) + goodsunit.Enumdicname,
  140. SpotMarketValue: 0.0,
  141. Hedgingqty: strconv.FormatFloat(asainfo[i].Futureqty, 'f', 2, 64) + goodsunit.Enumdicname,
  142. Spotpl: redisMsg.GetSpotPL(),
  143. Futureqty: strconv.FormatFloat(asainfo[i].Futureqty, 'f', 2, 64) + goodsunit.Enumdicname,
  144. Futurepl: redisMsg.GetFuturePL(),
  145. Totalqty: strconv.FormatFloat(asainfo[i].Pricedspotqty-asainfo[i].Futureqty, 'f', 2, 64) + goodsunit.Enumdicname,
  146. Totalpl: redisMsg.GetTotalPL(),
  147. Status: asainfo[i].Strategystatus,
  148. }
  149. rsp = append(rsp, business)
  150. }
  151. // 查询仓单回购业务.
  152. wrrcontracts, err := models.QueryWRRContract(accountids, req.Status)
  153. if err != nil {
  154. logger.GetLogger().Errorf("query wrrcontract failed: %s", err.Error())
  155. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  156. return
  157. }
  158. wrrids := make([]int64, 0, len(wrrcontracts))
  159. for i := range wrrcontracts {
  160. wrrids = append(wrrids, wrrcontracts[i].Wrrcontractid)
  161. }
  162. // 查询业务对应单据详细信息.
  163. wrtradeinfo, err := models.QueryBizTradeInfo(wrrids)
  164. if err != nil {
  165. logger.GetLogger().Errorf("query biztradedetail failed: %s", err.Error())
  166. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  167. return
  168. }
  169. for i := range wrrcontracts {
  170. goodsinfo := id2goods[int32(wrrcontracts[i].Deliverygoodsid)]
  171. goodsunit := id2enum[int64(goodsinfo.Goodsunitid)]
  172. detail := wrtradeinfo[wrrcontracts[i].Wrrcontractid]
  173. business := QueryBusinessInfoRsp{
  174. BusinessID: wrrcontracts[i].Wrrcontractid,
  175. Type: 2,
  176. GoodsID: strings.Join([]string{goodsinfo.Deliverygoodsname, goodsinfo.Deliverygoodscode}, "/"),
  177. Buyqty: strconv.FormatFloat(detail.Buyqty, 'f', 2, 64) + goodsunit.Enumdicname,
  178. BuyAmount: detail.Buyamount,
  179. Sellqty: strconv.FormatFloat(detail.Sellqty, 'f', 2, 64) + goodsunit.Enumdicname,
  180. SellAmount: detail.Sellamount,
  181. Spotqty: strconv.FormatFloat(wrrcontracts[i].Contractqty, 'f', 2, 64) + goodsunit.Enumdicname,
  182. SpotMarketValue: 0,
  183. Hedgingqty: "-",
  184. Spotpl: 0,
  185. Futureqty: "-",
  186. Futurepl: 0,
  187. Totalqty: "-",
  188. Totalpl: 0,
  189. Status: wrrcontracts[i].Contractstatus,
  190. }
  191. rsp = append(rsp, business)
  192. }
  193. // 查询现货贸易业务.
  194. spotTradeBiz, err := models.QuerySpotTradeBiz(accountids, req.Status)
  195. if err != nil {
  196. logger.GetLogger().Errorf("query spottradebiz failed: %s", err.Error())
  197. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  198. return
  199. }
  200. spotids := make([]int64, 0, len(spotTradeBiz))
  201. for i := range spotTradeBiz {
  202. spotids = append(spotids, spotTradeBiz[i].Spottradeid)
  203. }
  204. // 查询业务对应单据详细信息.
  205. spottradeinfo, err := models.QueryBizTradeInfo(spotids)
  206. if err != nil {
  207. logger.GetLogger().Errorf("query spottradedetail failed: %s", err.Error())
  208. appG.Response(http.StatusBadRequest, e.ERROR_QUERY_FAIL, nil)
  209. return
  210. }
  211. for i := range spotTradeBiz {
  212. goodsinfo := id2goods[int32(spotTradeBiz[i].Deliverygoodsid)]
  213. goodsunit := id2enum[int64(goodsinfo.Goodsunitid)]
  214. detail := spottradeinfo[spotTradeBiz[i].Spottradeid]
  215. business := QueryBusinessInfoRsp{
  216. BusinessID: spotTradeBiz[i].Spottradeid,
  217. Type: 3,
  218. GoodsID: strings.Join([]string{goodsinfo.Deliverygoodsname, goodsinfo.Deliverygoodscode}, "/"),
  219. Buyqty: strconv.FormatFloat(detail.Buyqty, 'f', 2, 64) + goodsunit.Enumdicname,
  220. BuyAmount: detail.Buyamount,
  221. Sellqty: strconv.FormatFloat(detail.Sellqty, 'f', 2, 64) + goodsunit.Enumdicname,
  222. SellAmount: detail.Sellamount,
  223. Spotqty: strconv.FormatFloat(spotTradeBiz[i].Buyqty+spotTradeBiz[i].Sellqty, 'f', 2, 64) + goodsunit.Enumdicname,
  224. SpotMarketValue: 0,
  225. Hedgingqty: "-",
  226. Spotpl: 0,
  227. Futureqty: "-",
  228. Futurepl: 0,
  229. Totalqty: "-",
  230. Totalpl: 0,
  231. Status: spotTradeBiz[i].Closestatus,
  232. }
  233. rsp = append(rsp, business)
  234. }
  235. // 查询成功
  236. logger.GetLogger().Debugf("QuerySpotContractDetail successed: %v", rsp)
  237. appG.Response(http.StatusOK, e.SUCCESS, rsp)
  238. }