// Code generated by eevee. DO NOT EDIT!

package repository

import (
	"context"
	"daoplugin/dao"
	"daoplugin/entity"
	"daoplugin/model"
	"database/sql"

	"golang.org/x/xerrors"
)

type Group interface {
	ToModel(*entity.Group) *model.Group
	ToModels(entity.Groups) *model.Groups
	FindAll(context.Context) (*model.Groups, error)
	FindByID(context.Context, uint64) (*model.Group, error)
	FindByIDs(context.Context, []uint64) (*model.Groups, error)
	Count(context.Context) (int64, error)
}

type GroupImpl struct {
	groupDAO dao.Group
	repo     Repository
}

func NewGroup(ctx context.Context, tx *sql.Tx) *GroupImpl {
	return &GroupImpl{groupDAO: dao.NewGroup(ctx, tx)}
}

func (r *GroupImpl) ToModel(value *entity.Group) *model.Group {
	return r.createCollection(entity.Groups{value}).First()
}

func (r *GroupImpl) ToModels(values entity.Groups) *model.Groups {
	return r.createCollection(values)
}

func (r *GroupImpl) FindAll(a0 context.Context) (*model.Groups, error) {
	values, err := r.groupDAO.FindAll(a0)
	if err != nil {
		return nil, xerrors.Errorf("failed to FindAll: %w", err)
	}
	collection := r.createCollection(values)
	return collection, nil
}

func (r *GroupImpl) FindByID(a0 context.Context, a1 uint64) (*model.Group, error) {
	value, err := r.groupDAO.FindByID(a0, a1)
	if err != nil {
		return nil, xerrors.Errorf("failed to FindByID: %w", err)
	}
	if value == nil {
		return nil, nil
	}
	v := r.createCollection(entity.Groups{value}).First()
	return v, nil
}

func (r *GroupImpl) FindByIDs(a0 context.Context, a1 []uint64) (*model.Groups, error) {
	values, err := r.groupDAO.FindByIDs(a0, a1)
	if err != nil {
		return nil, xerrors.Errorf("failed to FindByIDs: %w", err)
	}
	collection := r.createCollection(values)
	return collection, nil
}

func (r *GroupImpl) Count(a0 context.Context) (r0 int64, r1 error) {
	r0, r1 = r.groupDAO.Count(a0)
	if r1 != nil {
		r1 = xerrors.Errorf("failed to Count: %w", r1)
	}
	return
}

func (r *GroupImpl) createCollection(entities entity.Groups) *model.Groups {
	values := model.NewGroups(entities)
	for i := 0; i < len(entities); i += 1 {
		values.Add(r.create(entities[i], values))
	}
	return values
}

func (r *GroupImpl) create(entity *entity.Group, values *model.Groups) *model.Group {
	value := model.NewGroup(entity, r.groupDAO)
	value.SetConverter(r.repo.(model.ModelConverter))
	return value
}
