save_as_chart_logic.go 1.5 KB
package chart

import (
	"context"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/domain"
	"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 SaveAsChartLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

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

func (l *SaveAsChartLogic) SaveAsChart(req *types.ChartSaveAsRequest) (resp *types.ChartSaveResponse, err error) {
	var (
		conn         = l.svcCtx.DefaultDBConn()
		chartSetting *domain.ChartSetting
		chart        *domain.Chart
	)
	chart, err = l.svcCtx.ChartRepository.FindOne(l.ctx, conn, req.ChartId)
	if err != nil {
		return nil, xerr.NewErrMsgErr("未找到图表", err)
	}
	chartSetting, err = l.svcCtx.ChartSettingRepository.FindOne(l.ctx, conn, req.ChartId)
	if err != nil {
		return nil, xerr.NewErrMsgErr("未找到图表", err)
	}
	saveChartLogic := NewSaveChartLogic(l.ctx, l.svcCtx)

	resp, err = saveChartLogic.SaveChart(&types.ChartSaveRequest{
		Pid:           0,
		Type:          chart.Type,
		ChartType:     chart.ChartType,
		ChartProperty: types.NewPropertyItem(chartSetting.ChartProperty(chart.Cover)),
	})
	return
}