根据药房查询医生列表

This commit is contained in:
haomingming
2026-09-07 15:21:06 +08:00
parent 0cff5b3f8d
commit 338d56beda
7 changed files with 260 additions and 6 deletions
+50
View File
@@ -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)
}
+49
View File
@@ -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
}
+78
View File
@@ -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
}
+2 -1
View File
@@ -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 {
+21 -5
View File
@@ -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 获取药房列表
+6
View File
@@ -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)
}
// 科普分类管理
+54
View File
@@ -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
}