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.
157 lines
4.2 KiB
157 lines
4.2 KiB
package market |
|
|
|
import ( |
|
"fmt" |
|
"sig-pub/api/pb" |
|
"sig-pub/pkg/data" |
|
"sig-pub/pkg/data/args" |
|
"sig-pub/pkg/data/entity" |
|
"sig-pub/pkg/storage/rdb" |
|
"time" |
|
) |
|
|
|
// TradeInstanceService 交易产品管理 |
|
// TODO cache |
|
type TradeInstanceService struct { |
|
db *rdb.RDB |
|
} |
|
|
|
func NewTradeInstanceService(db *rdb.RDB) *TradeInstanceService { |
|
return &TradeInstanceService{ |
|
db: db, |
|
} |
|
} |
|
|
|
func (s *TradeInstanceService) Init() (err error) { |
|
return |
|
} |
|
|
|
func (s *TradeInstanceService) GetInstance(instId string) (inst *entity.TradeInstance, err error) { |
|
inst = new(entity.TradeInstance) |
|
err = s.db.Select(inst, `select * from t_trade_instance where inst_id = ? and status != ?`, instId, data.StatusDeleted) |
|
if err != nil { |
|
return |
|
} |
|
if inst.InstId == "" { |
|
err = data.ErrorNotExists |
|
return |
|
} |
|
err = s.AttachInstExchanges([]*entity.TradeInstance{inst}) |
|
return |
|
} |
|
|
|
func (s *TradeInstanceService) ListInstanceById(instIds ...string) (insts []*entity.TradeInstance, err error) { |
|
err = s.db.Select(&insts, `select * from t_trade_instance where inst_id in ? and status != ?`, instIds, data.StatusDeleted) |
|
if err != nil { |
|
return |
|
} |
|
return |
|
} |
|
|
|
func (s *TradeInstanceService) 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 *TradeInstanceService) PageInstance(page, size int, instCoin string) (total int64, insts []*entity.TradeInstance, err error) { |
|
var filter string |
|
var filterArgs []any |
|
if instCoin != "" { |
|
filter = " and inst_coin = ?" |
|
filterArgs = append(filterArgs, instCoin) |
|
} |
|
|
|
var args = []any{data.StatusDeleted} |
|
args = append(args, filterArgs...) |
|
err = s.db.Select(&total, fmt.Sprintf(`select count(*) from t_trade_instance where status != ? %s`, filter), args...) |
|
if err != nil { |
|
return |
|
} |
|
if total == 0 { |
|
return |
|
} |
|
|
|
offset, limit := data.PageCalc(page, size) |
|
args = append(args, offset, limit) |
|
err = s.db.Select(&insts, fmt.Sprintf(`select * from t_trade_instance where status != ? %s limit ?, ?`, filter), args...) |
|
if err != nil { |
|
return |
|
} |
|
err = s.AttachInstExchanges(insts) |
|
if err != nil { |
|
return |
|
} |
|
return |
|
} |
|
|
|
func (s *TradeInstanceService) 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 |
|
} |
|
|
|
func (s *TradeInstanceService) InsertInstance(updateBy string, inst *entity.TradeInstance) (err error) { |
|
if err = validateTradeInstance(inst); err != nil { |
|
return |
|
} |
|
inst.UpdateBy = updateBy |
|
inst.UpdateTime = time.Now().UnixMilli() |
|
err = s.db.Insert(inst) |
|
if err != nil { |
|
return |
|
} |
|
for _, ex := range inst.Exchanges { |
|
ex.UpdateBy = updateBy |
|
ex.UpdateTime = time.Now().UnixMilli() |
|
err = s.db.Insert(ex) |
|
if err != nil { |
|
return |
|
} |
|
} |
|
return |
|
} |
|
|
|
func (s *TradeInstanceService) UpdateInstance(inst *entity.TradeInstance) (err error) { |
|
_, err = s.db.UpdateBy(inst) |
|
return |
|
} |
|
|
|
// 更新交易产品状态 |
|
func (s *TradeInstanceService) UpdateInstanceStatus(inst *args.UpdateTradeInstanceStatusArg) (err error) { |
|
s.db.Update("update t_trade_instance set status = ") |
|
_, err = s.db.UpdateBy(inst) |
|
return |
|
} |
|
|
|
// ListExchangeTradeInstance 获取指定交易所的正常状态的交易产品 |
|
func (s *TradeInstanceService) ListExchangeTradeInstance(exchange pb.ExchangeType) (exchangesInsts []*entity.TradeInstanceExchange, err error) { |
|
err = s.db.Select(&exchangesInsts, ` |
|
select * from t_trade_instance_exchange where exchange = ? and status in ? order by inst_id |
|
`, exchange, []data.Status{data.StatusOk, data.StatusProcessing}) |
|
if err != nil { |
|
return |
|
} |
|
return |
|
}
|
|
|