1、增加导出药房关联医生列表导出
2、检测到有绑定/默认医生时直接报错,强制要求管理员先手动处理;
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"hospital-admin-api/api/model"
|
||||
"hospital-admin-api/api/requests"
|
||||
@@ -168,3 +172,94 @@ func (r *DoctorPharmacyDao) GetPharmacyDoctorListByPharmacyId(pharmacyId int64)
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// GetDoctorPharmacyCountsByPharmacyId 获取药房绑定的医生数量(有效绑定数、设为默认药房数)
|
||||
func (r *DoctorPharmacyDao) GetDoctorPharmacyCountsByPharmacyId(pharmacyId int64) (totalCount int64, defaultCount int64, err error) {
|
||||
err = global.Db.Model(&model.DoctorPharmacy{}).
|
||||
Where("pharmacy_id = ? AND status = 1", pharmacyId).
|
||||
Count(&totalCount).Error
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
|
||||
err = global.Db.Model(&model.DoctorPharmacy{}).
|
||||
Where("pharmacy_id = ? AND status = 1 AND is_default = 1", pharmacyId).
|
||||
Count(&defaultCount).Error
|
||||
if err != nil {
|
||||
return 0, 0, err
|
||||
}
|
||||
|
||||
return totalCount, defaultCount, nil
|
||||
}
|
||||
|
||||
// GetPharmacyDoctorExportListSearch 获取药房关联医生列表-导出
|
||||
func (r *DoctorPharmacyDao) GetPharmacyDoctorExportListSearch(req requests.PharmacyDoctorExportList) (m []*model.DoctorPharmacy, err error) {
|
||||
query := global.Db.Model(&model.DoctorPharmacy{}).
|
||||
Where("gdxz_doctor_pharmacy.status = 1")
|
||||
|
||||
// 药房id过滤
|
||||
if req.PharmacyId != "" {
|
||||
pharmacyId, _ := strconv.ParseInt(req.PharmacyId, 10, 64)
|
||||
if pharmacyId >= 0 {
|
||||
query = query.Where("gdxz_doctor_pharmacy.pharmacy_id = ?", pharmacyId)
|
||||
}
|
||||
}
|
||||
|
||||
// 1:当前搜索数据
|
||||
if req.Type == 1 {
|
||||
needJoinDoctor := req.DoctorName != "" || req.Mobile != ""
|
||||
if needJoinDoctor {
|
||||
query = query.Joins("JOIN gdxz_user_doctor ON gdxz_user_doctor.doctor_id = gdxz_doctor_pharmacy.doctor_id")
|
||||
if req.DoctorName != "" {
|
||||
query = query.Where("gdxz_user_doctor.user_name LIKE ?", "%"+req.DoctorName+"%")
|
||||
}
|
||||
if req.Mobile != "" {
|
||||
query = query.Joins("JOIN gdxz_user ON gdxz_user.user_id = gdxz_user_doctor.user_id").
|
||||
Where("gdxz_user.mobile LIKE ?", "%"+req.Mobile+"%")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 2:当前选中数据
|
||||
if req.Type == 2 {
|
||||
if req.Id == "" {
|
||||
return nil, errors.New("未提供需导出数据编号")
|
||||
}
|
||||
id := strings.Split(req.Id, ",")
|
||||
query = query.Where("gdxz_doctor_pharmacy.doctor_pharmacy_id IN (?) OR gdxz_doctor_pharmacy.doctor_id IN (?)", id, id)
|
||||
}
|
||||
|
||||
// 3:全部数据(无额外搜索条件)
|
||||
|
||||
err = query.Order("gdxz_doctor_pharmacy.is_default desc, gdxz_doctor_pharmacy.created_at desc").
|
||||
Preload("Pharmacy").
|
||||
Preload("UserDoctor.User").
|
||||
Preload("UserDoctor.Hospital").
|
||||
Find(&m).Error
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user