diff --git a/api/controller/base.go b/api/controller/base.go index 014a493..8dc0d94 100644 --- a/api/controller/base.go +++ b/api/controller/base.go @@ -44,7 +44,8 @@ type order struct { // 患者管理 type userPatientManage struct { - UserPatient // 患者列表 + UserPatient // 患者列表 + PatientFamily // 家庭成员管理 } // 病例管理 diff --git a/api/controller/patientFamily.go b/api/controller/patientFamily.go new file mode 100644 index 0000000..432865c --- /dev/null +++ b/api/controller/patientFamily.go @@ -0,0 +1,81 @@ +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/responses/patientFamilyResponse" + "hospital-admin-api/api/service" + "hospital-admin-api/global" + "hospital-admin-api/utils" + "strconv" +) + +type PatientFamily struct{} + +// GetPatientFamilyPage 获取就诊人列表-分页 +func (r *PatientFamily) GetPatientFamilyPage(c *gin.Context) { + req := requests.PatientFamilyRequest{} + if err := c.ShouldBind(&req.GetPatientFamilyPage); err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + // 参数验证 + if err := global.Validate.Struct(req.GetPatientFamilyPage); err != nil { + responses.FailWithMessage(utils.Translate(err), c) + return + } + + if req.GetPatientFamilyPage.Page == 0 { + req.GetPatientFamilyPage.Page = 1 + } + + if req.GetPatientFamilyPage.PageSize == 0 { + req.GetPatientFamilyPage.PageSize = 20 + } + + patientFamilyDao := dao.PatientFamilyDao{} + patientFamily, total, err := patientFamilyDao.GetPatientFamilyPageSearch(req.GetPatientFamilyPage, req.GetPatientFamilyPage.Page, req.GetPatientFamilyPage.PageSize) + if err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + // 处理返回值 + GetPatientFamilyPage := patientFamilyResponse.GetPatientFamilyPageResponse(patientFamily) + + result := make(map[string]interface{}) + result["page"] = req.GetPatientFamilyPage.Page + result["page_size"] = req.GetPatientFamilyPage.PageSize + result["total"] = total + result["data"] = GetPatientFamilyPage + responses.OkWithData(result, c) +} + +// GetUserPatient 患者详情 +func (r *PatientFamily) GetUserPatient(c *gin.Context) { + id := c.Param("patient_id") + if id == "" { + responses.FailWithMessage("缺少参数", c) + return + } + + // 将 id 转换为 int64 类型 + patientId, err := strconv.ParseInt(id, 10, 64) + if err != nil { + responses.Fail(c) + return + } + + // 业务处理 + userPatientService := service.UserPatientService{} + getUserPatientResponses, err := userPatientService.GetUserPatient(patientId) + if err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + responses.OkWithData(getUserPatientResponses, c) +} diff --git a/api/controller/userPatient.go b/api/controller/userPatient.go index a443ae4..e514d9f 100644 --- a/api/controller/userPatient.go +++ b/api/controller/userPatient.go @@ -80,3 +80,41 @@ func (r *UserPatient) GetUserPatient(c *gin.Context) { responses.OkWithData(getUserPatientResponses, c) } + +// PutUserDoctorStatus 修改患者状态 +func (r *UserPatient) PutUserDoctorStatus(c *gin.Context) { + userPatientRequest := requests.UserPatientRequest{} + if err := c.ShouldBind(&userPatientRequest.PutUserDoctorStatus); err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + // 参数验证 + if err := global.Validate.Struct(userPatientRequest.PutUserDoctorStatus); err != nil { + responses.FailWithMessage(utils.Translate(err), c) + return + } + + id := c.Param("patient_id") + if id == "" { + responses.FailWithMessage("缺少参数", c) + return + } + + // 将 id 转换为 int64 类型 + patientId, err := strconv.ParseInt(id, 10, 64) + if err != nil { + responses.Fail(c) + return + } + + // 业务处理 + userPatientService := service.UserPatientService{} + _, err = userPatientService.PutUserDoctorStatus(patientId, userPatientRequest.PutUserDoctorStatus) + if err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + responses.Ok(c) +} diff --git a/api/dao/patientFamily.go b/api/dao/patientFamily.go index d73ed34..37d8063 100644 --- a/api/dao/patientFamily.go +++ b/api/dao/patientFamily.go @@ -3,7 +3,10 @@ package dao import ( "gorm.io/gorm" "hospital-admin-api/api/model" + "hospital-admin-api/api/requests" "hospital-admin-api/global" + "strings" + "time" ) type PatientFamilyDao struct { @@ -79,3 +82,77 @@ func (r *PatientFamilyDao) AddPatientFamilyByMap(tx *gorm.DB, data map[string]in } return userDoctorInfo, nil } + +// GetPatientFamilyPageSearch 获取家庭成员列表-分页 +func (r *PatientFamilyDao) GetPatientFamilyPageSearch(req requests.GetPatientFamilyPage, page, pageSize int) (m []*model.PatientFamily, total int64, err error) { + var totalRecords int64 + + // 构建查询条件 + query := global.Db.Model(&model.PatientFamily{}).Select("family_id", "patient_id", "relation", "status", "card_name", "mobile_mask", "created_at") + + // 患者表 + query = query.Preload("UserPatient", func(db *gorm.DB) *gorm.DB { + return db.Omit("open_id", "union_id", "wx_session_key") + }) + + // 状态 + if req.Status != nil { + query = query.Where("status = ?", req.Status) + } + + // 手机号 + if req.Mobile != "" { + userSubQuery := global.Db.Model(&model.User{}). + Select("user_id"). + Where("mobile = ?", req.Mobile) + + userPatientSubQuery := global.Db.Model(&model.UserPatient{}). + Select("patient_id"). + Where(gorm.Expr("user_id IN (?)", userSubQuery)) + + query = query.Where(gorm.Expr("patient_id IN (?)", userPatientSubQuery)) + } + + // 用户名称-用户-就诊人 + if req.UserName != "" { + // 用户 + patientFamilySubQuery := global.Db.Model(&model.UserPatient{}). + Select("patient_id"). + Where("user_name LIKE ?", "%"+req.UserName+"%") + + // 就诊人 + query = query.Where("card_name LIKE ?", "%"+req.UserName+"%") + + query = query.Or("patient_id IN (?)", patientFamilySubQuery).Or(query) + } + + // 注册时间 + if req.CreatedAt != "" { + cancelTime := strings.Split(req.CreatedAt, "&") + if len(cancelTime) == 2 { + startTime, _ := time.Parse("2006-01-02", cancelTime[0]) + endTime, _ := time.Parse("2006-01-02", cancelTime[1]) + + if startTime == endTime { + endTime = endTime.Add(23*time.Hour + 59*time.Minute + 59*time.Second) + } + + query = query.Where("created_at BETWEEN ? AND ?", startTime, endTime) + } + } + + // 排序 + query = query.Order("created_at desc") + + // 查询总数量 + if err := query.Count(&totalRecords).Error; err != nil { + return nil, 0, err + } + + err = query.Scopes(model.Paginate(page, pageSize)).Find(&m).Error + if err != nil { + return nil, 0, err + } + + return m, totalRecords, nil +} diff --git a/api/dao/userPatient.go b/api/dao/userPatient.go index b4ff51e..26177ff 100644 --- a/api/dao/userPatient.go +++ b/api/dao/userPatient.go @@ -6,6 +6,8 @@ import ( "hospital-admin-api/api/model" "hospital-admin-api/api/requests" "hospital-admin-api/global" + "strings" + "time" ) type UserPatientDao struct { @@ -92,7 +94,7 @@ func (r *UserPatientDao) GetUserPatientPageSearch(req requests.GetUserPatientPag if req.Mobile != "" { subQuery := global.Db.Model(&model.User{}). Select("user_id"). - Where("mobile LIKE ?", "%"+req.Mobile+"%") + Where("mobile = ?", req.Mobile) query = query.Where(gorm.Expr("user_id IN (?)", subQuery)) } @@ -115,6 +117,21 @@ func (r *UserPatientDao) GetUserPatientPageSearch(req requests.GetUserPatientPag query = query.Where("status = ?", req.Status) } + // 注册时间 + if req.CreatedAt != "" { + cancelTime := strings.Split(req.CreatedAt, "&") + if len(cancelTime) == 2 { + startTime, _ := time.Parse("2006-01-02", cancelTime[0]) + endTime, _ := time.Parse("2006-01-02", cancelTime[1]) + + if startTime == endTime { + endTime = endTime.Add(23*time.Hour + 59*time.Minute + 59*time.Second) + } + + query = query.Where("created_at BETWEEN ? AND ?", startTime, endTime) + } + } + // 排序 query = query.Order("created_at desc") diff --git a/api/model/patientFamily.go b/api/model/patientFamily.go index 836c02a..5d251a9 100644 --- a/api/model/patientFamily.go +++ b/api/model/patientFamily.go @@ -8,33 +8,34 @@ import ( // PatientFamily 患者家庭成员信息表-基本信息 type PatientFamily struct { - FamilyId int64 `gorm:"column:family_id;type:bigint(19);primary_key;comment:主键id" json:"family_id"` - PatientId int64 `gorm:"column:patient_id;type:bigint(19);comment:患者id" json:"patient_id"` - Relation int `gorm:"column:relation;type:tinyint(1);comment:与患者关系(1:本人 2:父母 3:爱人 4:子女 5:亲戚 6:其他 )" json:"relation"` - Status int `gorm:"column:status;type:tinyint(1);default:1;comment:状态(1:正常 2:删除)" json:"status"` - IsDefault int `gorm:"column:is_default;type:tinyint(1);default:0;comment:是否默认(0:否 1:是)" json:"is_default"` - CardName string `gorm:"column:card_name;type:varchar(50);comment:姓名" json:"card_name"` - CardNameMask string `gorm:"column:card_name_mask;type:varchar(50);comment:姓名(掩码)" json:"card_name_mask"` - Mobile string `gorm:"column:mobile;type:varchar(20);comment:电话" json:"mobile"` - MobileMask string `gorm:"column:mobile_mask;type:varchar(20);comment:电话(掩码)" json:"mobile_mask"` - Type int `gorm:"column:type;type:tinyint(1);default:1;comment:身份类型(1:身份证 2:护照 3:港澳通行证 4:台胞证)" json:"type"` - IdNumber string `gorm:"column:id_number;type:varchar(30);comment:证件号码" json:"id_number"` - IdNumberMask string `gorm:"column:id_number_mask;type:varchar(30);comment:证件号码(掩码)" json:"id_number_mask"` - Sex int `gorm:"column:sex;type:tinyint(1);default:0;comment:性别(0:未知 1:男 2:女)" json:"sex"` - Age int `gorm:"column:age;type:int(11);comment:年龄" json:"age"` - ProvinceId int `gorm:"column:province_id;type:int(11);comment:省份id" json:"province_id"` - Province string `gorm:"column:province;type:varchar(50);comment:省份" json:"province"` - CityId int `gorm:"column:city_id;type:int(11);comment:城市id" json:"city_id"` - City string `gorm:"column:city;type:varchar(50);comment:城市" json:"city"` - CountyId int `gorm:"column:county_id;type:int(11);comment:区县id" json:"county_id"` - County string `gorm:"column:county;type:varchar(255);comment:区县" json:"county"` - Height string `gorm:"column:height;type:varchar(100);comment:身高(cm)" json:"height"` - Weight string `gorm:"column:weight;type:varchar(100);comment:体重(kg)" json:"weight"` - MaritalStatus int `gorm:"column:marital_status;type:tinyint(1);comment:婚姻状况(0:未婚 1:已婚 2:离异)" json:"marital_status"` - NationId int64 `gorm:"column:nation_id;type:bigint(19);comment:民族" json:"nation_id"` - NationName string `gorm:"column:nation_name;type:varchar(50);comment:民族名称" json:"nation_name"` - JobId int64 `gorm:"column:job_id;type:bigint(19);comment:职业" json:"job_id"` - JobName string `gorm:"column:job_name;type:varchar(50);comment:职业名称" json:"job_name"` + FamilyId int64 `gorm:"column:family_id;type:bigint(19);primary_key;comment:主键id" json:"family_id"` + PatientId int64 `gorm:"column:patient_id;type:bigint(19);comment:患者id" json:"patient_id"` + Relation int `gorm:"column:relation;type:tinyint(1);comment:与患者关系(1:本人 2:父母 3:爱人 4:子女 5:亲戚 6:其他 )" json:"relation"` + Status int `gorm:"column:status;type:tinyint(1);default:1;comment:状态(1:正常 2:删除)" json:"status"` + IsDefault int `gorm:"column:is_default;type:tinyint(1);default:0;comment:是否默认(0:否 1:是)" json:"is_default"` + CardName string `gorm:"column:card_name;type:varchar(50);comment:姓名" json:"card_name"` + CardNameMask string `gorm:"column:card_name_mask;type:varchar(50);comment:姓名(掩码)" json:"card_name_mask"` + Mobile string `gorm:"column:mobile;type:varchar(20);comment:电话" json:"mobile"` + MobileMask string `gorm:"column:mobile_mask;type:varchar(20);comment:电话(掩码)" json:"mobile_mask"` + Type int `gorm:"column:type;type:tinyint(1);default:1;comment:身份类型(1:身份证 2:护照 3:港澳通行证 4:台胞证)" json:"type"` + IdNumber string `gorm:"column:id_number;type:varchar(30);comment:证件号码" json:"id_number"` + IdNumberMask string `gorm:"column:id_number_mask;type:varchar(30);comment:证件号码(掩码)" json:"id_number_mask"` + Sex int `gorm:"column:sex;type:tinyint(1);default:0;comment:性别(0:未知 1:男 2:女)" json:"sex"` + Age int `gorm:"column:age;type:int(11);comment:年龄" json:"age"` + ProvinceId int `gorm:"column:province_id;type:int(11);comment:省份id" json:"province_id"` + Province string `gorm:"column:province;type:varchar(50);comment:省份" json:"province"` + CityId int `gorm:"column:city_id;type:int(11);comment:城市id" json:"city_id"` + City string `gorm:"column:city;type:varchar(50);comment:城市" json:"city"` + CountyId int `gorm:"column:county_id;type:int(11);comment:区县id" json:"county_id"` + County string `gorm:"column:county;type:varchar(255);comment:区县" json:"county"` + Height string `gorm:"column:height;type:varchar(100);comment:身高(cm)" json:"height"` + Weight string `gorm:"column:weight;type:varchar(100);comment:体重(kg)" json:"weight"` + MaritalStatus int `gorm:"column:marital_status;type:tinyint(1);comment:婚姻状况(0:未婚 1:已婚 2:离异)" json:"marital_status"` + NationId int64 `gorm:"column:nation_id;type:bigint(19);comment:民族" json:"nation_id"` + NationName string `gorm:"column:nation_name;type:varchar(50);comment:民族名称" json:"nation_name"` + JobId int64 `gorm:"column:job_id;type:bigint(19);comment:职业" json:"job_id"` + JobName string `gorm:"column:job_name;type:varchar(50);comment:职业名称" json:"job_name"` + UserPatient *UserPatient `gorm:"foreignKey:PatientId;references:patient_id" json:"user_patient"` // 患者 Model } diff --git a/api/requests/patientFamily.go b/api/requests/patientFamily.go new file mode 100644 index 0000000..c662e17 --- /dev/null +++ b/api/requests/patientFamily.go @@ -0,0 +1,15 @@ +package requests + +type PatientFamilyRequest struct { + GetPatientFamilyPage // 获取就诊人列表-分页 +} + +// GetPatientFamilyPage 获取就诊人列表-分页 +type GetPatientFamilyPage struct { + Page int `json:"page" form:"page" label:"页码"` + PageSize int `json:"page_size" form:"page_size" label:"每页个数"` + UserName string `json:"user_name" form:"user_name" label:"用户名称"` + Status *int `json:"status" form:"status" label:"状态"` // (0:禁用 1:正常 2:删除) + Mobile string `json:"mobile" form:"mobile" label:"手机号"` + CreatedAt string `json:"created_at" form:"created_at" label:"添加时间"` // 时间区间,数组形式,下标0为开始时间,下标1为结束时间 +} diff --git a/api/requests/userPatient.go b/api/requests/userPatient.go index 30a0379..6fb4d3c 100644 --- a/api/requests/userPatient.go +++ b/api/requests/userPatient.go @@ -1,7 +1,8 @@ package requests type UserPatientRequest struct { - GetUserPatientPage // 获取患者列表-分页 + GetUserPatientPage // 获取患者列表-分页 + PutUserDoctorStatus // 修改患者状态 } // GetUserPatientPage 获取患者列表-分页 @@ -11,5 +12,11 @@ type GetUserPatientPage struct { UserName string `json:"user_name" form:"user_name" label:"用户名称"` Status *int `json:"status" form:"status" label:"状态"` // (0:禁用 1:正常 2:删除) Mobile string `json:"mobile" form:"mobile" label:"手机号"` - CreatedAt string `json:"created_at" form:"created_at" label:"订单创建时间"` // 时间区间,数组形式,下标0为开始时间,下标1为结束时间 + CreatedAt string `json:"created_at" form:"created_at" label:"注册时间"` // 时间区间,数组形式,下标0为开始时间,下标1为结束时间 +} + +// PutUserDoctorStatus 修改患者状态 +type PutUserDoctorStatus struct { + Status int `json:"status" form:"status" validate:"oneof=0 1 2" label:"状态"` // (0:禁用 1:正常 2:删除) + DisableReason string `json:"disable_reason" form:"disable_reason" label:"禁用理由"` } diff --git a/api/responses/patientFamilyResponse/patientFamily.go b/api/responses/patientFamilyResponse/patientFamily.go index 5bd3549..c25f1a0 100644 --- a/api/responses/patientFamilyResponse/patientFamily.go +++ b/api/responses/patientFamilyResponse/patientFamily.go @@ -53,6 +53,19 @@ type GetUserPatient struct { UpdatedAt model.LocalTime `json:"updated_at"` // 更新时间 } +// 获取就诊人列表-分页 +type getPatientFamilyPage struct { + FamilyId string `json:"family_id"` // 主键id + PatientId string `json:"patient_id"` // 患者id + Relation *int `json:"relation"` // 与患者关系(1:本人 2:父母 3:爱人 4:子女 5:亲戚 6:其他) + Status *int `json:"status"` // 状态(1:正常 2:删除) + CardName string `json:"card_name"` // 姓名 + MobileMask string `json:"mobile_mask"` // 电话(掩码) + UserName string `json:"user_name"` // 账号名称 + CreatedAt model.LocalTime `json:"created_at"` // 创建时间 + UpdatedAt model.LocalTime `json:"updated_at"` // 更新时间 +} + // GetUserPatientResponse 获取患者详情 func GetUserPatientResponse(patientFamilys []*model.PatientFamily) []*GetUserPatient { // 处理返回值 @@ -82,3 +95,31 @@ func GetUserPatientResponse(patientFamilys []*model.PatientFamily) []*GetUserPat return items } + +// GetPatientFamilyPageResponse 获取用户列表-分页 +func GetPatientFamilyPageResponse(patientFamily []*model.PatientFamily) []getPatientFamilyPage { + // 处理返回值 + patientFamilyResponses := make([]getPatientFamilyPage, len(patientFamily)) + + if len(patientFamily) > 0 { + for i, v := range patientFamily { + // 将原始结构体转换为新结构体 + // 将原始结构体转换为新结构体 + patientFamilyResponse := getPatientFamilyPage{ + FamilyId: fmt.Sprintf("%d", v.FamilyId), + PatientId: fmt.Sprintf("%d", v.PatientId), + Relation: &v.Relation, + Status: &v.Status, + CardName: v.CardName, + MobileMask: v.MobileMask, + UserName: v.UserPatient.UserName, + CreatedAt: v.CreatedAt, + UpdatedAt: v.UpdatedAt, + } + + // 将转换后的结构体添加到新切片中 + patientFamilyResponses[i] = patientFamilyResponse + } + } + return patientFamilyResponses +} diff --git a/api/router/router.go b/api/router/router.go index 6950a0f..ea93fee 100644 --- a/api/router/router.go +++ b/api/router/router.go @@ -422,22 +422,17 @@ func privateRouter(r *gin.Engine, api controller.Api) { patientGroup.GET("/:patient_id", api.UserPatient.GetUserPatient) // 修改患者状态 - patientGroup.PUT("/status/:patient_id", api.UserDoctor.GetUserDoctor) + patientGroup.PUT("/status/:patient_id", api.UserPatient.PutUserDoctorStatus) // 就诊人管理 - patientFamilyGroup := doctorGroup.Group("/family") + patientFamilyGroup := patientGroup.Group("/family") { // 获取就诊人列表-分页 - patientFamilyGroup.GET("", api.UserDoctor.GetUserDoctorPendingPage) + patientFamilyGroup.GET("", api.PatientFamily.GetPatientFamilyPage) // 就诊人详情 patientFamilyGroup.GET("/:family_id", api.UserDoctor.GetUserDoctorPending) - // 删除就诊人 - patientFamilyGroup.DELETE("/:family_id", api.UserDoctor.PutUserDoctorPending) - - // 修改就诊人状态 - patientFamilyGroup.DELETE("/status/:family_id", api.UserDoctor.PutUserDoctorPending) } } } diff --git a/api/service/patientFamily.go b/api/service/patientFamily.go index 087198b..c994e1f 100644 --- a/api/service/patientFamily.go +++ b/api/service/patientFamily.go @@ -10,7 +10,7 @@ type PatientFamilyService struct { } // GetPatientFamilyListByPatientId 获取患者家庭成员-患者id -func (r *PatientFamilyService) GetPatientFamilyListByPatientId(patientId int64) (u []*patientFamilyResponse.GetUserPatient, err error) { +func (r *PatientFamilyService) GetPatientFamilyListByPatientId(patientId int64) (u []*patientFamilyResponse.PatientFamily, err error) { patientFamilyDao := dao.PatientFamilyDao{} patientFamilys, err := patientFamilyDao.GetPatientFamilyListByPatientId(patientId) @@ -19,24 +19,41 @@ func (r *PatientFamilyService) GetPatientFamilyListByPatientId(patientId int64) } // 处理返回值 - items := make([]*patientFamilyResponse.GetUserPatient, len(patientFamilys)) + items := make([]*patientFamilyResponse.PatientFamily, len(patientFamilys)) if len(patientFamilys) > 0 { for i, v := range patientFamilys { // 将原始结构体转换为新结构体 - item := &patientFamilyResponse.GetUserPatient{ - FamilyId: fmt.Sprintf("%d", v.FamilyId), - PatientId: fmt.Sprintf("%d", v.PatientId), - Relation: &v.Relation, - Status: &v.Status, - IsDefault: &v.IsDefault, - CardNameMask: v.CardNameMask, - MobileMask: v.MobileMask, - IdNumberMask: v.IdNumberMask, - Sex: &v.Sex, - Age: v.Age, - CreatedAt: v.CreatedAt, - UpdatedAt: v.UpdatedAt, + item := &patientFamilyResponse.PatientFamily{ + FamilyId: fmt.Sprintf("%d", v.FamilyId), + PatientId: fmt.Sprintf("%d", v.PatientId), + Relation: &v.Relation, + Status: &v.Status, + IsDefault: &v.IsDefault, + CardName: v.CardName, + CardNameMask: v.CardNameMask, + Mobile: v.Mobile, + MobileMask: v.MobileMask, + Type: &v.Type, + IdNumber: v.IdNumber, + IdNumberMask: v.IdNumberMask, + Sex: &v.Sex, + Age: v.Age, + ProvinceId: fmt.Sprintf("%d", v.ProvinceId), + Province: v.Province, + CityId: fmt.Sprintf("%d", v.CityId), + City: v.City, + CountyId: fmt.Sprintf("%d", v.CountyId), + County: v.County, + Height: v.Height, + Weight: v.Weight, + MaritalStatus: &v.MaritalStatus, + NationId: fmt.Sprintf("%d", v.NationId), + NationName: v.NationName, + JobId: fmt.Sprintf("%d", v.JobId), + JobName: v.JobName, + CreatedAt: v.CreatedAt, + UpdatedAt: v.UpdatedAt, } // 将转换后的结构体添加到新切片中 diff --git a/api/service/userPatient.go b/api/service/userPatient.go index 4a43ec2..ab136a2 100644 --- a/api/service/userPatient.go +++ b/api/service/userPatient.go @@ -4,10 +4,12 @@ import ( "errors" "fmt" "hospital-admin-api/api/dao" + "hospital-admin-api/api/requests" "hospital-admin-api/api/responses/patientFamilyResponse" "hospital-admin-api/api/responses/userPatientResponse" "hospital-admin-api/api/responses/userResponse" "hospital-admin-api/api/responses/userShipAddressResponse" + "hospital-admin-api/global" "hospital-admin-api/utils" ) @@ -56,3 +58,46 @@ func (r *UserPatientService) GetUserPatient(patientId int64) (getUserPatientResp return getUserPatientResponse, nil } + +// PutUserDoctorStatus 修改患者状态 +func (r *UserPatientService) PutUserDoctorStatus(patientId int64, req requests.PutUserDoctorStatus) (res bool, err error) { + // 获取患者数据 + userPatientDao := dao.UserPatientDao{} + userPatient, err := userPatientDao.GetUserPatientPreloadById(patientId) + if err != nil || userPatient == nil { + return false, errors.New("患者错误") + } + + if req.Status == userPatient.Status { + return true, nil + } + + if req.Status == 0 { + if req.DisableReason == "" { + return false, errors.New("请填写禁用理由") + } + } + + // 开始事务 + tx := global.Db.Begin() + defer func() { + if r := recover(); r != nil { + tx.Rollback() + } + }() + + // 修改部门 + data := make(map[string]interface{}) + data["status"] = req.Status + data["disable_reason"] = req.DisableReason + + err = userPatientDao.EditUserPatientById(tx, patientId, data) + if err != nil { + tx.Rollback() + return false, errors.New("修改失败") + } + + tx.Commit() + + return true, nil +}