package entity

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

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

// SchemaOrganization Organization对象
type SchemaOrganization schema.Organization

// ToOrganization 转换为Organization实体
func (a SchemaOrganization) ToOrganization() *Organization {
	item := &Organization{
		RecordID:  a.RecordID,
		Name:      a.Name,
		ChartNum:  a.ChartNum,
		Sequence:  &a.Sequence,
		CreatorId: a.CreatorId,
	}
	return item
}

// ToSchemaOrganization 转换为Organization对象
func (a Organization) ToSchemaOrganization() *schema.Organization {
	item := &schema.Organization{
		RecordID:  a.RecordID,
		Name:      a.Name,
		ChartNum:  a.ChartNum,
		Sequence:  *a.Sequence,
		CreatedAt: a.CreatedAt,
		CreatorId: a.CreatorId,
	}
	return item
}

// Organization Organization实体
type Organization struct {
	gorm.Model
	RecordID  string `gorm:"column:record_id;size:32;index;"`
	Name      string `gorm:"column:name;size:30;"`            // 组织名称
	ChartNum  int    `gorm:"column:chart_num;"`               // 对话轮次/日
	Sequence  *int   `gorm:"column:sequence;auto_increment;"` // 排序
	CreatorId string `gorm:"column:creator_id;size:32;"`      // 创建者id
}

// Organizations Organization列表
type Organizations []*Organization

// ToSchemaOrganizations 转换为Organization对象列表
func (a Organizations) ToSchemaOrganizations() []*schema.Organization {
	list := make([]*schema.Organization, len(a))
	for i, item := range a {
		list[i] = item.ToSchemaOrganization()
	}
	return list
}
