药品列表增加药房信息展示及筛选
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+18
-22
@@ -12,7 +12,10 @@ type ProductPlatformDto struct {
|
||||
ProductType int `json:"product_type"` // 药品类型(0:未知 1:中成药 2:西药)
|
||||
ProductPlatformCode string `json:"product_platform_code"` // 处方平台商品编码
|
||||
ProductPharmacyCode string `json:"product_pharmacy_code"` // 第三方药店商品编码
|
||||
PharmacyId string `json:"pharmacy_id"` // 所属药房ID
|
||||
PharmacyCode string `json:"pharmacy_code"` // 第三方药房编码
|
||||
PharmacyName string `json:"pharmacy_name"` // 所属药房名称
|
||||
Pharmacy *PharmacyDto `json:"pharmacy"` // 所属药房详情
|
||||
ProductSpec string `json:"product_spec"` // 商品规格
|
||||
LicenseNumber string `json:"license_number"` // 批准文号
|
||||
Manufacturer string `json:"manufacturer"` // 生产厂家
|
||||
@@ -27,14 +30,16 @@ type ProductPlatformDto struct {
|
||||
|
||||
// GetProductPlatformDto 平台商品详情
|
||||
func GetProductPlatformDto(m *model.ProductPlatform) *ProductPlatformDto {
|
||||
return &ProductPlatformDto{
|
||||
dto := &ProductPlatformDto{
|
||||
ProductPlatformId: fmt.Sprintf("%d", m.ProductPlatformId),
|
||||
ProductName: m.ProductName,
|
||||
ProductPrice: m.ProductPrice,
|
||||
ProductType: m.ProductType,
|
||||
ProductPlatformCode: m.ProductPlatformCode,
|
||||
ProductPharmacyCode: m.ProductPharmacyCode,
|
||||
PharmacyId: "0",
|
||||
PharmacyCode: m.PharmacyCode,
|
||||
PharmacyName: "",
|
||||
ProductSpec: m.ProductSpec,
|
||||
LicenseNumber: m.LicenseNumber,
|
||||
Manufacturer: m.Manufacturer,
|
||||
@@ -45,6 +50,17 @@ func GetProductPlatformDto(m *model.ProductPlatform) *ProductPlatformDto {
|
||||
CreatedAt: m.CreatedAt,
|
||||
UpdatedAt: m.UpdatedAt,
|
||||
}
|
||||
|
||||
if m.Pharmacy != nil {
|
||||
dto.Pharmacy = GetPharmacyDto(m.Pharmacy)
|
||||
dto.PharmacyId = fmt.Sprintf("%d", m.Pharmacy.PharmacyId)
|
||||
dto.PharmacyName = m.Pharmacy.PharmacyName
|
||||
if dto.PharmacyCode == "" {
|
||||
dto.PharmacyCode = m.Pharmacy.PharmacyCode
|
||||
}
|
||||
}
|
||||
|
||||
return dto
|
||||
}
|
||||
|
||||
// GetProductPlatformListDto 平台商品列表
|
||||
@@ -54,27 +70,7 @@ func GetProductPlatformListDto(m []*model.ProductPlatform) []*ProductPlatformDto
|
||||
|
||||
if len(m) > 0 {
|
||||
for i, v := range m {
|
||||
response := &ProductPlatformDto{
|
||||
ProductPlatformId: fmt.Sprintf("%d", v.ProductPlatformId),
|
||||
ProductName: v.ProductName,
|
||||
ProductPrice: v.ProductPrice,
|
||||
ProductType: v.ProductType,
|
||||
ProductPlatformCode: v.ProductPlatformCode,
|
||||
ProductPharmacyCode: v.ProductPharmacyCode,
|
||||
PharmacyCode: v.PharmacyCode,
|
||||
ProductSpec: v.ProductSpec,
|
||||
LicenseNumber: v.LicenseNumber,
|
||||
Manufacturer: v.Manufacturer,
|
||||
SingleUnit: v.SingleUnit,
|
||||
PackagingUnit: v.PackagingUnit,
|
||||
PackagingCount: v.PackagingCount,
|
||||
RetailUnit: v.RetailUnit,
|
||||
CreatedAt: v.CreatedAt,
|
||||
UpdatedAt: v.UpdatedAt,
|
||||
}
|
||||
|
||||
// 将转换后的结构体添加到新切片中
|
||||
responses[i] = response
|
||||
responses[i] = GetProductPlatformDto(v)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@ type ProductPlatform struct {
|
||||
PackagingCount string `gorm:"column:packaging_count;type:varchar(20);comment:基本包装数量" json:"packaging_count"`
|
||||
RetailUnit string `gorm:"column:retail_unit;type:varchar(20);comment:零售单位" json:"retail_unit"`
|
||||
Model
|
||||
Pharmacy *Pharmacy `gorm:"foreignKey:PharmacyCode;references:pharmacy_code" json:"pharmacy,omitempty"`
|
||||
}
|
||||
|
||||
func (m *ProductPlatform) TableName() string {
|
||||
|
||||
@@ -19,6 +19,7 @@ type GetPlatformProductPage struct {
|
||||
ProductType *int `json:"product_type" form:"product_type" label:"药品类型"` // 药品类型(0:未知 1:中成药 2:西药)
|
||||
ProductPlatformCode string `json:"product_platform_code" form:"product_platform_code" label:"处方平台编码"` // 处方平台商品编码
|
||||
ProductPharmacyCode string `json:"product_pharmacy_code" form:"product_pharmacy_code" label:"药店编码"` // 第三方药店商品编码
|
||||
PharmacyId string `json:"pharmacy_id" form:"pharmacy_id" label:"所属药房ID"` // 所属药房ID
|
||||
PharmacyCode string `json:"pharmacy_code" form:"pharmacy_code" label:"第三方药房编码"` // 第三方药房编码
|
||||
LicenseNumber string `json:"license_number" form:"license_number" label:"批准文号"` // 批准文号
|
||||
Manufacturer string `json:"manufacturer" form:"manufacturer" label:"生产厂家"` // 生产厂家
|
||||
@@ -30,6 +31,7 @@ type GetPlatformProductList struct {
|
||||
ProductType *int `json:"product_type" form:"product_type" label:"药品类型"` // 药品类型(0:未知 1:中成药 2:西药)
|
||||
ProductPlatformCode string `json:"product_platform_code" form:"product_platform_code" label:"处方平台编码"` // 处方平台商品编码
|
||||
ProductPharmacyCode string `json:"product_pharmacy_code" form:"product_pharmacy_code" label:"药店编码"` // 第三方药店商品编码
|
||||
PharmacyId string `json:"pharmacy_id" form:"pharmacy_id" label:"所属药房ID"` // 所属药房ID
|
||||
PharmacyCode string `json:"pharmacy_code" form:"pharmacy_code" label:"第三方药房编码"` // 第三方药房编码
|
||||
LicenseNumber string `json:"license_number" form:"license_number" label:"批准文号"` // 批准文号
|
||||
Manufacturer string `json:"manufacturer" form:"manufacturer" label:"生产厂家"` // 生产厂家
|
||||
|
||||
@@ -27,8 +27,13 @@ func (r *ProductService) GetProductPlatform(productPlatformId int64) (g *dto.Pro
|
||||
// 获取商品库存
|
||||
var quantity int
|
||||
|
||||
pharmacyCode := productPlatform.PharmacyCode
|
||||
if pharmacyCode == "" && productPlatform.Pharmacy != nil {
|
||||
pharmacyCode = productPlatform.Pharmacy.PharmacyCode
|
||||
}
|
||||
|
||||
ReportPreRequest := prescription.GetProdStockRequest{
|
||||
PharmacyCode: productPlatform.PharmacyCode,
|
||||
PharmacyCode: pharmacyCode,
|
||||
DrugCode: productPlatform.ProductPharmacyCode,
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user