From 338d56beda5acd473a2f59c53085e5673918a8a9 Mon Sep 17 00:00:00 2001 From: haomingming Date: Mon, 7 Sep 2026 15:21:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E8=8D=AF=E6=88=BF=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E5=8C=BB=E7=94=9F=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/controller/pharmacy.go | 50 ++++++++++++++++++++++++ api/dao/doctorPharmacy.go | 49 +++++++++++++++++++++++ api/dto/PharmacyDoctor.go | 78 +++++++++++++++++++++++++++++++++++++ api/model/doctorPharmacy.go | 3 +- api/requests/pharmacy.go | 26 ++++++++++--- api/router/router.go | 6 +++ api/service/pharmacy.go | 54 +++++++++++++++++++++++++ 7 files changed, 260 insertions(+), 6 deletions(-) create mode 100644 api/dto/PharmacyDoctor.go diff --git a/api/controller/pharmacy.go b/api/controller/pharmacy.go index 302e64d..3d725ce 100644 --- a/api/controller/pharmacy.go +++ b/api/controller/pharmacy.go @@ -232,3 +232,53 @@ func (r *Pharmacy) DeletePharmacy(c *gin.Context) { responses.Ok(c) } + +// GetPharmacyDoctorPage 获取药房绑定的医生列表-分页 +func (r *Pharmacy) GetPharmacyDoctorPage(c *gin.Context) { + req := requests.GetPharmacyDoctorPage{} + if err := c.ShouldBindJSON(&req); err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + if err := global.Validate.Struct(req); err != nil { + responses.FailWithMessage(utils.Translate(err), c) + return + } + + pharmacyService := service.PharmacyService{} + result, err := pharmacyService.GetPharmacyDoctorPage(req) + if err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + responses.OkWithData(result, c) +} + +// GetPharmacyDoctorList 获取药房绑定的医生列表 +func (r *Pharmacy) GetPharmacyDoctorList(c *gin.Context) { + id := c.Param("pharmacy_id") + if id == "" { + id = c.Param("id") + } + if id == "" { + responses.FailWithMessage("缺少药房ID参数", c) + return + } + + pharmacyId, err := strconv.ParseInt(id, 10, 64) + if err != nil { + responses.Fail(c) + return + } + + pharmacyService := service.PharmacyService{} + list, err := pharmacyService.GetPharmacyDoctorList(pharmacyId) + if err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + responses.OkWithData(list, c) +} diff --git a/api/dao/doctorPharmacy.go b/api/dao/doctorPharmacy.go index f37a28f..1abf340 100644 --- a/api/dao/doctorPharmacy.go +++ b/api/dao/doctorPharmacy.go @@ -96,3 +96,52 @@ func (r *DoctorPharmacyDao) SetDefaultPharmacyById(tx *gorm.DB, doctorPharmacyId } 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 +} diff --git a/api/dto/PharmacyDoctor.go b/api/dto/PharmacyDoctor.go new file mode 100644 index 0000000..9f4f59e --- /dev/null +++ b/api/dto/PharmacyDoctor.go @@ -0,0 +1,78 @@ +package dto + +import ( + "fmt" + "hospital-admin-api/api/model" +) + +type PharmacyDoctorDto struct { + DoctorPharmacyId string `json:"doctor_pharmacy_id"` // 绑定关系id + DoctorId string `json:"doctor_id"` // 医生id + UserId string `json:"user_id"` // 用户id + UserName string `json:"user_name"` // 医生姓名 + Avatar string `json:"avatar"` // 头像 + DoctorTitle int `json:"doctor_title"` // 职称代码 + DoctorTitleName string `json:"doctor_title_name"` // 职称名称 + DepartmentCustomId string `json:"department_custom_id"` // 科室id + DepartmentCustomName string `json:"department_custom_name"` // 科室名称 + DepartmentCustomMobile string `json:"department_custom_mobile"` // 科室电话 + HospitalId string `json:"hospital_id"` // 医院id + HospitalName string `json:"hospital_name"` // 医院名称 + Mobile string `json:"mobile"` // 手机号 + IsDefault int `json:"is_default"` // 是否为默认药房(0:否 1:是) + Status int `json:"status"` // 状态(1:正常) + BindTime model.LocalTime `json:"bind_time"` // 绑定时间 +} + +var doctorTitleMap = map[int]string{ + 1: "主任医师", + 2: "主任中医师", + 3: "副主任医师", + 4: "副主任中医师", + 5: "主治医师", + 6: "住院医师", +} + +func GetPharmacyDoctorDto(m *model.DoctorPharmacy) *PharmacyDoctorDto { + if m == nil { + return nil + } + + dto := &PharmacyDoctorDto{ + DoctorPharmacyId: fmt.Sprintf("%d", m.DoctorPharmacyId), + DoctorId: fmt.Sprintf("%d", m.DoctorId), + IsDefault: m.IsDefault, + Status: m.Status, + BindTime: m.CreatedAt, + } + + if m.UserDoctor != nil { + dto.UserId = fmt.Sprintf("%d", m.UserDoctor.UserId) + dto.UserName = m.UserDoctor.UserName + dto.Avatar = m.UserDoctor.Avatar + dto.DoctorTitle = m.UserDoctor.DoctorTitle + dto.DoctorTitleName = doctorTitleMap[m.UserDoctor.DoctorTitle] + dto.DepartmentCustomId = fmt.Sprintf("%d", m.UserDoctor.DepartmentCustomId) + dto.DepartmentCustomName = m.UserDoctor.DepartmentCustomName + dto.DepartmentCustomMobile = m.UserDoctor.DepartmentCustomMobile + dto.HospitalId = fmt.Sprintf("%d", m.UserDoctor.HospitalID) + + if m.UserDoctor.Hospital != nil { + dto.HospitalName = m.UserDoctor.Hospital.HospitalName + } + + if m.UserDoctor.User != nil { + dto.Mobile = m.UserDoctor.User.Mobile + } + } + + return dto +} + +func GetPharmacyDoctorListDto(list []*model.DoctorPharmacy) []PharmacyDoctorDto { + res := make([]PharmacyDoctorDto, len(list)) + for i, v := range list { + res[i] = *GetPharmacyDoctorDto(v) + } + return res +} diff --git a/api/model/doctorPharmacy.go b/api/model/doctorPharmacy.go index ab35604..737aa90 100644 --- a/api/model/doctorPharmacy.go +++ b/api/model/doctorPharmacy.go @@ -14,7 +14,8 @@ type DoctorPharmacy struct { IsDefault int `gorm:"column:is_default;type:tinyint(1);default:0;comment:是否默认药房(0:否 1:是)" json:"is_default"` Status int `gorm:"column:status;type:tinyint(1);default:1;comment:状态(0:禁用 1:正常)" json:"status"` Model - Pharmacy *Pharmacy `gorm:"foreignKey:PharmacyId;references:pharmacy_id" json:"pharmacy"` + Pharmacy *Pharmacy `gorm:"foreignKey:PharmacyId;references:pharmacy_id" json:"pharmacy"` + UserDoctor *UserDoctor `gorm:"foreignKey:DoctorId;references:doctor_id" json:"user_doctor"` } func (m *DoctorPharmacy) TableName() string { diff --git a/api/requests/pharmacy.go b/api/requests/pharmacy.go index 65e6372..d51d6b3 100644 --- a/api/requests/pharmacy.go +++ b/api/requests/pharmacy.go @@ -1,11 +1,27 @@ package requests type PharmacyRequest struct { - GetPharmacyList // 获取药房列表 - GetPharmacyPage // 获取药房列表-分页 - AddPharmacy // 新增药房 - PutPharmacy // 修改药房 - PutPharmacyStatus // 修改药房状态 + GetPharmacyList // 获取药房列表 + GetPharmacyPage // 获取药房列表-分页 + AddPharmacy // 新增药房 + PutPharmacy // 修改药房 + PutPharmacyStatus // 修改药房状态 + GetPharmacyDoctorPage // 药房绑定的医生列表-分页 + GetPharmacyDoctorList // 药房绑定的医生列表 +} + +// GetPharmacyDoctorPage 药房绑定的医生列表-分页 +type GetPharmacyDoctorPage struct { + PharmacyId string `json:"pharmacy_id" form:"pharmacy_id" label:"药房id" validate:"required"` + DoctorName string `json:"doctor_name" form:"doctor_name" label:"医生姓名"` + Mobile string `json:"mobile" form:"mobile" label:"手机号"` + Page int `json:"page" form:"page" label:"页码"` + PageSize int `json:"page_size" form:"page_size" label:"每页个数"` +} + +// GetPharmacyDoctorList 药房绑定的医生列表 +type GetPharmacyDoctorList struct { + PharmacyId string `json:"pharmacy_id" form:"pharmacy_id" label:"药房id"` } // GetPharmacyList 获取药房列表 diff --git a/api/router/router.go b/api/router/router.go index 61defd7..62b3914 100644 --- a/api/router/router.go +++ b/api/router/router.go @@ -937,6 +937,12 @@ func privateRouter(r *gin.Engine, api controller.Api) { // 删除药房 pharmacyGroup.DELETE("/:pharmacy_id", api.Pharmacy.DeletePharmacy) + + // 药房绑定的医生列表-分页 + pharmacyGroup.POST("/doctor/page", api.Pharmacy.GetPharmacyDoctorPage) + + // 药房绑定的医生列表 + pharmacyGroup.GET("/doctor/:pharmacy_id", api.Pharmacy.GetPharmacyDoctorList) } // 科普分类管理 diff --git a/api/service/pharmacy.go b/api/service/pharmacy.go index 800b688..c9595a5 100644 --- a/api/service/pharmacy.go +++ b/api/service/pharmacy.go @@ -9,6 +9,7 @@ import ( "hospital-admin-api/api/model" "hospital-admin-api/api/requests" "hospital-admin-api/global" + "strconv" ) type PharmacyService struct{} @@ -259,3 +260,56 @@ func (r *PharmacyService) GetPharmacy(pharmacyId int64) (*dto.PharmacyDto, error return dto.GetPharmacyDto(pharmacy), nil } + +// GetPharmacyDoctorPage 获取药房绑定的医生列表-分页 +func (r *PharmacyService) GetPharmacyDoctorPage(req requests.GetPharmacyDoctorPage) (map[string]interface{}, error) { + pharmacyId, err := strconv.ParseInt(req.PharmacyId, 10, 64) + if err != nil || pharmacyId == 0 { + return nil, errors.New("药房id无效") + } + + pharmacyDao := dao.PharmacyDao{} + pharmacy, err := pharmacyDao.GetPharmacyById(pharmacyId) + if err != nil || pharmacy == nil { + return nil, errors.New("药房不存在或已被删除") + } + + if req.Page <= 0 { + req.Page = 1 + } + if req.PageSize <= 0 { + req.PageSize = 20 + } + + doctorPharmacyDao := dao.DoctorPharmacyDao{} + list, total, err := doctorPharmacyDao.GetPharmacyDoctorPageSearch(pharmacyId, req, req.Page, req.PageSize) + if err != nil { + return nil, err + } + + resList := dto.GetPharmacyDoctorListDto(list) + + result := make(map[string]interface{}) + result["page"] = req.Page + result["page_size"] = req.PageSize + result["total"] = total + result["data"] = resList + return result, nil +} + +// GetPharmacyDoctorList 获取药房绑定的全部医生列表 +func (r *PharmacyService) GetPharmacyDoctorList(pharmacyId int64) ([]dto.PharmacyDoctorDto, error) { + pharmacyDao := dao.PharmacyDao{} + pharmacy, err := pharmacyDao.GetPharmacyById(pharmacyId) + if err != nil || pharmacy == nil { + return nil, errors.New("药房不存在或已被删除") + } + + doctorPharmacyDao := dao.DoctorPharmacyDao{} + list, err := doctorPharmacyDao.GetPharmacyDoctorListByPharmacyId(pharmacyId) + if err != nil { + return nil, err + } + + return dto.GetPharmacyDoctorListDto(list), nil +}