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

package db

import (
	"context"
	"database/sql"
	"time"
)

const checkPlannedConsignmentDocumentsByConsignmentNo = `-- name: CheckPlannedConsignmentDocumentsByConsignmentNo :one
SELECT EXISTS(
   SELECT 1 FROM plannedconsignments_documents
   WHERE plc_id = $1
)
`

func (q *Queries) CheckPlannedConsignmentDocumentsByConsignmentNo(ctx context.Context, plcID string) (bool, error) {
	row := q.db.QueryRowContext(ctx, checkPlannedConsignmentDocumentsByConsignmentNo, plcID)
	var exists bool
	err := row.Scan(&exists)
	return exists, err
}

const checkPlannedConsignmentDocumentsByID = `-- name: CheckPlannedConsignmentDocumentsByID :one
SELECT EXISTS(
   SELECT 1 FROM plannedconsignments_documents
   WHERE document_id = $1
)
`

func (q *Queries) CheckPlannedConsignmentDocumentsByID(ctx context.Context, documentID int32) (bool, error) {
	row := q.db.QueryRowContext(ctx, checkPlannedConsignmentDocumentsByID, documentID)
	var exists bool
	err := row.Scan(&exists)
	return exists, err
}

const checkPlannedConsignmentOtherDocumentsByConsignmentNo = `-- name: CheckPlannedConsignmentOtherDocumentsByConsignmentNo :one
SELECT EXISTS(
   SELECT 1 FROM plannedconsignments_otherdocuments
   WHERE plannedconsignments_id = $1
)
`

func (q *Queries) CheckPlannedConsignmentOtherDocumentsByConsignmentNo(ctx context.Context, plannedconsignmentsID string) (bool, error) {
	row := q.db.QueryRowContext(ctx, checkPlannedConsignmentOtherDocumentsByConsignmentNo, plannedconsignmentsID)
	var exists bool
	err := row.Scan(&exists)
	return exists, err
}

const createPlannedConsignmentDocuments = `-- name: CreatePlannedConsignmentDocuments :one
INSERT INTO plannedconsignments_documents(
    document_id,
    document,
    plc_id   
 ) VALUES (
    $1,
    $2,
    $3
  )
  RETURNING id, plc_id, created_at, document_id, document
`

type CreatePlannedConsignmentDocumentsParams struct {
	DocumentID int32  `json:"document_id"`
	Document   string `json:"document"`
	PlcID      string `json:"plc_id"`
}

func (q *Queries) CreatePlannedConsignmentDocuments(ctx context.Context, arg CreatePlannedConsignmentDocumentsParams) (PlannedconsignmentsDocument, error) {
	row := q.db.QueryRowContext(ctx, createPlannedConsignmentDocuments, arg.DocumentID, arg.Document, arg.PlcID)
	var i PlannedconsignmentsDocument
	err := row.Scan(
		&i.ID,
		&i.PlcID,
		&i.CreatedAt,
		&i.DocumentID,
		&i.Document,
	)
	return i, err
}

const createPlannedConsignmentQueries = `-- name: CreatePlannedConsignmentQueries :one
INSERT INTO consignment_queries(
    plannedconsignment_id,
    reason,
    user_id
 ) VALUES (
    $1, 
    $2, 
    $3
  )
  RETURNING id, plannedconsignment_id, reason, user_id, created_at
`

type CreatePlannedConsignmentQueriesParams struct {
	PlannedconsignmentID string `json:"plannedconsignment_id"`
	Reason               string `json:"reason"`
	UserID               int32  `json:"user_id"`
}

func (q *Queries) CreatePlannedConsignmentQueries(ctx context.Context, arg CreatePlannedConsignmentQueriesParams) (ConsignmentQuery, error) {
	row := q.db.QueryRowContext(ctx, createPlannedConsignmentQueries, arg.PlannedconsignmentID, arg.Reason, arg.UserID)
	var i ConsignmentQuery
	err := row.Scan(
		&i.ID,
		&i.PlannedconsignmentID,
		&i.Reason,
		&i.UserID,
		&i.CreatedAt,
	)
	return i, err
}

const deleteOtherPlcDocs = `-- name: DeleteOtherPlcDocs :exec
DELETE FROM plannedconsignments_otherdocuments WHERE plannedconsignments_id= $1
`

func (q *Queries) DeleteOtherPlcDocs(ctx context.Context, plannedconsignmentsID string) error {
	_, err := q.db.ExecContext(ctx, deleteOtherPlcDocs, plannedconsignmentsID)
	return err
}

const deletePlannedConsigmentDocument = `-- name: DeletePlannedConsigmentDocument :exec
DELETE FROM plannedconsignments_documents WHERE id= $1
`

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

const deletePlcDocs = `-- name: DeletePlcDocs :exec
DELETE FROM plannedconsignments_documents WHERE plc_id= $1
`

func (q *Queries) DeletePlcDocs(ctx context.Context, plcID string) error {
	_, err := q.db.ExecContext(ctx, deletePlcDocs, plcID)
	return err
}

const deletePlcDocsByID = `-- name: DeletePlcDocsByID :exec
DELETE FROM plannedconsignments_documents WHERE document_id= $1
`

