242 lines
7.4 KiB
Go
242 lines
7.4 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"sync"
|
|
"time"
|
|
|
|
"git.espin.casa/albert/cml04-plb018/internal/types"
|
|
"git.espin.casa/albert/logger"
|
|
"gorm.io/driver/sqlite"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Storager interface {
|
|
// Normas
|
|
Normas(ctx context.Context, codigo string) (*types.Normas, error)
|
|
SalvarNormas(ctx context.Context, etiqueta *types.NormaList) error
|
|
// Ordenes produccion
|
|
OrdenProduccion(ctx context.Context, po int) (*types.ProductionOrder, error)
|
|
OrdenesProduccion(ctx context.Context, limit int) ([]types.ProductionOrder, error)
|
|
SalvarOrdenProduccion(ctx context.Context, po *types.ProductionOrder) error
|
|
// Ordenes cliente
|
|
OrdenCliente(ctx context.Context, co int) (*types.CustomerOrder, error)
|
|
OrdenesCliente(ctx context.Context, po int) ([]types.CustomerOrder, error)
|
|
SalvarOrdenCliente(ctx context.Context, co *types.CustomerOrder) error
|
|
// Impresoras
|
|
Impresora(ctx context.Context, id string) (*types.Zebra, error)
|
|
Impresoras(ctx context.Context) ([]types.Zebra, error)
|
|
SalvarImpresora(ctx context.Context, zebra *types.Zebra) error
|
|
// Etiquetas
|
|
Etiqueta(ctx context.Context, id string) (*types.Label, error)
|
|
Etiquetas(ctx context.Context) ([]types.Label, error)
|
|
SalvarEtiqueta(ctx context.Context, label *types.Label) error
|
|
// Paquetes
|
|
Paquete(ctx context.Context, id string) (*types.Paquete, error)
|
|
Paquetes(ctx context.Context, inicio, final time.Time) ([]types.Paquete, error)
|
|
SalvarPaquete(ctx context.Context, paquete *types.Paquete) error
|
|
ActualizarPaquete(ctx context.Context, paquete *types.Paquete) error
|
|
}
|
|
|
|
type storage struct {
|
|
db *gorm.DB
|
|
mux *sync.RWMutex
|
|
log logger.LoggerAdapter
|
|
}
|
|
|
|
// ActualizarPaquete implements Storager.
|
|
func (s *storage) ActualizarPaquete(ctx context.Context, paquete *types.Paquete) error {
|
|
return s.db.Model(&types.Paquete{}).Where("nro_paquete = ?", paquete.NroPaquete).Updates(types.Paquete{
|
|
NroPaquete: paquete.NroPaquete,
|
|
OrdenProduccion: paquete.OrdenProduccion,
|
|
OrdenCliente: paquete.OrdenCliente,
|
|
Producto: paquete.Producto,
|
|
Colada: paquete.Colada,
|
|
Calidad: paquete.Calidad,
|
|
Matnr: paquete.Matnr,
|
|
Operador: paquete.Operador,
|
|
Turno: paquete.Turno,
|
|
Longitud: paquete.Longitud,
|
|
Barras: paquete.Barras,
|
|
Peso: paquete.Peso,
|
|
PesoTeorico: paquete.PesoTeorico,
|
|
Normed: paquete.Normed,
|
|
Norpro: paquete.Norpro,
|
|
Nortol: paquete.Nortol,
|
|
DesvioPeso: paquete.DesvioPeso,
|
|
Confirmado: true,
|
|
Procesado: false,
|
|
Observaciones: fmt.Sprintf("reclasificado por: %s en fecha: %s", paquete.Operador, time.Now().Format("02/01/2006 15:04")),
|
|
Reclasificado: true,
|
|
UpdatedAt: time.Now(),
|
|
}).Error
|
|
}
|
|
|
|
// Paquete implements Storager.
|
|
func (s *storage) Paquete(ctx context.Context, id string) (*types.Paquete, error) {
|
|
paquete := &types.Paquete{}
|
|
if err := s.db.Where("nro_paquete = ?", id).First(paquete).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return paquete, nil
|
|
}
|
|
|
|
// Paquetes implements Storager.
|
|
func (s *storage) Paquetes(ctx context.Context, inicio time.Time, final time.Time) ([]types.Paquete, error) {
|
|
paquetes := []types.Paquete{}
|
|
// get paquetes
|
|
if err := s.db.Where("created_at BETWEEN ? AND ?", inicio, final).Find(&paquetes).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return paquetes, nil
|
|
}
|
|
|
|
// SalvarPaquete implements Storager.
|
|
func (s *storage) SalvarPaquete(ctx context.Context, paquete *types.Paquete) error {
|
|
return s.db.Create(paquete).Error
|
|
}
|
|
|
|
// Etiquetas implements Storager.
|
|
func (s *storage) Etiquetas(ctx context.Context) ([]types.Label, error) {
|
|
etiquetas := []types.Label{}
|
|
if err := s.db.Order("codigo asc").Find(&etiquetas).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return etiquetas, nil
|
|
}
|
|
|
|
// Etiqueta implements Storager.
|
|
func (s *storage) Etiqueta(ctx context.Context, id string) (*types.Label, error) {
|
|
etiqueta := &types.Label{}
|
|
// get etiqueta
|
|
if err := s.db.Where("codigo = ?", id).First(etiqueta).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return etiqueta, nil
|
|
}
|
|
|
|
// SalvarEtiqueta implements Storager.
|
|
func (s *storage) SalvarEtiqueta(ctx context.Context, label *types.Label) error {
|
|
return s.db.Save(label).Error
|
|
}
|
|
|
|
// SalvarNormas implements Storager.
|
|
func (s *storage) SalvarNormas(ctx context.Context, normas *types.NormaList) error {
|
|
for _, norma := range normas.Lista {
|
|
if err := s.db.Save(&norma).Error; err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Impresora implements Storager.
|
|
func (s *storage) Impresora(ctx context.Context, id string) (*types.Zebra, error) {
|
|
zebra := &types.Zebra{}
|
|
if err := s.db.Where("id = ?", id).First(zebra).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return zebra, nil
|
|
}
|
|
|
|
// Impresoras implements Storager.
|
|
func (s *storage) Impresoras(ctx context.Context) ([]types.Zebra, error) {
|
|
zebras := []types.Zebra{}
|
|
if err := s.db.Order("id asc").Find(&zebras).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return zebras, nil
|
|
}
|
|
|
|
// SalvarImpresora implements Storager.
|
|
func (s *storage) SalvarImpresora(ctx context.Context, zebra *types.Zebra) error {
|
|
return s.db.Save(zebra).Error
|
|
}
|
|
|
|
// OrdenesProduccion implements Storager.
|
|
func (s *storage) OrdenesProduccion(ctx context.Context, limit int) ([]types.ProductionOrder, error) {
|
|
poos := []types.ProductionOrder{}
|
|
// get production orders
|
|
if err := s.db.Order("p_order_no desc").Limit(limit).Find(&poos).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return poos, nil
|
|
}
|
|
|
|
// Normas implements Storager.
|
|
func (s *storage) Normas(ctx context.Context, codigo string) (*types.Normas, error) {
|
|
normas := &types.Normas{}
|
|
// get normas
|
|
if err := s.db.Where("codigo = ?", codigo).First(normas).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return normas, nil
|
|
}
|
|
|
|
// OrdenCliente implements Storager.
|
|
func (s *storage) OrdenCliente(ctx context.Context, co int) (*types.CustomerOrder, error) {
|
|
coo := &types.CustomerOrder{}
|
|
// get customer order
|
|
if err := s.db.Where("c_order_no=?", co).First(coo).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return coo, nil
|
|
}
|
|
|
|
// OrdenProduccion implements Storager.
|
|
func (s *storage) OrdenProduccion(ctx context.Context, po int) (*types.ProductionOrder, error) {
|
|
pro := &types.ProductionOrder{}
|
|
// get production order
|
|
if err := s.db.Where("p_order_no=?", po).First(pro).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return pro, nil
|
|
}
|
|
|
|
// OrdenesCliente implements Storager.
|
|
func (s *storage) OrdenesCliente(ctx context.Context, po int) ([]types.CustomerOrder, error) {
|
|
cos := []types.CustomerOrder{}
|
|
if err := s.db.Where("p_order_no=?", po).Order("c_order_no asc").Find(&cos).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return cos, nil
|
|
}
|
|
|
|
// SalvarOrdenCliente implements Storager.
|
|
func (s *storage) SalvarOrdenCliente(ctx context.Context, co *types.CustomerOrder) error {
|
|
// save orden cliente
|
|
return s.db.Save(co).Error
|
|
}
|
|
|
|
// SalvarOrdenProduccion implements Storager.
|
|
func (s *storage) SalvarOrdenProduccion(ctx context.Context, po *types.ProductionOrder) error {
|
|
// save orden produccion
|
|
return s.db.Save(po).Error
|
|
}
|
|
|
|
func AutoMigrate(db *gorm.DB) error {
|
|
return db.AutoMigrate(
|
|
&types.Normas{},
|
|
&types.ProductionOrder{},
|
|
&types.CustomerOrder{},
|
|
&types.Zebra{},
|
|
&types.Paquete{},
|
|
&types.Label{},
|
|
)
|
|
}
|
|
|
|
func NewStorage(log logger.LoggerAdapter) (Storager, error) {
|
|
// create database connection
|
|
db, err := gorm.Open(sqlite.Open("db.sqlite"), &gorm.Config{})
|
|
// auto migrate database
|
|
if err := AutoMigrate(db); err != nil {
|
|
return nil, err
|
|
}
|
|
return &storage{
|
|
db: db,
|
|
mux: &sync.RWMutex{},
|
|
log: log,
|
|
}, err
|
|
}
|