You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.4 KiB
55 lines
1.4 KiB
package repository |
|
|
|
import ( |
|
"sig-pub/pkg/data" |
|
"sig-pub/pkg/data/entity" |
|
"sig-pub/pkg/storage/persist" |
|
) |
|
|
|
type MarketRepository struct { |
|
db *persist.DB |
|
} |
|
|
|
func NewMarketRepository(db *persist.DB) *MarketRepository { |
|
return &MarketRepository{ |
|
db: db, |
|
} |
|
} |
|
|
|
func (s *MarketRepository) ListAllInstanceId() (insts []string, err error) { |
|
err = s.db.Select(&insts, `select inst_id from t_trade_instance where status != ?`, data.StatusDeleted) |
|
return |
|
} |
|
|
|
func (s *MarketRepository) ListAllInstance() (insts []*entity.TradeInstance, err error) { |
|
err = s.db.Select(&insts, `select * from t_trade_instance where status != ?`, data.StatusDeleted) |
|
if err != nil { |
|
return |
|
} |
|
err = s.AttachInstExchanges(insts) |
|
return |
|
} |
|
|
|
func (s *MarketRepository) AttachInstExchanges(insts []*entity.TradeInstance) (err error) { |
|
// 交易产品交易所 |
|
var instIds []string |
|
var instMap = make(map[string]*entity.TradeInstance, len(insts)) |
|
for _, inst := range insts { |
|
instIds = append(instIds, inst.InstId) |
|
instMap[inst.InstId] = inst |
|
} |
|
|
|
var instExchanges []*entity.TradeInstanceExchange |
|
err = s.db.Select(&instExchanges, ` |
|
select * from t_trade_instance_exchange where inst_id in ? and status != ? order by inst_id, exchange |
|
`, instIds, data.StatusDeleted) |
|
if err != nil { |
|
return |
|
} |
|
for _, instEx := range instExchanges { |
|
if inst, ok := instMap[instEx.InstId]; ok { |
|
inst.Exchanges = append(inst.Exchanges, instEx) |
|
} |
|
} |
|
return |
|
}
|
|
|