func (q *Queries) DeletePlcDocsByID(ctx context.Context, documentID int32) error {
	_, err := q.db.ExecContext(ctx, deletePlcDocsByID, documentID)
	return err
}

const deletePlcDocsByRow = `-- name: DeletePlcDocsByRow :exec
DELETE FROM plannedconsignments_documents WHERE id= $1
`

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

const getPlannedConsignmentDocumentsByConsignmentNo = `-- name: GetPlannedConsignmentDocumentsByConsignmentNo :many
SELECT plannedconsignments_documents.document_id,plannedconsignments_documents.id,
plannedconsignments_documents.document,plannedconsignments_documents.plc_id,
documents.name,documents.description,documents.documentusage,documents.iscrop,
documents.isglobal,plannedconsignments_documents.created_at FROM plannedconsignments_documents
JOIN documents on documents.id = plannedconsignments_documents.document_id
WHERE plc_id = $1
`

type GetPlannedConsignmentDocumentsByConsignmentNoRow struct {
	DocumentID    int32     `json:"document_id"`
	ID            int64     `json:"id"`
	Document      string    `json:"document"`
	PlcID         string    `json:"plc_id"`
	Name          string    `json:"name"`
	Description   string    `json:"description"`
	Documentusage string    `json:"documentusage"`
	Iscrop        bool      `json:"iscrop"`
	Isglobal      bool      `json:"isglobal"`
	CreatedAt     time.Time `json:"created_at"`
}

func (q *Queries) GetPlannedConsignmentDocumentsByConsignmentNo(ctx context.Context, plcID string) ([]GetPlannedConsignmentDocumentsByConsignmentNoRow, error) {
	rows, err := q.db.QueryContext(ctx, getPlannedConsignmentDocumentsByConsignmentNo, plcID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []GetPlannedConsignmentDocumentsByConsignmentNoRow{}
	for rows.Next() {
		var i GetPlannedConsignmentDocumentsByConsignmentNoRow
		if err := rows.Scan(
			&i.DocumentID,
			&i.ID,
			&i.Document,
			&i.PlcID,
			&i.Name,
			&i.Description,
			&i.Documentusage,
			&i.Iscrop,
			&i.Isglobal,
			&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 getPlannedConsignmentOtherDocumentsByConsignmentNo = `-- name: GetPlannedConsignmentOtherDocumentsByConsignmentNo :many
SELECT id, plannedconsignments_id, name, description, created_at FROM plannedconsignments_otherdocuments
WHERE plannedconsignments_id = $1 LIMIT 1
`

func (q *Queries) GetPlannedConsignmentOtherDocumentsByConsignmentNo(ctx context.Context, plannedconsignmentsID string) ([]PlannedconsignmentsOtherdocument, error) {
	rows, err := q.db.QueryContext(ctx, getPlannedConsignmentOtherDocumentsByConsignmentNo, plannedconsignmentsID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []PlannedconsignmentsOtherdocument{}
	for rows.Next() {
		var i PlannedconsignmentsOtherdocument
		if err := rows.Scan(
			&i.ID,
			&i.PlannedconsignmentsID,
			&i.Name,
			&i.Description,
			&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 listPlannedConsigmentsDocuments = `-- name: ListPlannedConsigmentsDocuments :many
SELECT id, plc_id, created_at, document_id, document FROM plannedconsignments_documents
ORDER BY id LIMIT $1
OFFSET $2
`

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

func (q *Queries) ListPlannedConsigmentsDocuments(ctx context.Context, arg ListPlannedConsigmentsDocumentsParams) ([]PlannedconsignmentsDocument, error) {
	rows, err := q.db.QueryContext(ctx, listPlannedConsigmentsDocuments, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []PlannedconsignmentsDocument{}
	for rows.Next() {
		var i PlannedconsignmentsDocument
		if err := rows.Scan(
			&i.ID,
			&i.PlcID,
			&i.CreatedAt,
			&i.DocumentID,
			&i.Document,
		); 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 updatePlannedConsigmentDocuments = `-- name: UpdatePlannedConsigmentDocuments :one
UPDATE plannedconsignments_documents
  set 
      document                                    = COALESCE($1,document),
      document_id                                 = COALESCE($2,document_id)
WHERE id = $3 
RETURNING id, plc_id, created_at, document_id, document
`

type UpdatePlannedConsigmentDocumentsParams struct {
	Document   sql.NullString `json:"document"`
	DocumentID sql.NullInt32  `json:"document_id"`
	ID         int64          `json:"id"`
}

func (q *Queries) UpdatePlannedConsigmentDocuments(ctx context.Context, arg UpdatePlannedConsigmentDocumentsParams) (PlannedconsignmentsDocument, error) {
	row := q.db.QueryRowContext(ctx, updatePlannedConsigmentDocuments, arg.Document, arg.DocumentID, arg.ID)
	var i PlannedconsignmentsDocument
	err := row.Scan(
		&i.ID,
		&i.PlcID,
		&i.CreatedAt,
		&i.DocumentID,
		&i.Document,
	)
	return i, err
}
