2
This commit is contained in:
@@ -2,6 +2,7 @@ package controller
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"hospital-admin-api/api/dao"
|
||||
"hospital-admin-api/api/requests"
|
||||
"hospital-admin-api/api/responses"
|
||||
"hospital-admin-api/api/service"
|
||||
@@ -14,9 +15,12 @@ type DoctorPharmacy struct{}
|
||||
|
||||
// GetDoctorPharmacies 获取指定医生绑定的药房列表
|
||||
func (r *DoctorPharmacy) GetDoctorPharmacies(c *gin.Context) {
|
||||
id := c.Param("doctor_id")
|
||||
id := c.Param("id")
|
||||
if id == "" {
|
||||
responses.FailWithMessage("缺少参数", c)
|
||||
id = c.Param("doctor_id")
|
||||
}
|
||||
if id == "" {
|
||||
responses.FailWithMessage("缺少医生ID参数", c)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -38,9 +42,12 @@ func (r *DoctorPharmacy) GetDoctorPharmacies(c *gin.Context) {
|
||||
|
||||
// BindDoctorPharmacies 批量设置/覆盖医生绑定的药房列表
|
||||
func (r *DoctorPharmacy) BindDoctorPharmacies(c *gin.Context) {
|
||||
id := c.Param("doctor_id")
|
||||
id := c.Param("id")
|
||||
if id == "" {
|
||||
responses.FailWithMessage("缺少参数", c)
|
||||
id = c.Param("doctor_id")
|
||||
}
|
||||
if id == "" {
|
||||
responses.FailWithMessage("缺少医生ID参数", c)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -73,16 +80,14 @@ func (r *DoctorPharmacy) BindDoctorPharmacies(c *gin.Context) {
|
||||
|
||||
// AddDoctorPharmacy 为医生新增单个药房绑定
|
||||
func (r *DoctorPharmacy) AddDoctorPharmacy(c *gin.Context) {
|
||||
id := c.Param("doctor_id")
|
||||
id := c.Param("id")
|
||||
if id == "" {
|
||||
responses.FailWithMessage("缺少参数", c)
|
||||
return
|
||||
id = c.Param("doctor_id")
|
||||
}
|
||||
|
||||
doctorId, err := strconv.ParseInt(id, 10, 64)
|
||||
if err != nil {
|
||||
responses.Fail(c)
|
||||
return
|
||||
var doctorId int64
|
||||
if id != "" {
|
||||
doctorId, _ = strconv.ParseInt(id, 10, 64)
|
||||
}
|
||||
|
||||
req := requests.AddDoctorPharmacy{}
|
||||
@@ -91,13 +96,22 @@ func (r *DoctorPharmacy) AddDoctorPharmacy(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if doctorId == 0 && req.DoctorId != "" {
|
||||
doctorId, _ = strconv.ParseInt(req.DoctorId, 10, 64)
|
||||
}
|
||||
|
||||
if doctorId == 0 {
|
||||
responses.FailWithMessage("缺少医生ID", 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)
|
||||
_, err := doctorPharmacyService.AddDoctorPharmacy(doctorId, req)
|
||||
if err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
@@ -106,64 +120,174 @@ func (r *DoctorPharmacy) AddDoctorPharmacy(c *gin.Context) {
|
||||
responses.Ok(c)
|
||||
}
|
||||
|
||||
// UnbindDoctorPharmacy 解除医生与指定药房的绑定
|
||||
// UnbindDoctorPharmacy 解除医生与指定药房的绑定(DELETE /admin/doctor/pharmacy/:id)
|
||||
func (r *DoctorPharmacy) UnbindDoctorPharmacy(c *gin.Context) {
|
||||
docId := c.Param("doctor_id")
|
||||
pharId := c.Param("pharmacy_id")
|
||||
if docId == "" || pharId == "" {
|
||||
responses.FailWithMessage("缺少参数", c)
|
||||
return
|
||||
idStr := c.Param("id")
|
||||
if idStr == "" {
|
||||
idStr = c.Param("doctor_pharmacy_id")
|
||||
}
|
||||
|
||||
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)
|
||||
id, err := strconv.ParseInt(idStr, 10, 64)
|
||||
if err != nil || id == 0 {
|
||||
responses.FailWithMessage("缺少ID参数", c)
|
||||
return
|
||||
}
|
||||
|
||||
doctorPharmacyDao := dao.DoctorPharmacyDao{}
|
||||
doctorPharmacyService := service.DoctorPharmacyService{}
|
||||
_, err = doctorPharmacyService.UnbindDoctorPharmacy(doctorId, pharmacyId)
|
||||
if err != nil {
|
||||
|
||||
// 1. 优先作为 doctor_pharmacy_id 判定
|
||||
dp, _ := doctorPharmacyDao.GetDoctorPharmacyById(id)
|
||||
if dp != nil {
|
||||
_, err = doctorPharmacyService.UnbindDoctorPharmacyById(id)
|
||||
if err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
responses.Ok(c)
|
||||
return
|
||||
}
|
||||
|
||||
// 2. 若作为 doctor_id,从 query 或 body 获取 pharmacy_id
|
||||
pharmacyIdStr := c.Query("pharmacy_id")
|
||||
if pharmacyIdStr != "" {
|
||||
pharmacyId, err := strconv.ParseInt(pharmacyIdStr, 10, 64)
|
||||
if err == nil && pharmacyId > 0 {
|
||||
_, err = doctorPharmacyService.UnbindDoctorPharmacy(id, pharmacyId)
|
||||
if err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
responses.Ok(c)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
responses.FailWithMessage("未找到绑定记录或缺少药房ID", c)
|
||||
}
|
||||
|
||||
// UnbindDoctorPharmacyPost 解绑药房(POST /admin/doctor/pharmacy/unbind)
|
||||
func (r *DoctorPharmacy) UnbindDoctorPharmacyPost(c *gin.Context) {
|
||||
req := requests.UnbindDoctorPharmacy{}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
responses.Ok(c)
|
||||
doctorPharmacyService := service.DoctorPharmacyService{}
|
||||
|
||||
// 优先按 doctor_pharmacy_id
|
||||
if req.DoctorPharmacyId != "" {
|
||||
dpid, err := strconv.ParseInt(req.DoctorPharmacyId, 10, 64)
|
||||
if err == nil && dpid > 0 {
|
||||
_, err = doctorPharmacyService.UnbindDoctorPharmacyById(dpid)
|
||||
if err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
responses.Ok(c)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 其次按 doctor_id + pharmacy_id
|
||||
if req.DoctorId != "" && req.PharmacyId != "" {
|
||||
docId, err1 := strconv.ParseInt(req.DoctorId, 10, 64)
|
||||
pharId, err2 := strconv.ParseInt(req.PharmacyId, 10, 64)
|
||||
if err1 == nil && err2 == nil {
|
||||
_, err := doctorPharmacyService.UnbindDoctorPharmacy(docId, pharId)
|
||||
if err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
responses.Ok(c)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
responses.FailWithMessage("缺少解绑参数", c)
|
||||
}
|
||||
|
||||
// SetDefaultDoctorPharmacy 设为医生的默认药房
|
||||
// SetDefaultDoctorPharmacy 设置默认药房(PUT /admin/doctor/pharmacy/default/:id)
|
||||
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)
|
||||
idStr := c.Param("id")
|
||||
id, err := strconv.ParseInt(idStr, 10, 64)
|
||||
if err != nil || id == 0 {
|
||||
responses.FailWithMessage("缺少ID参数", c)
|
||||
return
|
||||
}
|
||||
|
||||
doctorPharmacyDao := dao.DoctorPharmacyDao{}
|
||||
doctorPharmacyService := service.DoctorPharmacyService{}
|
||||
_, err = doctorPharmacyService.SetDefaultPharmacy(doctorId, pharmacyId)
|
||||
if err != nil {
|
||||
|
||||
// 1. 优先作为 doctor_pharmacy_id 判定
|
||||
dp, _ := doctorPharmacyDao.GetDoctorPharmacyById(id)
|
||||
if dp != nil {
|
||||
_, err = doctorPharmacyService.SetDefaultPharmacyById(id)
|
||||
if err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
responses.Ok(c)
|
||||
return
|
||||
}
|
||||
|
||||
// 2. 若作为 doctor_id,从 query 或 body 获取 pharmacy_id
|
||||
pharmacyIdStr := c.Query("pharmacy_id")
|
||||
if pharmacyIdStr != "" {
|
||||
pharmacyId, err := strconv.ParseInt(pharmacyIdStr, 10, 64)
|
||||
if err == nil && pharmacyId > 0 {
|
||||
_, err = doctorPharmacyService.SetDefaultPharmacy(id, pharmacyId)
|
||||
if err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
responses.Ok(c)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
responses.FailWithMessage("未找到绑定记录或缺少药房ID", c)
|
||||
}
|
||||
|
||||
// SetDefaultDoctorPharmacyPut 设置默认药房(PUT /admin/doctor/pharmacy/default)
|
||||
func (r *DoctorPharmacy) SetDefaultDoctorPharmacyPut(c *gin.Context) {
|
||||
req := requests.SetDefaultDoctorPharmacy{}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
|
||||
responses.Ok(c)
|
||||
doctorPharmacyService := service.DoctorPharmacyService{}
|
||||
|
||||
// 优先按 doctor_pharmacy_id
|
||||
if req.DoctorPharmacyId != "" {
|
||||
dpid, err := strconv.ParseInt(req.DoctorPharmacyId, 10, 64)
|
||||
if err == nil && dpid > 0 {
|
||||
_, err = doctorPharmacyService.SetDefaultPharmacyById(dpid)
|
||||
if err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
responses.Ok(c)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 其次按 doctor_id + pharmacy_id
|
||||
if req.DoctorId != "" && req.PharmacyId != "" {
|
||||
docId, err1 := strconv.ParseInt(req.DoctorId, 10, 64)
|
||||
pharId, err2 := strconv.ParseInt(req.PharmacyId, 10, 64)
|
||||
if err1 == nil && err2 == nil {
|
||||
_, err := doctorPharmacyService.SetDefaultPharmacy(docId, pharId)
|
||||
if err != nil {
|
||||
responses.FailWithMessage(err.Error(), c)
|
||||
return
|
||||
}
|
||||
responses.Ok(c)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
responses.FailWithMessage("缺少参数", c)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user