作者 庄敏学

删除分组

... ... @@ -59,7 +59,8 @@ func (l *DeleteChartLogic) DeleteChart(req *types.ChartDeleteRequest) (resp *typ
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))
//删除分组
err = l.svcCtx.ObjectTableRepository.DeleteGroup(ctx, conn, tenantId, int(sourceId))
if err != nil {
return xerr.NewErrMsg("删除失败")
}
... ...
... ... @@ -127,44 +127,7 @@ func DeleteObjectTable(ctx context.Context, svcCtx *svc.ServiceContext, conn tra
return err
}
//删除分组
err = DeleteObjectGroup(ctx, svcCtx, conn, objectTable.CompanyId, objectTable.ParentId)
if err != nil {
return err
}
}
}
return nil
}
// DeleteObjectGroup 删除分组
func DeleteObjectGroup(ctx context.Context, svcCtx *svc.ServiceContext, conn transaction.Conn, companyId int64, parentId int) error {
//分组下是否有数据
_, list, err := svcCtx.ObjectTableRepository.Find(ctx, conn, domain.NewQueryOptions().WithKV("companyId", companyId).WithKV("parentId", parentId))
if err != nil {
return err
}
if len(list) > 0 {
return nil
}
_, items, err := svcCtx.ObjectTableRepository.Find(
ctx, conn,
domain.NewQueryOptions().
WithKV("companyId", companyId).
WithKV("id", parentId).
WithKV("flag", "Group"),
)
if err != nil {
return err
}
if len(items) > 0 {
for _, item := range items {
//删除分组
_, err = svcCtx.ObjectTableRepository.Delete(ctx, conn, item)
if err != nil {
return err
}
//递归删除上级分组
err = DeleteObjectGroup(ctx, svcCtx, conn, companyId, item.ParentId)
err = svcCtx.ObjectTableRepository.DeleteGroup(ctx, conn, objectTable.CompanyId, objectTable.ParentId)
if err != nil {
return err
}
... ...
... ... @@ -188,6 +188,11 @@ func (logic *ByteNoticeLogic) handleDelete(conn transaction.Conn, notice *domain
if err != nil {
return err
}
//删除分组
err = logic.svcCtx.ObjectTableRepository.DeleteGroup(logic.ctx, conn, objectTable.CompanyId, objectTable.ParentId)
if err != nil {
return err
}
}
}
return nil
... ... @@ -197,7 +202,7 @@ func (logic *ByteNoticeLogic) handleDelete(conn transaction.Conn, notice *domain
func (logic *ByteNoticeLogic) saveTables(conn transaction.Conn, tables []*domain.ObjectTable) error {
if len(tables) > 0 {
for _, item := range tables {
objectTable, err := logic.svcCtx.ObjectTableRepository.FindOne(logic.ctx, conn, item.Id)
objectTable, err := logic.svcCtx.ObjectTableRepository.FindOne(logic.ctx, conn, item.Id, item.TableType)
if err == nil && objectTable.Id > 0 {
item.Id = objectTable.Id
item.Version = objectTable.Version + 1
... ...
... ... @@ -163,7 +163,7 @@ func (l *SyncTableLogic) getFields(list []*domain.ObjectTable) []*domain.ObjectF
// SaveTables 保存数据
func (l *SyncTableLogic) SaveTables(conn transaction.Conn, list []*domain.ObjectTable) error {
for _, item := range list {
objectTable, err := l.svcCtx.ObjectTableRepository.FindOne(l.ctx, conn, item.Id)
objectTable, err := l.svcCtx.ObjectTableRepository.FindOne(l.ctx, conn, item.Id, item.TableType)
if err == nil && objectTable.Id > 0 {
item.IsLocal = objectTable.IsLocal
item.Version = objectTable.Version + 1
... ...
... ... @@ -78,10 +78,10 @@ func (repository *ObjectTableRepository) UpdateWithVersion(ctx context.Context,
func (repository *ObjectTableRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.ObjectTable) (*domain.ObjectTable, error) {
var (
tx = conn.DB()
m = &models.ObjectTable{Id: dm.Identify().(int)}
m = &models.ObjectTable{Id: dm.Identify().(int), TableType: dm.TableType}
)
queryFunc := func() (interface{}, error) {
tx = tx.Where("id = ?", m.Id).Unscoped().Delete(m)
tx = tx.Where("id = ? and table_type = ?", m.Id, dm.TableType).Unscoped().Delete(m)
return m, tx.Error
}
if _, err := repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
... ... @@ -90,7 +90,7 @@ func (repository *ObjectTableRepository) Delete(ctx context.Context, conn transa
return repository.ModelToDomainModel(m)
}
func (repository *ObjectTableRepository) FindOne(ctx context.Context, conn transaction.Conn, id int) (*domain.ObjectTable, error) {
func (repository *ObjectTableRepository) FindOne(ctx context.Context, conn transaction.Conn, id int, tableType string) (*domain.ObjectTable, error) {
var (
err error
tx = conn.DB()
... ... @@ -135,6 +135,15 @@ func (repository *ObjectTableRepository) Find(ctx context.Context, conn transact
if v, ok := queryOptions["companyId"]; ok {
tx = tx.Where("company_id = ?", v)
}
if v, ok := queryOptions["id"]; ok {
tx = tx.Where("id = ?", v)
}
if v, ok := queryOptions["flag"]; ok {
tx = tx.Where("flag = ?", v)
}
if v, ok := queryOptions["parentId"]; ok {
tx = tx.Where("parent_id = ?", v)
}
if v, ok := queryOptions["tableTypeIn"]; ok {
tx = tx.Where("table_type in ?", v)
}
... ... @@ -158,6 +167,43 @@ func (repository *ObjectTableRepository) Find(ctx context.Context, conn transact
return total, dms, nil
}
// DeleteGroup 删除分组
func (repository *ObjectTableRepository) DeleteGroup(ctx context.Context, conn transaction.Conn, companyId int64, parentId int) error {
//分组下是否有数据
_, list, err := repository.Find(ctx, conn, domain.NewQueryOptions().WithKV("companyId", companyId).WithKV("parentId", parentId))
if err != nil {
return err
}
if len(list) > 0 {
return nil
}
_, items, err := repository.Find(
ctx, conn,
domain.NewQueryOptions().
WithKV("companyId", companyId).
WithKV("id", parentId).
WithKV("flag", "Group"),
)
if err != nil {
return err
}
if len(items) > 0 {
for _, item := range items {
//删除分组
_, err = repository.Delete(ctx, conn, item)
if err != nil {
return err
}
//递归删除上级分组
err = repository.DeleteGroup(ctx, conn, companyId, item.ParentId)
if err != nil {
return err
}
}
}
return nil
}
func (repository *ObjectTableRepository) ModelToDomainModel(from *models.ObjectTable) (*domain.ObjectTable, error) {
to := &domain.ObjectTable{}
err := copier.Copy(to, from)
... ...
... ... @@ -29,9 +29,10 @@ type ObjectTableRepository interface {
Update(ctx context.Context, conn transaction.Conn, dm *ObjectTable) (*ObjectTable, error)
UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *ObjectTable) (*ObjectTable, error)
Delete(ctx context.Context, conn transaction.Conn, dm *ObjectTable) (*ObjectTable, error)
FindOne(ctx context.Context, conn transaction.Conn, id int) (*ObjectTable, error)
FindOne(ctx context.Context, conn transaction.Conn, id int, tableType string) (*ObjectTable, error)
FindOneByTableId(ctx context.Context, conn transaction.Conn, tableId int) (*ObjectTable, error)
Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ObjectTable, error)
DeleteGroup(ctx context.Context, conn transaction.Conn, companyId int64, parentId int) error
}
type ObjectTableDataQuery struct {
... ...