294 lines
7.3 KiB
Go
294 lines
7.3 KiB
Go
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"
|
||
"hospital-admin-api/global"
|
||
"hospital-admin-api/utils"
|
||
"strconv"
|
||
)
|
||
|
||
type DoctorPharmacy struct{}
|
||
|
||
// GetDoctorPharmacies 获取指定医生绑定的药房列表
|
||
func (r *DoctorPharmacy) GetDoctorPharmacies(c *gin.Context) {
|
||
id := c.Param("id")
|
||
if id == "" {
|
||
id = c.Param("doctor_id")
|
||
}
|
||
if id == "" {
|
||
responses.FailWithMessage("缺少医生ID参数", 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("id")
|
||
if id == "" {
|
||
id = c.Param("doctor_id")
|
||
}
|
||
if id == "" {
|
||
responses.FailWithMessage("缺少医生ID参数", 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("id")
|
||
if id == "" {
|
||
id = c.Param("doctor_id")
|
||
}
|
||
|
||
var doctorId int64
|
||
if id != "" {
|
||
doctorId, _ = strconv.ParseInt(id, 10, 64)
|
||
}
|
||
|
||
req := requests.AddDoctorPharmacy{}
|
||
if err := c.ShouldBindJSON(&req); err != nil {
|
||
responses.FailWithMessage(err.Error(), c)
|
||
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)
|
||
if err != nil {
|
||
responses.FailWithMessage(err.Error(), c)
|
||
return
|
||
}
|
||
|
||
responses.Ok(c)
|
||
}
|
||
|
||
// UnbindDoctorPharmacy 解除医生与指定药房的绑定(DELETE /admin/doctor/pharmacy/:id)
|
||
func (r *DoctorPharmacy) UnbindDoctorPharmacy(c *gin.Context) {
|
||
idStr := c.Param("id")
|
||
if idStr == "" {
|
||
idStr = c.Param("doctor_pharmacy_id")
|
||
}
|
||
|
||
id, err := strconv.ParseInt(idStr, 10, 64)
|
||
if err != nil || id == 0 {
|
||
responses.FailWithMessage("缺少ID参数", c)
|
||
return
|
||
}
|
||
|
||
doctorPharmacyDao := dao.DoctorPharmacyDao{}
|
||
doctorPharmacyService := service.DoctorPharmacyService{}
|
||
|
||
// 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
|
||
}
|
||
|
||
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 设置默认药房(PUT /admin/doctor/pharmacy/default/:id)
|
||
func (r *DoctorPharmacy) SetDefaultDoctorPharmacy(c *gin.Context) {
|
||
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{}
|
||
|
||
// 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
|
||
}
|
||
|
||
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)
|
||
}
|