package db

import (
	cortypes "gitlink.org.cn/cloudream/jcs-pub/coordinator/types"
)

type StorageDB struct {
	*DB
}

func (db *DB) Storage() *StorageDB {
	return &StorageDB{DB: db}
}

func (db *StorageDB) GetByID(ctx SQLContext, stgID cortypes.StorageID) (cortypes.Storage, error) {
	var stg cortypes.Storage
	err := ctx.Table("Storage").First(&stg, stgID).Error
	return stg, err
}

func (StorageDB) GetAllIDs(ctx SQLContext) ([]cortypes.StorageID, error) {
	var stgs []cortypes.StorageID
	err := ctx.Table("Storage").Select("StorageID").Find(&stgs).Error
	return stgs, err
}

func (db *StorageDB) BatchGetByID(ctx SQLContext, stgIDs []cortypes.StorageID) ([]cortypes.Storage, error) {
	var stgs []cortypes.Storage
	err := ctx.Table("Storage").Find(&stgs, "StorageID IN (?)", stgIDs).Error
	return stgs, err
}

func (db *StorageDB) BatchGetAllStorageIDs(ctx SQLContext, start int, count int) ([]cortypes.StorageID, error) {
	var ret []cortypes.StorageID
	err := ctx.Table("Storage").Select("StorageID").Find(&ret).Limit(count).Offset(start).Error
	return ret, err
}

func (db *StorageDB) GetHubStorages(ctx SQLContext, hubID cortypes.HubID) ([]cortypes.Storage, error) {
	var stgs []cortypes.Storage
	err := ctx.Table("Storage").Select("Storage.*").Find(&stgs, "MasterHub = ?", hubID).Error
	return stgs, err
}
