diff --git a/api/controller/base.go b/api/controller/base.go index 07d595d..822969f 100644 --- a/api/controller/base.go +++ b/api/controller/base.go @@ -5,4 +5,5 @@ type Api struct { Basic // 基础数据 Role // 角色数据 Menu // 菜单数据 + User // 用户数据 } diff --git a/api/controller/role.go b/api/controller/role.go index 6af49df..997b977 100644 --- a/api/controller/role.go +++ b/api/controller/role.go @@ -39,29 +39,29 @@ func (r *Role) GetRoleMenuList(c *gin.Context) { responses.OkWithData(roleMenuList, c) } -// GetRolePage 搜索角色列表 +// GetRolePage 获取角色列表-分页 func (r *Role) GetRolePage(c *gin.Context) { - if err := c.ShouldBind(&RoleRequest.GetRoleList); err != nil { + if err := c.ShouldBind(&RoleRequest.GetRolePage); err != nil { responses.FailWithMessage(err.Error(), c) return } // 参数验证 - if err := global.Validate.Struct(RoleRequest.GetRoleList); err != nil { + if err := global.Validate.Struct(RoleRequest.GetRolePage); err != nil { responses.FailWithMessage(utils.Translate(err), c) return } - if RoleRequest.GetRoleList.Page == 0 { - RoleRequest.GetRoleList.Page = 1 + if RoleRequest.GetRolePage.Page == 0 { + RoleRequest.GetRolePage.Page = 1 } - if RoleRequest.GetRoleList.PageSize == 0 { - RoleRequest.GetRoleList.PageSize = 20 + if RoleRequest.GetRolePage.PageSize == 0 { + RoleRequest.GetRolePage.PageSize = 20 } AdminRoleDao := dao.AdminRoleDao{} - adminRole, total, err := AdminRoleDao.GetAdminRolePageSearch(RoleRequest.GetRoleList.RoleName, RoleRequest.GetRoleList.Page, RoleRequest.GetRoleList.PageSize) + adminRole, total, err := AdminRoleDao.GetAdminRolePageSearch(RoleRequest.GetRolePage.RoleName, RoleRequest.GetRolePage.Page, RoleRequest.GetRolePage.PageSize) if err != nil { responses.FailWithMessage(err.Error(), c) @@ -70,8 +70,8 @@ func (r *Role) GetRolePage(c *gin.Context) { } result := make(map[string]interface{}) - result["page"] = RoleRequest.GetRoleList.Page - result["page_size"] = RoleRequest.GetRoleList.PageSize + result["page"] = RoleRequest.GetRolePage.Page + result["page_size"] = RoleRequest.GetRolePage.PageSize result["total"] = total result["data"] = adminRole responses.OkWithData(result, c) diff --git a/api/controller/user.go b/api/controller/user.go new file mode 100644 index 0000000..2fd4e86 --- /dev/null +++ b/api/controller/user.go @@ -0,0 +1,52 @@ +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/global" + "hospital-admin-api/utils" +) + +type User struct{} + +var UserRequest requests.UserRequest + +// GetUserPage 获取用户列表-分页 +func (r *User) GetUserPage(c *gin.Context) { + if err := c.ShouldBind(&UserRequest.GetUserPage); err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + // 参数验证 + if err := global.Validate.Struct(UserRequest.GetUserPage); err != nil { + responses.FailWithMessage(utils.Translate(err), c) + return + } + + if UserRequest.GetUserPage.Page == 0 { + UserRequest.GetUserPage.Page = 1 + } + + if UserRequest.GetUserPage.PageSize == 0 { + UserRequest.GetUserPage.PageSize = 20 + } + + AdminUserDao := dao.AdminUserDao{} + adminUser, total, err := AdminUserDao.GetAdminUserPageSearch(UserRequest.GetUserPage, UserRequest.GetUserPage.Page, UserRequest.GetUserPage.PageSize) + + if err != nil { + responses.FailWithMessage(err.Error(), c) + c.Abort() + return + } + + result := make(map[string]interface{}) + result["page"] = UserRequest.GetUserPage.Page + result["page_size"] = UserRequest.GetUserPage.PageSize + result["total"] = total + result["data"] = adminUser + responses.OkWithData(result, c) +} diff --git a/api/dao/adminRole.go b/api/dao/adminRole.go index ede7d97..a7dec5a 100644 --- a/api/dao/adminRole.go +++ b/api/dao/adminRole.go @@ -36,7 +36,7 @@ func (r *AdminRoleDao) AddAdminRole(tx *gorm.DB, model *model.AdminRole) (*model return model, nil } -// GetAdminRolePageSearch 搜索角色列表-分页 +// GetAdminRolePageSearch 获取角色列表-分页 func (r *AdminRoleDao) GetAdminRolePageSearch(roleName string, page, pageSize int) (m []*model.AdminRole, total int64, err error) { var totalRecords int64 diff --git a/api/dao/adminUser.go b/api/dao/adminUser.go index b8bafe4..2f297e2 100644 --- a/api/dao/adminUser.go +++ b/api/dao/adminUser.go @@ -3,6 +3,7 @@ package dao import ( "gorm.io/gorm" "hospital-admin-api/api/model" + "hospital-admin-api/api/requests" "hospital-admin-api/global" ) @@ -63,3 +64,49 @@ func (r *AdminUserDao) GetAdminUserList(maps interface{}) (m []*model.AdminUser, } return m, nil } + +// GetAdminUserPageSearch 获取用户列表-分页 +func (r *AdminUserDao) GetAdminUserPageSearch(getUserPage requests.GetUserPage, page, pageSize int) (m []*model.AdminUser, total int64, err error) { + var totalRecords int64 + + // 构建查询条件 + query := global.Db.Model(&model.AdminUser{}).Preload("Role") + if getUserPage.PostName != "" { + query = query.Preload("Post", "post_name like ?", "%"+getUserPage.PostName+"%") + } + + if getUserPage.DeptName != "" { + query = query.Preload("Dept", "dept_name like ?", "%"+getUserPage.DeptName+"%") + } + + if getUserPage.NickName != "" { + query = query.Where("nick_name = ?", getUserPage.NickName) + } + + if getUserPage.Phone != "" { + query = query.Where("phone = ?", getUserPage.Phone) + } + + if getUserPage.Status != 0 { + query = query.Where("status = ?", getUserPage.Status) + } + + if getUserPage.DeptId != 0 { + query = query.Where("dept_id = ?", getUserPage.DeptId) + } + + if getUserPage.PostId != 0 { + query = query.Where("post_id = ?", getUserPage.PostId) + } + + // 查询总数量 + 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/model/adminUser.go b/api/model/adminUser.go index 84de331..91df146 100644 --- a/api/model/adminUser.go +++ b/api/model/adminUser.go @@ -3,21 +3,24 @@ package model // AdminUser 后台-用户表 type AdminUser struct { Model - UserID int64 `gorm:"column:user_id;type:bigint(19);primary_key;comment:'主键id'" json:"user_id"` - Access string `gorm:"column:access;type:varchar(64);comment:'账号'" json:"access"` - Password string `gorm:"column:password;type:varchar(128);comment:'密码'" json:"password"` - Salt string `gorm:"column:salt;type:varchar(255);comment:'密码掩码'" json:"salt"` - Status int `gorm:"column:status;type:tinyint(1);default:2;comment:'状态(1:正常 2:审核中 3:审核失败)'" json:"status"` - IsDeleted int `gorm:"column:is_deleted;type:tinyint(1);default:0;comment:'是否被删除(0:否 1:是)'" json:"is_deleted"` - IsDisabled int `gorm:"column:is_disabled;type:tinyint(1);default:0;comment:'是否被禁用(0:否 1:是)'" json:"is_disabled"` - NickName string `gorm:"column:nick_name;type:varchar(255);comment:'昵称'" json:"nick_name"` - Phone string `gorm:"column:phone;type:varchar(11);comment:'手机号'" json:"phone"` - Avatar string `gorm:"column:avatar;type:varchar(255);comment:'头像'" json:"avatar"` - Sex int `gorm:"column:sex;type:tinyint(1);comment:'性别(1:男 2:女)'" json:"sex"` - Email string `gorm:"column:email;type:varchar(100);comment:'邮箱'" json:"email"` - RoleID int64 `gorm:"column:role_id;type:bigint(19);comment:'角色表'" json:"role_id"` - DeptID int64 `gorm:"column:dept_id;type:bigint(19);comment:'部门id'" json:"dept_id"` - PostID int64 `gorm:"column:post_id;type:bigint(19);comment:'岗位id'" json:"post_id"` + UserID int64 `gorm:"column:user_id;type:bigint(19);primary_key;comment:'主键id'" json:"user_id"` + Access string `gorm:"column:access;type:varchar(64);comment:'账号'" json:"access"` + Password string `gorm:"column:password;type:varchar(128);comment:'密码'" json:"password"` + Salt string `gorm:"column:salt;type:varchar(255);comment:'密码掩码'" json:"salt"` + Status int `gorm:"column:status;type:tinyint(1);default:2;comment:'状态(1:正常 2:审核中 3:审核失败)'" json:"status"` + IsDeleted int `gorm:"column:is_deleted;type:tinyint(1);default:0;comment:'是否被删除(0:否 1:是)'" json:"is_deleted"` + IsDisabled int `gorm:"column:is_disabled;type:tinyint(1);default:0;comment:'是否被禁用(0:否 1:是)'" json:"is_disabled"` + NickName string `gorm:"column:nick_name;type:varchar(255);comment:'昵称'" json:"nick_name"` + Phone string `gorm:"column:phone;type:varchar(11);comment:'手机号'" json:"phone"` + Avatar string `gorm:"column:avatar;type:varchar(255);comment:'头像'" json:"avatar"` + Sex int `gorm:"column:sex;type:tinyint(1);comment:'性别(1:男 2:女)'" json:"sex"` + Email string `gorm:"column:email;type:varchar(100);comment:'邮箱'" json:"email"` + RoleID int64 `gorm:"column:role_id;type:bigint(19);comment:'角色表'" json:"role_id"` + DeptID int64 `gorm:"column:dept_id;type:bigint(19);comment:'部门id'" json:"dept_id"` + PostID int64 `gorm:"column:post_id;type:bigint(19);comment:'岗位id'" json:"post_id"` + Role *AdminRole `gorm:"foreignKey:RoleID"` // 角色 + Dept *AdminMenu `gorm:"foreignKey:DeptID"` // 部门 + Post *AdminPost `gorm:"foreignKey:PostID"` // 岗位 } func (m *AdminUser) TableName() string { diff --git a/api/requests/role.go b/api/requests/role.go index 037159d..b96da67 100644 --- a/api/requests/role.go +++ b/api/requests/role.go @@ -1,15 +1,15 @@ package requests type RoleRequest struct { - GetRoleList // 获取角色列表 + GetRolePage // 获取角色列表-分页 PutRoleStatus // 角色禁用/启用 AddRole // 新增角色 PutRole // 修改角色 DeleteRole // 修改角色 } -// GetRoleList 获取角色列表 -type GetRoleList struct { +// GetRolePage 获取角色列表-分页 +type GetRolePage struct { RoleName string `json:"role_name" form:"role_name" label:"角色名称"` Page int `json:"page" form:"page" label:"页码"` PageSize int `json:"page_size" form:"page_size" label:"每页个数"` diff --git a/api/requests/user.go b/api/requests/user.go new file mode 100644 index 0000000..ce0b4c1 --- /dev/null +++ b/api/requests/user.go @@ -0,0 +1,18 @@ +package requests + +type UserRequest struct { + GetUserPage // 获取用户列表-分页 +} + +// GetUserPage 获取用户列表-分页 +type GetUserPage struct { + PostName string `json:"post_name" form:"post_name" label:"岗位名称"` + DeptName string `json:"dept_name" form:"dept_name" label:"部门名称"` + NickName string `json:"nick_name" form:"nick_name" label:"用户昵称"` + Phone string `json:"phone" form:"phone" label:"手机号"` + Status int `json:"status" form:"status" label:"用户状态"` // (1:正常 2:审核中 3:审核失败) + DeptId int64 `json:"dept_id" form:"dept_id" label:"部门"` + PostId int64 `json:"post_id" form:"post_id" label:"岗位"` + Page int `json:"page" form:"page" label:"页码"` + PageSize int `json:"page_size" form:"page_size" label:"每页个数"` +} diff --git a/api/router/router.go b/api/router/router.go index 9073582..16da8de 100644 --- a/api/router/router.go +++ b/api/router/router.go @@ -75,7 +75,7 @@ func privateRouter(r *gin.Engine, api controller.Api) { // 获取登陆角色菜单列表 roleGroup.GET("menu", api.Role.GetRoleMenuList) - // 搜索角色列表-分页 + // 获取角色列表-分页 roleGroup.GET("", api.Role.GetRolePage) // 角色禁用/启用 @@ -109,4 +109,20 @@ func privateRouter(r *gin.Engine, api controller.Api) { // 删除菜单-批量 menuGroup.DELETE("", api.Menu.DeleteMenu) } + + // 用户 + userGroup := adminGroup.Group("/user") + { + // 获取用户列表-分页 + userGroup.GET("", api.User.GetUserPage) + + // 新增用户 + userGroup.POST("", api.User.GetUserPage) + + // 修改用户 + userGroup.PUT("/:menu_id", api.User.GetUserPage) + + // 删除用户-批量 + userGroup.DELETE("", api.User.GetUserPage) + } }