123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- package service
- import (
- "errors"
- "go-admin/app/admin/models"
- log "github.com/go-admin-team/go-admin-core/logger"
- "github.com/go-admin-team/go-admin-core/sdk/pkg"
- "gorm.io/gorm"
- "go-admin/app/admin/service/dto"
- cDto "go-admin/common/dto"
- "github.com/go-admin-team/go-admin-core/sdk/service"
- )
- type SysDept struct {
- service.Service
- }
- // GetPage 获取SysDept列表
- //func (e *SysDept) GetPage(c *dto.SysDeptGetPageReq, list *[]models.SysDept) error {
- // var err error
- // var data models.SysDept
- //
- // 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
- //}
- // Get 获取SysDept对象
- func (e *SysDept) Get(d *dto.SysDeptGetReq, model *models.SysDept) error {
- var err error
- var data models.SysDept
- db := e.Orm.Model(&data).
- First(model, d.GetId())
- err = db.Error
- if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
- err = errors.New("查看对象不存在或无权查看")
- e.Log.Errorf("db error:%s", err)
- return err
- }
- if err = db.Error; err != nil {
- e.Log.Errorf("db error:%s", err)
- return err
- }
- return nil
- }
- // Insert 创建SysDept对象
- func (e *SysDept) Insert(c *dto.SysDeptInsertReq) error {
- var err error
- var data models.SysDept
- c.Generate(&data)
- tx := e.Orm.Debug().Begin()
- defer func() {
- if err != nil {
- tx.Rollback()
- } else {
- tx.Commit()
- }
- }()
- err = tx.Create(&data).Error
- if err != nil {
- e.Log.Errorf("db error:%s", err)
- return err
- }
- deptPath := pkg.IntToString(data.DeptId) + "/"
- if data.ParentId != 0 {
- var deptP models.SysDept
- tx.First(&deptP, data.ParentId)
- deptPath = deptP.DeptPath + deptPath
- } else {
- deptPath = "/0/" + deptPath
- }
- var mp = map[string]string{}
- mp["dept_path"] = deptPath
- if err := tx.Model(&data).Update("dept_path", deptPath).Error; err != nil {
- e.Log.Errorf("db error:%s", err)
- return err
- }
- return nil
- }
- // Update 修改SysDept对象
- func (e *SysDept) Update(c *dto.SysDeptUpdateReq) error {
- var err error
- var model = models.SysDept{}
- tx := e.Orm.Debug().Begin()
- defer func() {
- if err != nil {
- tx.Rollback()
- } else {
- tx.Commit()
- }
- }()
- tx.First(&model, c.GetId())
- c.Generate(&model)
- deptPath := pkg.IntToString(model.DeptId) + "/"
- if model.ParentId != 0 {
- var deptP models.SysDept
- tx.First(&deptP, model.ParentId)
- deptPath = deptP.DeptPath + deptPath
- } else {
- deptPath = "/0/" + deptPath
- }
- model.DeptPath = deptPath
- db := tx.Save(&model)
- if err = db.Error; err != nil {
- e.Log.Errorf("UpdateSysDept error:%s", err)
- return err
- }
- if db.RowsAffected == 0 {
- return errors.New("无权更新该数据")
- }
- return nil
- }
- // Remove 删除SysDept
- func (e *SysDept) Remove(d *dto.SysDeptDeleteReq) error {
- var err error
- var data models.SysDept
- db := e.Orm.Model(&data).Delete(&data, d.GetId())
- if err = db.Error; err != nil {
- err = db.Error
- e.Log.Errorf("Delete error: %s", err)
- return err
- }
- if db.RowsAffected == 0 {
- err = errors.New("无权删除该数据")
- return err
- }
- return nil
- }
- // GetSysDeptList 获取组织数据
- func (e *SysDept) getList(c *dto.SysDeptGetPageReq, list *[]models.SysDept) error {
- var err error
- var data models.SysDept
- 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
- }
- // SetDeptTree 设置组织数据
- func (e *SysDept) SetDeptTree(c *dto.SysDeptGetPageReq) (m []dto.DeptLabel, err error) {
- var list []models.SysDept
- err = e.getList(c, &list)
- 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].DeptId
- e.Label = list[i].DeptName
- deptsInfo := deptTreeCall(&list, e)
- m = append(m, deptsInfo)
- }
- return
- }
- // Call 递归构造组织数据
- func deptTreeCall(deptList *[]models.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].DeptId, 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) (m []models.SysDept, err error) {
- var list []models.SysDept
- err = e.getList(c, &list)
- 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 *[]models.SysDept, menu models.SysDept) models.SysDept {
- list := *deptlist
- min := make([]models.SysDept, 0)
- for j := 0; j < len(list); j++ {
- if menu.DeptId != list[j].ParentId {
- continue
- }
- mi := models.SysDept{}
- mi.DeptId = list[j].DeptId
- mi.ParentId = list[j].ParentId
- mi.DeptPath = list[j].DeptPath
- mi.DeptName = list[j].DeptName
- mi.Sort = list[j].Sort
- mi.Leader = list[j].Leader
- mi.Phone = list[j].Phone
- mi.Email = list[j].Email
- mi.Status = list[j].Status
- mi.CreatedAt = list[j].CreatedAt
- mi.Children = []models.SysDept{}
- ms := e.deptPageCall(deptlist, mi)
- min = append(min, ms)
- }
- menu.Children = min
- return menu
- }
- // GetRoleDeptId 获取角色的部门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.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.dept_id=sys_role_dept.dept_id where role_id =? )", roleId).
- Find(&deptList).Error; err != nil {
- return nil, err
- }
- 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([]models.SysDept, 0)
- err = e.Orm.Find(&list).Error
- if err != nil {
- log.Error("find dept list error, %s", err.Error())
- 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].DeptId
- item.Label = list[i].DeptName
- deptInfo := deptLabelCall(&list, item)
- m = append(m, deptInfo)
- }
- return
- }
- // deptLabelCall
- func deptLabelCall(deptList *[]models.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].DeptId, Label: list[j].DeptName, Children: []dto.DeptLabel{}}
- ms := deptLabelCall(deptList, mi)
- min = append(min, ms)
- }
- dept.Children = min
- return dept
- }
|