system_user_get_logic.go 1.7 KB
package user

import (
	"context"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
	"strings"

	"github.com/zeromicro/go-zero/core/logx"
)

type SystemUserGetLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSystemUserGetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemUserGetLogic {
	return &SystemUserGetLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SystemUserGetLogic) SystemUserGet(req *types.SystemUserGetRequest) (resp *types.SystemUserGetResponse, err error) {
	var (
		user      *domain.User
		conn      = l.svcCtx.DefaultDBConn()
		userToken = contextdata.GetUserTokenFromCtx(l.ctx)
	)
	if user, err = l.svcCtx.UserRepository.FindOne(l.ctx, conn, req.Id); err != nil {
		return nil, xerr.NewErrMsgErr("用户不存在", err)
	}
	var roles []*domain.Role
	if len(user.Roles) > 0 {
		_, roles, err = l.svcCtx.RoleRepository.Find(l.ctx, conn, domain.IndexCompanyId(userToken.CompanyId)().MustWithKV("ids", user.Roles))
		if err != nil {
			return nil, xerr.NewErrMsgErr("获取账号详情失败", err)
		}
	}
	rolesDesc := domain.Values(roles, func(item *domain.Role) string {
		return item.Name
	})
	userItem := NewSystemUser(user)
	userItem.RolesDesc = strings.Join(rolesDesc, "、")
	resp = &types.SystemUserGetResponse{
		User: userItem,
	}
	return
}