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

package db

import (
	"context"
	"database/sql"
)

const createOrderCrop = `-- name: CreateOrderCrop :one
INSERT INTO order_crops (           
    order_id,
    crop_id,
    weight,
    weightreceived
 ) VALUES (
    $1, $2, $3, $4
) 
RETURNING id, order_id, crop_id, weight, weightreceived, status, created_at
`

type CreateOrderCropParams struct {
	OrderID        int32           `json:"order_id"`
	CropID         int32           `json:"crop_id"`
	Weight         float64         `json:"weight"`
	Weightreceived sql.NullFloat64 `json:"weightreceived"`
}

func (q *Queries) CreateOrderCrop(ctx context.Context, arg CreateOrderCropParams) (OrderCrop, error) {
	row := q.db.QueryRowContext(ctx, createOrderCrop,
		arg.OrderID,
		arg.CropID,
		arg.Weight,
		arg.Weightreceived,
	)
	var i OrderCrop
	err := row.Scan(
		&i.ID,
		&i.OrderID,
		&i.CropID,
		&i.Weight,
		&i.Weightreceived,
		&i.Status,
		&i.CreatedAt,
	)
	return i, err
}

const cropOrderIDExistance = `-- name: CropOrderIDExistance :one
SELECT EXISTS (
   select 1 from order_crops
   WHERE crop_id = $1 
   AND   order_id = $2
)
`

type CropOrderIDExistanceParams struct {
	CropID  int32 `json:"crop_id"`
	OrderID int32 `json:"order_id"`
}

func (q *Queries) CropOrderIDExistance(ctx context.Context, arg CropOrderIDExistanceParams) (bool, error) {
	row := q.db.QueryRowContext(ctx, cropOrderIDExistance, arg.CropID, arg.OrderID)
	var exists bool
	err := row.Scan(&exists)
	return exists, err
}

const deleteOrderCrops = `-- name: DeleteOrderCrops :exec
DELETE FROM order_crops WHERE id= $1
`

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

const getOrderCrop = `-- name: GetOrderCrop :one
SELECT id, order_id, crop_id, weight, weightreceived, status, created_at FROM order_crops
WHERE id = $1 LIMIT 1
`

func (q *Queries) GetOrderCrop(ctx context.Context, id int64) (OrderCrop, error) {
	row := q.db.QueryRowContext(ctx, getOrderCrop, id)
	var i OrderCrop
	err := row.Scan(
		&i.ID,
		&i.OrderID,
		&i.CropID,
		&i.Weight,
		&i.Weightreceived,
		&i.Status,
		&i.CreatedAt,
	)
	return i, err
}

const getWeightOnOrderID = `-- name: GetWeightOnOrderID :one
SELECT weight from order_crops
WHERE order_id = $1 AND crop_id = $2
`

type GetWeightOnOrderIDParams struct {
	OrderID int32 `json:"order_id"`
	CropID  int32 `json:"crop_id"`
}

func (q *Queries) GetWeightOnOrderID(ctx context.Context, arg GetWeightOnOrderIDParams) (float64, error) {
	row := q.db.QueryRowContext(ctx, getWeightOnOrderID, arg.OrderID, arg.CropID)
	var weight float64
	err := row.Scan(&weight)
	return weight, err
}

const getWeightPerCropReceived = `-- name: GetWeightPerCropReceived :one
SELECT weightreceived from order_crops
WHERE  order_id = $1 
AND    crop_id  = $2 
LIMIT 1
`

type GetWeightPerCropReceivedParams struct {
	OrderID int32 `json:"order_id"`
	CropID  int32 `json:"crop_id"`
}

func (q *Queries) GetWeightPerCropReceived(ctx context.Context, arg GetWeightPerCropReceivedParams) (sql.NullFloat64, error) {
	row := q.db.QueryRowContext(ctx, getWeightPerCropReceived, arg.OrderID, arg.CropID)
	var weightreceived sql.NullFloat64
	err := row.Scan(&weightreceived)
	return weightreceived, err
}

const getWeightReceived = `-- name: GetWeightReceived :one
SELECT weightreceived from order_crops
WHERE order_id = $1
`

func (q *Queries) GetWeightReceived(ctx context.Context, orderID int32) (sql.NullFloat64, error) {
	row := q.db.QueryRowContext(ctx, getWeightReceived, orderID)
	var weightreceived sql.NullFloat64
	err := row.Scan(&weightreceived)
	return weightreceived, err
}

const listOrderCrop = `-- name: ListOrderCrop :many
SELECT id, order_id, crop_id, weight, weightreceived, status, created_at FROM order_crops
ORDER BY id LIMIT $1
OFFSET $2
`

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

