mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-24 23:35:43 +00:00
145 lines
4.1 KiB
Go
145 lines
4.1 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
entity "gitlab.com/mbugroup/lti-api.git/internal/entities"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type FifoStockV2RouteRule struct {
|
|
FlagGroupCode string `gorm:"column:flag_group_code"`
|
|
Lane string `gorm:"column:lane"`
|
|
FunctionCode string `gorm:"column:function_code"`
|
|
SourceTable string `gorm:"column:source_table"`
|
|
LegacyTypeKey string `gorm:"column:legacy_type_key"`
|
|
AllowPendingDefault bool `gorm:"column:allow_pending_default"`
|
|
}
|
|
|
|
func ResolveFifoStockV2RouteByProductIDAndLane(
|
|
ctx context.Context,
|
|
db *gorm.DB,
|
|
productID uint,
|
|
functionCode string,
|
|
lane FifoStockV2Lane,
|
|
) (*FifoStockV2RouteRule, error) {
|
|
rows, err := resolveFifoStockV2RoutesByProductID(ctx, db, productID, functionCode, lane)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(rows) == 0 {
|
|
return nil, nil
|
|
}
|
|
selected := rows[0]
|
|
return &selected, nil
|
|
}
|
|
|
|
func ResolveFifoStockV2RouteByProductWarehouseIDAndLane(
|
|
ctx context.Context,
|
|
db *gorm.DB,
|
|
productWarehouseID uint,
|
|
functionCode string,
|
|
lane FifoStockV2Lane,
|
|
) (*FifoStockV2RouteRule, error) {
|
|
rows, err := resolveFifoStockV2RoutesByProductWarehouseID(ctx, db, productWarehouseID, functionCode, lane)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if len(rows) == 0 {
|
|
return nil, nil
|
|
}
|
|
selected := rows[0]
|
|
return &selected, nil
|
|
}
|
|
|
|
func resolveFifoStockV2RoutesByProductID(
|
|
ctx context.Context,
|
|
db *gorm.DB,
|
|
productID uint,
|
|
functionCode string,
|
|
lane FifoStockV2Lane,
|
|
) ([]FifoStockV2RouteRule, error) {
|
|
normalizedCode := strings.ToUpper(strings.TrimSpace(functionCode))
|
|
if db == nil || productID == 0 || normalizedCode == "" {
|
|
return []FifoStockV2RouteRule{}, nil
|
|
}
|
|
|
|
query := db.WithContext(ctx).
|
|
Table("fifo_stock_v2_route_rules rr").
|
|
Select("rr.flag_group_code, rr.lane, rr.function_code, rr.source_table, rr.legacy_type_key, rr.allow_pending_default").
|
|
Joins("JOIN fifo_stock_v2_flag_groups fg ON fg.code = rr.flag_group_code AND fg.is_active = TRUE").
|
|
Where("rr.is_active = TRUE").
|
|
Where("rr.function_code = ?", normalizedCode).
|
|
Where(`
|
|
EXISTS (
|
|
SELECT 1
|
|
FROM flags f
|
|
JOIN fifo_stock_v2_flag_members fm ON fm.flag_name = f.name AND fm.is_active = TRUE
|
|
WHERE f.flagable_type = ?
|
|
AND f.flagable_id = ?
|
|
AND fm.flag_group_code = rr.flag_group_code
|
|
)
|
|
`, entity.FlagableTypeProduct, productID)
|
|
|
|
if lane == FifoStockV2LaneStockable || lane == FifoStockV2LaneUsable {
|
|
query = query.Where("rr.lane = ?", string(lane))
|
|
}
|
|
|
|
var rows []FifoStockV2RouteRule
|
|
err := query.
|
|
Order("CASE WHEN rr.source_table = 'adjustment_stocks' THEN 0 ELSE 1 END ASC").
|
|
Order("rr.id ASC").
|
|
Find(&rows).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return rows, nil
|
|
}
|
|
|
|
func resolveFifoStockV2RoutesByProductWarehouseID(
|
|
ctx context.Context,
|
|
db *gorm.DB,
|
|
productWarehouseID uint,
|
|
functionCode string,
|
|
lane FifoStockV2Lane,
|
|
) ([]FifoStockV2RouteRule, error) {
|
|
normalizedCode := strings.ToUpper(strings.TrimSpace(functionCode))
|
|
if db == nil || productWarehouseID == 0 || normalizedCode == "" {
|
|
return []FifoStockV2RouteRule{}, nil
|
|
}
|
|
|
|
query := db.WithContext(ctx).
|
|
Table("fifo_stock_v2_route_rules rr").
|
|
Select("rr.flag_group_code, rr.lane, rr.function_code, rr.source_table, rr.legacy_type_key, rr.allow_pending_default").
|
|
Joins("JOIN fifo_stock_v2_flag_groups fg ON fg.code = rr.flag_group_code AND fg.is_active = TRUE").
|
|
Where("rr.is_active = TRUE").
|
|
Where("rr.function_code = ?", normalizedCode).
|
|
Where(`
|
|
EXISTS (
|
|
SELECT 1
|
|
FROM product_warehouses pw
|
|
JOIN flags f ON f.flagable_type = ? AND f.flagable_id = pw.product_id
|
|
JOIN fifo_stock_v2_flag_members fm ON fm.flag_name = f.name AND fm.is_active = TRUE
|
|
WHERE pw.id = ?
|
|
AND fm.flag_group_code = rr.flag_group_code
|
|
)
|
|
`, entity.FlagableTypeProduct, productWarehouseID)
|
|
|
|
if lane == FifoStockV2LaneStockable || lane == FifoStockV2LaneUsable {
|
|
query = query.Where("rr.lane = ?", string(lane))
|
|
}
|
|
|
|
var rows []FifoStockV2RouteRule
|
|
err := query.
|
|
Order("CASE WHEN rr.source_table = 'adjustment_stocks' THEN 0 ELSE 1 END ASC").
|
|
Order("rr.id ASC").
|
|
Find(&rows).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return rows, nil
|
|
}
|