This commit is contained in:
haomingming
2026-09-07 14:44:54 +08:00
parent dc9dee6c98
commit 0cff5b3f8d
5 changed files with 298 additions and 59 deletions
+70 -2
View File
@@ -95,6 +95,13 @@ func (r *DoctorPharmacyService) BindDoctorPharmacies(doctorId int64, req request
// AddDoctorPharmacy 新增单个药房绑定
func (r *DoctorPharmacyService) AddDoctorPharmacy(doctorId int64, req requests.AddDoctorPharmacy) (bool, error) {
if doctorId == 0 && req.DoctorId != "" {
doctorId, _ = strconv.ParseInt(req.DoctorId, 10, 64)
}
if doctorId == 0 {
return false, errors.New("医生id无效")
}
pharmacyId, err := strconv.ParseInt(req.PharmacyId, 10, 64)
if err != nil || pharmacyId == 0 {
return false, errors.New("药房id无效")
@@ -157,7 +164,39 @@ func (r *DoctorPharmacyService) AddDoctorPharmacy(doctorId int64, req requests.A
return true, nil
}
// UnbindDoctorPharmacy 解绑单个药房
// UnbindDoctorPharmacyById 根据绑定记录ID解绑
func (r *DoctorPharmacyService) UnbindDoctorPharmacyById(doctorPharmacyId int64) (bool, error) {
doctorPharmacyDao := dao.DoctorPharmacyDao{}
exist, err := doctorPharmacyDao.GetDoctorPharmacyById(doctorPharmacyId)
if err != nil || exist == nil {
return false, errors.New("未找到绑定记录")
}
tx := global.Db.Begin()
defer func() {
if rec := recover(); rec != nil {
tx.Rollback()
}
}()
if err := doctorPharmacyDao.DeleteDoctorPharmacyById(tx, doctorPharmacyId); err != nil {
tx.Rollback()
return false, errors.New("解绑失败")
}
// 如果解绑的是默认药房,尝试将剩余的第一个药房设为默认
if exist.IsDefault == 1 {
list, _ := doctorPharmacyDao.GetDoctorPharmacyListByDoctorId(exist.DoctorId)
if len(list) > 0 {
_ = doctorPharmacyDao.SetDefaultPharmacy(tx, exist.DoctorId, list[0].PharmacyId)
}
}
tx.Commit()
return true, nil
}
// UnbindDoctorPharmacy 解绑单个药房(支持通过 doctor_pharmacy_id 或 (doctor_id, pharmacy_id))
func (r *DoctorPharmacyService) UnbindDoctorPharmacy(doctorId, pharmacyId int64) (bool, error) {
doctorPharmacyDao := dao.DoctorPharmacyDao{}
exist, err := doctorPharmacyDao.GetDoctorPharmacyByDoctorAndPharmacy(doctorId, pharmacyId)
@@ -189,7 +228,36 @@ func (r *DoctorPharmacyService) UnbindDoctorPharmacy(doctorId, pharmacyId int64)
return true, nil
}
// SetDefaultPharmacy 设为默认药房
// SetDefaultPharmacyById 根据绑定关系主键设为默认
func (r *DoctorPharmacyService) SetDefaultPharmacyById(doctorPharmacyId int64) (bool, error) {
doctorPharmacyDao := dao.DoctorPharmacyDao{}
exist, err := doctorPharmacyDao.GetDoctorPharmacyById(doctorPharmacyId)
if err != nil || exist == nil {
return false, errors.New("该药房未绑定,无法设为默认")
}
tx := global.Db.Begin()
defer func() {
if rec := recover(); rec != nil {
tx.Rollback()
}
}()
if err := doctorPharmacyDao.ResetDoctorDefaultPharmacy(tx, exist.DoctorId); err != nil {
tx.Rollback()
return false, errors.New("重置默认药房状态失败")
}
if err := doctorPharmacyDao.SetDefaultPharmacyById(tx, doctorPharmacyId); err != nil {
tx.Rollback()
return false, errors.New("设置默认药房失败")
}
tx.Commit()
return true, nil
}
// SetDefaultPharmacy 设为默认药房(通过 doctor_id 和 pharmacy_id)
func (r *DoctorPharmacyService) SetDefaultPharmacy(doctorId, pharmacyId int64) (bool, error) {
doctorPharmacyDao := dao.DoctorPharmacyDao{}
exist, err := doctorPharmacyDao.GetDoctorPharmacyByDoctorAndPharmacy(doctorId, pharmacyId)