// Code generated by BobGen psql v0.42.5. DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.

package models

import (
	"context"
	"fmt"
	"io"
	"time"

	"github.com/Gleipnir-Technology/bob"
	"github.com/Gleipnir-Technology/bob/dialect/psql"
	"github.com/Gleipnir-Technology/bob/dialect/psql/dialect"
	"github.com/Gleipnir-Technology/bob/dialect/psql/dm"
	"github.com/Gleipnir-Technology/bob/dialect/psql/sm"
	"github.com/Gleipnir-Technology/bob/dialect/psql/um"
	"github.com/Gleipnir-Technology/bob/expr"
	"github.com/Gleipnir-Technology/bob/orm"
	"github.com/Gleipnir-Technology/bob/types/pgtypes"
	"github.com/aarondl/opt/null"
	"github.com/aarondl/opt/omit"
	"github.com/aarondl/opt/omitnull"
	"github.com/google/uuid"
	"github.com/stephenafamo/scan"
)

// PublicreportImage is an object representing the database table.
type PublicreportImage struct {
	ID               int32            `db:"id,pk" `
	ContentType      string           `db:"content_type" `
	Created          time.Time        `db:"created" `
	ResolutionX      int32            `db:"resolution_x" `
	ResolutionY      int32            `db:"resolution_y" `
	StorageUUID      uuid.UUID        `db:"storage_uuid" `
	StorageSize      int64            `db:"storage_size" `
	UploadedFilename string           `db:"uploaded_filename" `
	Location         null.Val[string] `db:"location" `

	R publicreportImageR `db:"-" `
}

// PublicreportImageSlice is an alias for a slice of pointers to PublicreportImage.
// This should almost always be used instead of []*PublicreportImage.
type PublicreportImageSlice []*PublicreportImage

// PublicreportImages contains methods to work with the image table
var PublicreportImages = psql.NewTablex[*PublicreportImage, PublicreportImageSlice, *PublicreportImageSetter]("publicreport", "image", buildPublicreportImageColumns("publicreport.image"))

// PublicreportImagesQuery is a query on the image table
type PublicreportImagesQuery = *psql.ViewQuery[*PublicreportImage, PublicreportImageSlice]

// publicreportImageR is where relationships are stored.
type publicreportImageR struct {
	ImageExifs   PublicreportImageExifSlice   // publicreport.image_exif.image_exif_image_id_fkey
	NuisanceOlds PublicreportNuisanceOldSlice // publicreport.nuisance_image_old.nuisance_image_image_id_fkeypublicreport.nuisance_image_old.nuisance_image_nuisance_id_fkey
	Reports      PublicreportReportSlice      // publicreport.report_image.report_image_image_id_fkeypublicreport.report_image.report_image_report_id_fkey
	WaterOlds    PublicreportWaterOldSlice    // publicreport.water_image_old.pool_image_image_id_fkeypublicreport.water_image_old.pool_image_pool_id_fkey
}

func buildPublicreportImageColumns(alias string) publicreportImageColumns {
	return publicreportImageColumns{
		ColumnsExpr: expr.NewColumnsExpr(
			"id", "content_type", "created", "resolution_x", "resolution_y", "storage_uuid", "storage_size", "uploaded_filename", "location",
		).WithParent("publicreport.image"),
		tableAlias:       alias,
		ID:               psql.Quote(alias, "id"),
		ContentType:      psql.Quote(alias, "content_type"),
		Created:          psql.Quote(alias, "created"),
		ResolutionX:      psql.Quote(alias, "resolution_x"),
		ResolutionY:      psql.Quote(alias, "resolution_y"),
		StorageUUID:      psql.Quote(alias, "storage_uuid"),
		StorageSize:      psql.Quote(alias, "storage_size"),
		UploadedFilename: psql.Quote(alias, "uploaded_filename"),
		Location:         psql.Quote(alias, "location"),
	}
}

type publicreportImageColumns struct {
	expr.ColumnsExpr
	tableAlias       string
	ID               psql.Expression
	ContentType      psql.Expression
	Created          psql.Expression
	ResolutionX      psql.Expression
	ResolutionY      psql.Expression
	StorageUUID      psql.Expression
	StorageSize      psql.Expression
	UploadedFilename psql.Expression
	Location         psql.Expression
}

func (c publicreportImageColumns) Alias() string {
	return c.tableAlias
}

func (publicreportImageColumns) AliasedAs(alias string) publicreportImageColumns {
	return buildPublicreportImageColumns(alias)
}

// PublicreportImageSetter is used for insert/upsert/update operations
// All values are optional, and do not have to be set
// Generated columns are not included
type PublicreportImageSetter struct {
	ID               omit.Val[int32]      `db:"id,pk" `
	ContentType      omit.Val[string]     `db:"content_type" `
	Created          omit.Val[time.Time]  `db:"created" `
	ResolutionX      omit.Val[int32]      `db:"resolution_x" `
	ResolutionY      omit.Val[int32]      `db:"resolution_y" `
	StorageUUID      omit.Val[uuid.UUID]  `db:"storage_uuid" `
	StorageSize      omit.Val[int64]      `db:"storage_size" `
	UploadedFilename omit.Val[string]     `db:"uploaded_filename" `
	Location         omitnull.Val[string] `db:"location" `
}

func (s PublicreportImageSetter) SetColumns() []string {
	vals := make([]string, 0, 9)
	if s.ID.IsValue() {
		vals = append(vals, "id")
	}
	if s.ContentType.IsValue() {
		vals = append(vals, "content_type")
	}
	if s.Created.IsValue() {
		vals = append(vals, "created")
	}
	if s.ResolutionX.IsValue() {
		vals = append(vals, "resolution_x")
	}
	if s.ResolutionY.IsValue() {
		vals = append(vals, "resolution_y")
	}
	if s.StorageUUID.IsValue() {
		vals = append(vals, "storage_uuid")
	}
	if s.StorageSize.IsValue() {
		vals = append(vals, "storage_size")
	}
	if s.UploadedFilename.IsValue() {
		vals = append(vals, "uploaded_filename")
	}
	if !s.Location.IsUnset() {
		vals = append(vals, "location")
	}
	return vals
}

