2023-09-28 08:40:43 +08:00

197 lines
4.4 KiB
Go

package service
import (
"errors"
"hospital-admin-api/api/dao"
"hospital-admin-api/api/dto"
"hospital-admin-api/api/model"
"hospital-admin-api/api/requests"
"hospital-admin-api/global"
"strconv"
)
type DeptService struct {
}
// GetDeptList 获取部门列表
func (r *DeptService) GetDeptList() ([]*dto.AdminDeptDto, error) {
// 获取全部部门
adminDeptDao := dao.AdminDeptDao{}
maps := make(map[string]interface{})
maps["dept_status"] = 1
adminDept, _ := adminDeptDao.GetAdminDeptList(maps)
if adminDept == nil {
return nil, nil
}
deptMap := make(map[int64]*dto.AdminDeptDto)
var deptTree []*dto.AdminDeptDto
for _, dept := range adminDept {
node := &dto.AdminDeptDto{
DeptId: strconv.FormatInt(dept.DeptId, 10),
ParentId: strconv.FormatInt(dept.ParentId, 10),
DeptName: dept.DeptName,
DeptStatus: dept.DeptStatus,
CreatedAt: dept.CreatedAt,
UpdatedAt: dept.UpdatedAt,
Children: nil,
}
deptMap[dept.DeptId] = node
}
// 构建菜单树
for _, dept := range adminDept {
if dept.ParentId == 0 {
if deptMap[dept.DeptId] == nil {
continue
}
deptTree = append(deptTree, deptMap[dept.DeptId])
} else if parent, ok := deptMap[dept.ParentId]; ok {
parent.Children = append(parent.Children, deptMap[dept.DeptId])
}
}
return deptTree, nil
}
// AddDept 新增部门
func (r *DeptService) AddDept(addDeptRequest requests.AddDept) (bool, error) {
adminDeptDao := dao.AdminDeptDao{}
parentId, err := strconv.ParseInt(addDeptRequest.ParentId, 10, 64)
if err != nil {
return false, errors.New("新增失败")
}
// 判断父级id
if parentId != 0 {
adminDept, _ := adminDeptDao.GetAdminDeptById(parentId)
if adminDept == nil {
return false, errors.New("上级部门错误")
}
}
// 开始事务
tx := global.Db.Begin()
defer func() {
if r := recover(); r != nil {
tx.Rollback()
}
}()
// 新增菜单
adminDeptModel := &model.AdminDept{
ParentId: parentId,
DeptName: addDeptRequest.DeptName,
DeptStatus: 1,
}
adminMenu, _ := adminDeptDao.AddAdminDept(tx, adminDeptModel)
if adminMenu == nil {
tx.Rollback()
return false, errors.New("新增失败")
}
tx.Commit()
return true, nil
}
// DeleteDept 删除部门-批量
func (r *DeptService) DeleteDept(deleteDeptRequest requests.DeleteDept) (bool, error) {
adminDeptDao := dao.AdminDeptDao{}
// 开始事务
tx := global.Db.Begin()
defer func() {
if r := recover(); r != nil {
tx.Rollback()
}
}()
for _, v := range deleteDeptRequest.DeptIds {
// 将字符串转换为int64类型
v, err := strconv.ParseInt(v, 10, 64)
if err != nil {
tx.Rollback()
return false, errors.New("菜单错误")
}
// 检测删除部门
adminDept, err := adminDeptDao.GetAdminDeptById(v)
if err != nil || adminDept == nil {
tx.Rollback()
return false, errors.New("删除失败")
}
// 检测部门是否存在子集
childrenAdminDepts, err := adminDeptDao.GetAdminDeptListByParentId(v)
if err != nil || len(childrenAdminDepts) > 0 {
tx.Rollback()
return false, errors.New("存在下级部门,删除失败")
}
// 修改部门为删除
data := make(map[string]interface{})
data["dept_status"] = 2
err = adminDeptDao.EditAdminDeptById(tx, v, data)
if err != nil {
tx.Rollback()
return false, errors.New("删除失败")
}
}
tx.Commit()
return true, nil
}
// PutDept 修改部门
func (r *DeptService) PutDept(requestDeptId int64, putDeptRequest requests.PutDept) (bool, error) {
adminDeptDao := dao.AdminDeptDao{}
// 获取需修改部门数据
adminDept, err := adminDeptDao.GetAdminDeptById(requestDeptId)
if err != nil || adminDept == nil {
return false, errors.New("部门数据错误")
}
// 判断父级id
parentId, err := strconv.ParseInt(putDeptRequest.ParentId, 10, 64)
if err != nil {
return false, errors.New("新增失败")
}
if parentId != 0 {
adminDept, _ := adminDeptDao.GetAdminDeptById(parentId)
if adminDept == nil {
return false, errors.New("上级部门错误")
}
}
// 开始事务
tx := global.Db.Begin()
defer func() {
if r := recover(); r != nil {
tx.Rollback()
}
}()
// 修改部门
data := make(map[string]interface{})
data["parent_id"] = parentId
data["dept_name"] = putDeptRequest.DeptName
data["dept_status"] = putDeptRequest.DeptStatus
err = adminDeptDao.EditAdminDeptById(tx, requestDeptId, data)
if err != nil {
tx.Rollback()
return false, errors.New("修改失败")
}
tx.Commit()
return true, nil
}