package entity

import (
	"context"
	"gorm.io/gorm"
	"yx-dataset-server/app/schema"
)

// GetUserDatasetDB 获取UserDataset存储
func GetUserDatasetDB(ctx context.Context, defDB *gorm.DB) *gorm.DB {
	return getDBWithModel(ctx, defDB, UserDataset{})
}

// SchemaUserDataset UserDataset对象
type SchemaUserDataset schema.UserDataset

// ToUserDataset 转换为UserDataset实体
func (a SchemaUserDataset) ToUserDataset() *UserDataset {
	item := &UserDataset{
		RecordID:  a.RecordID,
		UserId:    a.UserId,
		DatasetId: a.DatasetId,
		Type:      a.Type,
	}
	return item
}

// ToSchemaUserDataset 转换为UserDataset对象
func (a UserDataset) ToSchemaUserDataset() *schema.UserDataset {
	item := &schema.UserDataset{
		RecordID:  a.RecordID,
		UserId:    a.UserId,
		DatasetId: a.DatasetId,
		Type:      a.Type,
	}
	return item
}

// UserDataset UserDataset实体
type UserDataset struct {
	gorm.Model
	RecordID  string `gorm:"column:record_id;size:32;index;"`
	UserId    string `gorm:"column:user_id;size:32;index;"`   // 用户id
	DatasetId string `gorm:"column:dataset_id;size:32;index"` // 数据集id
	Type      int    `gorm:"column:type;"`                    //可用类型  1 只读 2 可编辑
}

// UserDatasets UserDataset列表
type UserDatasets []*UserDataset

// ToSchemaUserDatasets 转换为UserDataset对象列表
func (a UserDatasets) ToSchemaUserDatasets() []*schema.UserDataset {
	list := make([]*schema.UserDataset, len(a))
	for i, item := range a {
		list[i] = item.ToSchemaUserDataset()
	}
	return list
}
