// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.28.0
// source: reception_attributes.sql

package db

import (
	"context"
	"database/sql"
)

const createReceptionAttribute = `-- name: CreateReceptionAttribute :one
INSERT INTO reception_attributes (           
    qualityattribute_id,
    qtyremoved,
    qtytaken,
    others,
    receptionform_id,
    percentage
 ) VALUES (
    $1,
    $2,
    $3,
    $4,
    $5,
    $6
) 
RETURNING id, qualityattribute_id, others, receptionform_id, qtyremoved, qtytaken, percentage, created_at
`

type CreateReceptionAttributeParams struct {
	QualityattributeID sql.NullInt32  `json:"qualityattribute_id"`
	Qtyremoved         float64        `json:"qtyremoved"`
	Qtytaken           float64        `json:"qtytaken"`
	Others             sql.NullString `json:"others"`
	ReceptionformID    int32          `json:"receptionform_id"`
	Percentage         float64        `json:"percentage"`
}

func (q *Queries) CreateReceptionAttribute(ctx context.Context, arg CreateReceptionAttributeParams) (ReceptionAttribute, error) {
	row := q.db.QueryRowContext(ctx, createReceptionAttribute,
		arg.QualityattributeID,
		arg.Qtyremoved,
		arg.Qtytaken,
		arg.Others,
		arg.ReceptionformID,
		arg.Percentage,
	)
	var i ReceptionAttribute
	err := row.Scan(
		&i.ID,
		&i.QualityattributeID,
		&i.Others,
		&i.ReceptionformID,
		&i.Qtyremoved,
		&i.Qtytaken,
		&i.Percentage,
		&i.CreatedAt,
	)
	return i, err
}

const deleteReceptionAttribute = `-- name: DeleteReceptionAttribute :exec
DELETE FROM reception_attributes WHERE id= $1
`

func (q *Queries) DeleteReceptionAttribute(ctx context.Context, id int64) error {
	_, err := q.db.ExecContext(ctx, deleteReceptionAttribute, id)
	return err
}

const deleteReceptionAttributeByRecepForm = `-- name: DeleteReceptionAttributeByRecepForm :exec
DELETE FROM reception_attributes WHERE receptionform_id= $1
`

func (q *Queries) DeleteReceptionAttributeByRecepForm(ctx context.Context, receptionformID int32) error {
	_, err := q.db.ExecContext(ctx, deleteReceptionAttributeByRecepForm, receptionformID)
	return err
}

const getReceptionFormAttributes = `-- name: GetReceptionFormAttributes :many
SELECT  qtyremoved,qtytaken,reception_attributes.id reception_attribute_id,others,percentage,qualityattributes.description,qualityattribute_id as qualityattributes_id 
FROM    reception_attributes
JOIN    qualityattributes on qualityattributes.id = reception_attributes.qualityattribute_id
WHERE   receptionform_id = $1
`

type GetReceptionFormAttributesRow struct {
	Qtyremoved           float64        `json:"qtyremoved"`
	Qtytaken             float64        `json:"qtytaken"`
	ReceptionAttributeID int64          `json:"reception_attribute_id"`
	Others               sql.NullString `json:"others"`
	Percentage           float64        `json:"percentage"`
	Description          string         `json:"description"`
	QualityattributesID  sql.NullInt32  `json:"qualityattributes_id"`
}

func (q *Queries) GetReceptionFormAttributes(ctx context.Context, receptionformID int32) ([]GetReceptionFormAttributesRow, error) {
	rows, err := q.db.QueryContext(ctx, getReceptionFormAttributes, receptionformID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []GetReceptionFormAttributesRow{}
	for rows.Next() {
		var i GetReceptionFormAttributesRow
		if err := rows.Scan(
			&i.Qtyremoved,
			&i.Qtytaken,
			&i.ReceptionAttributeID,
			&i.Others,
			&i.Percentage,
			&i.Description,
			&i.QualityattributesID,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listReceptionAttributes = `-- name: ListReceptionAttributes :many
SELECT id, qualityattribute_id, others, receptionform_id, qtyremoved, qtytaken, percentage, created_at FROM reception_attributes
ORDER BY id LIMIT $1
OFFSET $2
`

type ListReceptionAttributesParams struct {
	Limit  int32 `json:"limit"`
	Offset int32 `json:"offset"`
}

func (q *Queries) ListReceptionAttributes(ctx context.Context, arg ListReceptionAttributesParams) ([]ReceptionAttribute, error) {
	rows, err := q.db.QueryContext(ctx, listReceptionAttributes, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ReceptionAttribute{}
	for rows.Next() {
		var i ReceptionAttribute
		if err := rows.Scan(
			&i.ID,
			&i.QualityattributeID,
			&i.Others,
			&i.ReceptionformID,
			&i.Qtyremoved,
			&i.Qtytaken,
			&i.Percentage,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const updateReceptionAttribute = `-- name: UpdateReceptionAttribute :one
UPDATE reception_attributes
  set 
    qualityattribute_id = COALESCE($1, qualityattribute_id),
    others              = COALESCE($2, others),
    qtyremoved          = COALESCE($3, qtyremoved),
    qtytaken            = COALESCE($4, qtytaken),
    receptionform_id    =  $5,
    percentage          =  $6
WHERE id = $7
RETURNING id, qualityattribute_id, others, receptionform_id, qtyremoved, qtytaken, percentage, created_at
`

type UpdateReceptionAttributeParams struct {
	QualityattributeID sql.NullInt32   `json:"qualityattribute_id"`
	Others             sql.NullString  `json:"others"`
	Qtyremoved         sql.NullFloat64 `json:"qtyremoved"`
	Qtytaken           sql.NullFloat64 `json:"qtytaken"`
	ReceptionformID    int32           `json:"receptionform_id"`
	Percentage         float64         `json:"percentage"`
	ID                 int64           `json:"id"`
}

func (q *Queries) UpdateReceptionAttribute(ctx context.Context, arg UpdateReceptionAttributeParams) (ReceptionAttribute, error) {
	row := q.db.QueryRowContext(ctx, updateReceptionAttribute,
		arg.QualityattributeID,
		arg.Others,
		arg.Qtyremoved,
		arg.Qtytaken,
		arg.ReceptionformID,
		arg.Percentage,
		arg.ID,
	)
	var i ReceptionAttribute
	err := row.Scan(
		&i.ID,
		&i.QualityattributeID,
		&i.Others,
		&i.ReceptionformID,
		&i.Qtyremoved,
		&i.Qtytaken,
		&i.Percentage,
		&i.CreatedAt,
	)
	return i, err
}
