作者 庄敏学

删除图表时删除已下载数据表

... ... @@ -3,6 +3,7 @@ package chart
import (
"context"
"fmt"
"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"
... ... @@ -35,6 +36,7 @@ func (l *DeleteChartLogic) DeleteChart(req *types.ChartDeleteRequest) (resp *typ
appPage *domain.AppPage
)
resp = &types.ChartDeleteResponse{}
if chart, err = l.svcCtx.ChartRepository.FindOne(l.ctx, conn, req.Id); err != nil {
return nil, xerr.NewErrMsgErr("不存在", err)
}
... ... @@ -48,9 +50,23 @@ func (l *DeleteChartLogic) DeleteChart(req *types.ChartDeleteRequest) (resp *typ
if appPage.Identify() != nil {
return nil, xerr.NewErrMsgErr(fmt.Sprintf("该图表已被页面\"%v\"引用", appPage.Name), err)
}
err = transaction.UseTrans(l.ctx, conn.DB(), func(ctx context.Context, conn transaction.Conn) error {
if chart, err = l.svcCtx.ChartRepository.Delete(l.ctx, conn, chart); err != nil {
return nil, xerr.NewErrMsgErr("删除失败", err)
return xerr.NewErrMsgErr("删除失败", err)
}
//删除数据源
chartSetting, err := l.svcCtx.ChartSettingRepository.FindOne(l.ctx, conn, chart.Id)
if err == nil && len(chartSetting.DataSourceIds) > 0 {
for _, sourceId := range chartSetting.DataSourceIds {
err = DeleteObjectTable(l.ctx, l.svcCtx, conn, int(sourceId))
if err != nil {
return xerr.NewErrMsg("删除失败")
}
}
}
return nil
}, true)
resp = &types.ChartDeleteResponse{}
return
... ...
... ... @@ -96,29 +96,37 @@ func (l *UpdateChartLogic) SyncTableData(tenantId int64, conn transaction.Conn,
//删除
if len(right) > 0 {
for _, sourceId := range right {
err := DeleteObjectTable(l.ctx, l.svcCtx, conn, int(sourceId))
if err != nil {
return err
}
}
}
return nil
}
func DeleteObjectTable(ctx context.Context, svcCtx *svc.ServiceContext, conn transaction.Conn, sourceId int) error {
//验证其他图表是否使用
used, err := l.svcCtx.ChartSettingRepository.CheckUseDataSource(l.ctx, conn, int(sourceId))
used, err := svcCtx.ChartSettingRepository.CheckUseDataSource(ctx, conn, sourceId)
if err == nil && !used { //未使用,删除
err = l.svcCtx.ObjectTableDataRepository.DropTable(l.ctx, conn, int(sourceId))
err = svcCtx.ObjectTableDataRepository.DropTable(ctx, conn, sourceId)
if err != nil {
return err
}
//是否远程已删除
objectTable, err := l.svcCtx.ObjectTableRepository.FindOne(l.ctx, conn, int(sourceId))
objectTable, err := svcCtx.ObjectTableRepository.FindOne(ctx, conn, sourceId)
if err == nil && objectTable.Id > 0 && objectTable.RemoteDeleted == 1 {
//删除表
_, err = l.svcCtx.ObjectTableRepository.Delete(l.ctx, conn, &domain.ObjectTable{Id: int(sourceId)})
_, err = svcCtx.ObjectTableRepository.Delete(ctx, conn, &domain.ObjectTable{Id: sourceId})
if err != nil {
return err
}
//删除字段
_, err = l.svcCtx.ObjectFieldRepository.Delete(l.ctx, conn, &domain.ObjectField{Id: sourceId})
_, err = svcCtx.ObjectFieldRepository.Delete(ctx, conn, &domain.ObjectField{Id: int64(sourceId)})
if err != nil {
return err
}
}
}
}
}
return nil
}
... ...