// Code scaffolded by goctl. Safe to edit. // goctl 1.9.2 package organization import ( "context" "fmt" "github.com/youruser/base/internal/svc" "github.com/youruser/base/internal/types" "github.com/youruser/base/model" "github.com/zeromicro/go-zero/core/logx" "gorm.io/gorm" ) type GetOrganizationListLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } // 获取机构列表 func NewGetOrganizationListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetOrganizationListLogic { return &GetOrganizationListLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } func (l *GetOrganizationListLogic) GetOrganizationList() (resp *types.OrgListResponse, err error) { orgs, err := model.OrgFindAll(l.ctx, l.svcCtx.DB) if err != nil { return nil, fmt.Errorf("获取机构列表失败: %v", err) } tree := buildOrgTree(l.ctx, l.svcCtx.DB, orgs, 0) return &types.OrgListResponse{ List: tree, }, nil } func buildOrgTree(ctx context.Context, db *gorm.DB, orgs []model.Organization, parentId int64) []types.OrgInfo { var tree []types.OrgInfo for _, o := range orgs { if o.ParentId == parentId { memberCount, _ := model.UserOrgCountByOrgId(ctx, db, o.Id) item := types.OrgInfo{ Id: o.Id, ParentId: o.ParentId, Name: o.Name, Code: o.Code, Leader: o.Leader, Phone: o.Phone, Email: o.Email, SortOrder: o.SortOrder, Status: o.Status, MemberCount: memberCount, Children: buildOrgTree(ctx, db, orgs, o.Id), CreatedAt: o.CreatedAt.Format("2006-01-02 15:04:05"), UpdatedAt: o.UpdatedAt.Format("2006-01-02 15:04:05"), } tree = append(tree, item) } } if tree == nil { tree = []types.OrgInfo{} } return tree }