func (s PublicreportImageSetter) Overwrite(t *PublicreportImage) {
	if s.ID.IsValue() {
		t.ID = s.ID.MustGet()
	}
	if s.ContentType.IsValue() {
		t.ContentType = s.ContentType.MustGet()
	}
	if s.Created.IsValue() {
		t.Created = s.Created.MustGet()
	}
	if s.ResolutionX.IsValue() {
		t.ResolutionX = s.ResolutionX.MustGet()
	}
	if s.ResolutionY.IsValue() {
		t.ResolutionY = s.ResolutionY.MustGet()
	}
	if s.StorageUUID.IsValue() {
		t.StorageUUID = s.StorageUUID.MustGet()
	}
	if s.StorageSize.IsValue() {
		t.StorageSize = s.StorageSize.MustGet()
	}
	if s.UploadedFilename.IsValue() {
		t.UploadedFilename = s.UploadedFilename.MustGet()
	}
	if !s.Location.IsUnset() {
		t.Location = s.Location.MustGetNull()
	}
}

func (s *PublicreportImageSetter) Apply(q *dialect.InsertQuery) {
	q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
		return PublicreportImages.BeforeInsertHooks.RunHooks(ctx, exec, s)
	})

	q.AppendValues(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
		vals := make([]bob.Expression, 9)
		if s.ID.IsValue() {
			vals[0] = psql.Arg(s.ID.MustGet())
		} else {
			vals[0] = psql.Raw("DEFAULT")
		}

		if s.ContentType.IsValue() {
			vals[1] = psql.Arg(s.ContentType.MustGet())
		} else {
			vals[1] = psql.Raw("DEFAULT")
		}

		if s.Created.IsValue() {
			vals[2] = psql.Arg(s.Created.MustGet())
		} else {
			vals[2] = psql.Raw("DEFAULT")
		}

		if s.ResolutionX.IsValue() {
			vals[3] = psql.Arg(s.ResolutionX.MustGet())
		} else {
			vals[3] = psql.Raw("DEFAULT")
		}

		if s.ResolutionY.IsValue() {
			vals[4] = psql.Arg(s.ResolutionY.MustGet())
		} else {
			vals[4] = psql.Raw("DEFAULT")
		}

		if s.StorageUUID.IsValue() {
			vals[5] = psql.Arg(s.StorageUUID.MustGet())
		} else {
			vals[5] = psql.Raw("DEFAULT")
		}

		if s.StorageSize.IsValue() {
			vals[6] = psql.Arg(s.StorageSize.MustGet())
		} else {
			vals[6] = psql.Raw("DEFAULT")
		}

		if s.UploadedFilename.IsValue() {
			vals[7] = psql.Arg(s.UploadedFilename.MustGet())
		} else {
			vals[7] = psql.Raw("DEFAULT")
		}

		if !s.Location.IsUnset() {
			vals[8] = psql.Arg(s.Location.MustGetNull())
		} else {
			vals[8] = psql.Raw("DEFAULT")
		}

		return bob.ExpressSlice(ctx, w, d, start, vals, "", ", ", "")
	}))
}

func (s PublicreportImageSetter) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
	return um.Set(s.Expressions()...)
}

func (s PublicreportImageSetter) Expressions(prefix ...string) []bob.Expression {
	exprs := make([]bob.Expression, 0, 9)

	if s.ID.IsValue() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			psql.Quote(append(prefix, "id")...),
			psql.Arg(s.ID),
		}})
	}

	if s.ContentType.IsValue() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			psql.Quote(append(prefix, "content_type")...),
			psql.Arg(s.ContentType),
		}})
	}

	if s.Created.IsValue() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			psql.Quote(append(prefix, "created")...),
			psql.Arg(s.Created),
		}})
	}

	if s.ResolutionX.IsValue() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			psql.Quote(append(prefix, "resolution_x")...),
			psql.Arg(s.ResolutionX),
		}})
	}

	if s.ResolutionY.IsValue() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			psql.Quote(append(prefix, "resolution_y")...),
			psql.Arg(s.ResolutionY),
		}})
	}

	if s.StorageUUID.IsValue() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			psql.Quote(append(prefix, "storage_uuid")...),
			psql.Arg(s.StorageUUID),
		}})
	}

	if s.StorageSize.IsValue() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			psql.Quote(append(prefix, "storage_size")...),
			psql.Arg(s.StorageSize),
		}})
	}

	if s.UploadedFilename.IsValue() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			psql.Quote(append(prefix, "uploaded_filename")...),
			psql.Arg(s.UploadedFilename),
		}})
	}

	if !s.Location.IsUnset() {
		exprs = append(exprs, expr.Join{Sep: " = ", Exprs: []bob.Expression{
			psql.Quote(append(prefix, "location")...),
			psql.Arg(s.Location),
		}})
	}

	return exprs
}

// FindPublicreportImage retrieves a single record by primary key
// If cols is empty Find will return all columns.
func FindPublicreportImage(ctx context.Context, exec bob.Executor, IDPK int32, cols ...string) (*PublicreportImage, error) {
	if len(cols) == 0 {
		return PublicreportImages.Query(
			sm.Where(PublicreportImages.Columns.ID.EQ(psql.Arg(IDPK))),
		).One(ctx, exec)
	}

	return PublicreportImages.Query(
		sm.Where(PublicreportImages.Columns.ID.EQ(psql.Arg(IDPK))),
		sm.Columns(PublicreportImages.Columns.Only(cols...)),
	).One(ctx, exec)
}

