diff --git a/api/controller/base.go b/api/controller/base.go index 1c35f58..bffac3d 100644 --- a/api/controller/base.go +++ b/api/controller/base.go @@ -35,6 +35,7 @@ type sysSetting struct { type userDoctorManage struct { UserDoctor // 医生列表 DoctorAccount + DoctorPharmacy // 医生药房管理 } // Basic 基础数据 diff --git a/api/controller/doctorPharmacy.go b/api/controller/doctorPharmacy.go new file mode 100644 index 0000000..1a240dc --- /dev/null +++ b/api/controller/doctorPharmacy.go @@ -0,0 +1,169 @@ +package controller + +import ( + "github.com/gin-gonic/gin" + "hospital-admin-api/api/requests" + "hospital-admin-api/api/responses" + "hospital-admin-api/api/service" + "hospital-admin-api/global" + "hospital-admin-api/utils" + "strconv" +) + +type DoctorPharmacy struct{} + +// GetDoctorPharmacies 获取指定医生绑定的药房列表 +func (r *DoctorPharmacy) GetDoctorPharmacies(c *gin.Context) { + id := c.Param("doctor_id") + if id == "" { + responses.FailWithMessage("缺少参数", c) + return + } + + doctorId, err := strconv.ParseInt(id, 10, 64) + if err != nil { + responses.Fail(c) + return + } + + doctorPharmacyService := service.DoctorPharmacyService{} + list, err := doctorPharmacyService.GetDoctorPharmacies(doctorId) + if err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + responses.OkWithData(list, c) +} + +// BindDoctorPharmacies 批量设置/覆盖医生绑定的药房列表 +func (r *DoctorPharmacy) BindDoctorPharmacies(c *gin.Context) { + id := c.Param("doctor_id") + if id == "" { + responses.FailWithMessage("缺少参数", c) + return + } + + doctorId, err := strconv.ParseInt(id, 10, 64) + if err != nil { + responses.Fail(c) + return + } + + req := requests.BindDoctorPharmacies{} + 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 + } + + doctorPharmacyService := service.DoctorPharmacyService{} + _, err = doctorPharmacyService.BindDoctorPharmacies(doctorId, req) + if err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + responses.Ok(c) +} + +// AddDoctorPharmacy 为医生新增单个药房绑定 +func (r *DoctorPharmacy) AddDoctorPharmacy(c *gin.Context) { + id := c.Param("doctor_id") + if id == "" { + responses.FailWithMessage("缺少参数", c) + return + } + + doctorId, err := strconv.ParseInt(id, 10, 64) + if err != nil { + responses.Fail(c) + return + } + + req := requests.AddDoctorPharmacy{} + 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 + } + + doctorPharmacyService := service.DoctorPharmacyService{} + _, err = doctorPharmacyService.AddDoctorPharmacy(doctorId, req) + if err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + responses.Ok(c) +} + +// UnbindDoctorPharmacy 解除医生与指定药房的绑定 +func (r *DoctorPharmacy) UnbindDoctorPharmacy(c *gin.Context) { + docId := c.Param("doctor_id") + pharId := c.Param("pharmacy_id") + if docId == "" || pharId == "" { + responses.FailWithMessage("缺少参数", c) + return + } + + doctorId, err := strconv.ParseInt(docId, 10, 64) + if err != nil { + responses.Fail(c) + return + } + + pharmacyId, err := strconv.ParseInt(pharId, 10, 64) + if err != nil { + responses.Fail(c) + return + } + + doctorPharmacyService := service.DoctorPharmacyService{} + _, err = doctorPharmacyService.UnbindDoctorPharmacy(doctorId, pharmacyId) + if err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + responses.Ok(c) +} + +// SetDefaultDoctorPharmacy 设为医生的默认药房 +func (r *DoctorPharmacy) SetDefaultDoctorPharmacy(c *gin.Context) { + docId := c.Param("doctor_id") + pharId := c.Param("pharmacy_id") + if docId == "" || pharId == "" { + responses.FailWithMessage("缺少参数", c) + return + } + + doctorId, err := strconv.ParseInt(docId, 10, 64) + if err != nil { + responses.Fail(c) + return + } + + pharmacyId, err := strconv.ParseInt(pharId, 10, 64) + if err != nil { + responses.Fail(c) + return + } + + doctorPharmacyService := service.DoctorPharmacyService{} + _, err = doctorPharmacyService.SetDefaultPharmacy(doctorId, pharmacyId) + if err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + responses.Ok(c) +} diff --git a/api/dao/doctorPharmacy.go b/api/dao/doctorPharmacy.go new file mode 100644 index 0000000..16a67ea --- /dev/null +++ b/api/dao/doctorPharmacy.go @@ -0,0 +1,71 @@ +package dao + +import ( + "gorm.io/gorm" + "hospital-admin-api/api/model" + "hospital-admin-api/global" +) + +type DoctorPharmacyDao struct{} + +// 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 + } + 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 +} + +// 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 +} diff --git a/api/dto/DoctorPharmacy.go b/api/dto/DoctorPharmacy.go new file mode 100644 index 0000000..8490ade --- /dev/null +++ b/api/dto/DoctorPharmacy.go @@ -0,0 +1,64 @@ +package dto + +import ( + "fmt" + "hospital-admin-api/api/model" +) + +type DoctorPharmacyDto struct { + DoctorPharmacyId string `json:"doctor_pharmacy_id"` // 绑定关系id + DoctorId string `json:"doctor_id"` // 医生id + PharmacyId string `json:"pharmacy_id"` // 药房id + PharmacyName string `json:"pharmacy_name"` // 药房名称 + PharmacyCode string `json:"pharmacy_code"` // 药房代码 + Postage float64 `json:"postage"` // 基础邮费 + FreeShippingThreshold float64 `json:"free_shipping_threshold"` // 满XX包邮门槛金额 + IsPickup int `json:"is_pickup"` // 是否支持自提(0:否 1:是) + Telephone string `json:"telephone"` // 电话 + Province string `json:"province"` // 省份 + City string `json:"city"` // 城市 + County string `json:"county"` // 区县 + Address string `json:"address"` // 详细地址 + FullAddress string `json:"full_address"` // 完整地址 + IsDefault int `json:"is_default"` // 是否默认药房(0:否 1:是) + Status int `json:"status"` // 状态(0:禁用 1:正常) + CreatedAt model.LocalTime `json:"created_at"` // 绑定时间 + UpdatedAt model.LocalTime `json:"updated_at"` // 更新时间 +} + +func GetDoctorPharmacyDto(m *model.DoctorPharmacy) *DoctorPharmacyDto { + if m == nil { + return nil + } + dto := &DoctorPharmacyDto{ + DoctorPharmacyId: fmt.Sprintf("%d", m.DoctorPharmacyId), + DoctorId: fmt.Sprintf("%d", m.DoctorId), + PharmacyId: fmt.Sprintf("%d", m.PharmacyId), + IsDefault: m.IsDefault, + Status: m.Status, + CreatedAt: m.CreatedAt, + UpdatedAt: m.UpdatedAt, + } + if m.Pharmacy != nil { + dto.PharmacyName = m.Pharmacy.PharmacyName + dto.PharmacyCode = m.Pharmacy.PharmacyCode + dto.Postage = m.Pharmacy.Postage + dto.FreeShippingThreshold = m.Pharmacy.FreeShippingThreshold + dto.IsPickup = m.Pharmacy.IsPickup + dto.Telephone = m.Pharmacy.Telephone + dto.Province = m.Pharmacy.Province + dto.City = m.Pharmacy.City + dto.County = m.Pharmacy.County + dto.Address = m.Pharmacy.Address + dto.FullAddress = fmt.Sprintf("%s%s%s%s", m.Pharmacy.Province, m.Pharmacy.City, m.Pharmacy.County, m.Pharmacy.Address) + } + return dto +} + +func GetDoctorPharmacyListDto(list []*model.DoctorPharmacy) []*DoctorPharmacyDto { + res := make([]*DoctorPharmacyDto, len(list)) + for i, v := range list { + res[i] = GetDoctorPharmacyDto(v) + } + return res +} diff --git a/api/dto/UserDoctor.go b/api/dto/UserDoctor.go index 22fcb1e..093a976 100644 --- a/api/dto/UserDoctor.go +++ b/api/dto/UserDoctor.go @@ -52,6 +52,7 @@ type UserDoctorDto struct { Hospital *HospitalDto `json:"hospital"` // 医院 UserDoctorInfo *UserDoctorInfoDto `json:"user_doctor_info"` // 医生详情 DoctorExpertise []*DoctorExpertiseDto `json:"doctor_expertise"` // 医生专长 + DoctorPharmacy []*DoctorPharmacyDto `json:"doctor_pharmacy"` // 医生药房 DoctorBankCard *DoctorBankCardDto `json:"doctor_bank_card"` // 医生银行卡 InquiryType string `json:"inquiry_type"` // 服务类型 UserCaCert *UserCaCertDto `json:"user_ca_cert"` // ca监管证书 diff --git a/api/model/doctorPharmacy.go b/api/model/doctorPharmacy.go new file mode 100644 index 0000000..ab35604 --- /dev/null +++ b/api/model/doctorPharmacy.go @@ -0,0 +1,36 @@ +package model + +import ( + "gorm.io/gorm" + "hospital-admin-api/global" + "time" +) + +// DoctorPharmacy 医生-药房关联表 +type DoctorPharmacy struct { + DoctorPharmacyId int64 `gorm:"column:doctor_pharmacy_id;type:bigint(20);primary_key;comment:主键id" json:"doctor_pharmacy_id"` + DoctorId int64 `gorm:"column:doctor_id;type:bigint(19);comment:医生id;NOT NULL" json:"doctor_id"` + PharmacyId int64 `gorm:"column:pharmacy_id;type:bigint(20);comment:药房id;NOT NULL" json:"pharmacy_id"` + 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"` +} + +func (m *DoctorPharmacy) TableName() string { + return "gdxz_doctor_pharmacy" +} + +func (m *DoctorPharmacy) BeforeCreate(tx *gorm.DB) error { + if m.DoctorPharmacyId == 0 { + m.DoctorPharmacyId = global.Snowflake.Generate().Int64() + } + + m.CreatedAt = LocalTime(time.Now()) + tx.Statement.SetColumn("CreatedAt", m.CreatedAt) + + m.UpdatedAt = LocalTime(time.Now()) + tx.Statement.SetColumn("UpdatedAt", m.UpdatedAt) + + return nil +} diff --git a/api/requests/doctorPharmacy.go b/api/requests/doctorPharmacy.go new file mode 100644 index 0000000..fb32db9 --- /dev/null +++ b/api/requests/doctorPharmacy.go @@ -0,0 +1,18 @@ +package requests + +type DoctorPharmacyRequest struct { + BindDoctorPharmacies // 批量绑定药房 + AddDoctorPharmacy // 新增单个药房绑定 +} + +// BindDoctorPharmacies 批量绑定药房 +type BindDoctorPharmacies struct { + PharmacyIds []string `json:"pharmacy_ids" form:"pharmacy_ids" label:"药房id列表"` + DefaultPharmacyId string `json:"default_pharmacy_id" form:"default_pharmacy_id" label:"默认药房id"` +} + +// AddDoctorPharmacy 单个药房绑定 +type AddDoctorPharmacy struct { + PharmacyId string `json:"pharmacy_id" form:"pharmacy_id" label:"药房id" validate:"required"` + IsDefault *int `json:"is_default" form:"is_default" label:"是否默认" validate:"omitempty,oneof=0 1"` +} diff --git a/api/requests/userDoctor.go b/api/requests/userDoctor.go index 23f7117..f37e488 100644 --- a/api/requests/userDoctor.go +++ b/api/requests/userDoctor.go @@ -58,6 +58,7 @@ type PutUserDoctor struct { IdCardBack string `json:"id_card_back" form:"id_card_back" label:"身份证背面图片"` SignImage string `json:"sign_image" form:"sign_image" label:"签名图片"` DoctorExpertise []string `json:"doctor_expertise" form:"doctor_expertise" label:"专长"` + DoctorPharmacy []string `json:"doctor_pharmacy" form:"doctor_pharmacy" label:"药房"` Email string `json:"email" form:"email" label:"邮箱"` BankId string `json:"bank_id" form:"bank_id" validate:"required_with_all=BankCardCode BankCardProvinceId BankCardCityId BankCardCountyId" label:"银行id"` BankCardCode string `json:"bank_card_code" form:"bank_card_code" validate:"required_with_all=BankId BankCardProvinceId BankCardCityId BankCardCountyId" label:"银行卡号"` @@ -90,6 +91,7 @@ type AddUserDoctor struct { SignImage string `json:"sign_image" form:"sign_image" label:"签名图片"` CardNum string `json:"card_num" form:"card_num" validate:"required" label:"证件号码"` DoctorExpertise []string `json:"doctor_expertise" form:"doctor_expertise" label:"专长"` + DoctorPharmacy []string `json:"doctor_pharmacy" form:"doctor_pharmacy" label:"药房"` Email string `json:"email" form:"email" label:"邮箱"` BankId string `json:"bank_id" form:"bank_id" validate:"required_with_all=BankCardCode BankCardProvinceId BankCardCityId BankCardCountyId" label:"银行id"` BankCardCode string `json:"bank_card_code" form:"bank_card_code" validate:"required_with_all=BankId BankCardProvinceId BankCardCityId BankCardCountyId" label:"银行卡号"` diff --git a/api/router/router.go b/api/router/router.go index 160fccf..72d49f1 100644 --- a/api/router/router.go +++ b/api/router/router.go @@ -371,6 +371,25 @@ func privateRouter(r *gin.Engine, api controller.Api) { // 新增医生 doctorGroup.POST("", api.UserDoctor.AddUserDoctor) + // 医生药房管理 + doctorPharmacyGroup := doctorGroup.Group("/:doctor_id/pharmacy") + { + // 获取医生绑定的药房列表 + doctorPharmacyGroup.GET("", api.DoctorPharmacy.GetDoctorPharmacies) + + // 批量设置医生绑定的药房列表 + doctorPharmacyGroup.PUT("", api.DoctorPharmacy.BindDoctorPharmacies) + + // 为医生新增单个药房绑定 + doctorPharmacyGroup.POST("", api.DoctorPharmacy.AddDoctorPharmacy) + + // 解除医生与指定药房的绑定 + doctorPharmacyGroup.DELETE("/:pharmacy_id", api.DoctorPharmacy.UnbindDoctorPharmacy) + + // 设置医生的默认药房 + doctorPharmacyGroup.PUT("/default/:pharmacy_id", api.DoctorPharmacy.SetDefaultDoctorPharmacy) + } + // 身份审核列表 doctorPendingGroup := doctorGroup.Group("/pending") { diff --git a/api/service/doctorPharmacy.go b/api/service/doctorPharmacy.go new file mode 100644 index 0000000..da9059b --- /dev/null +++ b/api/service/doctorPharmacy.go @@ -0,0 +1,219 @@ +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 +} diff --git a/api/service/userDoctor.go b/api/service/userDoctor.go index f16f5c3..83ed782 100644 --- a/api/service/userDoctor.go +++ b/api/service/userDoctor.go @@ -60,6 +60,11 @@ func (r *UserDoctorService) GetUserDoctor(doctorId int64) (getUserDoctorResponse // 加载医生专长 getUserDoctorResponse.DoctorExpertise = doctorExpertise + // 加载医生药房 + doctorPharmacyDao := dao.DoctorPharmacyDao{} + doctorPharmacies, _ := doctorPharmacyDao.GetDoctorPharmacyListByDoctorId(doctorId) + getUserDoctorResponse.DoctorPharmacy = dto.GetDoctorPharmacyListDto(doctorPharmacies) + // 加载医生银行卡 getUserDoctorResponse.LoadDoctorBankCard(doctorBankCard) @@ -552,6 +557,47 @@ func (r *UserDoctorService) PutUserDoctor(doctorId int64, req requests.PutUserDo } } + // 修改药房数据 + if len(req.DoctorPharmacy) > 0 { + doctorPharmacyDao := dao.DoctorPharmacyDao{} + + // 删除原绑定 + maps := make(map[string]interface{}) + maps["doctor_id"] = userDoctor.DoctorId + err = doctorPharmacyDao.DeleteDoctorPharmacy(tx, maps) + if err != nil { + tx.Rollback() + return false, errors.New("清空药房绑定失败") + } + + for i, v := range req.DoctorPharmacy { + pharmacyId, err := strconv.ParseInt(v, 10, 64) + if err != nil { + tx.Rollback() + return false, errors.New("药房数据错误") + } + + isDefault := 0 + if i == 0 { + isDefault = 1 + } + + // 新增药房绑定数据 + doctorPharmacy := &model.DoctorPharmacy{ + DoctorId: userDoctor.DoctorId, + PharmacyId: pharmacyId, + IsDefault: isDefault, + Status: 1, + } + + _, err = doctorPharmacyDao.AddDoctorPharmacy(tx, doctorPharmacy) + if err != nil { + tx.Rollback() + return false, errors.New(err.Error()) + } + } + } + // 修改医生银行卡数据-新增 if doctorBankCard == nil { if provinceId != 0 && cityId != 0 && countyId != 0 && bankId != 0 && bankCardCode != "" {