save_app_page_logic.go 1.8 KB
package page

import (
	"context"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/db/transaction"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/pkg/contextdata"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/pkg/xerr"

	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/svc"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/types"

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

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

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

func (l *SaveAppPageLogic) SaveAppPage(req *types.AppPageSaveRequest) (resp *types.AppPageSaveResponse, err error) {
	var (
		conn     = l.svcCtx.DefaultDBConn()
		appPage  *domain.AppPage
		tenantId = contextdata.GetTenantFromCtx(l.ctx)
	)
	appPage = &domain.AppPage{
		Name:     req.Name,
		Charts:   req.Charts,
		TenantId: tenantId,
		Cover:    req.Cover,
	}
	if total, _, _ := l.svcCtx.AppPageRepository.FindByName(l.ctx, conn, tenantId, req.Name); total > 0 {
		return nil, xerr.NewErrMsgErr("页面名字重复", nil)
	}
	if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
		if appPage, err = l.svcCtx.AppPageRepository.Insert(l.ctx, conn, appPage); err != nil {
			return err
		}
		return nil
	}, true); err != nil {
		return nil, xerr.NewErrMsgErr("创建页面失败", err)
	}
	resp = &types.AppPageSaveResponse{
		Id: appPage.Id,
	}
	return
}