mini_user_info_logic.go 3.1 KB
package user

import (
	"context"
	"github.com/zeromicro/go-zero/core/collection"
	"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"

	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"

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

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

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

func (l *MiniUserInfoLogic) MiniUserInfo(req *types.MiniUserInfoRequest) (resp *types.MiniUserInfoResponse, err error) {
	var (
		userToken  = contextdata.GetUserTokenFromCtx(l.ctx)
		user       *domain.User
		conn       = l.svcCtx.DefaultDBConn()
		accounts   []*domain.User
		companyMap = make(map[int64]*domain.Company)
		roles      []*domain.Role
	)
	if user, err = l.svcCtx.UserRepository.FindOne(l.ctx, conn, userToken.UserId); err != nil {
		return nil, xerr.NewErrMsgErr("用户不存在", err)
	}
	if _, roles, err = l.svcCtx.RoleRepository.Find(l.ctx, conn, domain.IndexCompanyId(user.CompanyId)().MustWithKV("ids", user.Roles)); err != nil {
		return nil, xerr.NewErrMsgErr("角色不存在", err)
	}

	resp = &types.MiniUserInfoResponse{
		User: &types.UserItem{
			Id:       user.Id,
			Name:     user.Name,
			Avatar:   user.Avatar,
			Position: user.Position,
		},
		Accounts: make([]types.Account, 0),
		Auths:    make([]types.Auth, 0),
	}
	if company, _ := domain.LazyLoad(companyMap, l.ctx, conn, user.CompanyId, l.svcCtx.CompanyRepository.FindOne); company != nil {
		resp.User.CompanyName = company.Name
		resp.User.CompanyCode = company.Code
	}
	_, accounts, err = l.svcCtx.UserRepository.Find(l.ctx, conn, domain.NewQueryOptions().MustWithKV("phone", user.Phone).MustWithKV("auditStatus", []int{domain.UserAuditStatusPassed}))
	if err != nil {
		return nil, xerr.NewErrMsgErr("用户不存在", err)
	}
	// 拥有的账号
	for _, account := range accounts {
		if account.Enable != domain.UserEnable {
			continue
		}
		var (
			companyName = ""
			logo        = ""
		)
		if company, _ := domain.LazyLoad(companyMap, l.ctx, conn, account.CompanyId, l.svcCtx.CompanyRepository.FindOne); company != nil {
			companyName = company.Name
			logo = company.Logo
		}
		resp.Accounts = append(resp.Accounts, types.Account{
			CompanyId:   account.CompanyId,
			CompanyName: companyName,
			Logo:        logo,
			UserId:      account.Id,
			Name:        user.Name,
			Position:    user.Position,
		})
	}
	// 拥有的权限
	authSet := collection.NewSet()
	for _, role := range roles {
		for _, auth := range role.Auths {
			if !authSet.Contains(auth) {
				authSet.Add(auth)
				if item := role.GetAuth(auth); item != nil {
					resp.Auths = append(resp.Auths, types.Auth{
						Id:   item.Id,
						Name: item.Name,
						Code: item.Code,
					})
				}
			}
		}
	}
	return
}