mirror of
https://github.com/serty2005/rmser.git
synced 2026-02-04 19:02:33 -06:00
143 lines
3.7 KiB
Go
143 lines
3.7 KiB
Go
package drafts
|
|
|
|
import (
|
|
"rmser/internal/domain/drafts"
|
|
|
|
"github.com/google/uuid"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type pgRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewRepository(db *gorm.DB) drafts.Repository {
|
|
return &pgRepository{db: db}
|
|
}
|
|
|
|
func (r *pgRepository) Create(draft *drafts.DraftInvoice) error {
|
|
return r.db.Create(draft).Error
|
|
}
|
|
|
|
func (r *pgRepository) GetByID(id uuid.UUID) (*drafts.DraftInvoice, error) {
|
|
var draft drafts.DraftInvoice
|
|
err := r.db.
|
|
Preload("Items", func(db *gorm.DB) *gorm.DB {
|
|
return db.Order("draft_invoice_items.raw_name ASC")
|
|
}).
|
|
Preload("Items.Product").
|
|
Preload("Items.Product.MainUnit").
|
|
Preload("Items.Container").
|
|
Where("id = ?", id).
|
|
First(&draft).Error
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &draft, nil
|
|
}
|
|
|
|
func (r *pgRepository) GetByRMSInvoiceID(rmsInvoiceID uuid.UUID) (*drafts.DraftInvoice, error) {
|
|
var draft drafts.DraftInvoice
|
|
err := r.db.
|
|
Where("rms_invoice_id = ?", rmsInvoiceID).
|
|
First(&draft).Error
|
|
if err != nil {
|
|
if err == gorm.ErrRecordNotFound {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
return &draft, nil
|
|
}
|
|
|
|
func (r *pgRepository) Update(draft *drafts.DraftInvoice) error {
|
|
return r.db.Model(draft).Updates(map[string]interface{}{
|
|
"status": draft.Status,
|
|
"document_number": draft.DocumentNumber,
|
|
"incoming_document_number": draft.IncomingDocumentNumber,
|
|
"date_incoming": draft.DateIncoming,
|
|
"supplier_id": draft.SupplierID,
|
|
"store_id": draft.StoreID,
|
|
"comment": draft.Comment,
|
|
"rms_invoice_id": draft.RMSInvoiceID,
|
|
"rms_server_id": draft.RMSServerID,
|
|
"updated_at": gorm.Expr("NOW()"),
|
|
}).Error
|
|
}
|
|
|
|
func (r *pgRepository) CreateItems(items []drafts.DraftInvoiceItem) error {
|
|
if len(items) == 0 {
|
|
return nil
|
|
}
|
|
return r.db.CreateInBatches(items, 100).Error
|
|
}
|
|
|
|
func (r *pgRepository) CreateItem(item *drafts.DraftInvoiceItem) error {
|
|
return r.db.Create(item).Error
|
|
}
|
|
|
|
func (r *pgRepository) DeleteItem(itemID uuid.UUID) error {
|
|
return r.db.Delete(&drafts.DraftInvoiceItem{}, itemID).Error
|
|
}
|
|
|
|
// GetItemByID - новый метод
|
|
func (r *pgRepository) GetItemByID(itemID uuid.UUID) (*drafts.DraftInvoiceItem, error) {
|
|
var item drafts.DraftInvoiceItem
|
|
err := r.db.Where("id = ?", itemID).First(&item).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &item, nil
|
|
}
|
|
|
|
// UpdateItem - обновленный метод, принимает map
|
|
func (r *pgRepository) UpdateItem(itemID uuid.UUID, updates map[string]interface{}) error {
|
|
return r.db.Model(&drafts.DraftInvoiceItem{}).
|
|
Where("id = ?", itemID).
|
|
Updates(updates).Error
|
|
}
|
|
|
|
func (r *pgRepository) Delete(id uuid.UUID) error {
|
|
return r.db.Delete(&drafts.DraftInvoice{}, id).Error
|
|
}
|
|
|
|
func (r *pgRepository) GetActive(serverID uuid.UUID) ([]drafts.DraftInvoice, error) {
|
|
var list []drafts.DraftInvoice
|
|
|
|
activeStatuses := []string{
|
|
drafts.StatusProcessing,
|
|
drafts.StatusReadyToVerify,
|
|
drafts.StatusError,
|
|
drafts.StatusCanceled,
|
|
}
|
|
|
|
err := r.db.
|
|
Preload("Items").
|
|
Preload("Store").
|
|
Where("rms_server_id = ? AND status IN ?", serverID, activeStatuses).
|
|
Order("created_at DESC").
|
|
Find(&list).Error
|
|
|
|
return list, err
|
|
}
|
|
|
|
func (r *pgRepository) GetRMSInvoiceIDToPhotoURLMap(serverID uuid.UUID) (map[uuid.UUID]string, error) {
|
|
var draftsList []drafts.DraftInvoice
|
|
err := r.db.
|
|
Select("rms_invoice_id", "sender_photo_url").
|
|
Where("rms_server_id = ? AND rms_invoice_id IS NOT NULL", serverID).
|
|
Find(&draftsList).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
result := make(map[uuid.UUID]string)
|
|
for _, d := range draftsList {
|
|
if d.RMSInvoiceID != nil {
|
|
result[*d.RMSInvoiceID] = d.SenderPhotoURL
|
|
}
|
|
}
|
|
return result, nil
|
|
}
|