mirror of
https://github.com/serty2005/rmser.git
synced 2026-02-05 03:12:34 -06:00
added front - react+ts
ocr improved
This commit is contained in:
@@ -16,22 +16,53 @@ func NewRepository(db *gorm.DB) catalog.Repository {
|
||||
return &pgRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *pgRepository) SaveProducts(products []catalog.Product) error {
|
||||
// Сортировка (родители -> дети)
|
||||
sorted := sortProductsByHierarchy(products)
|
||||
func (r *pgRepository) SaveMeasureUnits(units []catalog.MeasureUnit) error {
|
||||
if len(units) == 0 {
|
||||
return nil
|
||||
}
|
||||
return r.db.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "id"}},
|
||||
UpdateAll: true,
|
||||
}).CreateInBatches(sorted, 100).Error
|
||||
}).CreateInBatches(units, 100).Error
|
||||
}
|
||||
|
||||
func (r *pgRepository) SaveProducts(products []catalog.Product) error {
|
||||
sorted := sortProductsByHierarchy(products)
|
||||
return r.db.Transaction(func(tx *gorm.DB) error {
|
||||
// 1. Сохраняем продукты (без контейнеров, чтобы ускорить и не дублировать)
|
||||
if err := tx.Omit("Containers").Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "id"}},
|
||||
UpdateAll: true,
|
||||
}).CreateInBatches(sorted, 100).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 2. Собираем все контейнеры в один слайс
|
||||
var allContainers []catalog.ProductContainer
|
||||
for _, p := range products {
|
||||
allContainers = append(allContainers, p.Containers...)
|
||||
}
|
||||
|
||||
// 3. Сохраняем контейнеры
|
||||
if len(allContainers) > 0 {
|
||||
if err := tx.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "id"}},
|
||||
UpdateAll: true,
|
||||
}).CreateInBatches(allContainers, 100).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (r *pgRepository) GetAll() ([]catalog.Product, error) {
|
||||
var products []catalog.Product
|
||||
err := r.db.Find(&products).Error
|
||||
err := r.db.Preload("MainUnit").Find(&products).Error
|
||||
return products, err
|
||||
}
|
||||
|
||||
// Вспомогательная функция сортировки
|
||||
// Вспомогательная функция сортировки (оставляем как была)
|
||||
func sortProductsByHierarchy(products []catalog.Product) []catalog.Product {
|
||||
if len(products) == 0 {
|
||||
return products
|
||||
@@ -73,11 +104,14 @@ func sortProductsByHierarchy(products []catalog.Product) []catalog.Product {
|
||||
return result
|
||||
}
|
||||
|
||||
// GetActiveGoods возвращает только активные товары (не удаленные, тип GOODS)
|
||||
// GetActiveGoods возвращает только активные товары c подгруженной единицей измерения
|
||||
// GetActiveGoods оптимизирован: подгружаем Units и Containers
|
||||
func (r *pgRepository) GetActiveGoods() ([]catalog.Product, error) {
|
||||
var products []catalog.Product
|
||||
// iikoRMS: GOODS - товары, PREPARED - заготовки (иногда их тоже покупают)
|
||||
err := r.db.Where("is_deleted = ? AND type IN ?", false, []string{"GOODS"}).
|
||||
err := r.db.
|
||||
Preload("MainUnit").
|
||||
Preload("Containers"). // <-- Подгружаем фасовки
|
||||
Where("is_deleted = ? AND type IN ?", false, []string{"GOODS"}).
|
||||
Order("name ASC").
|
||||
Find(&products).Error
|
||||
return products, err
|
||||
|
||||
@@ -2,6 +2,7 @@ package ocr
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
@@ -9,6 +10,7 @@ import (
|
||||
"rmser/internal/domain/ocr"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
type pgRepository struct {
|
||||
@@ -19,39 +21,88 @@ func NewRepository(db *gorm.DB) ocr.Repository {
|
||||
return &pgRepository{db: db}
|
||||
}
|
||||
|
||||
func (r *pgRepository) SaveMatch(rawName string, productID uuid.UUID) error {
|
||||
func (r *pgRepository) SaveMatch(rawName string, productID uuid.UUID, quantity decimal.Decimal, containerID *uuid.UUID) error {
|
||||
normalized := strings.ToLower(strings.TrimSpace(rawName))
|
||||
match := ocr.ProductMatch{
|
||||
RawName: normalized,
|
||||
ProductID: productID,
|
||||
RawName: normalized,
|
||||
ProductID: productID,
|
||||
Quantity: quantity,
|
||||
ContainerID: containerID,
|
||||
}
|
||||
|
||||
// Upsert: если такая строка уже была, обновляем ссылку на товар
|
||||
return r.db.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "raw_name"}},
|
||||
DoUpdates: clause.AssignmentColumns([]string{"product_id", "updated_at"}),
|
||||
}).Create(&match).Error
|
||||
return r.db.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "raw_name"}},
|
||||
DoUpdates: clause.AssignmentColumns([]string{"product_id", "quantity", "container_id", "updated_at"}),
|
||||
}).Create(&match).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := tx.Where("raw_name = ?", normalized).Delete(&ocr.UnmatchedItem{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (r *pgRepository) FindMatch(rawName string) (*uuid.UUID, error) {
|
||||
func (r *pgRepository) FindMatch(rawName string) (*ocr.ProductMatch, error) {
|
||||
normalized := strings.ToLower(strings.TrimSpace(rawName))
|
||||
var match ocr.ProductMatch
|
||||
|
||||
err := r.db.Where("raw_name = ?", normalized).First(&match).Error
|
||||
// Preload Container на случай, если нам сразу нужна инфа
|
||||
err := r.db.Preload("Container").Where("raw_name = ?", normalized).First(&match).Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &match.ProductID, nil
|
||||
return &match, nil
|
||||
}
|
||||
|
||||
func (r *pgRepository) GetAllMatches() ([]ocr.ProductMatch, error) {
|
||||
var matches []ocr.ProductMatch
|
||||
// Preload("Product") загружает связанную сущность товара,
|
||||
// чтобы мы видели не только ID, но и название товара из каталога.
|
||||
err := r.db.Preload("Product").Order("updated_at DESC").Find(&matches).Error
|
||||
// Подгружаем Товар, Единицу и Фасовку
|
||||
err := r.db.
|
||||
Preload("Product").
|
||||
Preload("Product.MainUnit").
|
||||
Preload("Container").
|
||||
Order("updated_at DESC").
|
||||
Find(&matches).Error
|
||||
return matches, err
|
||||
}
|
||||
|
||||
// UpsertUnmatched увеличивает счетчик встречаемости
|
||||
func (r *pgRepository) UpsertUnmatched(rawName string) error {
|
||||
normalized := strings.ToLower(strings.TrimSpace(rawName))
|
||||
if normalized == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Используем сырой SQL или GORM upsert expression
|
||||
// PostgreSQL: INSERT ... ON CONFLICT DO UPDATE SET count = count + 1
|
||||
item := ocr.UnmatchedItem{
|
||||
RawName: normalized,
|
||||
Count: 1,
|
||||
LastSeen: time.Now(),
|
||||
}
|
||||
|
||||
return r.db.Clauses(clause.OnConflict{
|
||||
Columns: []clause.Column{{Name: "raw_name"}},
|
||||
DoUpdates: clause.Assignments(map[string]interface{}{
|
||||
"count": gorm.Expr("unmatched_items.count + 1"),
|
||||
"last_seen": time.Now(),
|
||||
}),
|
||||
}).Create(&item).Error
|
||||
}
|
||||
|
||||
func (r *pgRepository) GetTopUnmatched(limit int) ([]ocr.UnmatchedItem, error) {
|
||||
var items []ocr.UnmatchedItem
|
||||
err := r.db.Order("count DESC, last_seen DESC").Limit(limit).Find(&items).Error
|
||||
return items, err
|
||||
}
|
||||
|
||||
func (r *pgRepository) DeleteUnmatched(rawName string) error {
|
||||
normalized := strings.ToLower(strings.TrimSpace(rawName))
|
||||
return r.db.Where("raw_name = ?", normalized).Delete(&ocr.UnmatchedItem{}).Error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user