package schema

import "time"

// Organization Organization对象
type Organization struct {
	RecordID    string    `json:"record_id"`                    //记录id
	Name        string    `json:"name" binding:"required"`      // 组织名称
	ChartNum    int       `json:"chart_num" binding:"required"` // 对话轮次/日
	Used        int       `json:"used"`                         // 已使用轮次
	Unused      int       `json:"unused"`                       // 剩余轮次
	Sequence    int       `json:"sequence"`                     // 排序
	CreatedAt   time.Time `json:"created_at"`                   // 创建时间
	CreatorId   string    `json:"creator_id"`                   // 创建者Id
	CreatorName string    `json:"creator_name"`                 // 创建者
	Datasets    Datasets  `json:"datasets"`                     // 创建组织时勾选共享给该组织的知识库（元素填 record_id）
}

// OrganizationQueryParam 查询条件
type OrganizationQueryParam struct {
	RecordIds []string
	LikeName  string
}

// OrganizationQueryOptions Organization对象查询可选参数项
type OrganizationQueryOptions struct {
	PageParam *PaginationParam // 分页参数
}

type Organizations []*Organization

// OrganizationQueryResult Organization对象查询结果
type OrganizationQueryResult struct {
	Data       Organizations
	PageResult *PaginationResult
}

func (a Organizations) ToUserIds() []string {
	uniqueMap := make(map[string]struct{})
	for _, v := range a {
		uniqueMap[v.CreatorId] = struct{}{}
	}

	// 将去重后的map转换为切片返回
	userIds := make([]string, 0, len(uniqueMap))
	for id := range uniqueMap {
		userIds = append(userIds, id)
	}
	return userIds
}

// FillCreator 填充创建者信息
func (a Organizations) FillCreator(users Users) {
	for _, o := range a {
		for _, u := range users {
			if o.CreatorId == u.RecordID {
				o.CreatorName = u.RealName
				continue
			}
		}
	}
}

// FillDataset 填充知识库
func (a Organizations) FillDataset(datasets Datasets) {
	for _, o := range a {
		for _, d := range datasets {
			if o.RecordID == d.OrgId {
				o.Datasets = append(o.Datasets, d)
				continue
			}
		}
	}
}
