2024-05-30 12:03:12 +00:00
|
|
|
package database
|
|
|
|
|
2024-06-01 21:18:33 +00:00
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
)
|
2024-05-30 12:03:12 +00:00
|
|
|
|
2024-09-05 09:53:14 +00:00
|
|
|
type CardLocation struct {
|
2024-05-30 12:03:12 +00:00
|
|
|
Id int
|
2024-06-03 16:08:15 +00:00
|
|
|
CardPrintingId string
|
2024-09-05 09:53:14 +00:00
|
|
|
StorageAreaId int
|
2024-05-30 12:03:12 +00:00
|
|
|
Position int
|
|
|
|
}
|
|
|
|
|
2024-09-05 09:53:14 +00:00
|
|
|
func GetLastPositionInStorageArea(db *sql.DB, storageAreaId int) (int, error) {
|
|
|
|
query := "SELECT Position FROM CardLocation WHERE StorageAreaId = ? ORDER BY Position DESC LIMIT 1;"
|
2024-05-30 12:03:12 +00:00
|
|
|
|
|
|
|
var lastPosition int
|
2024-09-05 09:53:14 +00:00
|
|
|
err := db.QueryRow(query, storageAreaId).Scan(&lastPosition)
|
2024-05-30 12:03:12 +00:00
|
|
|
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return 0, nil
|
2024-06-01 21:18:33 +00:00
|
|
|
} else if err != nil {
|
2024-05-30 12:03:12 +00:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return lastPosition, nil
|
|
|
|
}
|
|
|
|
|
2024-10-10 14:43:56 +00:00
|
|
|
func InsertCardLocation(db *sql.DB, storageLocation CardLocation) (int64, error) {
|
2024-09-05 09:53:14 +00:00
|
|
|
query := `INSERT INTO CardLocation
|
|
|
|
(CardPrintingId, StorageAreaId, Position)
|
|
|
|
VALUES (?, ?, ?);`
|
2024-05-30 12:03:12 +00:00
|
|
|
|
|
|
|
insert, err := db.Prepare(query)
|
|
|
|
if err != nil {
|
2024-10-10 14:43:56 +00:00
|
|
|
return -1, err
|
2024-05-30 12:03:12 +00:00
|
|
|
}
|
|
|
|
|
2024-10-10 14:43:56 +00:00
|
|
|
result, err := insert.Exec(storageLocation.CardPrintingId, storageLocation.StorageAreaId, storageLocation.Position)
|
2024-05-30 12:03:12 +00:00
|
|
|
if err != nil {
|
2024-10-10 14:43:56 +00:00
|
|
|
return -1, err
|
2024-05-30 12:03:12 +00:00
|
|
|
}
|
2024-05-30 13:33:37 +00:00
|
|
|
|
2024-10-10 14:43:56 +00:00
|
|
|
id, err := result.LastInsertId()
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return id, nil
|
2024-05-30 12:03:12 +00:00
|
|
|
}
|
2024-09-16 16:44:57 +00:00
|
|
|
|
|
|
|
func InsertCardInExistingLocation(db *sql.DB, cardLocation CardLocation) error {
|
|
|
|
query := `UPDATE CardLocation SET CardPrintingId = ? WHERE Id = ?;`
|
|
|
|
|
|
|
|
update, err := db.Prepare(query)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = update.Exec(cardLocation.CardPrintingId, cardLocation.Id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|