作者 庄敏学

Merge branch 'dev-data' into dev

@@ -3,6 +3,7 @@ package chart @@ -3,6 +3,7 @@ package chart
3 import ( 3 import (
4 "context" 4 "context"
5 "fmt" 5 "fmt"
  6 + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/db/transaction"
6 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/domain" 7 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/domain"
7 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/pkg/contextdata" 8 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/pkg/contextdata"
8 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/pkg/xerr" 9 "gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/pkg/xerr"
@@ -35,6 +36,7 @@ func (l *DeleteChartLogic) DeleteChart(req *types.ChartDeleteRequest) (resp *typ @@ -35,6 +36,7 @@ func (l *DeleteChartLogic) DeleteChart(req *types.ChartDeleteRequest) (resp *typ
35 appPage *domain.AppPage 36 appPage *domain.AppPage
36 ) 37 )
37 resp = &types.ChartDeleteResponse{} 38 resp = &types.ChartDeleteResponse{}
  39 +
38 if chart, err = l.svcCtx.ChartRepository.FindOne(l.ctx, conn, req.Id); err != nil { 40 if chart, err = l.svcCtx.ChartRepository.FindOne(l.ctx, conn, req.Id); err != nil {
39 return nil, xerr.NewErrMsgErr("不存在", err) 41 return nil, xerr.NewErrMsgErr("不存在", err)
40 } 42 }
@@ -48,9 +50,23 @@ func (l *DeleteChartLogic) DeleteChart(req *types.ChartDeleteRequest) (resp *typ @@ -48,9 +50,23 @@ func (l *DeleteChartLogic) DeleteChart(req *types.ChartDeleteRequest) (resp *typ
48 if appPage.Identify() != nil { 50 if appPage.Identify() != nil {
49 return nil, xerr.NewErrMsgErr(fmt.Sprintf("该图表已被页面\"%v\"引用", appPage.Name), err) 51 return nil, xerr.NewErrMsgErr(fmt.Sprintf("该图表已被页面\"%v\"引用", appPage.Name), err)
50 } 52 }
  53 +
  54 + err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, conn transaction.Conn) error {
51 if chart, err = l.svcCtx.ChartRepository.Delete(l.ctx, conn, chart); err != nil { 55 if chart, err = l.svcCtx.ChartRepository.Delete(l.ctx, conn, chart); err != nil {
52 - return nil, xerr.NewErrMsgErr("删除失败", err) 56 + return xerr.NewErrMsgErr("删除失败", err)
  57 + }
  58 + //删除数据源
  59 + chartSetting, err := l.svcCtx.ChartSettingRepository.FindOne(l.ctx, conn, chart.Id)
  60 + if err == nil && len(chartSetting.DataSourceIds) > 0 {
  61 + for _, sourceId := range chartSetting.DataSourceIds {
  62 + err = DeleteObjectTable(l.ctx, l.svcCtx, conn, int(sourceId))
  63 + if err != nil {
  64 + return xerr.NewErrMsg("删除失败")
  65 + }
  66 + }
53 } 67 }
  68 + return nil
  69 + }, true)
54 70
55 resp = &types.ChartDeleteResponse{} 71 resp = &types.ChartDeleteResponse{}
56 return 72 return
@@ -96,29 +96,37 @@ func (l *UpdateChartLogic) SyncTableData(tenantId int64, conn transaction.Conn, @@ -96,29 +96,37 @@ func (l *UpdateChartLogic) SyncTableData(tenantId int64, conn transaction.Conn,
96 //删除 96 //删除
97 if len(right) > 0 { 97 if len(right) > 0 {
98 for _, sourceId := range right { 98 for _, sourceId := range right {
  99 + err := DeleteObjectTable(l.ctx, l.svcCtx, conn, int(sourceId))
  100 + if err != nil {
  101 + return err
  102 + }
  103 + }
  104 + }
  105 + return nil
  106 +}
  107 +
  108 +func DeleteObjectTable(ctx context.Context, svcCtx *svc.ServiceContext, conn transaction.Conn, sourceId int) error {
99 //验证其他图表是否使用 109 //验证其他图表是否使用
100 - used, err := l.svcCtx.ChartSettingRepository.CheckUseDataSource(l.ctx, conn, int(sourceId)) 110 + used, err := svcCtx.ChartSettingRepository.CheckUseDataSource(ctx, conn, sourceId)
101 if err == nil && !used { //未使用,删除 111 if err == nil && !used { //未使用,删除
102 - err = l.svcCtx.ObjectTableDataRepository.DropTable(l.ctx, conn, int(sourceId)) 112 + err = svcCtx.ObjectTableDataRepository.DropTable(ctx, conn, sourceId)
103 if err != nil { 113 if err != nil {
104 return err 114 return err
105 } 115 }
106 //是否远程已删除 116 //是否远程已删除
107 - objectTable, err := l.svcCtx.ObjectTableRepository.FindOne(l.ctx, conn, int(sourceId)) 117 + objectTable, err := svcCtx.ObjectTableRepository.FindOne(ctx, conn, sourceId)
108 if err == nil && objectTable.Id > 0 && objectTable.RemoteDeleted == 1 { 118 if err == nil && objectTable.Id > 0 && objectTable.RemoteDeleted == 1 {
109 //删除表 119 //删除表
110 - _, err = l.svcCtx.ObjectTableRepository.Delete(l.ctx, conn, &domain.ObjectTable{Id: int(sourceId)}) 120 + _, err = svcCtx.ObjectTableRepository.Delete(ctx, conn, &domain.ObjectTable{Id: sourceId})
111 if err != nil { 121 if err != nil {
112 return err 122 return err
113 } 123 }
114 //删除字段 124 //删除字段
115 - _, err = l.svcCtx.ObjectFieldRepository.Delete(l.ctx, conn, &domain.ObjectField{Id: sourceId}) 125 + _, err = svcCtx.ObjectFieldRepository.Delete(ctx, conn, &domain.ObjectField{Id: int64(sourceId)})
116 if err != nil { 126 if err != nil {
117 return err 127 return err
118 } 128 }
119 } 129 }
120 } 130 }
121 - }  
122 - }  
123 return nil 131 return nil
124 } 132 }