184 lines
		
	
	
		
			4.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			184 lines
		
	
	
		
			4.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package system
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 	"encoding/json"
 | |
| 	"strconv"
 | |
| 	"time"
 | |
| 
 | |
| 	"management/internal/db/model/dto"
 | |
| 	db "management/internal/db/sqlc"
 | |
| 	"management/internal/erpserver/model/view"
 | |
| 	"management/internal/global/keys"
 | |
| 	"management/internal/pkg/redis"
 | |
| 	"management/internal/pkg/session"
 | |
| )
 | |
| 
 | |
| type DepartmentBiz interface {
 | |
| 	Create(ctx context.Context, arg *db.CreateSysDepartmentParams) (*db.SysDepartment, error)
 | |
| 	Update(ctx context.Context, arg *db.UpdateSysDepartmentParams) (*db.SysDepartment, error)
 | |
| 	All(ctx context.Context) ([]*db.SysDepartment, error)
 | |
| 	List(ctx context.Context, q dto.SearchDto) ([]*db.SysDepartment, int64, error)
 | |
| 	Get(ctx context.Context, id int32) (*db.SysDepartment, error)
 | |
| 	Refresh(ctx context.Context) ([]*db.SysDepartment, error)
 | |
| 	RebuildParentPath(ctx context.Context) error
 | |
| 
 | |
| 	Tree(ctx context.Context, id int32) ([]*view.LayuiTree, error)
 | |
| 	XmSelect(ctx context.Context, id int32) ([]*view.XmSelectTree, error)
 | |
| 
 | |
| 	DepartmentExpansion
 | |
| }
 | |
| 
 | |
| type DepartmentExpansion interface{}
 | |
| 
 | |
| type departmentBiz struct {
 | |
| 	store   db.Store
 | |
| 	redis   redis.IRedis
 | |
| 	session session.ISession
 | |
| }
 | |
| 
 | |
| var _ DepartmentBiz = (*departmentBiz)(nil)
 | |
| 
 | |
| func NewDepartment(store db.Store, redis redis.IRedis, session session.ISession) *departmentBiz {
 | |
| 	return &departmentBiz{
 | |
| 		store:   store,
 | |
| 		redis:   redis,
 | |
| 		session: session,
 | |
| 	}
 | |
| }
 | |
| 
 | |
