/** * @Author: zou.yingbin * @Create : 2021/1/18 9:26 * @Modify : 2021/1/18 9:26 */ package models import ( "fmt" "github.com/golang/protobuf/proto" "mtp2_if/mtpcache" "mtp2_if/pb" "mtp2_if/rediscli" ) //实时敞口 type ErmcpRealExposureModel struct { MiddleGoodsID uint32 // 套保品种 AreaUserID uint32 // 所属机构 OriBuyPlanQty float64 // 期初采购计划数量 OriBuyPricedQty float64 // 期初采购合同已定价数量 OriSellPlanQty float64 // 期初销售计划数量 OriSellPricedQty float64 // 期初销售合同已定价数量 OriBuyFutureQty uint64 // 期初买入期货数量 OriSellFutureQty uint64 // 期初卖出期货数量 BuyPlanQty float64 // 采购计划数量 BuyPricedQty float64 // 采购合同已定价数量 SellPlanQty float64 // 销售计划数量 SellPricedQty float64 // 销售合同已定价数量 BuyFutureQty float64 // 买入期货数量 SellFutureQty float64 // 卖出期货数量 TotalSpotQty float64 // 现货数量 TotalFutureQty float64 // 期货数量 TotalExposure float64 // 总敞口 TotalHedgeRatio float64 // 敞口比例 TotalNeedHedgeQty float64 // 期货应套保量 NeedHedgeExposoure float64 // 应套保敞口 NeedHedgeRatio float64 // 应套保敞口比例 //************以下需计算或非redis数据************// MiddleGoodsName string // 套保品种名称 MiddleGoodsCode string // 套保品种代码 MiddleGoodsHedgeRatio float64 // 应套保比例 OriTotalSpotQty float64 // 期初现货数量=(期初销售计划数量-期初销售合同已定价数量)-(期初采购计划数量-期初采购合同已定价数量) OriTotalFutuQty uint64 // 期初期货数量=期初买入期货数量-期初卖出期货数量 } // 计算相关字段 func (r *ErmcpRealExposureModel) calc() { r.OriTotalSpotQty = (r.OriSellPlanQty - r.OriSellPricedQty) - (r.OriBuyPlanQty - r.OriBuyPricedQty) r.OriTotalFutuQty = r.OriBuyFutureQty - r.OriSellFutureQty } func (r *ErmcpRealExposureModel) ParseFromProto(v *pb.ErmcpAreaExposure) { r.MiddleGoodsID = *v.MiddleGoodsID r.AreaUserID = *v.AreaUserID r.OriBuyPlanQty = *v.OriBuyPlanQty r.OriBuyPricedQty = *v.OriBuyPricedQty r.OriSellPlanQty = *v.OriSellPlanQty r.OriSellPricedQty = *v.OriSellPricedQty r.OriBuyFutureQty = *v.OriBuyFutureQty r.OriSellFutureQty = *v.OriSellFutureQty r.BuyPlanQty = *v.BuyPlanQty r.BuyPricedQty = *v.BuyPricedQty r.SellPlanQty = *v.SellPlanQty r.SellPricedQty = *v.SellPricedQty r.BuyFutureQty = *v.BuyFutureQty r.SellFutureQty = *v.SellFutureQty r.TotalSpotQty = *v.TotalSpotQty r.TotalFutureQty = *v.TotalFutureQty r.TotalExposure = *v.TotalExposure r.TotalHedgeRatio = *v.TotalHedgeRatio r.TotalNeedHedgeQty = *v.TotalNeedHedgeQty r.NeedHedgeExposoure = *v.NeedHedgeExposoure r.NeedHedgeRatio = *v.NeedHedgeRatio // 执行相关计算 r.calc() } // 实时敞口数据: Redis数据 + 套保品种信息表 func (r *ErmcpRealExposureModel) GetData() ([]ErmcpRealExposureModel, error) { // 获取关联的套路商品 if sGoods, err := mtpcache.GetMiddleGoodsByUserID(r.AreaUserID); err == nil { sData := make([]ErmcpRealExposureModel, 0) // 从Redis获取数据 for i := range sGoods { key := fmt.Sprintf("ErmcpAreaExposure:%d_%d", sGoods[i].AREAUSERID, sGoods[i].MIDDLEGOODSID) if ret, err := rediscli.GetRedisClient().Get(key).Result(); err == nil { if len(ret) > 0 { var data pb.ErmcpAreaExposure if err := proto.Unmarshal([]byte(ret), &data); err == nil { var m ErmcpRealExposureModel m.ParseFromProto(&data) sData = append(sData, m) } } } } return sData, nil } return nil, nil }