system_user_update_logic.go 1.9 KB
package user

import (
	"context"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
	"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 SystemUserUpdateLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

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

func (l *SystemUserUpdateLogic) SystemUserUpdate(req *types.SystemUserUpdateRequest) (resp *types.SystemUserUpdateResponse, err error) {
	var (
		userToken = contextdata.GetUserTokenFromCtx(l.ctx)
		user      *domain.User
		conn      = l.svcCtx.DefaultDBConn()
	)
	if user, err = l.svcCtx.UserRepository.FindOne(l.ctx, conn, req.Id); err != nil {
		return nil, xerr.NewErrMsgErr("用户不存在", err)
	}
	if user.CompanyId != userToken.CompanyId {
		return nil, xerr.NewErrMsgErr("无权限更改", err)
	}
	user.Avatar = req.Avatar
	user.Position = req.Position
	user.Departments = req.Departments
	user.WithName(user.Name)
	if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
		if user, err = l.svcCtx.UserRepository.UpdateWithVersion(ctx, conn, user); err != nil {
			return err
		}
		return nil
	}, true); err != nil {
		return nil, xerr.NewErrMsgErr("更新用户信息失败", err)
	}
	resp = &types.SystemUserUpdateResponse{}
	return
}