// PublicreportImageExists checks the presence of a single record by primary key
func PublicreportImageExists(ctx context.Context, exec bob.Executor, IDPK int32) (bool, error) {
	return PublicreportImages.Query(
		sm.Where(PublicreportImages.Columns.ID.EQ(psql.Arg(IDPK))),
	).Exists(ctx, exec)
}

// AfterQueryHook is called after PublicreportImage is retrieved from the database
func (o *PublicreportImage) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
	var err error

	switch queryType {
	case bob.QueryTypeSelect:
		ctx, err = PublicreportImages.AfterSelectHooks.RunHooks(ctx, exec, PublicreportImageSlice{o})
	case bob.QueryTypeInsert:
		ctx, err = PublicreportImages.AfterInsertHooks.RunHooks(ctx, exec, PublicreportImageSlice{o})
	case bob.QueryTypeUpdate:
		ctx, err = PublicreportImages.AfterUpdateHooks.RunHooks(ctx, exec, PublicreportImageSlice{o})
	case bob.QueryTypeDelete:
		ctx, err = PublicreportImages.AfterDeleteHooks.RunHooks(ctx, exec, PublicreportImageSlice{o})
	}

	return err
}

// primaryKeyVals returns the primary key values of the PublicreportImage
func (o *PublicreportImage) primaryKeyVals() bob.Expression {
	return psql.Arg(o.ID)
}

func (o *PublicreportImage) pkEQ() dialect.Expression {
	return psql.Quote("publicreport.image", "id").EQ(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
		return o.primaryKeyVals().WriteSQL(ctx, w, d, start)
	}))
}

// Update uses an executor to update the PublicreportImage
func (o *PublicreportImage) Update(ctx context.Context, exec bob.Executor, s *PublicreportImageSetter) error {
	v, err := PublicreportImages.Update(s.UpdateMod(), um.Where(o.pkEQ())).One(ctx, exec)
	if err != nil {
		return err
	}

	o.R = v.R
	*o = *v

	return nil
}

// Delete deletes a single PublicreportImage record with an executor
func (o *PublicreportImage) Delete(ctx context.Context, exec bob.Executor) error {
	_, err := PublicreportImages.Delete(dm.Where(o.pkEQ())).Exec(ctx, exec)
	return err
}

// Reload refreshes the PublicreportImage using the executor
func (o *PublicreportImage) Reload(ctx context.Context, exec bob.Executor) error {
	o2, err := PublicreportImages.Query(
		sm.Where(PublicreportImages.Columns.ID.EQ(psql.Arg(o.ID))),
	).One(ctx, exec)
	if err != nil {
		return err
	}
	o2.R = o.R
	*o = *o2

	return nil
}

// AfterQueryHook is called after PublicreportImageSlice is retrieved from the database
func (o PublicreportImageSlice) AfterQueryHook(ctx context.Context, exec bob.Executor, queryType bob.QueryType) error {
	var err error

	switch queryType {
	case bob.QueryTypeSelect:
		ctx, err = PublicreportImages.AfterSelectHooks.RunHooks(ctx, exec, o)
	case bob.QueryTypeInsert:
		ctx, err = PublicreportImages.AfterInsertHooks.RunHooks(ctx, exec, o)
	case bob.QueryTypeUpdate:
		ctx, err = PublicreportImages.AfterUpdateHooks.RunHooks(ctx, exec, o)
	case bob.QueryTypeDelete:
		ctx, err = PublicreportImages.AfterDeleteHooks.RunHooks(ctx, exec, o)
	}

	return err
}

func (o PublicreportImageSlice) pkIN() dialect.Expression {
	if len(o) == 0 {
		return psql.Raw("NULL")
	}

	return psql.Quote("publicreport.image", "id").In(bob.ExpressionFunc(func(ctx context.Context, w io.StringWriter, d bob.Dialect, start int) ([]any, error) {
		pkPairs := make([]bob.Expression, len(o))
		for i, row := range o {
			pkPairs[i] = row.primaryKeyVals()
		}
		return bob.ExpressSlice(ctx, w, d, start, pkPairs, "", ", ", "")
	}))
}

// copyMatchingRows finds models in the given slice that have the same primary key
// then it first copies the existing relationships from the old model to the new model
// and then replaces the old model in the slice with the new model
func (o PublicreportImageSlice) copyMatchingRows(from ...*PublicreportImage) {
	for i, old := range o {
		for _, new := range from {
			if new.ID != old.ID {
				continue
			}
			new.R = old.R
			o[i] = new
			break
		}
	}
}

// UpdateMod modifies an update query with "WHERE primary_key IN (o...)"
func (o PublicreportImageSlice) UpdateMod() bob.Mod[*dialect.UpdateQuery] {
	return bob.ModFunc[*dialect.UpdateQuery](func(q *dialect.UpdateQuery) {
		q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
			return PublicreportImages.BeforeUpdateHooks.RunHooks(ctx, exec, o)
		})

		q.AppendLoader(bob.LoaderFunc(func(ctx context.Context, exec bob.Executor, retrieved any) error {
			var err error
			switch retrieved := retrieved.(type) {
			case *PublicreportImage:
				o.copyMatchingRows(retrieved)
			case []*PublicreportImage:
				o.copyMatchingRows(retrieved...)
			case PublicreportImageSlice:
				o.copyMatchingRows(retrieved...)
			default:
				// If the retrieved value is not a PublicreportImage or a slice of PublicreportImage
				// then run the AfterUpdateHooks on the slice
				_, err = PublicreportImages.AfterUpdateHooks.RunHooks(ctx, exec, o)
			}

			return err
		}))

		q.AppendWhere(o.pkIN())
	})
}

