新增岗位接口
This commit is contained in:
parent
002353cc30
commit
a2a1f1666c
@ -8,4 +8,5 @@ type Api struct {
|
|||||||
User // 用户数据
|
User // 用户数据
|
||||||
AdminApi // 接口数据
|
AdminApi // 接口数据
|
||||||
Dept // 部门数据
|
Dept // 部门数据
|
||||||
|
Post // 岗位数据
|
||||||
}
|
}
|
||||||
|
|||||||
209
api/controller/post.go
Normal file
209
api/controller/post.go
Normal file
@ -0,0 +1,209 @@
|
|||||||
|
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/postResponse"
|
||||||
|
"hospital-admin-api/api/service"
|
||||||
|
"hospital-admin-api/global"
|
||||||
|
"hospital-admin-api/utils"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Post struct{}
|
||||||
|
|
||||||
|
// GetPostPage 获取部门列表-分页
|
||||||
|
func (r *Post) GetPostPage(c *gin.Context) {
|
||||||
|
postRequest := requests.PostRequest{}
|
||||||
|
if err := c.ShouldBind(&postRequest.GetPostPage); err != nil {
|
||||||
|
responses.FailWithMessage(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 参数验证
|
||||||
|
if err := global.Validate.Struct(postRequest.GetPostPage); err != nil {
|
||||||
|
responses.FailWithMessage(utils.Translate(err), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if postRequest.GetPostPage.Page == 0 {
|
||||||
|
postRequest.GetPostPage.Page = 1
|
||||||
|
}
|
||||||
|
|
||||||
|
if postRequest.GetPostPage.PageSize == 0 {
|
||||||
|
postRequest.GetPostPage.PageSize = 20
|
||||||
|
}
|
||||||
|
|
||||||
|
adminPostDao := dao.AdminPostDao{}
|
||||||
|
adminPost, total, err := adminPostDao.GetAdminPostPageSearch(postRequest.GetPostPage, postRequest.GetPostPage.Page, postRequest.GetPostPage.PageSize)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
responses.FailWithMessage(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理返回值
|
||||||
|
getPostPageResponses := postResponse.GetPostPageResponse(adminPost)
|
||||||
|
|
||||||
|
result := make(map[string]interface{})
|
||||||
|
result["page"] = postRequest.GetPostPage.Page
|
||||||
|
result["page_size"] = postRequest.GetPostPage.PageSize
|
||||||
|
result["total"] = total
|
||||||
|
result["data"] = getPostPageResponses
|
||||||
|
responses.OkWithData(result, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddPost 新增部门
|
||||||
|
func (r *Post) AddPost(c *gin.Context) {
|
||||||
|
deptRequest := requests.PostRequest{}
|
||||||
|
if err := c.ShouldBind(&deptRequest.AddPost); err != nil {
|
||||||
|
responses.FailWithMessage(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 参数验证
|
||||||
|
if err := global.Validate.Struct(deptRequest.AddPost); err != nil {
|
||||||
|
responses.FailWithMessage(utils.Translate(err), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取角色身份
|
||||||
|
roleService := service.RoleService{}
|
||||||
|
isAdmin, err := roleService.GetRoleIden(c)
|
||||||
|
if err != nil {
|
||||||
|
responses.FailWithMessage(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !isAdmin {
|
||||||
|
responses.FailWithMessage("非管理员,无法添加部门", c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 业务处理
|
||||||
|
deptService := service.PostService{}
|
||||||
|
_, err = deptService.AddPost(deptRequest.AddPost)
|
||||||
|
if err != nil {
|
||||||
|
responses.FailWithMessage(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
responses.Ok(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetPost 部门详情
|
||||||
|
func (r *Post) GetPost(c *gin.Context) {
|
||||||
|
id := c.Param("post_id")
|
||||||
|
if id == "" {
|
||||||
|
responses.FailWithMessage("缺少参数", c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将 id 转换为 int64 类型
|
||||||
|
postId, err := strconv.ParseInt(id, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
responses.Fail(c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取部门详情
|
||||||
|
adminPostDao := dao.AdminPostDao{}
|
||||||
|
adminPost, err := adminPostDao.GetAdminPostById(postId)
|
||||||
|
if err != nil || adminPost == nil {
|
||||||
|
responses.FailWithMessage("部门数据错误", c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 处理返回值
|
||||||
|
getPostResponses := postResponse.GetPostResponse(adminPost)
|
||||||
|
|
||||||
|
responses.OkWithData(getPostResponses, c)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeletePost 删除部门-批量
|
||||||
|
func (r *Post) DeletePost(c *gin.Context) {
|
||||||
|
deptRequest := requests.PostRequest{}
|
||||||
|
if err := c.ShouldBindJSON(&deptRequest.DeletePost); err != nil {
|
||||||
|
responses.FailWithMessage(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 参数验证
|
||||||
|
if err := global.Validate.Struct(deptRequest.DeletePost); err != nil {
|
||||||
|
responses.FailWithMessage(utils.Translate(err), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取角色身份
|
||||||
|
roleService := service.RoleService{}
|
||||||
|
isAdmin, err := roleService.GetRoleIden(c)
|
||||||
|
if err != nil {
|
||||||
|
responses.FailWithMessage(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !isAdmin {
|
||||||
|
responses.FailWithMessage("非管理员,无法添加部门", c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 业务处理
|
||||||
|
postService := service.PostService{}
|
||||||
|
_, err = postService.DeletePost(deptRequest.DeletePost)
|
||||||
|
if err != nil {
|
||||||
|
responses.FailWithMessage(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
responses.Ok(c)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PutPost 修改部门
|
||||||
|
func (r *Post) PutPost(c *gin.Context) {
|
||||||
|
deptRequest := requests.PostRequest{}
|
||||||
|
if err := c.ShouldBind(&deptRequest.PutPost); err != nil {
|
||||||
|
responses.FailWithMessage(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 参数验证
|
||||||
|
if err := global.Validate.Struct(deptRequest.PutPost); err != nil {
|
||||||
|
responses.FailWithMessage(utils.Translate(err), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
id := c.Param("post_id")
|
||||||
|
if id == "" {
|
||||||
|
responses.FailWithMessage("缺少参数", c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将 id 转换为 int64 类型
|
||||||
|
postId, err := strconv.ParseInt(id, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
responses.Fail(c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 获取角色身份
|
||||||
|
roleService := service.RoleService{}
|
||||||
|
isAdmin, err := roleService.GetRoleIden(c)
|
||||||
|
if err != nil {
|
||||||
|
responses.FailWithMessage(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !isAdmin {
|
||||||
|
responses.FailWithMessage("非管理员,无法添加部门", c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// 业务处理
|
||||||
|
apiService := service.PostService{}
|
||||||
|
_, err = apiService.PutPost(postId, deptRequest.PutPost)
|
||||||
|
if err != nil {
|
||||||
|
responses.FailWithMessage(err.Error(), c)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
responses.Ok(c)
|
||||||
|
}
|
||||||
@ -1,18 +1,78 @@
|
|||||||
package dao
|
package dao
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"gorm.io/gorm"
|
||||||
"hospital-admin-api/api/model"
|
"hospital-admin-api/api/model"
|
||||||
|
"hospital-admin-api/api/requests"
|
||||||
"hospital-admin-api/global"
|
"hospital-admin-api/global"
|
||||||
)
|
)
|
||||||
|
|
||||||
type AdminPostDao struct {
|
type AdminPostDao struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAdminPostFirstById 获取岗位数据-部门id
|
// GetAdminPostById 获取岗位数据-岗位id
|
||||||
func (r *AdminPostDao) GetAdminPostFirstById(postId int64) (m *model.AdminPost, err error) {
|
func (r *AdminPostDao) GetAdminPostById(postId int64) (m *model.AdminPost, err error) {
|
||||||
err = global.Db.First(&m, postId).Error
|
err = global.Db.First(&m, postId).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetAdminPostList 获取岗位列表
|
||||||
|
func (r *AdminPostDao) GetAdminPostList(maps interface{}) (m []*model.AdminPost, err error) {
|
||||||
|
err = global.Db.Where(maps).Find(&m).Error
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddAdminPost 新增gan
|
||||||
|
func (r *AdminPostDao) AddAdminPost(tx *gorm.DB, model *model.AdminPost) (*model.AdminPost, error) {
|
||||||
|
if err := tx.Create(model).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return model, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeleteAdminPostById 删除岗位-岗位id
|
||||||
|
func (r *AdminPostDao) DeleteAdminPostById(tx *gorm.DB, deptId int64) error {
|
||||||
|
if err := tx.Delete(&model.AdminPost{}, deptId).Error; err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// EditAdminPostById 修改岗位-岗位id
|
||||||
|
func (r *AdminPostDao) EditAdminPostById(tx *gorm.DB, deptId int64, data interface{}) error {
|
||||||
|
err := tx.Model(&model.AdminPost{}).Where("dept_id = ?", deptId).Updates(data).Error
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetAdminPostPageSearch 获取岗位列表-分页
|
||||||
|
func (r *AdminPostDao) GetAdminPostPageSearch(getPostPage requests.GetPostPage, page, pageSize int) (m []*model.AdminPost, total int64, err error) {
|
||||||
|
var totalRecords int64
|
||||||
|
|
||||||
|
// 构建查询条件
|
||||||
|
query := global.Db.Model(&model.AdminPost{})
|
||||||
|
|
||||||
|
query = query.Where("dept_status = ? ", 1)
|
||||||
|
if getPostPage.DeptName != "" {
|
||||||
|
query = query.Where("dept_name like ?", "%"+getPostPage.DeptName+"%")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查询总数量
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|||||||
@ -79,9 +79,9 @@ func Auth() gin.HandlerFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 获取用户部门数据
|
// 获取用户部门数据
|
||||||
if adminUser.PostID != 0 {
|
if adminUser.DeptID != 0 {
|
||||||
adminDeptDao := dao.AdminDeptDao{}
|
adminDeptDao := dao.AdminDeptDao{}
|
||||||
adminDept, err := adminDeptDao.GetAdminDeptById(adminUser.PostID)
|
adminDept, err := adminDeptDao.GetAdminDeptById(adminUser.DeptID)
|
||||||
if err != nil || adminDept == nil {
|
if err != nil || adminDept == nil {
|
||||||
responses.FailWithMessage("用户部门数据错误", c)
|
responses.FailWithMessage("用户部门数据错误", c)
|
||||||
c.Abort()
|
c.Abort()
|
||||||
@ -95,6 +95,23 @@ func Auth() gin.HandlerFunc {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 获取用户部门数据
|
||||||
|
if adminUser.PostID != 0 {
|
||||||
|
adminPostDao := dao.AdminPostDao{}
|
||||||
|
adminPost, err := adminPostDao.GetAdminPostById(adminUser.PostID)
|
||||||
|
if err != nil || adminPost == nil {
|
||||||
|
responses.FailWithMessage("用户岗位数据错误", c)
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if adminPost.PostStatus == 2 {
|
||||||
|
responses.FailWithMessage("您所在的岗位已被删除,请联系管理员修改", c)
|
||||||
|
c.Abort()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 获取角色菜单id
|
// 获取角色菜单id
|
||||||
AdminRoleMenuDao := dao.AdminRoleMenuDao{}
|
AdminRoleMenuDao := dao.AdminRoleMenuDao{}
|
||||||
adminRoleMenu, _ := AdminRoleMenuDao.GetAdminRoleMenuListByRoleId(roleId)
|
adminRoleMenu, _ := AdminRoleMenuDao.GetAdminRoleMenuListByRoleId(roleId)
|
||||||
|
|||||||
@ -1,16 +1,11 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
import (
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
// AdminPost 后台-岗位表
|
// AdminPost 后台-岗位表
|
||||||
type AdminPost struct {
|
type AdminPost struct {
|
||||||
PostId int64 `gorm:"column:post_id;type:bigint(19);primary_key;comment:主键id" json:"post_id"`
|
PostId int64 `gorm:"column:post_id;type:bigint(19);primary_key;comment:主键id" json:"post_id"`
|
||||||
PostName string `gorm:"column:post_name;type:varchar(255);comment:部门名称" json:"post_name"`
|
PostName string `gorm:"column:post_name;type:varchar(255);comment:部门名称" json:"post_name"`
|
||||||
PostStatus int `gorm:"column:post_status;type:tinyint(1);default:1;comment:状态(1:正常 2:删除)" json:"post_status"`
|
PostStatus int `gorm:"column:post_status;type:tinyint(1);default:1;comment:状态(1:正常 2:删除)" json:"post_status"`
|
||||||
CreatedAt time.Time `gorm:"column:created_at;type:datetime;comment:创建时间" json:"created_at"`
|
Model
|
||||||
UpdatedAt time.Time `gorm:"column:updated_at;type:datetime;comment:修改时间" json:"updated_at"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *AdminPost) TableName() string {
|
func (m *AdminPost) TableName() string {
|
||||||
|
|||||||
32
api/requests/post.go
Normal file
32
api/requests/post.go
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
package requests
|
||||||
|
|
||||||
|
type PostRequest struct {
|
||||||
|
GetPostPage // 获取岗位列表-分页
|
||||||
|
AddPost // 新增岗位
|
||||||
|
DeletePost // 删除岗位-批量
|
||||||
|
PutPost // 修改岗位
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddPost 新增岗位
|
||||||
|
type AddPost struct {
|
||||||
|
PostName string `json:"dept_name" form:"dept_name" validate:"required" label:"岗位名称"`
|
||||||
|
PostStatus int `json:"dept_status" form:"dept_status" validate:"required,oneof=1 2" label:"岗位状态"` // (1:正常 2:删除)
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetPostPage 获取岗位列表-分页
|
||||||
|
type GetPostPage struct {
|
||||||
|
DeptName string `json:"dept_name" form:"dept_name" label:"api名称"`
|
||||||
|
Page int `json:"page" form:"page" label:"页码"`
|
||||||
|
PageSize int `json:"page_size" form:"page_size" label:"每页个数"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeletePost 删除岗位-批量
|
||||||
|
type DeletePost struct {
|
||||||
|
PostIds []string `json:"post_ids" form:"post_ids" validate:"required" label:"岗位id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// PutPost 修改岗位
|
||||||
|
type PutPost struct {
|
||||||
|
PostName string `json:"post_name" form:"post_name" validate:"required" label:"岗位名称"`
|
||||||
|
PostStatus int `json:"post_status" form:"post_status" validate:"required,oneof=1 2" label:"岗位状态"` // (1:正常 2:删除)
|
||||||
|
}
|
||||||
59
api/responses/postResponse/post.go
Normal file
59
api/responses/postResponse/post.go
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
package postResponse
|
||||||
|
|
||||||
|
import (
|
||||||
|
"hospital-admin-api/api/model"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// getPostPage 获取岗位列表-分页
|
||||||
|
type getPostPage struct {
|
||||||
|
PostId string `json:"post_id"` // 主键id
|
||||||
|
PostName string `json:"post_name"` // 岗位名称
|
||||||
|
DeptStatus int `json:"dept_status"` // 岗位状态(1:正常 2:删除)
|
||||||
|
CreatedAt model.LocalTime `json:"created_at"` // 创建时间
|
||||||
|
UpdatedAt model.LocalTime `json:"updated_at"` // 修改时间
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetPost 岗位详情
|
||||||
|
type GetPost struct {
|
||||||
|
PostId string `json:"post_id"` // 主键id
|
||||||
|
PostName string `json:"post_name"` // 岗位名称
|
||||||
|
DeptStatus int `json:"dept_status"` // 岗位状态(1:正常 2:删除)
|
||||||
|
CreatedAt model.LocalTime `json:"created_at"` // 创建时间
|
||||||
|
UpdatedAt model.LocalTime `json:"updated_at"` // 修改时间
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetPostPageResponse 获取接口列表-分页
|
||||||
|
func GetPostPageResponse(adminPost []*model.AdminPost) []getPostPage {
|
||||||
|
// 处理返回值
|
||||||
|
getPostPageResponses := make([]getPostPage, len(adminPost))
|
||||||
|
|
||||||
|
if len(adminPost) > 0 {
|
||||||
|
for i, v := range adminPost {
|
||||||
|
// 将原始结构体转换为新结构体
|
||||||
|
getPostPageResponse := getPostPage{
|
||||||
|
PostId: strconv.Itoa(int(v.PostId)),
|
||||||
|
PostName: v.PostName,
|
||||||
|
DeptStatus: v.PostStatus,
|
||||||
|
CreatedAt: v.CreatedAt,
|
||||||
|
UpdatedAt: v.UpdatedAt,
|
||||||
|
}
|
||||||
|
|
||||||
|
// 将转换后的结构体添加到新切片中
|
||||||
|
getPostPageResponses[i] = getPostPageResponse
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return getPostPageResponses
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetPostResponse 部门详情
|
||||||
|
func GetPostResponse(adminPost *model.AdminPost) *GetPost {
|
||||||
|
return &GetPost{
|
||||||
|
PostId: strconv.Itoa(int(adminPost.PostId)),
|
||||||
|
PostName: adminPost.PostName,
|
||||||
|
DeptStatus: adminPost.PostStatus,
|
||||||
|
CreatedAt: adminPost.CreatedAt,
|
||||||
|
UpdatedAt: adminPost.UpdatedAt,
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -169,4 +169,23 @@ func privateRouter(r *gin.Engine, api controller.Api) {
|
|||||||
// 修改部门
|
// 修改部门
|
||||||
deptGroup.PUT("/:dept_id", api.Dept.PutDept)
|
deptGroup.PUT("/:dept_id", api.Dept.PutDept)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 岗位
|
||||||
|
postGroup := adminGroup.Group("/post")
|
||||||
|
{
|
||||||
|
// 获取岗位列表-分页
|
||||||
|
postGroup.GET("", api.Post.GetPostPage)
|
||||||
|
|
||||||
|
// 新增岗位
|
||||||
|
postGroup.POST("", api.Post.AddPost)
|
||||||
|
|
||||||
|
// 岗位详情
|
||||||
|
postGroup.GET("/:post_id", api.Post.GetPost)
|
||||||
|
|
||||||
|
// 删除岗位-批量
|
||||||
|
postGroup.DELETE("", api.Post.DeletePost)
|
||||||
|
|
||||||
|
// 修改岗位
|
||||||
|
postGroup.PUT("/:post_id", api.Post.PutPost)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
117
api/service/post.go
Normal file
117
api/service/post.go
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"hospital-admin-api/api/dao"
|
||||||
|
"hospital-admin-api/api/model"
|
||||||
|
"hospital-admin-api/api/requests"
|
||||||
|
"hospital-admin-api/global"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PostService struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
// AddPost 新增岗位
|
||||||
|
func (r *PostService) AddPost(addPostRequest requests.AddPost) (bool, error) {
|
||||||
|
adminPostDao := dao.AdminPostDao{}
|
||||||
|
|
||||||
|
// 开始事务
|
||||||
|
tx := global.Db.Begin()
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// 新增岗位
|
||||||
|
adminPostModel := &model.AdminPost{
|
||||||
|
PostName: addPostRequest.PostName,
|
||||||
|
PostStatus: 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
adminMenu, _ := adminPostDao.AddAdminPost(tx, adminPostModel)
|
||||||
|
if adminMenu == nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return false, errors.New("新增失败")
|
||||||
|
}
|
||||||
|
|
||||||
|
tx.Commit()
|
||||||
|
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// DeletePost 删除岗位-批量
|
||||||
|
func (r *PostService) DeletePost(deletePostRequest requests.DeletePost) (bool, error) {
|
||||||
|
adminPostDao := dao.AdminPostDao{}
|
||||||
|
|
||||||
|
// 开始事务
|
||||||
|
tx := global.Db.Begin()
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
for _, v := range deletePostRequest.PostIds {
|
||||||
|
// 将字符串转换为int64类型
|
||||||
|
v, err := strconv.ParseInt(v, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return false, errors.New("菜单错误")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检测删除岗位
|
||||||
|
adminPost, err := adminPostDao.GetAdminPostById(v)
|
||||||
|
if err != nil || adminPost == nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return false, errors.New("删除失败")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 修改岗位为删除
|
||||||
|
data := make(map[string]interface{})
|
||||||
|
data["post_status"] = 2
|
||||||
|
err = adminPostDao.EditAdminPostById(tx, v, data)
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return false, errors.New("删除失败")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tx.Commit()
|
||||||
|
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// PutPost 修改岗位
|
||||||
|
func (r *PostService) PutPost(requestPostId int64, putPostRequest requests.PutPost) (bool, error) {
|
||||||
|
adminPostDao := dao.AdminPostDao{}
|
||||||
|
|
||||||
|
// 获取需修改岗位数据
|
||||||
|
adminPost, err := adminPostDao.GetAdminPostById(requestPostId)
|
||||||
|
if err != nil || adminPost == nil {
|
||||||
|
return false, errors.New("岗位数据错误")
|
||||||
|
}
|
||||||
|
|
||||||
|
// 开始事务
|
||||||
|
tx := global.Db.Begin()
|
||||||
|
defer func() {
|
||||||
|
if r := recover(); r != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
// 修改岗位
|
||||||
|
data := make(map[string]interface{})
|
||||||
|
data["post_name"] = putPostRequest.PostName
|
||||||
|
data["post_status"] = putPostRequest.PostStatus
|
||||||
|
err = adminPostDao.EditAdminPostById(tx, requestPostId, data)
|
||||||
|
if err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return false, errors.New("修改失败")
|
||||||
|
}
|
||||||
|
|
||||||
|
tx.Commit()
|
||||||
|
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
@ -84,7 +84,7 @@ func (r *UserService) AddUser(c *gin.Context, AddUserRequest requests.AddUser) (
|
|||||||
return false, errors.New("岗位错误")
|
return false, errors.New("岗位错误")
|
||||||
}
|
}
|
||||||
|
|
||||||
adminPost, err := AdminPostDao.GetAdminPostFirstById(postID)
|
adminPost, err := AdminPostDao.GetAdminPostById(postID)
|
||||||
if err != nil || adminPost == nil {
|
if err != nil || adminPost == nil {
|
||||||
return false, errors.New("岗位错误")
|
return false, errors.New("岗位错误")
|
||||||
}
|
}
|
||||||
@ -299,7 +299,7 @@ func (r *UserService) PutUser(c *gin.Context, requestUserId int64, putUserReques
|
|||||||
|
|
||||||
if postID != adminUser.PostID {
|
if postID != adminUser.PostID {
|
||||||
adminPostDao := dao.AdminPostDao{}
|
adminPostDao := dao.AdminPostDao{}
|
||||||
res, err := adminPostDao.GetAdminPostFirstById(postID)
|
res, err := adminPostDao.GetAdminPostById(postID)
|
||||||
if err != nil || res == nil {
|
if err != nil || res == nil {
|
||||||
return false, errors.New("岗位错误")
|
return false, errors.New("岗位错误")
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user