201 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			201 lines
		
	
	
		
			4.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package system
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"errors"
 | |
| 	"fmt"
 | |
| 	"strconv"
 | |
| 	"time"
 | |
| 
 | |
| 	"management/internal/erpserver/model/dto"
 | |
| 	"management/internal/erpserver/model/form"
 | |
| 	"management/internal/erpserver/model/system"
 | |
| 	"management/internal/erpserver/model/view"
 | |
| 	"management/internal/erpserver/service/util"
 | |
| 	"management/internal/erpserver/service/v1"
 | |
| 	"management/internal/pkg/convertor"
 | |
| 	"management/internal/pkg/database"
 | |
| 	"management/internal/pkg/know"
 | |
| )
 | |
| 
 | |
| type roleService struct {
 | |
| 	*v1.Service
 | |
| 	repo system.RoleRepository
 | |
| }
 | |
| 
 | |
| func NewRoleService(service *v1.Service, repo system.RoleRepository) v1.RoleService {
 | |
| 	return &roleService{
 | |
| 		Service: service,
 | |
| 		repo:    repo,
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func (s *roleService) Create(ctx context.Context, req *form.Role) error {
 | |
| 	parent := &system.Role{
 | |
| 		ID:         0,
 | |
| 		ParentID:   0,
 | |
| 		ParentPath: ",0,",
 | |
| 	}
 | |
| 	if *req.ParentID > 0 {
 | |
| 		var err error
 | |
| 		parent, err = s.repo.Get(ctx, *req.ParentID)
 | |
| 		if err != nil {
 | |
| 			return errors.New("父级节点错误")
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	var order int32 = 6666
 | |
| 	if *req.Sort > 0 {
 | |
| 		order = *req.Sort
 | |
| 	}
 | |
| 
 | |
| 	arg := &system.Role{
 | |
| 		Name:        req.Name,
 | |
| 		DisplayName: req.DisplayName,
 | |
| 		ParentID:    parent.ID,
 | |
| 		ParentPath:  convertor.HandleParentPath(fmt.Sprintf("%s,%d,", parent.ParentPath, parent.ID)),
 | |
| 		Vip:         false,
 | |
| 		Status:      *req.Status,
 | |
| 		Sort:        order,
 | |
| 		CreatedAt:   time.Now(),
 | |
| 		UpdatedAt:   time.Now(),
 | |
| 	}
 | |
| 	err := s.repo.Create(ctx, arg)
 | |
| 	if err != nil {
 | |
| 		if database.IsUniqueViolation(err) {
 | |
| 			return errors.New("角色名称已存在")
 | |
| 		}
 | |
| 		return err
 | |
| 	}
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| func (s *roleService) Update(ctx context.Context, req *form.Role) error {
 | |
| 	parent := &system.Role{
 | |
| 		ID:         0,
 | |
| 		ParentID:   0,
 | |
| 		ParentPath: ",0,",
 | |
| 	}
 | |
| 	if *req.ParentID > 0 {
 | |
| 		var err error
 | |
| 		parent, err = s.repo.Get(ctx, *req.ParentID)
 | |
| 		if err != nil {
 | |
| 			return errors.New("父级节点错误")
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	role, err := s.repo.Get(ctx, *req.ID)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	var order int32 = 6666
 | |
| 	if *req.Sort > 0 {
 | |
| 		order = *req.Sort
 | |
| 	}
 | |
| 
 | |
| 	role.DisplayName = req.DisplayName
 | |
| 	role.Status = *req.Status
 | |
| 	role.ParentID = parent.ID
 | |
| 	role.ParentPath = convertor.HandleParentPath(fmt.Sprintf("%s,%d,", parent.ParentPath, parent.ID))
 | |
| 	role.Sort = order
 | |
| 	role.UpdatedAt = time.Now()
 | |
| 	err = s.repo.Update(ctx, role)
 | |
| 	return err
 | |
| }
 | |
| 
 | |
| func (s *roleService) Get(ctx context.Context, id int32) (*system.Role, error) {
 | |
| 	return s.repo.Get(ctx, id)
 | |
| }
 | |
| 
 | |
| func (s *roleService) All(ctx context.Context) ([]*system.Role, error) {
 | |
| 	var res []*system.Role
 | |
| 	key := know.GetManageKey(ctx, know.AllRoles)
 | |
| 	err := util.GetOrSetCache(ctx, s.Redis, key, util.GetCacheExpire(), func() (any, error) {
 | |
| 		return s.repo.All(ctx)
 | |
| 	}, &res)
 | |
| 	return res, err
 | |
| }
 | |
| 
 | |
| func (s *roleService) List(ctx context.Context, q dto.SearchDto) ([]*system.Role, int64, error) {
 | |
| 	return s.repo.List(ctx, q)
 | |
| }
 | |
| 
 | |
| func (s *roleService) RefreshCache(ctx context.Context) error {
 | |
| 	key := know.GetManageKey(ctx, know.AllRoles)
 | |
| 	return s.Redis.Del(ctx, key)
 | |
| }
 | |
| 
 | |
| func (s *roleService) RebuildParentPath(ctx context.Context) error {
 | |
| 	return s.repo.RebuildParentPath(ctx)
 | |
| }
 | |
| 
 | |
| func (s *roleService) Tree(ctx context.Context, id int32) ([]*view.LayuiTree, error) {
 | |
| 	all, err := s.All(ctx)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 
 | |
| 	return s.toTree(id, all), nil
 | |
| }
 | |
| 
 | |
| func (s *roleService) XmSelectTree(ctx context.Context, id int32) ([]*view.XmSelectTree, error) {
 | |
| 	all, err := s.All(ctx)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 
 | |
| 	return s.toXmSelectTree(id, all), nil
 | |
| }
 | |
| 
 | |
| func (s *roleService) toTree(parentId int32, data []*system.Role) []*view.LayuiTree {
 | |
| 	//idFunc := func(s *system.Role) int32 {
 | |
| 	//	return s.ParentID
 | |
| 	//}
 | |
| 	//childrenFunc := func(s *system.Role, childNodes []*view.LayuiTree) *view.LayuiTree {
 | |
| 	//	spread := false
 | |
| 	//	if s.ParentID == 0 {
 | |
| 	//		spread = true
 | |
| 	//	}
 | |
| 	//	return &view.LayuiTree{
 | |
| 	//		Title:    s.Name,
 | |
| 	//		ID:       strconv.FormatInt(int64(s.ID), 10),
 | |
| 	//		Children: childNodes,
 | |
| 	//		Spread:   spread,
 | |
| 	//	}
 | |
| 	//}
 | |
| 	//return util.BuildTree(parentId, data, idFunc, childrenFunc)
 | |
| 
 | |
| 	var res []*view.LayuiTree
 | |
| 	for _, v := range data {
 | |
| 		if v.ParentID == parentId {
 | |
| 			item := view.LayuiTree{}
 | |
| 			item.ID = strconv.FormatInt(int64(v.ID), 10)
 | |
| 			item.Title = v.DisplayName
 | |
| 			item.Children = s.toTree(v.ID, data)
 | |
| 			if v.ParentID == 0 {
 | |
| 				item.Spread = true
 | |
| 			}
 | |
| 			res = append(res, &item)
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	return res
 | |
| }
 | |
| 
 | |
| func (s *roleService) toXmSelectTree(parentId int32, data []*system.Role) []*view.XmSelectTree {
 | |
| 	var res []*view.XmSelectTree
 | |
| 	for _, v := range data {
 | |
| 		if v.ParentID == parentId {
 | |
| 			item := view.XmSelectTree{
 | |
| 				Name:     v.DisplayName,
 | |
| 				Value:    strconv.FormatInt(int64(v.ID), 10),
 | |
| 				Children: s.toXmSelectTree(v.ID, data),
 | |
| 			}
 | |
| 			res = append(res, &item)
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	return res
 | |
| }
 |