171 lines
5.6 KiB
Go
171 lines
5.6 KiB
Go
package dao
|
|
|
|
import (
|
|
"gorm.io/gorm"
|
|
"hospital-admin-api/api/model"
|
|
"hospital-admin-api/api/requests"
|
|
"hospital-admin-api/global"
|
|
)
|
|
|
|
type DoctorPharmacyDao struct{}
|
|
|
|
// GetDoctorPharmacyById 获取绑定关系-主键id
|
|
func (r *DoctorPharmacyDao) GetDoctorPharmacyById(doctorPharmacyId int64) (m *model.DoctorPharmacy, err error) {
|
|
err = global.Db.Where("doctor_pharmacy_id = ?", doctorPharmacyId).First(&m).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// GetDoctorPharmacyListByDoctorId 获取医生绑定的药房列表(带预加载药房详情)
|
|
func (r *DoctorPharmacyDao) GetDoctorPharmacyListByDoctorId(doctorId int64) (m []*model.DoctorPharmacy, err error) {
|
|
err = global.Db.Preload("Pharmacy").Where("doctor_id = ? AND status = 1", doctorId).Order("is_default desc, created_at desc").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
|
|
}
|
|
|
|
// GetDoctorPharmacyByDoctorAndPharmacy 获取某医生与某药房的绑定关系
|
|
func (r *DoctorPharmacyDao) GetDoctorPharmacyByDoctorAndPharmacy(doctorId, pharmacyId int64) (m *model.DoctorPharmacy, err error) {
|
|
err = global.Db.Where("doctor_id = ? AND pharmacy_id = ?", doctorId, pharmacyId).First(&m).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// AddDoctorPharmacy 新增绑定
|
|
func (r *DoctorPharmacyDao) AddDoctorPharmacy(tx *gorm.DB, m *model.DoctorPharmacy) (*model.DoctorPharmacy, error) {
|
|
if err := tx.Create(m).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|
|
|
|
// DeleteDoctorPharmacy 删除绑定记录
|
|
func (r *DoctorPharmacyDao) DeleteDoctorPharmacy(tx *gorm.DB, maps interface{}) error {
|
|
err := tx.Where(maps).Delete(&model.DoctorPharmacy{}).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DeleteDoctorPharmacyById 根据绑定关系主键id删除
|
|
func (r *DoctorPharmacyDao) DeleteDoctorPharmacyById(tx *gorm.DB, doctorPharmacyId int64) error {
|
|
err := tx.Where("doctor_pharmacy_id = ?", doctorPharmacyId).Delete(&model.DoctorPharmacy{}).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DeleteDoctorPharmacyByDoctorAndPharmacy 解绑指定医生和指定药房
|
|
func (r *DoctorPharmacyDao) DeleteDoctorPharmacyByDoctorAndPharmacy(tx *gorm.DB, doctorId, pharmacyId int64) error {
|
|
err := tx.Where("doctor_id = ? AND pharmacy_id = ?", doctorId, pharmacyId).Delete(&model.DoctorPharmacy{}).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ResetDoctorDefaultPharmacy 重置医生的默认药房(设为非默认)
|
|
func (r *DoctorPharmacyDao) ResetDoctorDefaultPharmacy(tx *gorm.DB, doctorId int64) error {
|
|
err := tx.Model(&model.DoctorPharmacy{}).Where("doctor_id = ?", doctorId).Update("is_default", 0).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SetDefaultPharmacy 设指定药房为默认
|
|
func (r *DoctorPharmacyDao) SetDefaultPharmacy(tx *gorm.DB, doctorId, pharmacyId int64) error {
|
|
err := tx.Model(&model.DoctorPharmacy{}).Where("doctor_id = ? AND pharmacy_id = ?", doctorId, pharmacyId).Update("is_default", 1).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SetDefaultPharmacyById 根据主键设为默认
|
|
func (r *DoctorPharmacyDao) SetDefaultPharmacyById(tx *gorm.DB, doctorPharmacyId int64) error {
|
|
err := tx.Model(&model.DoctorPharmacy{}).Where("doctor_pharmacy_id = ?", doctorPharmacyId).Update("is_default", 1).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetPharmacyDoctorPageSearch 获取药房绑定的医生列表-分页
|
|
func (r *DoctorPharmacyDao) GetPharmacyDoctorPageSearch(pharmacyId int64, req requests.GetPharmacyDoctorPage, page, pageSize int) (m []*model.DoctorPharmacy, total int64, err error) {
|
|
var totalRecords int64
|
|
|
|
query := global.Db.Model(&model.DoctorPharmacy{}).
|
|
Where("gdxz_doctor_pharmacy.pharmacy_id = ? AND gdxz_doctor_pharmacy.status = 1", pharmacyId)
|
|
|
|
// 如果有医生姓名或手机号筛选,做表连接
|
|
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+"%")
|
|
}
|
|
}
|
|
|
|
if err := query.Count(&totalRecords).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
err = query.Order("gdxz_doctor_pharmacy.is_default desc, gdxz_doctor_pharmacy.created_at desc").
|
|
Preload("UserDoctor.User").
|
|
Preload("UserDoctor.Hospital").
|
|
Scopes(model.Paginate(page, pageSize)).
|
|
Find(&m).Error
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
return m, totalRecords, nil
|
|
}
|
|
|
|
// GetPharmacyDoctorListByPharmacyId 获取药房绑定的所有医生列表
|
|
func (r *DoctorPharmacyDao) GetPharmacyDoctorListByPharmacyId(pharmacyId int64) (m []*model.DoctorPharmacy, err error) {
|
|
err = global.Db.Preload("UserDoctor.User").
|
|
Preload("UserDoctor.Hospital").
|
|
Where("pharmacy_id = ? AND status = 1", pharmacyId).
|
|
Order("is_default desc, created_at desc").
|
|
Find(&m).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return m, nil
|
|
}
|