daShangDao_psiServer/database/product_sync.go
2026-06-17 15:29:47 +08:00

158 lines
5.3 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package database
import (
"fmt"
"psi/models"
"gorm.io/gorm"
)
// SyncProductToMainDB 将租户库的商品信息同步到主库的 product 表和 product_book 表
// aboutID: 租户ID
// product: 商品对象
// warehouseID: 仓库ID
// warehouseName: 仓库名称
// locationID: 库位ID
// locationName: 库位名称
func SyncProductToMainDB(aboutID int64, product *models.Product, warehouseID int64, warehouseName string, locationID int64, locationName string) error {
if aboutID == 0 {
return nil
}
// 1. 同步到 product 表
if err := syncToProductTable(aboutID, product, warehouseID, warehouseName, locationID, locationName); err != nil {
return fmt.Errorf("同步到product表失败: %v", err)
}
// 2. 同步到 product_book 表(如果有条码/ISBN
if product.Barcode != "" {
if err := syncToProductBookTable(aboutID, product, warehouseID, warehouseName, locationID, locationName); err != nil {
return fmt.Errorf("同步到product_book表失败: %v", err)
}
}
return nil
}
// syncToProductTable 同步到主库 product 表
func syncToProductTable(aboutID int64, product *models.Product, warehouseID int64, warehouseName string, locationID int64, locationName string) error {
var existingProduct models.Product
err := DB.Where("barcode = ? AND about_id = ? AND is_del = ?", product.Barcode, aboutID, 0).First(&existingProduct).Error
if err != nil && err == gorm.ErrRecordNotFound {
mainProduct := models.Product{
AboutId: aboutID,
WarehouseID: warehouseID,
WarehouseName: warehouseName,
LocationID: locationID,
LocationName: locationName,
CategoryID: product.CategoryID,
Name: product.Name,
Appearance: product.Appearance,
Barcode: product.Barcode,
Price: product.Price,
SalePrice: product.SalePrice,
Cost: product.Cost,
LiveImage: product.LiveImage,
Status: product.Status,
CreatedAt: product.CreatedAt,
UpdatedAt: product.UpdatedAt,
IsDel: 0,
}
if createErr := DB.Create(&mainProduct).Error; createErr != nil {
return fmt.Errorf("创建主库商品失败: %v", createErr)
}
} else if err == nil {
updateData := map[string]interface{}{
"warehouse_id": warehouseID,
"warehouse_name": warehouseName,
"location_id": locationID,
"location_name": locationName,
"name": product.Name,
"appearance": product.Appearance,
"price": product.Price,
"sale_price": product.SalePrice,
"cost": product.Cost,
"live_image": product.LiveImage,
"status": product.Status,
"updated_at": product.UpdatedAt,
}
if updateErr := DB.Model(&existingProduct).Updates(updateData).Error; updateErr != nil {
return fmt.Errorf("更新主库商品失败: %v", updateErr)
}
} else {
return fmt.Errorf("查询主库商品失败: %v", err)
}
return nil
}
// syncToProductBookTable 同步到主库 product_book 分表
func syncToProductBookTable(aboutID int64, product *models.Product, warehouseID int64, warehouseName string, locationID int64, locationName string) error {
isbn := product.Barcode
tableName := models.ProductBookTableName(isbn)
var existingBook models.ProductBook
err := DB.Table(tableName).Where("isbn = ? AND about_id = ? AND is_del = ?", isbn, aboutID, 0).First(&existingBook).Error
if err != nil && err == gorm.ErrRecordNotFound {
bookRecord := models.ProductBook{
ID: product.ID,
AboutId: aboutID,
WarehouseID: warehouseID,
WarehouseName: warehouseName,
LocationID: locationID,
LocationName: locationName,
CategoryID: product.CategoryID,
StandardProductID: product.StandardProductID,
ISBN: isbn,
Barcode: product.Barcode,
Name: product.Name,
BookName: product.Name,
Appearance: product.Appearance,
Price: product.Price,
SalePrice: product.SalePrice,
Cost: product.Cost,
LiveImage: product.LiveImage,
Status: product.Status,
CreatedAt: product.CreatedAt,
UpdatedAt: product.UpdatedAt,
IsDel: 0,
}
if createErr := DB.Table(tableName).Create(&bookRecord).Error; createErr != nil {
return fmt.Errorf("创建product_book记录失败: %v", createErr)
}
} else if err == nil {
updateData := map[string]interface{}{
"id": product.ID,
"warehouse_id": warehouseID,
"warehouse_name": warehouseName,
"location_id": locationID,
"location_name": locationName,
"category_id": product.CategoryID,
"standard_product_id": product.StandardProductID,
"name": product.Name,
"book_name": product.Name,
"appearance": product.Appearance,
"barcode": product.Barcode,
"price": product.Price,
"sale_price": product.SalePrice,
"cost": product.Cost,
"live_image": product.LiveImage,
"status": product.Status,
"updated_at": product.UpdatedAt,
}
if updateErr := DB.Table(tableName).Model(&existingBook).Updates(updateData).Error; updateErr != nil {
return fmt.Errorf("更新product_book记录失败: %v", updateErr)
}
} else {
return fmt.Errorf("查询product_book记录失败: %v", err)
}
return nil
}