| func (b *departmentBiz) All(ctx context.Context) ([]*db.SysDepartment, error) {
 | |
| 	key := keys.GetManageKey(ctx, keys.AllDepartments)
 | |
| 	bs, err := redis.GetBytes(ctx, key)
 | |
| 	if err == nil {
 | |
| 		var res []*db.SysDepartment
 | |
| 		if err := json.Unmarshal(bs, &res); err == nil {
 | |
| 			return res, nil
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	return b.Refresh(ctx)
 | |
| }
 | |
| 
 | |
| func (b *departmentBiz) List(ctx context.Context, q dto.SearchDto) ([]*db.SysDepartment, int64, error) {
 | |
| 	countArg := &db.CountSysDepartmentConditionParams{
 | |
| 		IsStatus:   q.SearchStatus != 9999,
 | |
| 		Status:     int32(q.SearchStatus),
 | |
| 		IsID:       q.SearchID != 0,
 | |
| 		ID:         int32(q.SearchID),
 | |
| 		IsParentID: q.SearchParentID != 0,
 | |
| 		ParentID:   int32(q.SearchParentID),
 | |
| 		Name:       q.SearchName,
 | |
| 	}
 | |
| 
 | |
| 	dataArg := &db.ListSysDepartmentConditionParams{
 | |
| 		IsStatus:   q.SearchStatus != 9999,
 | |
| 		Status:     int32(q.SearchStatus),
 | |
| 		IsID:       q.SearchID != 0,
 | |
| 		ID:         int32(q.SearchID),
 | |
| 		IsParentID: q.SearchParentID != 0,
 | |
| 		ParentID:   int32(q.SearchParentID),
 | |
| 		Name:       q.SearchName,
 | |
| 		Skip:       (int32(q.Page) - 1) * int32(q.Rows),
 | |
| 		Size:       int32(q.Rows),
 | |
| 	}
 | |
| 	count, err := b.store.CountSysDepartmentCondition(ctx, countArg)
 | |
| 	if err != nil {
 | |
| 		return nil, 0, err
 | |
| 	}
 | |
| 
 | |
| 	departs, err := b.store.ListSysDepartmentCondition(ctx, dataArg)
 | |
| 	if err != nil {
 | |
| 		return nil, 0, err
 | |
| 	}
 | |
| 
 | |
| 	return departs, count, nil
 | |
| }
 | |
| 
 | |
| func (b *departmentBiz) Get(ctx context.Context, id int32) (*db.SysDepartment, error) {
 | |
| 	return b.store.GetSysDepartment(ctx, id)
 | |
| }
 | |
| 
 | |
| func (b *departmentBiz) Create(ctx context.Context, arg *db.CreateSysDepartmentParams) (*db.SysDepartment, error) {
 | |
| 	return b.store.CreateSysDepartment(ctx, arg)
 | |
| }
 | |
| 
 | |
| func (b *departmentBiz) Update(ctx context.Context, arg *db.UpdateSysDepartmentParams) (*db.SysDepartment, error) {
 | |
| 	return b.store.UpdateSysDepartment(ctx, arg)
 | |
| }
 | |
| 
 | |
| func (b *departmentBiz) Refresh(ctx context.Context) ([]*db.SysDepartment, error) {
 | |
| 	all, err := b.store.AllSysDepartment(ctx)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 
 | |
| 	bs, err := json.Marshal(all)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 
 | |
| 	key := keys.GetManageKey(ctx, keys.AllDepartments)
 | |
| 	err = redis.Set(ctx, key, bs, time.Hour*6)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 	return all, nil
 | |
| }
 | |
| 
 | |
| func (h *departmentBiz) RebuildParentPath(ctx context.Context) error {
 | |
| 	return h.store.SysDepartmentRebuildPath(ctx)
 | |
| }
 | |
| 
 | |
| func (h *departmentBiz) Tree(ctx context.Context, id int32) ([]*view.LayuiTree, error) {
 | |
| 	all, err := h.All(ctx)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 
 | |
| 	return toLayuiTree(id, all), nil
 | |
| }
 | |
| 
 | |
| func (h *departmentBiz) XmSelect(ctx context.Context, id int32) ([]*view.XmSelectTree, error) {
 | |
| 	all, err := h.All(ctx)
 | |
| 	if err != nil {
 | |
| 		return nil, err
 | |
| 	}
 | |
| 
 | |
| 	return toXmSelectTree(id, all), nil
 | |
| }
 | |
| 
 | |
| func toXmSelectTree(parentId int32, data []*db.SysDepartment) []*view.XmSelectTree {
 | |
| 	var res []*view.XmSelectTree
 | |
| 	for _, v := range data {
 | |
| 		if v.ParentID == parentId {
 | |
| 			item := view.XmSelectTree{
 | |
| 				Name:     v.Name,
 | |
| 				Value:    strconv.FormatInt(int64(v.ID), 10),
 | |
| 				Children: toXmSelectTree(v.ID, data),
 | |
| 			}
 | |
| 			res = append(res, &item)
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	return res
 | |
| }
 | |
| 
 | |
| func toLayuiTree(parentId int32, data []*db.SysDepartment) []*view.LayuiTree {
 | |
| 	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.Name
 | |
| 			item.Children = toLayuiTree(v.ID, data)
 | |
| 			if v.ParentID == 0 {
 | |
| 				item.Spread = true
 | |
| 			}
 | |
| 			res = append(res, &item)
 | |
| 		}
 | |
| 	}
 | |
| 
 | |
| 	return res
 | |
| }
 |