// DeleteMod modifies an delete query with "WHERE primary_key IN (o...)"
func (o PublicreportImageSlice) DeleteMod() bob.Mod[*dialect.DeleteQuery] {
	return bob.ModFunc[*dialect.DeleteQuery](func(q *dialect.DeleteQuery) {
		q.AppendHooks(func(ctx context.Context, exec bob.Executor) (context.Context, error) {
			return PublicreportImages.BeforeDeleteHooks.RunHooks(ctx, exec, o)
		})

		q.AppendLoader(bob.LoaderFunc(func(ctx context.Context, exec bob.Executor, retrieved any) error {
			var err error
			switch retrieved := retrieved.(type) {
			case *PublicreportImage:
				o.copyMatchingRows(retrieved)
			case []*PublicreportImage:
				o.copyMatchingRows(retrieved...)
			case PublicreportImageSlice:
				o.copyMatchingRows(retrieved...)
			default:
				// If the retrieved value is not a PublicreportImage or a slice of PublicreportImage
				// then run the AfterDeleteHooks on the slice
				_, err = PublicreportImages.AfterDeleteHooks.RunHooks(ctx, exec, o)
			}

			return err
		}))

		q.AppendWhere(o.pkIN())
	})
}

func (o PublicreportImageSlice) UpdateAll(ctx context.Context, exec bob.Executor, vals PublicreportImageSetter) error {
	if len(o) == 0 {
		return nil
	}

	_, err := PublicreportImages.Update(vals.UpdateMod(), o.UpdateMod()).All(ctx, exec)
	return err
}

func (o PublicreportImageSlice) DeleteAll(ctx context.Context, exec bob.Executor) error {
	if len(o) == 0 {
		return nil
	}

	_, err := PublicreportImages.Delete(o.DeleteMod()).Exec(ctx, exec)
	return err
}

func (o PublicreportImageSlice) ReloadAll(ctx context.Context, exec bob.Executor) error {
	if len(o) == 0 {
		return nil
	}

	o2, err := PublicreportImages.Query(sm.Where(o.pkIN())).All(ctx, exec)
	if err != nil {
		return err
	}

	o.copyMatchingRows(o2...)

	return nil
}

// ImageExifs starts a query for related objects on publicreport.image_exif
func (o *PublicreportImage) ImageExifs(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportImageExifsQuery {
	return PublicreportImageExifs.Query(append(mods,
		sm.Where(PublicreportImageExifs.Columns.ImageID.EQ(psql.Arg(o.ID))),
	)...)
}

func (os PublicreportImageSlice) ImageExifs(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportImageExifsQuery {
	pkID := make(pgtypes.Array[int32], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkID = append(pkID, o.ID)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkID), "integer[]")),
	))

	return PublicreportImageExifs.Query(append(mods,
		sm.Where(psql.Group(PublicreportImageExifs.Columns.ImageID).OP("IN", PKArgExpr)),
	)...)
}

// NuisanceOlds starts a query for related objects on publicreport.nuisance_old
func (o *PublicreportImage) NuisanceOlds(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportNuisanceOldsQuery {
	return PublicreportNuisanceOlds.Query(append(mods,
		sm.InnerJoin(PublicreportNuisanceImageOlds.NameAs()).On(
			PublicreportNuisanceOlds.Columns.ID.EQ(PublicreportNuisanceImageOlds.Columns.NuisanceID)),
		sm.Where(PublicreportNuisanceImageOlds.Columns.ImageID.EQ(psql.Arg(o.ID))),
	)...)
}

func (os PublicreportImageSlice) NuisanceOlds(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportNuisanceOldsQuery {
	pkID := make(pgtypes.Array[int32], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkID = append(pkID, o.ID)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkID), "integer[]")),
	))

	return PublicreportNuisanceOlds.Query(append(mods,
		sm.InnerJoin(PublicreportNuisanceImageOlds.NameAs()).On(
			PublicreportNuisanceOlds.Columns.ID.EQ(PublicreportNuisanceImageOlds.Columns.NuisanceID),
		),
		sm.Where(psql.Group(PublicreportNuisanceImageOlds.Columns.ImageID).OP("IN", PKArgExpr)),
	)...)
}

// Reports starts a query for related objects on publicreport.report
func (o *PublicreportImage) Reports(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportReportsQuery {
	return PublicreportReports.Query(append(mods,
		sm.InnerJoin(PublicreportReportImages.NameAs()).On(
			PublicreportReports.Columns.ID.EQ(PublicreportReportImages.Columns.ReportID)),
		sm.Where(PublicreportReportImages.Columns.ImageID.EQ(psql.Arg(o.ID))),
	)...)
}

func (os PublicreportImageSlice) Reports(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportReportsQuery {
	pkID := make(pgtypes.Array[int32], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkID = append(pkID, o.ID)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkID), "integer[]")),
	))

	return PublicreportReports.Query(append(mods,
		sm.InnerJoin(PublicreportReportImages.NameAs()).On(
			PublicreportReports.Columns.ID.EQ(PublicreportReportImages.Columns.ReportID),
		),
		sm.Where(psql.Group(PublicreportReportImages.Columns.ImageID).OP("IN", PKArgExpr)),
	)...)
}

