mini_user_audit_logic.go 1.6 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/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 MiniUserAuditLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

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

func (l *MiniUserAuditLogic) MiniUserAudit(req *types.MiniUserAuditRequest) (err error) {
	var (
		conn = l.svcCtx.DefaultDBConn()
		user *domain.User
	)
	if user, err = l.svcCtx.UserRepository.FindOne(l.ctx, conn, req.UserId); err != nil {
		return xerr.NewErrMsgErr("用户不存在", err)
	}
	if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
		if err = user.Audit(req.Status); err != nil {
			return err
		}
		if user, err = l.svcCtx.UserRepository.UpdateWithVersion(ctx, conn, user); err != nil {
			return err
		}
		if req.Status == domain.UserAuditStatusReject {
			if user, err = l.svcCtx.UserRepository.Delete(ctx, conn, user); err != nil {
				return err
			}
		}
		return nil
	}, true); err != nil {
		return xerr.NewErrMsgErr("审核失败", err)
	}
	return nil
}