func (q *Queries) ListOrderCrop(ctx context.Context, arg ListOrderCropParams) ([]OrderCrop, error) {
	rows, err := q.db.QueryContext(ctx, listOrderCrop, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []OrderCrop{}
	for rows.Next() {
		var i OrderCrop
		if err := rows.Scan(
			&i.ID,
			&i.OrderID,
			&i.CropID,
			&i.Weight,
			&i.Weightreceived,
			&i.Status,
			&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 retrieveProductsOnOrders = `-- name: RetrieveProductsOnOrders :many
SELECT crops.id, crops.variant, crops.cropname, crops.crophscode, crops.cic_code, crops.riskstatus, crops.status, crops.created_at
FROM crops
JOIN order_crops on crops.id = order_crops.crop_id
WHERE order_crops.order_id = $1 
AND   order_crops.status   = 'Active'
`

func (q *Queries) RetrieveProductsOnOrders(ctx context.Context, orderID int32) ([]Crop, error) {
	rows, err := q.db.QueryContext(ctx, retrieveProductsOnOrders, orderID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Crop{}
	for rows.Next() {
		var i Crop
		if err := rows.Scan(
			&i.ID,
			&i.Variant,
			&i.Cropname,
			&i.Crophscode,
			&i.CicCode,
			&i.Riskstatus,
			&i.Status,
			&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 updateCropWeightOnOrder = `-- name: UpdateCropWeightOnOrder :one
UPDATE order_crops
  set 
   weight = $3
WHERE crop_id = $1 
AND   order_id= $2
RETURNING id, order_id, crop_id, weight, weightreceived, status, created_at
`

type UpdateCropWeightOnOrderParams struct {
	CropID  int32   `json:"crop_id"`
	OrderID int32   `json:"order_id"`
	Weight  float64 `json:"weight"`
}

func (q *Queries) UpdateCropWeightOnOrder(ctx context.Context, arg UpdateCropWeightOnOrderParams) (OrderCrop, error) {
	row := q.db.QueryRowContext(ctx, updateCropWeightOnOrder, arg.CropID, arg.OrderID, arg.Weight)
	var i OrderCrop
	err := row.Scan(
		&i.ID,
		&i.OrderID,
		&i.CropID,
		&i.Weight,
		&i.Weightreceived,
		&i.Status,
		&i.CreatedAt,
	)
	return i, err
}

const updateOrderCrop = `-- name: UpdateOrderCrop :one
UPDATE order_crops
  set 
    weightreceived = $3
WHERE order_id = $1 
AND   crop_id  = $2
RETURNING id, order_id, crop_id, weight, weightreceived, status, created_at
`

type UpdateOrderCropParams struct {
	OrderID        int32           `json:"order_id"`
	CropID         int32           `json:"crop_id"`
	Weightreceived sql.NullFloat64 `json:"weightreceived"`
}

func (q *Queries) UpdateOrderCrop(ctx context.Context, arg UpdateOrderCropParams) (OrderCrop, error) {
	row := q.db.QueryRowContext(ctx, updateOrderCrop, arg.OrderID, arg.CropID, arg.Weightreceived)
	var i OrderCrop
	err := row.Scan(
		&i.ID,
		&i.OrderID,
		&i.CropID,
		&i.Weight,
		&i.Weightreceived,
		&i.Status,
		&i.CreatedAt,
	)
	return i, err
}

const updateOrderWeightCrop = `-- name: UpdateOrderWeightCrop :one
UPDATE order_crops
  set 
     weight = $3
WHERE crop_id = $1 AND order_id= $2
RETURNING id, order_id, crop_id, weight, weightreceived, status, created_at
`

type UpdateOrderWeightCropParams struct {
	CropID  int32   `json:"crop_id"`
	OrderID int32   `json:"order_id"`
	Weight  float64 `json:"weight"`
}

func (q *Queries) UpdateOrderWeightCrop(ctx context.Context, arg UpdateOrderWeightCropParams) (OrderCrop, error) {
	row := q.db.QueryRowContext(ctx, updateOrderWeightCrop, arg.CropID, arg.OrderID, arg.Weight)
	var i OrderCrop
	err := row.Scan(
		&i.ID,
		&i.OrderID,
		&i.CropID,
		&i.Weight,
		&i.Weightreceived,
		&i.Status,
		&i.CreatedAt,
	)
	return i, err
}

const updateStatusOnCrop = `-- name: UpdateStatusOnCrop :one
UPDATE order_crops
  set 
   status = $3
WHERE crop_id = $1 
AND   order_id= $2
AND   weightreceived IS NULL 
OR    weightreceived = 0
RETURNING id, order_id, crop_id, weight, weightreceived, status, created_at
`

type UpdateStatusOnCropParams struct {
	CropID  int32  `json:"crop_id"`
	OrderID int32  `json:"order_id"`
	Status  string `json:"status"`
}

func (q *Queries) UpdateStatusOnCrop(ctx context.Context, arg UpdateStatusOnCropParams) (OrderCrop, error) {
	row := q.db.QueryRowContext(ctx, updateStatusOnCrop, arg.CropID, arg.OrderID, arg.Status)
	var i OrderCrop
	err := row.Scan(
		&i.ID,
		&i.OrderID,
		&i.CropID,
		&i.Weight,
		&i.Weightreceived,
		&i.Status,
		&i.CreatedAt,
	)
	return i, err
}

const updateWeightPerCropReceived = `-- name: UpdateWeightPerCropReceived :one
UPDATE  order_crops 
   SET  weightreceived = $3
WHERE  order_id = $1 
AND    crop_id  = $2
RETURNING id, order_id, crop_id, weight, weightreceived, status, created_at
`

type UpdateWeightPerCropReceivedParams struct {
	OrderID        int32           `json:"order_id"`
	CropID         int32           `json:"crop_id"`
	Weightreceived sql.NullFloat64 `json:"weightreceived"`
}

func (q *Queries) UpdateWeightPerCropReceived(ctx context.Context, arg UpdateWeightPerCropReceivedParams) (OrderCrop, error) {
	row := q.db.QueryRowContext(ctx, updateWeightPerCropReceived, arg.OrderID, arg.CropID, arg.Weightreceived)
	var i OrderCrop
	err := row.Scan(
		&i.ID,
		&i.OrderID,
		&i.CropID,
		&i.Weight,
		&i.Weightreceived,
		&i.Status,
		&i.CreatedAt,
	)
	return i, err
}
