Facturacion/daemonService/internal/repositories/leyendasFacturaRepository.go
2025-05-17 11:36:26 -04:00

86 lines
2.2 KiB
Go

package repositories
import (
"context"
"daemonService/internal/models"
bg "daemonService/internal/models/sincronizacionDatos/bodyGeneric"
"daemonService/internal/models/sincronizacionDatos/request"
"encoding/json"
"fmt"
"time"
)
// func (s *ListaLeyendasFacturaService) guardarRegistro(solicitud models.SolicitudSincronizacionLeyendas, cuisID int, soapReq string, bodyBytes []byte, leyendas []models.LeyendaDetalle) error {
func GuardarRegistro[T any](s *models.ServiceModel, solicitud request.SolicitudSincronizacion, cuisID int, soapReq string, bodyBytes []byte, bodyGeneric T) error {
dbCtx, dbCancel := context.WithTimeout(context.Background(), 30*time.Minute)
defer dbCancel()
tx, err := s.DB.BeginTx(dbCtx, nil)
if err != nil {
return fmt.Errorf("error iniciando transacción: %v", err)
}
defer func() {
if err != nil {
tx.Rollback()
}
}()
solicitudJSON, _ := json.Marshal(solicitud)
// Creación de la respuesta utilizando la estructura genérica
respJson := bg.CustomResponse[T]{
Success: true,
Status: 200,
Message: s.MsgCustomResponse,
Data: bodyGeneric,
}
respuestaJSON, _ := json.Marshal(respJson)
_, err = tx.ExecContext(dbCtx, s.QueryInsert,
solicitud.CodigoSistema,
soapReq,
string(bodyBytes),
string(solicitudJSON),
string(respuestaJSON),
cuisID,
)
//_, err = tx.ExecContext(dbCtx, `
// INSERT INTO leyendas_factura (
// codigo,
// reqSoap,
// respSoap,
// reqJson,
// respJson,
// cuis_id,
// fecha_creacion
// )
// VALUES ($1, $2, $3, $4, $5, $6, NOW() AT TIME ZONE 'America/La_Paz')
// ON CONFLICT (codigo, cuis_id)
// DO UPDATE SET
// reqSoap = EXCLUDED.reqSoap,
// respSoap = EXCLUDED.respSoap,
// reqJson = EXCLUDED.reqJson,
// respJson = EXCLUDED.respJson,
// cuis_id = EXCLUDED.cuis_id,
// fecha_actualizacion = NOW() AT TIME ZONE 'America/La_Paz'`,
// solicitud.CodigoSistema,
// soapReq,
// string(bodyBytes),
// string(solicitudJSON),
// string(respuestaJSON),
// cuisID,
//)
if err != nil {
return fmt.Errorf("error ejecutando UPSERT: %v", err)
}
if err := tx.Commit(); err != nil {
return fmt.Errorf("error confirmando transacción: %v", err)
}
return nil
}