save_chart_logic.go
3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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/tool"
"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 SaveChartLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewSaveChartLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SaveChartLogic {
return &SaveChartLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *SaveChartLogic) SaveChart(req *types.ChartSaveRequest) (resp *types.ChartSaveResponse, err error) {
var (
conn = l.svcCtx.DefaultDBConn()
//parentChart *domain.Chart
chart *domain.Chart
chartSetting *domain.ChartSetting
tenantId = contextdata.GetTenantFromCtx(l.ctx)
)
if !domain.ChartTypeContain(req.Type) {
return nil, xerr.NewErrMsgErr("未知类型:"+req.Type, err)
}
chart = &domain.Chart{
Name: req.Name,
Type: req.Type,
Pid: req.Pid,
Sort: 1,
Group: tool.Krand(10, tool.KC_RAND_KIND_UPPER),
TenantId: tenantId,
ChartType: req.ChartType,
Cover: req.Cover,
}
if chart.Name == "" {
chart.Name = chart.RandName()
}
//if req.Pid > 0 {
// if parentChart, err = l.svcCtx.ChartRepository.FindOne(l.ctx, conn, req.Pid); err != nil {
// return nil, xerr.NewErrMsgErr("父级不存在", err)
// }
// chart.Group = parentChart.Group
//}
// current sort
//if currentSortChart, _ := l.svcCtx.ChartRepository.FindOneByGroup(l.ctx, conn, tenantId, req.Pid); currentSortChart != nil {
// chart.Sort = currentSortChart.Sort + 1
//}
if total, _, _ := l.svcCtx.ChartRepository.FindByTypeAndName(l.ctx, conn, tenantId, req.Type, 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 chart, err = l.svcCtx.ChartRepository.Insert(ctx, conn, chart); err != nil {
return err
}
chartProperty := types.NewProperty(req.ChartProperty)
chartSetting = &domain.ChartSetting{
ChartId: chart.Id,
DataSourceId: chartProperty.GetFirstDataSourceId(),
DataSourceIds: chartProperty.GetAllDataSourceId(),
ChartType: chart.ChartType,
Title: chartProperty.Title,
TableAbility: chartProperty.TableAbility,
Series: chartProperty.Series,
Other: chartProperty.Other,
TenantId: chart.TenantId,
}
if chartSetting, err = l.svcCtx.ChartSettingRepository.Insert(l.ctx, conn, chartSetting); err != nil {
return err
}
return nil
}, true); err != nil {
return nil, xerr.NewErrMsgErr("创建失败", err)
}
resp = &types.ChartSaveResponse{
Chart: types.NewChartItemWithSetting(chart, chartSetting),
}
return
}