package service import ( "Medical_OAuth/common/actions" cDto "Medical_OAuth/common/dto" "Medical_OAuth/common/global" "encoding/json" "errors" "fmt" "gogs.baozhida.cn/zoie/OAuth-core/service" "gorm.io/gorm" "gorm.io/gorm/clause" "Medical_OAuth/app/admin/model" "Medical_OAuth/app/admin/service/dto" "gogs.baozhida.cn/zoie/OAuth-core/pkg" ) type SysDept struct { service.Service } // Get 获取SysDept对象 func (e *SysDept) Get(d *dto.SysDeptGetReq, deptModel *model.SysDept) error { err := e.Orm. First(deptModel, d.GetId()).Error if err != nil { e.Log.Errorf("db error: %s", err) if errors.Is(err, gorm.ErrRecordNotFound) { return global.GetNotFoundOrNoPermissionErr } return global.GetFailedErr } return nil } // Insert 创建SysDept对象 func (e *SysDept) Insert(c *dto.SysDeptInsertReq, deptId int) error { var err error var data model.SysDept c.Generate(&data) tx := e.Orm.Debug().Begin() defer func() { if err != nil { tx.Rollback() } else { tx.Commit() } }() if deptId != 0 && c.ParentId == 0 { data.ParentId = deptId } err = tx.Create(&data).Error if err != nil { e.Log.Errorf("db error: %s", err) return global.CreateFailedErr } c.Id = data.Id deptPath := pkg.IntToString(data.Id) + "/" if data.ParentId != 0 { var deptP model.SysDept tx.First(&deptP, data.ParentId) deptPath = deptP.DeptPath + deptPath } else { deptPath = "/0/" + deptPath } var mp = map[string]string{} mp["dept_path"] = deptPath err = tx.Model(&data).Update("dept_path", deptPath).Error if err != nil { e.Log.Errorf("db error: %s", err) return global.CreateFailedErr } return nil } // Update 修改SysDept对象 func (e *SysDept) Update(c *dto.SysDeptUpdateReq) error { var err error var deptModel = model.SysDept{} tx := e.Orm.Debug().Begin() defer func() { if err != nil { tx.Rollback() } else { tx.Commit() } }() err = tx.First(&deptModel, c.GetId()).Error if err != nil { e.Log.Errorf("db error: %s", err) if errors.Is(err, gorm.ErrRecordNotFound) { return global.UpdateNotFoundOrNoPermissionErr } return global.UpdateFailedErr } c.Generate(&deptModel) DeptPath := pkg.IntToString(deptModel.Id) + "/" if deptModel.ParentId != 0 { var DeptP model.SysDept tx.First(&DeptP, deptModel.ParentId) DeptPath = DeptP.DeptPath + DeptPath } else { DeptPath = "/0/" + DeptPath } deptModel.DeptPath = DeptPath db := tx.Save(&deptModel) if err = db.Error; err != nil { e.Log.Errorf("UpdateSysDept error: %s", err) return err } return nil } // Remove 删除SysDept // 删除部门 同时删除其子部门、部门下的用户 func (e *SysDept) Remove(d *dto.SysDeptDeleteReq, p *actions.DataPermission) error { var err error tx := e.Orm.Begin() defer func() { if err != nil { tx.Rollback() } else { tx.Commit() } }() var dept model.SysDept err = e.Orm. First(&dept, d.GetId()).Error if err != nil { e.Log.Errorf("db error: %s", err) if errors.Is(err, gorm.ErrRecordNotFound) { return global.GetNotFoundOrNoPermissionErr } return global.DeleteFailedErr } var deptList = make([]model.SysDept, 0) var deptIds = make([]int, 0) tx.Where("dept_path like ?", dept.DeptPath+"%").Find(&deptList) // 获取部门及其子部门下ID for _, v := range deptList { deptIds = append(deptIds, v.Id) } db := tx.Select(clause.Associations).Delete(&deptList) if err = db.Error; err != nil { e.Log.Errorf("Delete error: %s", err) return global.DeleteFailedErr } if db.RowsAffected == 0 { return global.DeleteNotFoundOrNoPermissionErr } // 删除部门下的用户 dbUser := tx.Where("dept_id in (?)", deptIds).Delete(&model.SysUser{}) if err = dbUser.Error; err != nil { e.Log.Errorf("Delete User error: %s", err) return global.DeleteFailedErr } return nil } // getList 获取组织数据 func (e SysDept) getList(c *dto.SysDeptGetPageReq, list *[]model.SysDept, p *actions.DataPermission) error { var err error var data model.SysDept if len(c.DeptName) == 0 && c.DeptId == 0 { err = e.Orm.Model(&data). Scopes( cDto.MakeCondition(c.GetNeedSearch()), ). Find(list).Error if err != nil { e.Log.Errorf("db error:%s", err) return err } return nil } var tempList []model.SysDept err = e.Orm.Model(&data).Where("id in (select id from sys_dept where dept_path like ? )", "%/"+pkg.IntToString(p.DeptId)+"/%"). Scopes( cDto.MakeCondition(c.GetNeedSearch()), ). Find(&tempList).Error if err != nil { e.Log.Errorf("db error: %s", err) return global.GetFailedErr } // 获取部门及其子部门下ID for _, dept := range tempList { var deptList = make([]model.SysDept, 0) err = e.Orm.Where("dept_path like ?", dept.DeptPath+"%").Order(fmt.Sprintf("sort %s", c.SortOrder)).Find(&deptList).Error *list = append(*list, deptList...) } if err != nil { e.Log.Errorf("db error: %s", err) return global.GetFailedErr } *list = DeduplicateDept(*list) return nil } // SetDeptTree 设置组织数据 func (e *SysDept) SetDeptTree(c *dto.SysDeptGetPageReq, p *actions.DataPermission) (m []dto.DeptLabel, err error) { var list []model.SysDept err = e.getList(c, &list, p) m = make([]dto.DeptLabel, 0) for i := 0; i < len(list); i++ { if list[i].ParentId != 0 { continue } e := dto.DeptLabel{} e.Id = list[i].Id e.Label = list[i].DeptName DeptInfo := DeptTreeCall(&list, e) m = append(m, DeptInfo) } return } // DeptTreeCall 递归构造组织数据 func DeptTreeCall(DeptList *[]model.SysDept, Dept dto.DeptLabel) dto.DeptLabel { list := *DeptList min := make([]dto.DeptLabel, 0) for j := 0; j < len(list); j++ { if Dept.Id != list[j].ParentId { continue } mi := dto.DeptLabel{Id: list[j].Id, Label: list[j].DeptName, Children: []dto.DeptLabel{}} ms := DeptTreeCall(DeptList, mi) min = append(min, ms) } Dept.Children = min return Dept } // SetDeptPage 设置dept页面数据 //func (e *SysDept) SetDeptPage(c *dto.SysDeptGetPageReq, p *actions.DataPermission) (m []model.SysDept, err error) { // var list []model.SysDept // err = e.getList(c, &list, p) // for i := 0; i < len(list); i++ { // if list[i].ParentId != 0 { // continue // } // info := e.deptPageCall(&list, list[i]) // m = append(m, info) // } // return //} // //func (e *SysDept) deptPageCall(deptlist *[]model.SysDept, dept model.SysDept) model.SysDept { // list := *deptlist // min := make([]model.SysDept, 0) // for j := 0; j < len(list); j++ { // if dept.Id != list[j].ParentId { // continue // } // mi := model.SysDept{} // mi.Id = list[j].Id // mi.ParentId = list[j].ParentId // mi.DeptPath = list[j].DeptPath // mi.DeptName = list[j].DeptName // mi.Sort = list[j].Sort // mi.Status = list[j].Status // mi.CreatedAt = list[j].CreatedAt // mi.Children = []model.SysDept{} // ms := e.deptPageCall(deptlist, mi) // min = append(min, ms) // } // dept.Children = min // return dept //} // SetDeptPage 设置Dept页面数据 func (e *SysDept) SetDeptPage(c *dto.SysDeptGetPageReq, p *actions.DataPermission) (m []model.SysDept, err error) { var list []model.SysDept err = e.getList(c, &list, p) for i := 0; i < len(list); i++ { if list[i].ParentId != 0 { continue } info := e.DeptPageCall(&list, list[i]) m = append(m, info) } if len(m) == 0 { parentMap, flag := getDeptParent(list) if flag { for i := 0; i < len(list); i++ { if parentMap[list[i].ParentId] { continue } info := e.DeptPageCall(&list, list[i]) m = append(m, info) } } else { for i := 0; i < len(list); i++ { info := e.DeptPageCall(&list, list[i]) m = append(m, info) } } } return } func getDeptParent(deptList []model.SysDept) (map[int]bool, bool) { list := deptList var flag = false var parentMap = map[int]bool{} for j := 0; j < len(list); j++ { parentMap[list[j].ParentId] = false } for j := 0; j < len(list); j++ { if _, ok := parentMap[list[j].Id]; !ok { continue } parentMap[list[j].Id] = true flag = true } return parentMap, flag } func (e *SysDept) DeptPageCall(deptList *[]model.SysDept, menu model.SysDept) model.SysDept { list := *deptList min := make([]model.SysDept, 0) for j := 0; j < len(list); j++ { if menu.Id != list[j].ParentId { continue } mi := model.SysDept{} mi.Id = list[j].Id mi.ParentId = list[j].ParentId mi.DeptPath = list[j].DeptPath mi.DeptName = list[j].DeptName mi.Sort = list[j].Sort mi.Status = list[j].Status mi.CreatedAt = list[j].CreatedAt mi.Children = []model.SysDept{} ms := e.DeptPageCall(deptList, mi) min = append(min, ms) } menu.Children = min return menu } // GetWithRoleId 获取角色的部门ID集合 func (e *SysDept) GetWithRoleId(roleId int) ([]int, error) { DeptIds := make([]int, 0) DeptList := make([]dto.DeptIdList, 0) if err := e.Orm.Table("sys_role_dept"). Select("sys_role_dept.dept_id"). Joins("LEFT JOIN sys_dept on sys_dept.id=sys_role_dept.dept_id"). Where("role_id = ? ", roleId). Where(" sys_role_dept.dept_id not in(select sys_dept.parent_id from sys_role_dept LEFT JOIN sys_dept on sys_dept.id=sys_role_dept.dept_id where role_id =? )", roleId). Find(&DeptList).Error; err != nil { e.Log.Errorf("db error: %s", err) return nil, global.GetFailedErr } for i := 0; i < len(DeptList); i++ { DeptIds = append(DeptIds, DeptList[i].DeptId) } return DeptIds, nil } func (e *SysDept) SetDeptLabel() (m []dto.DeptLabel, err error) { list := make([]model.SysDept, 0) err = e.Orm.Find(&list).Error if err != nil { e.Log.Error("find Dept list error, %s", err) err = global.GetFailedErr return } m = make([]dto.DeptLabel, 0) var item dto.DeptLabel for i := range list { if list[i].ParentId != 0 { continue } item = dto.DeptLabel{} item.Id = list[i].Id item.Label = list[i].DeptName DeptInfo := DeptLabelCall(&list, item) m = append(m, DeptInfo) } return } // DeptLabelCall func DeptLabelCall(DeptList *[]model.SysDept, Dept dto.DeptLabel) dto.DeptLabel { list := *DeptList var mi dto.DeptLabel min := make([]dto.DeptLabel, 0) for j := 0; j < len(list); j++ { if Dept.Id != list[j].ParentId { continue } mi = dto.DeptLabel{Id: list[j].Id, Label: list[j].DeptName, Children: []dto.DeptLabel{}} ms := DeptLabelCall(DeptList, mi) min = append(min, ms) } Dept.Children = min return Dept } func DeduplicateDept(list []model.SysDept) []model.SysDept { seen := make(map[string]bool) deduplicated := []model.SysDept{} for _, v := range list { // 将结构体编码为 JSON 字符串 key, err := json.Marshal(v) if err != nil { // 处理错误 continue } // 将 JSON 字符串作为键插入到 map 中 if _, ok := seen[string(key)]; !ok { seen[string(key)] = true deduplicated = append(deduplicated, v) } } return deduplicated }