处理 GORM 0 的bug
This commit is contained in:
@@ -24,6 +24,28 @@ func (r *DoctorPharmacyDao) GetDoctorPharmacyListByDoctorId(doctorId int64) (m [
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// GORM 在外键为 0 时会自动跳过 Preload,因此针对未能预加载的记录进行兜底补全
|
||||
var zeroPharmacy *model.Pharmacy
|
||||
for _, dp := range m {
|
||||
if dp.Pharmacy == nil {
|
||||
if dp.PharmacyId == 0 {
|
||||
if zeroPharmacy == nil {
|
||||
var ph model.Pharmacy
|
||||
if err := global.Db.Where("pharmacy_id = 0").First(&ph).Error; err == nil {
|
||||
zeroPharmacy = &ph
|
||||
}
|
||||
}
|
||||
dp.Pharmacy = zeroPharmacy
|
||||
} else {
|
||||
var ph model.Pharmacy
|
||||
if err := global.Db.Where("pharmacy_id = ?", dp.PharmacyId).First(&ph).Error; err == nil {
|
||||
dp.Pharmacy = &ph
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return m, nil
|
||||
}
|
||||
|
||||
|
||||
+59
-6
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,6 +90,11 @@ func (r *ProductPlatformDao) GetPlatformProductPageSearch(req requests.GetPlatfo
|
||||
query = query.Where("product_pharmacy_code LIKE ?", "%"+req.ProductPharmacyCode+"%")
|
||||
}
|
||||
|
||||
// 第三方药房编码
|
||||
if req.PharmacyCode != "" {
|
||||
query = query.Where("pharmacy_code LIKE ?", "%"+req.PharmacyCode+"%")
|
||||
}
|
||||
|
||||
// 批准文号
|
||||
if req.LicenseNumber != "" {
|
||||
query = query.Where("license_number LIKE ?", "%"+req.LicenseNumber+"%")
|
||||
@@ -140,6 +145,11 @@ func (r *ProductPlatformDao) GetPlatformProductListSearch(req requests.GetPlatfo
|
||||
query = query.Where("product_pharmacy_code LIKE ?", "%"+req.ProductPharmacyCode+"%")
|
||||
}
|
||||
|
||||
// 第三方药房编码
|
||||
if req.PharmacyCode != "" {
|
||||
query = query.Where("pharmacy_code LIKE ?", "%"+req.PharmacyCode+"%")
|
||||
}
|
||||
|
||||
// 批准文号
|
||||
if req.LicenseNumber != "" {
|
||||
query = query.Where("license_number LIKE ?", "%"+req.LicenseNumber+"%")
|
||||
|
||||
Reference in New Issue
Block a user