mirror of
https://gitlab.com/mbugroup/lti-api.git
synced 2026-05-23 06:45:43 +00:00
Merge branch 'dev/ragil-before-sso' of https://gitlab.com/mbugroup/lti-api into dev/teguh
This commit is contained in:
+70
@@ -2,6 +2,7 @@ package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"gitlab.com/mbugroup/lti-api.git/internal/common/repository"
|
||||
@@ -25,6 +26,8 @@ type ProductWarehouseRepository interface {
|
||||
AdjustQuantities(ctx context.Context, deltas map[uint]float64, modifier func(*gorm.DB) *gorm.DB) error
|
||||
GetDetailByID(ctx context.Context, id uint) (*entity.ProductWarehouse, error)
|
||||
IdExists(ctx context.Context, id uint) (bool, error)
|
||||
CleanupEmpty(ctx context.Context, affected map[uint]struct{}) error
|
||||
EnsureProductWarehouse(ctx context.Context, productID, warehouseID uint, createdBy uint64) (uint, error)
|
||||
}
|
||||
|
||||
type ProductWarehouseRepositoryImpl struct {
|
||||
@@ -155,6 +158,73 @@ func (r *ProductWarehouseRepositoryImpl) AdjustQuantities(ctx context.Context, d
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ProductWarehouseRepositoryImpl) CleanupEmpty(ctx context.Context, affected map[uint]struct{}) error {
|
||||
if len(affected) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
ids := make([]uint, 0, len(affected))
|
||||
for id := range affected {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
|
||||
var emptyIDs []uint
|
||||
if err := r.DB().WithContext(ctx).
|
||||
Model(&entity.ProductWarehouse{}).
|
||||
Where("id IN ? AND COALESCE(quantity,0) <= 0", ids).
|
||||
Pluck("id", &emptyIDs).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if len(emptyIDs) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := r.DB().WithContext(ctx).
|
||||
Model(&entity.PurchaseItem{}).
|
||||
Where("product_warehouse_id IN ?", emptyIDs).
|
||||
Update("product_warehouse_id", nil).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := r.DB().WithContext(ctx).
|
||||
Where("id IN ?", emptyIDs).
|
||||
Delete(&entity.ProductWarehouse{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ProductWarehouseRepositoryImpl) EnsureProductWarehouse(
|
||||
ctx context.Context,
|
||||
productID uint,
|
||||
warehouseID uint,
|
||||
createdBy uint64,
|
||||
) (uint, error) {
|
||||
record, err := r.GetProductWarehouseByProductAndWarehouseID(ctx, productID, warehouseID)
|
||||
if err == nil {
|
||||
return record.Id, nil
|
||||
}
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
entity := &entity.ProductWarehouse{
|
||||
ProductId: productID,
|
||||
WarehouseId: warehouseID,
|
||||
Quantity: 0,
|
||||
CreatedBy: uint(createdBy),
|
||||
}
|
||||
if entity.CreatedBy == 0 {
|
||||
entity.CreatedBy = 1
|
||||
}
|
||||
|
||||
if err := r.CreateOne(ctx, entity, nil); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return entity.Id, nil
|
||||
}
|
||||
|
||||
func (r *ProductWarehouseRepositoryImpl) GetDetailByID(ctx context.Context, id uint) (*entity.ProductWarehouse, error) {
|
||||
var productWarehouse entity.ProductWarehouse
|
||||
err := r.DB().WithContext(ctx).
|
||||
|
||||
Reference in New Issue
Block a user