233 lines
		
	
	
		
			5.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			233 lines
		
	
	
		
			5.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package system
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"encoding/json"
 | |
| 	"strconv"
 | |
| 	"strings"
 | |
| 	"time"
 | |
| 
 | |
| 	"management/internal/db/model/dto"
 | |
| 	db "management/internal/db/sqlc"
 | |
| 	"management/internal/global/keys"
 | |
| 	"management/internal/pkg/redis"
 | |
| )
 | |
| 
 | |
| func CreateSysDepartment(ctx context.Context, arg *db.CreateSysDepartmentParams) (*db.SysDepartment, error) {
 | |
| 	return db.Engine.CreateSysDepartment(ctx, arg)
 | |
| }
 | |
| 
 | |
| func UpdateSysDepartment(ctx context.Context, arg *db.UpdateSysDepartmentParams) (*db.SysDepartment, error) {
 | |
| 	return db.Engine.UpdateSysDepartment(ctx, arg)
 | |
| }
 | |
| 
 | |
| func GetSysDepartment(ctx context.Context, id int32) (*db.SysDepartment, error) {
 | |
| 	return db.Engine.GetSysDepartment(ctx, id)
 | |
| }
 | |
| 
 | |
| func AllCache(ctx context.Context) ([]*db.SysDepartment, error) {
 | |
| 	key := keys.GetManageKey(ctx, keys.AllDepartments)
 | |
| 	b, err := redis.GetBytes(ctx, key)
 | |
| 	if err == nil {
 | |
| 		var res []*db.SysDepartment
 | |
| 		if err := json.Unmarshal(b, &res); err == nil {
 | |
| 			return res, nil
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	all, err := db.Engine.AllSysDepartment(ctx)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 
 | |
| 	b, err = json.Marshal(all)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 
 | |
| 	_ = redis.Set(ctx, key, b, time.Hour*6)
 | |
| 	return all, nil
 | |
| }
 | |
| 
 | |
| func ListSysDepartmentCondition(ctx context.Context, q dto.SearchDto) ([]*db.SysDepartment, int64, error) {
 | |
| 	countArg := &db.CountSysDepartmentConditionParams{
 | |
| 		IsStatus:   q.SearchStatus != 9999,
 | |
| 		Status:     int32(q.SearchStatus),
 | |
| 		IsParentID: q.SearchParentID != 0,
 | |
| 		ParentID:   int32(q.SearchParentID),
 | |
| 	}
 | |
| 
 | |
| 	dataArg := &db.ListSysDepartmentConditionParams{
 | |
| 		IsStatus:   q.SearchStatus != 9999,
 | |
| 		Status:     int32(q.SearchStatus),
 | |
| 		IsParentID: q.SearchParentID != 0,
 | |
| 		ParentID:   int32(q.SearchParentID),
 | |
| 		Skip:       (int32(q.Page) - 1) * int32(q.Rows),
 | |
| 		Size:       int32(q.Rows),
 | |
| 	}
 | |
| 
 | |
| 	if len(q.SearchKey) > 0 {
 | |
| 		switch strings.ToLower(q.SearchName) {
 | |
| 		case "id":
 | |
| 			id, err := strconv.Atoi(q.SearchKey)
 | |
| 			if err == nil {
 | |
| 				countArg.IsID = true
 | |
| 				countArg.ID = int32(id)
 | |
| 
 | |
| 				dataArg.IsID = true
 | |
| 				dataArg.ID = int32(id)
 | |
| 			}
 | |
| 		case "name":
 | |
| 			countArg.Name = q.SearchKey
 | |
| 			dataArg.Name = q.SearchKey
 | |
| 		}
 | |
| 	}
 | |
| 	count, err := db.Engine.CountSysDepartmentCondition(ctx, countArg)
 | |
| 	if err != nil {
 | |
| 		return nil, 0, err
 | |
| 	}
 | |
| 
 | |
| 	departs, err := db.Engine.ListSysDepartmentCondition(ctx, dataArg)
 | |
| 	if err != nil {
 | |
| 		return nil, 0, err
 | |
| 	}
 | |
| 
 | |
| 	return departs, count, nil
 | |
| }
 | |
| 
 | |
| func ListTreeSysDepartment(ctx context.Context) ([]*db.SysDepartmentDto, error) {
 | |
| 	all, err := db.Engine.ListSysDepartment(ctx)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 
 | |
| 	return ToDtoTreeSysDepartment(0, all), nil
 | |
| }
 | |
| 
 | |
| func DTreeSysDepartment(ctx context.Context, id int32) ([]*dto.DTreeDto, error) {
 | |
| 	all, err := db.Engine.AllSysDepartment(ctx)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 
 | |
| 	return toDtree(id, all), nil
 | |
| }
 | |
| 
 | |
| func RebuildSysDepartmentParentPath(ctx context.Context) error {
 | |
| 	return db.Engine.SysDepartmentRebuildPath(ctx)
 | |
| }
 | |
| 
 | |
| func toDtree(parentId int32, data []*db.SysDepartment) []*dto.DTreeDto {
 | |
| 	var res []*dto.DTreeDto
 | |
| 	for _, v := range data {
 | |
| 		if v.ParentID == parentId {
 | |
| 			item := dto.DTreeDto{}
 | |
| 			item.ID = strconv.FormatInt(int64(v.ID), 10)
 | |
| 			item.Title = v.Name
 | |
| 			item.Last = !hasChildren(v.ID, data)
 | |
| 			item.ParentId = strconv.FormatInt(int64(v.ParentID), 10)
 | |
| 			item.Children = toDtree(v.ID, data)
 | |
| 			res = append(res, &item)
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	return res
 | |
| }
 | |
| 
 | |
| func hasChildren(parentId int32, data []*db.SysDepartment) bool {
 | |
| 	if len(data) > 0 {
 | |
| 		for _, v := range data {
 | |
| 			if v.ParentID == parentId {
 | |
| 				return true
 | |
| 			}
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	return false
 | |
| }
 | |
| 
 | |
| func ToTreeSysDepartment(ctx context.Context, id int, isRoot bool) ([]*dto.TreeDto, error) {
 | |
| 	all, err := db.Engine.AllSysDepartment(ctx)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 
 | |
| 	if isRoot {
 | |
| 		root := getSysDepartmentRootParentId(int32(id), all)
 | |
| 		if root == nil {
 | |
| 			root = &dto.TreeDto{
 | |
| 				ID:    0,
 | |
| 				Title: "根节点",
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 		root.Children = toDtoTreeSysDepartment(int32(id), all)
 | |
| 		return []*dto.TreeDto{root}, nil
 | |
| 	}
 | |
| 
 | |
| 	return toDtoTreeSysDepartment(int32(id), all), nil
 | |
| }
 | |
| 
 | |
| func RefreshSysDepartment(ctx context.Context) error {
 | |
| 	all, err := db.Engine.AllSysDepartment(ctx)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	b, err := json.Marshal(all)
 | |
| 	if err != nil {
 | |
| 		return err
 | |
| 	}
 | |
| 
 | |
| 	key := keys.GetManageKey(ctx, keys.AllDepartments)
 | |
| 	err = redis.Set(ctx, key, b, time.Hour*6)
 | |
| 	return err
 | |
| }
 | |
| 
 | |
| func getSysDepartmentRootParentId(parentId int32, data []*db.SysDepartment) *dto.TreeDto {
 | |
| 	for _, v := range data {
 | |
| 		if v.ID == parentId {
 | |
| 			return &dto.TreeDto{
 | |
| 				ID:    int(v.ID),
 | |
| 				Title: v.Name,
 | |
| 			}
 | |
| 		}
 | |
| 	}
 | |
| 	return nil
 | |
| }
 | |
| 
 | |
| func ToDtoTreeSysDepartment(parentId int32, data []*db.SysDepartment) []*db.SysDepartmentDto {
 | |
| 	var res []*db.SysDepartmentDto
 | |
| 	for _, v := range data {
 | |
| 		if v.ParentID == parentId {
 | |
| 			item := db.SysDepartmentDto{}
 | |
| 			item.ID = v.ID
 | |
| 			item.Name = v.Name
 | |
| 			item.ParentID = v.ParentID
 | |
| 			item.ParentPath = v.ParentPath
 | |
| 			item.Status = v.Status
 | |
| 			item.CreatedAt = v.CreatedAt
 | |
| 			item.UpdatedAt = v.UpdatedAt
 | |
| 			item.Children = ToDtoTreeSysDepartment(v.ID, data)
 | |
| 			res = append(res, &item)
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	return res
 | |
| }
 | |
| 
 | |
| func toDtoTreeSysDepartment(parentId int32, data []*db.SysDepartment) []*dto.TreeDto {
 | |
| 	var res []*dto.TreeDto
 | |
| 	for _, v := range data {
 | |
| 		if v.ParentID == parentId {
 | |
| 			item := dto.TreeDto{}
 | |
| 			item.ID = int(v.ID)
 | |
| 			item.Title = v.Name
 | |
| 			item.Children = toDtoTreeSysDepartment(v.ID, data)
 | |
| 			res = append(res, &item)
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	return res
 | |
| }
 |