update_chart_logic.go 2.3 KB
package chart

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 UpdateChartLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

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

func (l *UpdateChartLogic) UpdateChart(req *types.ChartUpdateRequest) (resp *types.ChartUpdateResponse, err error) {
	var (
		conn         = l.svcCtx.DefaultDBConn()
		chart        *domain.Chart
		chartSetting *domain.ChartSetting
		tenantId     = contextdata.GetTenantFromCtx(l.ctx)
	)

	if chart, err = l.svcCtx.ChartRepository.FindOne(l.ctx, conn, req.Id); err != nil || chart.TenantId != tenantId {
		return nil, xerr.NewErrMsgErr("图表不存在", err)
	}
	if chartSetting, err = l.svcCtx.ChartSettingRepository.FindOne(l.ctx, conn, req.Id); err != nil {
		return nil, xerr.NewErrMsgErr("图表配置不存在", err)
	}

	if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error {
		if len(req.Cover) > 0 && chart.Cover != req.Cover {
			chart.Cover = req.Cover
		}
		if chart, err = l.svcCtx.ChartRepository.UpdateWithVersion(l.ctx, conn, chart); err != nil {
			return err
		}
		chartProperty := types.NewProperty(req.ChartProperty)
		chartSetting.Title = chartProperty.Title
		chartSetting.TableAbility = chartProperty.TableAbility
		chartSetting.Series = chartProperty.Series
		chartSetting.Other = chartProperty.Other
		if chartSetting, err = l.svcCtx.ChartSettingRepository.UpdateWithVersion(l.ctx, conn, chartSetting); err != nil {
			return err
		}
		return nil
	}, true); err != nil {
		return nil, xerr.NewErrMsgErr("创建失败", err)
	}
	return
}