293 lines
7.8 KiB
Go
293 lines
7.8 KiB
Go
package dao
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"gorm.io/gorm"
|
|
"hospital-admin-api/api/model"
|
|
"hospital-admin-api/api/requests"
|
|
"hospital-admin-api/global"
|
|
)
|
|
|
|
type ProductPlatformDao struct {
|
|
}
|
|
|
|
// GetProductPlatformById 获取平台商品数据-平台商品id
|
|
func (r *ProductPlatformDao) GetProductPlatformById(productPlatformId int64) (m *model.ProductPlatform, err error) {
|
|
err = global.Db.First(&m, productPlatformId).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if m != nil {
|
|
fillPlatformPharmacy([]*model.ProductPlatform{m})
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// GetProductPlatformList 获取平台商品列表
|
|
func (r *ProductPlatformDao) GetProductPlatformList(maps interface{}) (m []*model.ProductPlatform, err error) {
|
|
err = global.Db.Where(maps).Find(&m).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
fillPlatformPharmacy(m)
|
|
return m, nil
|
|
}
|
|
|
|
// DeleteProductPlatform 删除平台商品
|
|
func (r *ProductPlatformDao) DeleteProductPlatform(tx *gorm.DB, maps interface{}) error {
|
|
err := tx.Where(maps).Delete(&model.ProductPlatform{}).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EditProductPlatform 修改平台商品
|
|
func (r *ProductPlatformDao) EditProductPlatform(tx *gorm.DB, maps interface{}, data interface{}) error {
|
|
err := tx.Model(&model.ProductPlatform{}).Where(maps).Updates(data).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// EditProductPlatformByUserId 修改平台商品-平台商品id
|
|
func (r *ProductPlatformDao) EditProductPlatformByUserId(tx *gorm.DB, productPlatformId int64, data interface{}) error {
|
|
err := tx.Model(&model.ProductPlatform{}).Where("product_platform_id = ?", productPlatformId).Updates(data).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// AddProductPlatform 新增平台商品
|
|
func (r *ProductPlatformDao) AddProductPlatform(tx *gorm.DB, model *model.ProductPlatform) (*model.ProductPlatform, error) {
|
|
if err := tx.Create(model).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return model, nil
|
|
}
|
|
|
|
// GetPlatformProductPageSearch 获取平台商品列表-分页
|
|
func (r *ProductPlatformDao) GetPlatformProductPageSearch(req requests.GetPlatformProductPage, page, pageSize int) (m []*model.ProductPlatform, total int64, err error) {
|
|
var totalRecords int64
|
|
|
|
// 构建查询条件
|
|
query := global.Db.Model(&model.ProductPlatform{})
|
|
|
|
// 商品名称
|
|
if req.ProductName != "" {
|
|
query = query.Where("product_name LIKE ?", "%"+req.ProductName+"%")
|
|
}
|
|
|
|
// 药品类型
|
|
if req.ProductType != nil {
|
|
query = query.Where("product_type = ?", req.ProductType)
|
|
}
|
|
|
|
// 处方平台编码
|
|
if req.ProductPlatformCode != "" {
|
|
query = query.Where("product_platform_code LIKE ?", "%"+req.ProductPlatformCode+"%")
|
|
}
|
|
|
|
// 药店编码
|
|
if req.ProductPharmacyCode != "" {
|
|
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+"%")
|
|
}
|
|
|
|
// 批准文号
|
|
if req.LicenseNumber != "" {
|
|
query = query.Where("license_number LIKE ?", "%"+req.LicenseNumber+"%")
|
|
}
|
|
|
|
// 生产厂家
|
|
if req.Manufacturer != "" {
|
|
query = query.Where("manufacturer LIKE ?", "%"+req.Manufacturer+"%")
|
|
}
|
|
|
|
// 排序
|
|
query = query.Order("created_at desc")
|
|
|
|
// 查询总数量
|
|
if err := query.Count(&totalRecords).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
err = query.Scopes(model.Paginate(page, pageSize)).Find(&m).Error
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
fillPlatformPharmacy(m)
|
|
return m, totalRecords, nil
|
|
}
|
|
|
|
// GetPlatformProductListSearch 获取平台商品列表
|
|
func (r *ProductPlatformDao) GetPlatformProductListSearch(req requests.GetPlatformProductList) (m []*model.ProductPlatform, err error) {
|
|
// 构建查询条件
|
|
query := global.Db.Model(&model.ProductPlatform{})
|
|
|
|
// 商品名称
|
|
if req.ProductName != "" {
|
|
query = query.Where("product_name LIKE ?", "%"+req.ProductName+"%")
|
|
}
|
|
|
|
// 药品类型
|
|
if req.ProductType != nil {
|
|
query = query.Where("product_type = ?", req.ProductType)
|
|
}
|
|
|
|
// 处方平台编码
|
|
if req.ProductPlatformCode != "" {
|
|
query = query.Where("product_platform_code LIKE ?", "%"+req.ProductPlatformCode+"%")
|
|
}
|
|
|
|
// 药店编码
|
|
if req.ProductPharmacyCode != "" {
|
|
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+"%")
|
|
}
|
|
|
|
// 批准文号
|
|
if req.LicenseNumber != "" {
|
|
query = query.Where("license_number LIKE ?", "%"+req.LicenseNumber+"%")
|
|
}
|
|
|
|
// 生产厂家
|
|
if req.Manufacturer != "" {
|
|
query = query.Where("manufacturer LIKE ?", "%"+req.Manufacturer+"%")
|
|
}
|
|
|
|
// 排序
|
|
query = query.Order("created_at desc")
|
|
|
|
err = query.Scopes(model.Paginate(1, 20)).Find(&m).Error
|
|
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
|
|
}
|
|
}
|
|
}
|
|
}
|