// WaterOlds starts a query for related objects on publicreport.water_old
func (o *PublicreportImage) WaterOlds(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportWaterOldsQuery {
	return PublicreportWaterOlds.Query(append(mods,
		sm.InnerJoin(PublicreportWaterImageOlds.NameAs()).On(
			PublicreportWaterOlds.Columns.ID.EQ(PublicreportWaterImageOlds.Columns.WaterID)),
		sm.Where(PublicreportWaterImageOlds.Columns.ImageID.EQ(psql.Arg(o.ID))),
	)...)
}

func (os PublicreportImageSlice) WaterOlds(mods ...bob.Mod[*dialect.SelectQuery]) PublicreportWaterOldsQuery {
	pkID := make(pgtypes.Array[int32], 0, len(os))
	for _, o := range os {
		if o == nil {
			continue
		}
		pkID = append(pkID, o.ID)
	}
	PKArgExpr := psql.Select(sm.Columns(
		psql.F("unnest", psql.Cast(psql.Arg(pkID), "integer[]")),
	))

	return PublicreportWaterOlds.Query(append(mods,
		sm.InnerJoin(PublicreportWaterImageOlds.NameAs()).On(
			PublicreportWaterOlds.Columns.ID.EQ(PublicreportWaterImageOlds.Columns.WaterID),
		),
		sm.Where(psql.Group(PublicreportWaterImageOlds.Columns.ImageID).OP("IN", PKArgExpr)),
	)...)
}

func insertPublicreportImageImageExifs0(ctx context.Context, exec bob.Executor, publicreportImageExifs1 []*PublicreportImageExifSetter, publicreportImage0 *PublicreportImage) (PublicreportImageExifSlice, error) {
	for i := range publicreportImageExifs1 {
		publicreportImageExifs1[i].ImageID = omit.From(publicreportImage0.ID)
	}

	ret, err := PublicreportImageExifs.Insert(bob.ToMods(publicreportImageExifs1...)).All(ctx, exec)
	if err != nil {
		return ret, fmt.Errorf("insertPublicreportImageImageExifs0: %w", err)
	}

	return ret, nil
}

func attachPublicreportImageImageExifs0(ctx context.Context, exec bob.Executor, count int, publicreportImageExifs1 PublicreportImageExifSlice, publicreportImage0 *PublicreportImage) (PublicreportImageExifSlice, error) {
	setter := &PublicreportImageExifSetter{
		ImageID: omit.From(publicreportImage0.ID),
	}

	err := publicreportImageExifs1.UpdateAll(ctx, exec, *setter)
	if err != nil {
		return nil, fmt.Errorf("attachPublicreportImageImageExifs0: %w", err)
	}

	return publicreportImageExifs1, nil
}

func (publicreportImage0 *PublicreportImage) InsertImageExifs(ctx context.Context, exec bob.Executor, related ...*PublicreportImageExifSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	publicreportImageExifs1, err := insertPublicreportImageImageExifs0(ctx, exec, related, publicreportImage0)
	if err != nil {
		return err
	}

	publicreportImage0.R.ImageExifs = append(publicreportImage0.R.ImageExifs, publicreportImageExifs1...)

	for _, rel := range publicreportImageExifs1 {
		rel.R.Image = publicreportImage0
	}
	return nil
}

func (publicreportImage0 *PublicreportImage) AttachImageExifs(ctx context.Context, exec bob.Executor, related ...*PublicreportImageExif) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	publicreportImageExifs1 := PublicreportImageExifSlice(related)

	_, err = attachPublicreportImageImageExifs0(ctx, exec, len(related), publicreportImageExifs1, publicreportImage0)
	if err != nil {
		return err
	}

	publicreportImage0.R.ImageExifs = append(publicreportImage0.R.ImageExifs, publicreportImageExifs1...)

	for _, rel := range related {
		rel.R.Image = publicreportImage0
	}

	return nil
}

func attachPublicreportImageNuisanceOlds0(ctx context.Context, exec bob.Executor, count int, publicreportImage0 *PublicreportImage, publicreportNuisanceOlds2 PublicreportNuisanceOldSlice) (PublicreportNuisanceImageOldSlice, error) {
	setters := make([]*PublicreportNuisanceImageOldSetter, count)
	for i := range count {
		setters[i] = &PublicreportNuisanceImageOldSetter{
			ImageID:    omit.From(publicreportImage0.ID),
			NuisanceID: omit.From(publicreportNuisanceOlds2[i].ID),
		}
	}

	publicreportNuisanceImageOlds1, err := PublicreportNuisanceImageOlds.Insert(bob.ToMods(setters...)).All(ctx, exec)
	if err != nil {
		return nil, fmt.Errorf("attachPublicreportImageNuisanceOlds0: %w", err)
	}

	return publicreportNuisanceImageOlds1, nil
}

func (publicreportImage0 *PublicreportImage) InsertNuisanceOlds(ctx context.Context, exec bob.Executor, related ...*PublicreportNuisanceOldSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	inserted, err := PublicreportNuisanceOlds.Insert(bob.ToMods(related...)).All(ctx, exec)
	if err != nil {
		return fmt.Errorf("inserting related objects: %w", err)
	}
	publicreportNuisanceOlds2 := PublicreportNuisanceOldSlice(inserted)

	_, err = attachPublicreportImageNuisanceOlds0(ctx, exec, len(related), publicreportImage0, publicreportNuisanceOlds2)
	if err != nil {
		return err
	}

	publicreportImage0.R.NuisanceOlds = append(publicreportImage0.R.NuisanceOlds, publicreportNuisanceOlds2...)

	for _, rel := range publicreportNuisanceOlds2 {
		rel.R.Images = append(rel.R.Images, publicreportImage0)
	}
	return nil
}

func (publicreportImage0 *PublicreportImage) AttachNuisanceOlds(ctx context.Context, exec bob.Executor, related ...*PublicreportNuisanceOld) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	publicreportNuisanceOlds2 := PublicreportNuisanceOldSlice(related)

	_, err = attachPublicreportImageNuisanceOlds0(ctx, exec, len(related), publicreportImage0, publicreportNuisanceOlds2)
	if err != nil {
		return err
	}

	publicreportImage0.R.NuisanceOlds = append(publicreportImage0.R.NuisanceOlds, publicreportNuisanceOlds2...)

	for _, rel := range related {
		rel.R.Images = append(rel.R.Images, publicreportImage0)
	}

	return nil
}

