// Code generated by eevee. DO NOT EDIT!

package repository

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

	"golang.org/x/xerrors"
)

type Group interface {
	ToModel(*entity.Group) *model.Group
	ToModels(entity.Groups) *model.Groups
	Create(context.Context, *entity.Group) (*model.Group, error)
	Creates(context.Context, entity.Groups) (*model.Groups, error)
	FindAll(context.Context) (*model.Groups, error)
	FindByID(context.Context, uint64) (*model.Group, error)
	FindByIDs(context.Context, []uint64) (*model.Groups, error)
	UpdateByID(context.Context, uint64, map[string]interface{}) error
	UpdateByIDs(context.Context, []uint64, map[string]interface{}) error
	DeleteByID(context.Context, uint64) error
	DeleteByIDs(context.Context, []uint64) error
	Count(context.Context) (int64, error)
	Delete(context.Context, *entity.Group) error
	Update(context.Context, *entity.Group) 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) Create(ctx context.Context, value *entity.Group) (*model.Group, error) {
	if err := r.groupDAO.Create(ctx, value); err != nil {
		return nil, xerrors.Errorf("cannot Create: %w", err)
	}
	v := r.ToModel(value)
	v.SetSavedValue(value)
	v.SetAlreadyCreated(true)
	return v, nil
}

func (r *GroupImpl) Creates(ctx context.Context, entities entity.Groups) (*model.Groups, error) {
	for _, v := range entities {
		if _, err := r.Create(ctx, v); err != nil {
			return nil, xerrors.Errorf("cannot Create: %w", err)
		}
	}
	values := r.ToModels(entities)
	values.Each(func(v *model.Group) {
		v.SetSavedValue(v.Group)
		v.SetAlreadyCreated(true)
	})
	return values, nil
}

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)
	collection.Each(func(v *model.Group) {
		v.SetSavedValue(v.Group)
		v.SetAlreadyCreated(true)
	})
	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()
	v.SetSavedValue(v.Group)
	v.SetAlreadyCreated(true)
	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)
	collection.Each(func(v *model.Group) {
		v.SetSavedValue(v.Group)
		v.SetAlreadyCreated(true)
	})
	return collection, nil
}

func (r *GroupImpl) UpdateByID(a0 context.Context, a1 uint64, a2 map[string]interface{}) error {
	if err := r.groupDAO.UpdateByID(a0, a1, a2); err != nil {
		return xerrors.Errorf("failed to update: %w", err)
	}
	return nil
}

func (r *GroupImpl) UpdateByIDs(a0 context.Context, a1 []uint64, a2 map[string]interface{}) error {
	if err := r.groupDAO.UpdateByIDs(a0, a1, a2); err != nil {
		return xerrors.Errorf("failed to update: %w", err)
	}
	return nil
}

func (r *GroupImpl) DeleteByID(a0 context.Context, a1 uint64) error {
	if err := r.groupDAO.DeleteByID(a0, a1); err != nil {
		return xerrors.Errorf("failed to delete: %w", err)
	}
	return nil
}

func (r *GroupImpl) DeleteByIDs(a0 context.Context, a1 []uint64) error {
	if err := r.groupDAO.DeleteByIDs(a0, a1); err != nil {
		return xerrors.Errorf("failed to delete: %w", err)
	}
	return 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) Delete(a0 context.Context, a1 *entity.Group) (r0 error) {
	r0 = r.groupDAO.Delete(a0, a1)
	if r0 != nil {
		r0 = xerrors.Errorf("failed to Delete: %w", r0)
	}
	return
}

func (r *GroupImpl) Update(a0 context.Context, a1 *entity.Group) (r0 error) {
	r0 = r.groupDAO.Update(a0, a1)
	if r0 != nil {
		r0 = xerrors.Errorf("failed to Update: %w", r0)
	}
	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
}
