ermcpExposure.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * @Author: zou.yingbin
  3. * @Create : 2021/1/18 9:26
  4. * @Modify : 2021/1/18 9:26
  5. */
  6. package models
  7. import (
  8. "fmt"
  9. "github.com/golang/protobuf/proto"
  10. "mtp2_if/mtpcache"
  11. "mtp2_if/pb"
  12. "mtp2_if/rediscli"
  13. )
  14. //实时敞口
  15. type ErmcpRealExposureModel struct {
  16. MiddleGoodsID uint32 // 套保品种
  17. AreaUserID uint32 // 所属机构
  18. OriBuyPlanQty float64 // 期初采购计划数量
  19. OriBuyPricedQty float64 // 期初采购合同已定价数量
  20. OriSellPlanQty float64 // 期初销售计划数量
  21. OriSellPricedQty float64 // 期初销售合同已定价数量
  22. OriBuyFutureQty uint64 // 期初买入期货数量
  23. OriSellFutureQty uint64 // 期初卖出期货数量
  24. BuyPlanQty float64 // 采购计划数量
  25. BuyPricedQty float64 // 采购合同已定价数量
  26. SellPlanQty float64 // 销售计划数量
  27. SellPricedQty float64 // 销售合同已定价数量
  28. BuyFutureQty float64 // 买入期货数量
  29. SellFutureQty float64 // 卖出期货数量
  30. TotalSpotQty float64 // 现货数量
  31. TotalFutureQty float64 // 期货数量
  32. TotalExposure float64 // 总敞口
  33. TotalHedgeRatio float64 // 敞口比例
  34. TotalNeedHedgeQty float64 // 期货应套保量
  35. NeedHedgeExposoure float64 // 应套保敞口
  36. NeedHedgeRatio float64 // 应套保敞口比例
  37. //************以下需计算或非redis数据************//
  38. MiddleGoodsName string // 套保品种名称
  39. MiddleGoodsCode string // 套保品种代码
  40. MiddleGoodsHedgeRatio float64 // 应套保比例
  41. OriTotalSpotQty float64 // 期初现货数量=(期初销售计划数量-期初销售合同已定价数量)-(期初采购计划数量-期初采购合同已定价数量)
  42. OriTotalFutuQty uint64 // 期初期货数量=期初买入期货数量-期初卖出期货数量
  43. }
  44. // 计算相关字段
  45. func (r *ErmcpRealExposureModel) calc() {
  46. r.OriTotalSpotQty = (r.OriSellPlanQty - r.OriSellPricedQty) - (r.OriBuyPlanQty - r.OriBuyPricedQty)
  47. r.OriTotalFutuQty = r.OriBuyFutureQty - r.OriSellFutureQty
  48. }
  49. func (r *ErmcpRealExposureModel) ParseFromProto(v *pb.ErmcpAreaExposure) {
  50. r.MiddleGoodsID = *v.MiddleGoodsID
  51. r.AreaUserID = *v.AreaUserID
  52. r.OriBuyPlanQty = *v.OriBuyPlanQty
  53. r.OriBuyPricedQty = *v.OriBuyPricedQty
  54. r.OriSellPlanQty = *v.OriSellPlanQty
  55. r.OriSellPricedQty = *v.OriSellPricedQty
  56. r.OriBuyFutureQty = *v.OriBuyFutureQty
  57. r.OriSellFutureQty = *v.OriSellFutureQty
  58. r.BuyPlanQty = *v.BuyPlanQty
  59. r.BuyPricedQty = *v.BuyPricedQty
  60. r.SellPlanQty = *v.SellPlanQty
  61. r.SellPricedQty = *v.SellPricedQty
  62. r.BuyFutureQty = *v.BuyFutureQty
  63. r.SellFutureQty = *v.SellFutureQty
  64. r.TotalSpotQty = *v.TotalSpotQty
  65. r.TotalFutureQty = *v.TotalFutureQty
  66. r.TotalExposure = *v.TotalExposure
  67. r.TotalHedgeRatio = *v.TotalHedgeRatio
  68. r.TotalNeedHedgeQty = *v.TotalNeedHedgeQty
  69. r.NeedHedgeExposoure = *v.NeedHedgeExposoure
  70. r.NeedHedgeRatio = *v.NeedHedgeRatio
  71. // 执行相关计算
  72. r.calc()
  73. }
  74. // 实时敞口数据: Redis数据 + 套保品种信息表
  75. func (r *ErmcpRealExposureModel) GetData() ([]ErmcpRealExposureModel, error) {
  76. // 获取关联的套路商品
  77. if sGoods, err := mtpcache.GetMiddleGoodsByUserID(r.AreaUserID); err == nil {
  78. sData := make([]ErmcpRealExposureModel, 0)
  79. // 从Redis获取数据
  80. for i := range sGoods {
  81. key := fmt.Sprintf("ErmcpAreaExposure:%d_%d", sGoods[i].AREAUSERID, sGoods[i].MIDDLEGOODSID)
  82. if ret, err := rediscli.GetRedisClient().Get(key).Result(); err == nil {
  83. if len(ret) > 0 {
  84. var data pb.ErmcpAreaExposure
  85. if err := proto.Unmarshal([]byte(ret), &data); err == nil {
  86. var m ErmcpRealExposureModel
  87. m.ParseFromProto(&data)
  88. sData = append(sData, m)
  89. }
  90. }
  91. }
  92. }
  93. return sData, nil
  94. }
  95. return nil, nil
  96. }