func attachPublicreportImageReports0(ctx context.Context, exec bob.Executor, count int, publicreportImage0 *PublicreportImage, publicreportReports2 PublicreportReportSlice) (PublicreportReportImageSlice, error) {
	setters := make([]*PublicreportReportImageSetter, count)
	for i := range count {
		setters[i] = &PublicreportReportImageSetter{
			ImageID:  omit.From(publicreportImage0.ID),
			ReportID: omit.From(publicreportReports2[i].ID),
		}
	}

	publicreportReportImages1, err := PublicreportReportImages.Insert(bob.ToMods(setters...)).All(ctx, exec)
	if err != nil {
		return nil, fmt.Errorf("attachPublicreportImageReports0: %w", err)
	}

	return publicreportReportImages1, nil
}

func (publicreportImage0 *PublicreportImage) InsertReports(ctx context.Context, exec bob.Executor, related ...*PublicreportReportSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	inserted, err := PublicreportReports.Insert(bob.ToMods(related...)).All(ctx, exec)
	if err != nil {
		return fmt.Errorf("inserting related objects: %w", err)
	}
	publicreportReports2 := PublicreportReportSlice(inserted)

	_, err = attachPublicreportImageReports0(ctx, exec, len(related), publicreportImage0, publicreportReports2)
	if err != nil {
		return err
	}

	publicreportImage0.R.Reports = append(publicreportImage0.R.Reports, publicreportReports2...)

	for _, rel := range publicreportReports2 {
		rel.R.Images = append(rel.R.Images, publicreportImage0)
	}
	return nil
}

func (publicreportImage0 *PublicreportImage) AttachReports(ctx context.Context, exec bob.Executor, related ...*PublicreportReport) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	publicreportReports2 := PublicreportReportSlice(related)

	_, err = attachPublicreportImageReports0(ctx, exec, len(related), publicreportImage0, publicreportReports2)
	if err != nil {
		return err
	}

	publicreportImage0.R.Reports = append(publicreportImage0.R.Reports, publicreportReports2...)

	for _, rel := range related {
		rel.R.Images = append(rel.R.Images, publicreportImage0)
	}

	return nil
}

func attachPublicreportImageWaterOlds0(ctx context.Context, exec bob.Executor, count int, publicreportImage0 *PublicreportImage, publicreportWaterOlds2 PublicreportWaterOldSlice) (PublicreportWaterImageOldSlice, error) {
	setters := make([]*PublicreportWaterImageOldSetter, count)
	for i := range count {
		setters[i] = &PublicreportWaterImageOldSetter{
			ImageID: omit.From(publicreportImage0.ID),
			WaterID: omit.From(publicreportWaterOlds2[i].ID),
		}
	}

	publicreportWaterImageOlds1, err := PublicreportWaterImageOlds.Insert(bob.ToMods(setters...)).All(ctx, exec)
	if err != nil {
		return nil, fmt.Errorf("attachPublicreportImageWaterOlds0: %w", err)
	}

	return publicreportWaterImageOlds1, nil
}

func (publicreportImage0 *PublicreportImage) InsertWaterOlds(ctx context.Context, exec bob.Executor, related ...*PublicreportWaterOldSetter) error {
	if len(related) == 0 {
		return nil
	}

	var err error

	inserted, err := PublicreportWaterOlds.Insert(bob.ToMods(related...)).All(ctx, exec)
	if err != nil {
		return fmt.Errorf("inserting related objects: %w", err)
	}
	publicreportWaterOlds2 := PublicreportWaterOldSlice(inserted)

	_, err = attachPublicreportImageWaterOlds0(ctx, exec, len(related), publicreportImage0, publicreportWaterOlds2)
	if err != nil {
		return err
	}

	publicreportImage0.R.WaterOlds = append(publicreportImage0.R.WaterOlds, publicreportWaterOlds2...)

	for _, rel := range publicreportWaterOlds2 {
		rel.R.Images = append(rel.R.Images, publicreportImage0)
	}
	return nil
}

func (publicreportImage0 *PublicreportImage) AttachWaterOlds(ctx context.Context, exec bob.Executor, related ...*PublicreportWaterOld) error {
	if len(related) == 0 {
		return nil
	}

	var err error
	publicreportWaterOlds2 := PublicreportWaterOldSlice(related)

	_, err = attachPublicreportImageWaterOlds0(ctx, exec, len(related), publicreportImage0, publicreportWaterOlds2)
	if err != nil {
		return err
	}

	publicreportImage0.R.WaterOlds = append(publicreportImage0.R.WaterOlds, publicreportWaterOlds2...)

	for _, rel := range related {
		rel.R.Images = append(rel.R.Images, publicreportImage0)
	}

	return nil
}

type publicreportImageWhere[Q psql.Filterable] struct {
	ID               psql.WhereMod[Q, int32]
	ContentType      psql.WhereMod[Q, string]
	Created          psql.WhereMod[Q, time.Time]
	ResolutionX      psql.WhereMod[Q, int32]
	ResolutionY      psql.WhereMod[Q, int32]
	StorageUUID      psql.WhereMod[Q, uuid.UUID]
	StorageSize      psql.WhereMod[Q, int64]
	UploadedFilename psql.WhereMod[Q, string]
	Location         psql.WhereNullMod[Q, string]
}

func (publicreportImageWhere[Q]) AliasedAs(alias string) publicreportImageWhere[Q] {
	return buildPublicreportImageWhere[Q](buildPublicreportImageColumns(alias))
}

