Files
hospital-admin-api/api/service/doctorPharmacy.go
T
2026-09-07 14:07:00 +08:00

220 lines
6.0 KiB
Go

package service
import (
"errors"
"hospital-admin-api/api/dao"
"hospital-admin-api/api/dto"
"hospital-admin-api/api/model"
"hospital-admin-api/api/requests"
"hospital-admin-api/global"
"strconv"
)
type DoctorPharmacyService struct{}
// GetDoctorPharmacies 获取指定医生绑定的药房列表
func (r *DoctorPharmacyService) GetDoctorPharmacies(doctorId int64) ([]*dto.DoctorPharmacyDto, error) {
doctorPharmacyDao := dao.DoctorPharmacyDao{}
list, err := doctorPharmacyDao.GetDoctorPharmacyListByDoctorId(doctorId)
if err != nil {
return nil, err
}
return dto.GetDoctorPharmacyListDto(list), nil
}
// BindDoctorPharmacies 批量绑定医生药房(全量覆盖)
func (r *DoctorPharmacyService) BindDoctorPharmacies(doctorId int64, req requests.BindDoctorPharmacies) (bool, error) {
// 校验医生是否存在
userDoctorDao := dao.UserDoctorDao{}
doctor, err := userDoctorDao.GetUserDoctorById(doctorId)
if err != nil || doctor == nil {
return false, errors.New("医生不存在或已删除")
}
pharmacyDao := dao.PharmacyDao{}
var defaultPharmacyId int64
if req.DefaultPharmacyId != "" {
defaultPharmacyId, _ = strconv.ParseInt(req.DefaultPharmacyId, 10, 64)
}
// 开启事务
tx := global.Db.Begin()
defer func() {
if rec := recover(); rec != nil {
tx.Rollback()
}
}()
doctorPharmacyDao := dao.DoctorPharmacyDao{}
// 删除原绑定记录
if err := doctorPharmacyDao.DeleteDoctorPharmacy(tx, map[string]interface{}{"doctor_id": doctorId}); err != nil {
tx.Rollback()
return false, errors.New("清空原绑定关系失败")
}
// 遍历新增绑定
for i, v := range req.PharmacyIds {
pharmacyId, err := strconv.ParseInt(v, 10, 64)
if err != nil || pharmacyId == 0 {
continue
}
// 检查药房是否存在且正常
pharmacy, err := pharmacyDao.GetPharmacyById(pharmacyId)
if err != nil || pharmacy == nil {
tx.Rollback()
return false, errors.New("选择的药房不存在或已被禁用")
}
isDefault := 0
if defaultPharmacyId != 0 && pharmacyId == defaultPharmacyId {
isDefault = 1
} else if defaultPharmacyId == 0 && i == 0 {
// 未指定默认药房时,默认第1个为默认药房
isDefault = 1
}
dp := &model.DoctorPharmacy{
DoctorId: doctorId,
PharmacyId: pharmacyId,
IsDefault: isDefault,
Status: 1,
}
_, err = doctorPharmacyDao.AddDoctorPharmacy(tx, dp)
if err != nil {
tx.Rollback()
return false, errors.New("保存绑定关系失败: " + err.Error())
}
}
tx.Commit()
return true, nil
}
// AddDoctorPharmacy 新增单个药房绑定
func (r *DoctorPharmacyService) AddDoctorPharmacy(doctorId int64, req requests.AddDoctorPharmacy) (bool, error) {
pharmacyId, err := strconv.ParseInt(req.PharmacyId, 10, 64)
if err != nil || pharmacyId == 0 {
return false, errors.New("药房id无效")
}
// 校验药房
pharmacyDao := dao.PharmacyDao{}
pharmacy, err := pharmacyDao.GetPharmacyById(pharmacyId)
if err != nil || pharmacy == nil {
return false, errors.New("药房不存在或已禁用")
}
doctorPharmacyDao := dao.DoctorPharmacyDao{}
// 检查是否已绑定
exist, _ := doctorPharmacyDao.GetDoctorPharmacyByDoctorAndPharmacy(doctorId, pharmacyId)
if exist != nil {
return false, errors.New("该药房已绑定,请勿重复添加")
}
isDefault := 0
if req.IsDefault != nil && *req.IsDefault == 1 {
isDefault = 1
}
tx := global.Db.Begin()
defer func() {
if rec := recover(); rec != nil {
tx.Rollback()
}
}()
// 如果要设为默认,先将该医生现有的其他药房设为非默认
if isDefault == 1 {
if err := doctorPharmacyDao.ResetDoctorDefaultPharmacy(tx, doctorId); err != nil {
tx.Rollback()
return false, errors.New("重置默认药房失败")
}
} else {
// 检查当前是否已有绑定药房,若无任何药房,则当前第一个自动作为默认
existingList, _ := doctorPharmacyDao.GetDoctorPharmacyListByDoctorId(doctorId)
if len(existingList) == 0 {
isDefault = 1
}
}
dp := &model.DoctorPharmacy{
DoctorId: doctorId,
PharmacyId: pharmacyId,
IsDefault: isDefault,
Status: 1,
}
_, err = doctorPharmacyDao.AddDoctorPharmacy(tx, dp)
if err != nil {
tx.Rollback()
return false, errors.New("添加绑定失败")
}
tx.Commit()
return true, nil
}
// UnbindDoctorPharmacy 解绑单个药房
func (r *DoctorPharmacyService) UnbindDoctorPharmacy(doctorId, pharmacyId int64) (bool, error) {
doctorPharmacyDao := dao.DoctorPharmacyDao{}
exist, err := doctorPharmacyDao.GetDoctorPharmacyByDoctorAndPharmacy(doctorId, pharmacyId)
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.DeleteDoctorPharmacyByDoctorAndPharmacy(tx, doctorId, pharmacyId); err != nil {
tx.Rollback()
return false, errors.New("解绑失败")
}
// 如果解绑的是默认药房,尝试将剩余的第一个药房设为默认
if exist.IsDefault == 1 {
list, _ := doctorPharmacyDao.GetDoctorPharmacyListByDoctorId(doctorId)
if len(list) > 0 {
_ = doctorPharmacyDao.SetDefaultPharmacy(tx, doctorId, list[0].PharmacyId)
}
}
tx.Commit()
return true, nil
}
// SetDefaultPharmacy 设为默认药房
func (r *DoctorPharmacyService) SetDefaultPharmacy(doctorId, pharmacyId int64) (bool, error) {
doctorPharmacyDao := dao.DoctorPharmacyDao{}
exist, err := doctorPharmacyDao.GetDoctorPharmacyByDoctorAndPharmacy(doctorId, pharmacyId)
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, doctorId); err != nil {
tx.Rollback()
return false, errors.New("重置默认药房状态失败")
}
if err := doctorPharmacyDao.SetDefaultPharmacy(tx, doctorId, pharmacyId); err != nil {
tx.Rollback()
return false, errors.New("设置默认药房失败")
}
tx.Commit()
return true, nil
}