处理 GORM 0 的bug

This commit is contained in:
haomingming
2026-09-08 10:55:15 +08:00
parent b74a8aa898
commit 86f3cc2fbc
11 changed files with 192 additions and 9 deletions
+59 -6
View File
@@ -15,10 +15,16 @@ type ProductDao struct {
// GetProductById 获取商品数据-商品id
func (r *ProductDao) GetProductById(productId int64) (m *model.Product, err error) {
err = global.Db.First(&m, productId).Error
err = global.Db.Preload("Pharmacy").First(&m, productId).Error
if err != nil {
return nil, err
}
if m != nil && m.Pharmacy == nil && m.PharmacyId == 0 {
var ph model.Pharmacy
if err := global.Db.Where("pharmacy_id = 0").First(&ph).Error; err == nil {
m.Pharmacy = &ph
}
}
return m, nil
}
@@ -85,7 +91,7 @@ func (r *ProductDao) GetProductPageSearch(req requests.GetProductPage, page, pag
// 库存表
query = query.Preload("ProductPlatformAmount", func(db *gorm.DB) *gorm.DB {
return db.Select("amount_id", "product_platform_id", "product_platform_code", "stock")
})
}).Preload("Pharmacy")
// 商品名称
if req.ProductName != "" {
@@ -132,6 +138,16 @@ func (r *ProductDao) GetProductPageSearch(req requests.GetProductPage, page, pag
query = query.Where("product_pharmacy_code LIKE ?", "%"+req.ProductPharmacyCode+"%")
}
// 所属药房ID
if req.PharmacyId != "" {
query = query.Where("gdxz_product.pharmacy_id = ?", req.PharmacyId)
}
// 所属药房编码
if req.PharmacyCode != "" {
query = query.Where("gdxz_product.pharmacy_code LIKE ?", "%"+req.PharmacyCode+"%")
}
// 批准文号
if req.LicenseNumber != "" {
query = query.Where("license_number LIKE ?", "%"+req.LicenseNumber+"%")
@@ -171,6 +187,7 @@ func (r *ProductDao) GetProductPageSearch(req requests.GetProductPage, page, pag
if err != nil {
return nil, 0, err
}
fillZeroPharmacy(m)
return m, totalRecords, nil
}
@@ -182,7 +199,7 @@ func (r *ProductDao) GetProductExportListSearch(req requests.ProductExportList)
// 库存表
query = query.Preload("ProductPlatformAmount", func(db *gorm.DB) *gorm.DB {
return db.Select("amount_id", "product_platform_id", "product_platform_code", "stock")
})
}).Preload("Pharmacy")
// 当前搜索数据
if req.Type == 1 {
@@ -226,6 +243,16 @@ func (r *ProductDao) GetProductExportListSearch(req requests.ProductExportList)
query = query.Where("product_pharmacy_code LIKE ?", "%"+req.ProductPharmacyCode+"%")
}
// 所属药房ID
if req.PharmacyId != "" {
query = query.Where("gdxz_product.pharmacy_id = ?", req.PharmacyId)
}
// 所属药房编码
if req.PharmacyCode != "" {
query = query.Where("gdxz_product.pharmacy_code LIKE ?", "%"+req.PharmacyCode+"%")
}
// 批准文号
if req.LicenseNumber != "" {
query = query.Where("license_number LIKE ?", "%"+req.LicenseNumber+"%")
@@ -259,7 +286,7 @@ func (r *ProductDao) GetProductExportListSearch(req requests.ProductExportList)
if err != nil {
return nil, err
}
fillZeroPharmacy(m)
return m, nil
}
@@ -271,7 +298,7 @@ func (r *ProductDao) GetProductListSearch(req requests.GetProductList) (m []*mod
// 库存表
query = query.Preload("ProductPlatformAmount", func(db *gorm.DB) *gorm.DB {
return db.Select("amount_id", "product_platform_id", "product_platform_code", "stock")
})
}).Preload("Pharmacy")
// 商品id
if req.ProductId != "" {
@@ -323,6 +350,16 @@ func (r *ProductDao) GetProductListSearch(req requests.GetProductList) (m []*mod
query = query.Where("product_pharmacy_code LIKE ?", "%"+req.ProductPharmacyCode+"%")
}
// 所属药房ID
if req.PharmacyId != "" {
query = query.Where("gdxz_product.pharmacy_id = ?", req.PharmacyId)
}
// 所属药房编码
if req.PharmacyCode != "" {
query = query.Where("gdxz_product.pharmacy_code LIKE ?", "%"+req.PharmacyCode+"%")
}
// 批准文号
if req.LicenseNumber != "" {
query = query.Where("license_number LIKE ?", "%"+req.LicenseNumber+"%")
@@ -348,6 +385,22 @@ func (r *ProductDao) GetProductListSearch(req requests.GetProductList) (m []*mod
if err != nil {
return nil, err
}
fillZeroPharmacy(m)
return m, nil
}
// fillZeroPharmacy 补全因 GORM 零值跳过预加载的 pharmacy_id = 0 药房实体
func fillZeroPharmacy(products []*model.Product) {
var zeroPharmacy *model.Pharmacy
for _, p := range products {
if p.Pharmacy == nil && p.PharmacyId == 0 {
if zeroPharmacy == nil {
var ph model.Pharmacy
if err := global.Db.Where("pharmacy_id = 0").First(&ph).Error; err == nil {
zeroPharmacy = &ph
}
}
p.Pharmacy = zeroPharmacy
}
}
}