func buildPublicreportImageWhere[Q psql.Filterable](cols publicreportImageColumns) publicreportImageWhere[Q] {
	return publicreportImageWhere[Q]{
		ID:               psql.Where[Q, int32](cols.ID),
		ContentType:      psql.Where[Q, string](cols.ContentType),
		Created:          psql.Where[Q, time.Time](cols.Created),
		ResolutionX:      psql.Where[Q, int32](cols.ResolutionX),
		ResolutionY:      psql.Where[Q, int32](cols.ResolutionY),
		StorageUUID:      psql.Where[Q, uuid.UUID](cols.StorageUUID),
		StorageSize:      psql.Where[Q, int64](cols.StorageSize),
		UploadedFilename: psql.Where[Q, string](cols.UploadedFilename),
		Location:         psql.WhereNull[Q, string](cols.Location),
	}
}

func (o *PublicreportImage) Preload(name string, retrieved any) error {
	if o == nil {
		return nil
	}

	switch name {
	case "ImageExifs":
		rels, ok := retrieved.(PublicreportImageExifSlice)
		if !ok {
			return fmt.Errorf("publicreportImage cannot load %T as %q", retrieved, name)
		}

		o.R.ImageExifs = rels

		for _, rel := range rels {
			if rel != nil {
				rel.R.Image = o
			}
		}
		return nil
	case "NuisanceOlds":
		rels, ok := retrieved.(PublicreportNuisanceOldSlice)
		if !ok {
			return fmt.Errorf("publicreportImage cannot load %T as %q", retrieved, name)
		}

		o.R.NuisanceOlds = rels

		for _, rel := range rels {
			if rel != nil {
				rel.R.Images = PublicreportImageSlice{o}
			}
		}
		return nil
	case "Reports":
		rels, ok := retrieved.(PublicreportReportSlice)
		if !ok {
			return fmt.Errorf("publicreportImage cannot load %T as %q", retrieved, name)
		}

		o.R.Reports = rels

		for _, rel := range rels {
			if rel != nil {
				rel.R.Images = PublicreportImageSlice{o}
			}
		}
		return nil
	case "WaterOlds":
		rels, ok := retrieved.(PublicreportWaterOldSlice)
		if !ok {
			return fmt.Errorf("publicreportImage cannot load %T as %q", retrieved, name)
		}

		o.R.WaterOlds = rels

		for _, rel := range rels {
			if rel != nil {
				rel.R.Images = PublicreportImageSlice{o}
			}
		}
		return nil
	default:
		return fmt.Errorf("publicreportImage has no relationship %q", name)
	}
}

type publicreportImagePreloader struct{}

func buildPublicreportImagePreloader() publicreportImagePreloader {
	return publicreportImagePreloader{}
}

type publicreportImageThenLoader[Q orm.Loadable] struct {
	ImageExifs   func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	NuisanceOlds func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	Reports      func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
	WaterOlds    func(...bob.Mod[*dialect.SelectQuery]) orm.Loader[Q]
}

