// Code generated by eevee. DO NOT EDIT!

package repository

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

	"golang.org/x/xerrors"
)

type Field interface {
	ToModel(*entity.Field) *model.Field
	ToModels(entity.Fields) *model.Fields
	FindAll(context.Context) (*model.Fields, error)
	FindByDifficulties(context.Context, []int) (*model.Fields, error)
	FindByDifficulty(context.Context, int) (*model.Fields, error)
	FindByDifficultyAndLevel(context.Context, int, int) (*model.Fields, error)
	FindByID(context.Context, uint64) (*model.Field, error)
	FindByIDs(context.Context, []uint64) (*model.Fields, error)
	FindByLocationX(context.Context, int) (*model.Fields, error)
	FindByLocationXAndLocationY(context.Context, int, int) (*model.Field, error)
	FindByLocationXes(context.Context, []int) (*model.Fields, error)
	FindByName(context.Context, string) (*model.Field, error)
	FindByNames(context.Context, []string) (*model.Fields, error)
	FindByObjectNum(context.Context, int) (*model.Fields, error)
	FindByObjectNums(context.Context, []int) (*model.Fields, error)
	Count(context.Context) (int64, error)
}

type FieldImpl struct {
	fieldDAO dao.Field
	repo     Repository
}

func NewField(ctx context.Context, tx *sql.Tx) *FieldImpl {
	return &FieldImpl{fieldDAO: dao.NewField(ctx, tx)}
}

func (r *FieldImpl) ToModel(value *entity.Field) *model.Field {
	return r.createCollection(entity.Fields{value}).First()
}

func (r *FieldImpl) ToModels(values entity.Fields) *model.Fields {
	return r.createCollection(values)
}

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

func (r *FieldImpl) FindByDifficulties(a0 context.Context, a1 []int) (*model.Fields, error) {
	values, err := r.fieldDAO.FindByDifficulties(a0, a1)
	if err != nil {
		return nil, xerrors.Errorf("failed to FindByDifficulties: %w", err)
	}
	collection := r.createCollection(values)
	return collection, nil
}

func (r *FieldImpl) FindByDifficulty(a0 context.Context, a1 int) (*model.Fields, error) {
	values, err := r.fieldDAO.FindByDifficulty(a0, a1)
	if err != nil {
		return nil, xerrors.Errorf("failed to FindByDifficulty: %w", err)
	}
	collection := r.createCollection(values)
	return collection, nil
}

func (r *FieldImpl) FindByDifficultyAndLevel(a0 context.Context, a1 int, a2 int) (*model.Fields, error) {
	values, err := r.fieldDAO.FindByDifficultyAndLevel(a0, a1, a2)
	if err != nil {
		return nil, xerrors.Errorf("failed to FindByDifficultyAndLevel: %w", err)
	}
	collection := r.createCollection(values)
	return collection, nil
}

func (r *FieldImpl) FindByID(a0 context.Context, a1 uint64) (*model.Field, error) {
	value, err := r.fieldDAO.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.Fields{value}).First()
	return v, nil
}

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

func (r *FieldImpl) FindByLocationX(a0 context.Context, a1 int) (*model.Fields, error) {
	values, err := r.fieldDAO.FindByLocationX(a0, a1)
	if err != nil {
		return nil, xerrors.Errorf("failed to FindByLocationX: %w", err)
	}
	collection := r.createCollection(values)
	return collection, nil
}

func (r *FieldImpl) FindByLocationXAndLocationY(a0 context.Context, a1 int, a2 int) (*model.Field, error) {
	value, err := r.fieldDAO.FindByLocationXAndLocationY(a0, a1, a2)
	if err != nil {
		return nil, xerrors.Errorf("failed to FindByLocationXAndLocationY: %w", err)
	}
	if value == nil {
		return nil, nil
	}
	v := r.createCollection(entity.Fields{value}).First()
	return v, nil
}

func (r *FieldImpl) FindByLocationXes(a0 context.Context, a1 []int) (*model.Fields, error) {
	values, err := r.fieldDAO.FindByLocationXes(a0, a1)
	if err != nil {
		return nil, xerrors.Errorf("failed to FindByLocationXes: %w", err)
	}
	collection := r.createCollection(values)
	return collection, nil
}

func (r *FieldImpl) FindByName(a0 context.Context, a1 string) (*model.Field, error) {
	value, err := r.fieldDAO.FindByName(a0, a1)
	if err != nil {
		return nil, xerrors.Errorf("failed to FindByName: %w", err)
	}
	if value == nil {
		return nil, nil
	}
	v := r.createCollection(entity.Fields{value}).First()
	return v, nil
}

func (r *FieldImpl) FindByNames(a0 context.Context, a1 []string) (*model.Fields, error) {
	values, err := r.fieldDAO.FindByNames(a0, a1)
	if err != nil {
		return nil, xerrors.Errorf("failed to FindByNames: %w", err)
	}
	collection := r.createCollection(values)
	return collection, nil
}

func (r *FieldImpl) FindByObjectNum(a0 context.Context, a1 int) (*model.Fields, error) {
	values, err := r.fieldDAO.FindByObjectNum(a0, a1)
	if err != nil {
		return nil, xerrors.Errorf("failed to FindByObjectNum: %w", err)
	}
	collection := r.createCollection(values)
	return collection, nil
}

func (r *FieldImpl) FindByObjectNums(a0 context.Context, a1 []int) (*model.Fields, error) {
	values, err := r.fieldDAO.FindByObjectNums(a0, a1)
	if err != nil {
		return nil, xerrors.Errorf("failed to FindByObjectNums: %w", err)
	}
	collection := r.createCollection(values)
	return collection, nil
}

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

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

func (r *FieldImpl) create(entity *entity.Field, values *model.Fields) *model.Field {
	value := model.NewField(entity, r.fieldDAO)
	value.SetConverter(r.repo.(model.ModelConverter))
	return value
}
