药品列表增加药房信息展示及筛选

This commit is contained in:
haomingming
2026-09-08 14:20:13 +08:00
parent f6f6845692
commit d95e2bc0ec
5 changed files with 149 additions and 24 deletions
+121
View File
@@ -1,6 +1,8 @@
package dao
import (
"strconv"
"gorm.io/gorm"
"hospital-admin-api/api/model"
"hospital-admin-api/api/requests"
@@ -16,6 +18,9 @@ func (r *ProductPlatformDao) GetProductPlatformById(productPlatformId int64) (m
if err != nil {
return nil, err
}
if m != nil {
fillPlatformPharmacy([]*model.ProductPlatform{m})
}
return m, nil
}
@@ -25,6 +30,7 @@ func (r *ProductPlatformDao) GetProductPlatformList(maps interface{}) (m []*mode
if err != nil {
return nil, err
}
fillPlatformPharmacy(m)
return m, nil
}
@@ -90,6 +96,32 @@ func (r *ProductPlatformDao) GetPlatformProductPageSearch(req requests.GetPlatfo
query = query.Where("product_pharmacy_code LIKE ?", "%"+req.ProductPharmacyCode+"%")
}
// 所属药房ID
if req.PharmacyId != "" {
pId, err := strconv.ParseInt(req.PharmacyId, 10, 64)
if err == nil {
var ph model.Pharmacy
var dbErr error
if pId == 0 {
dbErr = global.Db.Where("pharmacy_id = 0").First(&ph).Error
} else {
dbErr = global.Db.Where("pharmacy_id = ? AND status != 2", pId).First(&ph).Error
}
if dbErr == nil {
if ph.PharmacyCode != "" {
query = query.Where("pharmacy_code = ?", ph.PharmacyCode)
} else {
query = query.Where("(pharmacy_code = '' OR pharmacy_code IS NULL)")
}
} else if pId == 0 {
// 兜底:若数据库暂无 pharmacy_id = 0 的记录,仍匹配 pharmacy_code 为空的默认商品
query = query.Where("(pharmacy_code = '' OR pharmacy_code IS NULL)")
} else {
query = query.Where("1 = 0")
}
}
}
// 第三方药房编码
if req.PharmacyCode != "" {
query = query.Where("pharmacy_code LIKE ?", "%"+req.PharmacyCode+"%")
@@ -117,6 +149,7 @@ func (r *ProductPlatformDao) GetPlatformProductPageSearch(req requests.GetPlatfo
if err != nil {
return nil, 0, err
}
fillPlatformPharmacy(m)
return m, totalRecords, nil
}
@@ -145,6 +178,31 @@ func (r *ProductPlatformDao) GetPlatformProductListSearch(req requests.GetPlatfo
query = query.Where("product_pharmacy_code LIKE ?", "%"+req.ProductPharmacyCode+"%")
}
// 所属药房ID
if req.PharmacyId != "" {
pId, err := strconv.ParseInt(req.PharmacyId, 10, 64)
if err == nil {
var ph model.Pharmacy
var dbErr error
if pId == 0 {
dbErr = global.Db.Where("pharmacy_id = 0").First(&ph).Error
} else {
dbErr = global.Db.Where("pharmacy_id = ? AND status != 2", pId).First(&ph).Error
}
if dbErr == nil {
if ph.PharmacyCode != "" {
query = query.Where("pharmacy_code = ?", ph.PharmacyCode)
} else {
query = query.Where("(pharmacy_code = '' OR pharmacy_code IS NULL)")
}
} else if pId == 0 {
query = query.Where("(pharmacy_code = '' OR pharmacy_code IS NULL)")
} else {
query = query.Where("1 = 0")
}
}
}
// 第三方药房编码
if req.PharmacyCode != "" {
query = query.Where("pharmacy_code LIKE ?", "%"+req.PharmacyCode+"%")
@@ -167,5 +225,68 @@ func (r *ProductPlatformDao) GetPlatformProductListSearch(req requests.GetPlatfo
if err != nil {
return nil, err
}
fillPlatformPharmacy(m)
return m, nil
}
// fillPlatformPharmacy 批量补全平台商品的所属药房实体
func fillPlatformPharmacy(list []*model.ProductPlatform) {
if len(list) == 0 {
return
}
// 1. 收集非空的 pharmacy_code
var codes []string
codeMap := make(map[string]bool)
for _, item := range list {
if item.PharmacyCode != "" && !codeMap[item.PharmacyCode] {
codeMap[item.PharmacyCode] = true
codes = append(codes, item.PharmacyCode)
}
}
// 2. 批量查出对应药房
pharmacyByCode := make(map[string]*model.Pharmacy)
if len(codes) > 0 {
var pharmacies []*model.Pharmacy
if err := global.Db.Where("pharmacy_code IN ? AND status != 2", codes).Find(&pharmacies).Error; err == nil {
for _, ph := range pharmacies {
pharmacyByCode[ph.PharmacyCode] = ph
}
}
}
// 3. 兜底获取默认药房 (pharmacy_id = 0)
var zeroPharmacy *model.Pharmacy
hasLoadedZero := false
getZeroPharmacy := func() *model.Pharmacy {
if !hasLoadedZero {
hasLoadedZero = true
var ph model.Pharmacy
if err := global.Db.Where("pharmacy_id = 0").First(&ph).Error; err == nil {
zeroPharmacy = &ph
}
}
return zeroPharmacy
}
// 4. 回填
for _, item := range list {
if item.PharmacyCode != "" {
if ph, ok := pharmacyByCode[item.PharmacyCode]; ok {
item.Pharmacy = ph
} else {
zPh := getZeroPharmacy()
if zPh != nil && zPh.PharmacyCode == item.PharmacyCode {
item.Pharmacy = zPh
}
}
} else {
// pharmacy_code 为空,默认归属到 pharmacy_id = 0
zPh := getZeroPharmacy()
if zPh != nil {
item.Pharmacy = zPh
}
}
}
}