func buildPublicreportImageThenLoader[Q orm.Loadable]() publicreportImageThenLoader[Q] {
	type ImageExifsLoadInterface interface {
		LoadImageExifs(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type NuisanceOldsLoadInterface interface {
		LoadNuisanceOlds(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type ReportsLoadInterface interface {
		LoadReports(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}
	type WaterOldsLoadInterface interface {
		LoadWaterOlds(context.Context, bob.Executor, ...bob.Mod[*dialect.SelectQuery]) error
	}

	return publicreportImageThenLoader[Q]{
		ImageExifs: thenLoadBuilder[Q](
			"ImageExifs",
			func(ctx context.Context, exec bob.Executor, retrieved ImageExifsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadImageExifs(ctx, exec, mods...)
			},
		),
		NuisanceOlds: thenLoadBuilder[Q](
			"NuisanceOlds",
			func(ctx context.Context, exec bob.Executor, retrieved NuisanceOldsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadNuisanceOlds(ctx, exec, mods...)
			},
		),
		Reports: thenLoadBuilder[Q](
			"Reports",
			func(ctx context.Context, exec bob.Executor, retrieved ReportsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadReports(ctx, exec, mods...)
			},
		),
		WaterOlds: thenLoadBuilder[Q](
			"WaterOlds",
			func(ctx context.Context, exec bob.Executor, retrieved WaterOldsLoadInterface, mods ...bob.Mod[*dialect.SelectQuery]) error {
				return retrieved.LoadWaterOlds(ctx, exec, mods...)
			},
		),
	}
}

// LoadImageExifs loads the publicreportImage's ImageExifs into the .R struct
func (o *PublicreportImage) LoadImageExifs(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	// Reset the relationship
	o.R.ImageExifs = nil

	related, err := o.ImageExifs(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, rel := range related {
		rel.R.Image = o
	}

	o.R.ImageExifs = related
	return nil
}

// LoadImageExifs loads the publicreportImage's ImageExifs into the .R struct
func (os PublicreportImageSlice) LoadImageExifs(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	publicreportImageExifs, err := os.ImageExifs(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		o.R.ImageExifs = nil
	}

	for _, o := range os {
		if o == nil {
			continue
		}

		for _, rel := range publicreportImageExifs {

			if !(o.ID == rel.ImageID) {
				continue
			}

			rel.R.Image = o

			o.R.ImageExifs = append(o.R.ImageExifs, rel)
		}
	}

	return nil
}

// LoadNuisanceOlds loads the publicreportImage's NuisanceOlds into the .R struct
func (o *PublicreportImage) LoadNuisanceOlds(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	// Reset the relationship
	o.R.NuisanceOlds = nil

	related, err := o.NuisanceOlds(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, rel := range related {
		rel.R.Images = PublicreportImageSlice{o}
	}

	o.R.NuisanceOlds = related
	return nil
}

// LoadNuisanceOlds loads the publicreportImage's NuisanceOlds into the .R struct
func (os PublicreportImageSlice) LoadNuisanceOlds(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	// since we are changing the columns, we need to check if the original columns were set or add the defaults
	sq := dialect.SelectQuery{}
	for _, mod := range mods {
		mod.Apply(&sq)
	}

	if len(sq.SelectList.Columns) == 0 {
		mods = append(mods, sm.Columns(PublicreportNuisanceOlds.Columns))
	}

	q := os.NuisanceOlds(append(
		mods,
		sm.Columns(PublicreportNuisanceImageOlds.Columns.ImageID.As("related_publicreport.image.ID")),
	)...)

	IDSlice := []int32{}

	mapper := scan.Mod(scan.StructMapper[*PublicreportNuisanceOld](), func(ctx context.Context, cols []string) (scan.BeforeFunc, func(any, any) error) {
		return func(row *scan.Row) (any, error) {
				IDSlice = append(IDSlice, *new(int32))
				row.ScheduleScanByName("related_publicreport.image.ID", &IDSlice[len(IDSlice)-1])

				return nil, nil
			},
			func(any, any) error {
				return nil
			}
	})

	publicreportNuisanceOlds, err := bob.Allx[bob.SliceTransformer[*PublicreportNuisanceOld, PublicreportNuisanceOldSlice]](ctx, exec, q, mapper)
	if err != nil {
		return err
	}

	for _, o := range os {
		o.R.NuisanceOlds = nil
	}

	for _, o := range os {
		for i, rel := range publicreportNuisanceOlds {
			if !(o.ID == IDSlice[i]) {
				continue
			}

			rel.R.Images = append(rel.R.Images, o)

			o.R.NuisanceOlds = append(o.R.NuisanceOlds, rel)
		}
	}

	return nil
}

// LoadReports loads the publicreportImage's Reports into the .R struct
func (o *PublicreportImage) LoadReports(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	// Reset the relationship
	o.R.Reports = nil

	related, err := o.Reports(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, rel := range related {
		rel.R.Images = PublicreportImageSlice{o}
	}

	o.R.Reports = related
	return nil
}

// LoadReports loads the publicreportImage's Reports into the .R struct
func (os PublicreportImageSlice) LoadReports(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	// since we are changing the columns, we need to check if the original columns were set or add the defaults
	sq := dialect.SelectQuery{}
	for _, mod := range mods {
		mod.Apply(&sq)
	}

	if len(sq.SelectList.Columns) == 0 {
		mods = append(mods, sm.Columns(PublicreportReports.Columns))
	}

	q := os.Reports(append(
		mods,
		sm.Columns(PublicreportReportImages.Columns.ImageID.As("related_publicreport.image.ID")),
	)...)

	IDSlice := []int32{}

	mapper := scan.Mod(scan.StructMapper[*PublicreportReport](), func(ctx context.Context, cols []string) (scan.BeforeFunc, func(any, any) error) {
		return func(row *scan.Row) (any, error) {
				IDSlice = append(IDSlice, *new(int32))
				row.ScheduleScanByName("related_publicreport.image.ID", &IDSlice[len(IDSlice)-1])

				return nil, nil
			},
			func(any, any) error {
				return nil
			}
	})

	publicreportReports, err := bob.Allx[bob.SliceTransformer[*PublicreportReport, PublicreportReportSlice]](ctx, exec, q, mapper)
	if err != nil {
		return err
	}

	for _, o := range os {
		o.R.Reports = nil
	}

	for _, o := range os {
		for i, rel := range publicreportReports {
			if !(o.ID == IDSlice[i]) {
				continue
			}

			rel.R.Images = append(rel.R.Images, o)

			o.R.Reports = append(o.R.Reports, rel)
		}
	}

	return nil
}

// LoadWaterOlds loads the publicreportImage's WaterOlds into the .R struct
func (o *PublicreportImage) LoadWaterOlds(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if o == nil {
		return nil
	}

	// Reset the relationship
	o.R.WaterOlds = nil

	related, err := o.WaterOlds(mods...).All(ctx, exec)
	if err != nil {
		return err
	}

	for _, rel := range related {
		rel.R.Images = PublicreportImageSlice{o}
	}

	o.R.WaterOlds = related
	return nil
}

// LoadWaterOlds loads the publicreportImage's WaterOlds into the .R struct
func (os PublicreportImageSlice) LoadWaterOlds(ctx context.Context, exec bob.Executor, mods ...bob.Mod[*dialect.SelectQuery]) error {
	if len(os) == 0 {
		return nil
	}

	// since we are changing the columns, we need to check if the original columns were set or add the defaults
	sq := dialect.SelectQuery{}
	for _, mod := range mods {
		mod.Apply(&sq)
	}

	if len(sq.SelectList.Columns) == 0 {
		mods = append(mods, sm.Columns(PublicreportWaterOlds.Columns))
	}

	q := os.WaterOlds(append(
		mods,
		sm.Columns(PublicreportWaterImageOlds.Columns.ImageID.As("related_publicreport.image.ID")),
	)...)

	IDSlice := []int32{}

	mapper := scan.Mod(scan.StructMapper[*PublicreportWaterOld](), func(ctx context.Context, cols []string) (scan.BeforeFunc, func(any, any) error) {
		return func(row *scan.Row) (any, error) {
				IDSlice = append(IDSlice, *new(int32))
				row.ScheduleScanByName("related_publicreport.image.ID", &IDSlice[len(IDSlice)-1])

				return nil, nil
			},
			func(any, any) error {
				return nil
			}
	})

	publicreportWaterOlds, err := bob.Allx[bob.SliceTransformer[*PublicreportWaterOld, PublicreportWaterOldSlice]](ctx, exec, q, mapper)
	if err != nil {
		return err
	}

	for _, o := range os {
		o.R.WaterOlds = nil
	}

	for _, o := range os {
		for i, rel := range publicreportWaterOlds {
			if !(o.ID == IDSlice[i]) {
				continue
			}

			rel.R.Images = append(rel.R.Images, o)

			o.R.WaterOlds = append(o.R.WaterOlds, rel)
		}
	}

	return nil
}
