作者 庄敏学

删除分组

@@ -59,7 +59,8 @@ func (l *DeleteChartLogic) DeleteChart(req *types.ChartDeleteRequest) (resp *typ @@ -59,7 +59,8 @@ func (l *DeleteChartLogic) DeleteChart(req *types.ChartDeleteRequest) (resp *typ
59 chartSetting, err := l.svcCtx.ChartSettingRepository.FindOne(l.ctx, conn, chart.Id) 59 chartSetting, err := l.svcCtx.ChartSettingRepository.FindOne(l.ctx, conn, chart.Id)
60 if err == nil && len(chartSetting.DataSourceIds) > 0 { 60 if err == nil && len(chartSetting.DataSourceIds) > 0 {
61 for _, sourceId := range chartSetting.DataSourceIds { 61 for _, sourceId := range chartSetting.DataSourceIds {
62 - err = DeleteObjectTable(l.ctx, l.svcCtx, conn, int(sourceId)) 62 + //删除分组
  63 + err = l.svcCtx.ObjectTableRepository.DeleteGroup(ctx, conn, tenantId, int(sourceId))
63 if err != nil { 64 if err != nil {
64 return xerr.NewErrMsg("删除失败") 65 return xerr.NewErrMsg("删除失败")
65 } 66 }
@@ -127,44 +127,7 @@ func DeleteObjectTable(ctx context.Context, svcCtx *svc.ServiceContext, conn tra @@ -127,44 +127,7 @@ func DeleteObjectTable(ctx context.Context, svcCtx *svc.ServiceContext, conn tra
127 return err 127 return err
128 } 128 }
129 //删除分组 129 //删除分组
130 - err = DeleteObjectGroup(ctx, svcCtx, conn, objectTable.CompanyId, objectTable.ParentId)  
131 - if err != nil {  
132 - return err  
133 - }  
134 - }  
135 - }  
136 - return nil  
137 -}  
138 -  
139 -// DeleteObjectGroup 删除分组  
140 -func DeleteObjectGroup(ctx context.Context, svcCtx *svc.ServiceContext, conn transaction.Conn, companyId int64, parentId int) error {  
141 - //分组下是否有数据  
142 - _, list, err := svcCtx.ObjectTableRepository.Find(ctx, conn, domain.NewQueryOptions().WithKV("companyId", companyId).WithKV("parentId", parentId))  
143 - if err != nil {  
144 - return err  
145 - }  
146 - if len(list) > 0 {  
147 - return nil  
148 - }  
149 - _, items, err := svcCtx.ObjectTableRepository.Find(  
150 - ctx, conn,  
151 - domain.NewQueryOptions().  
152 - WithKV("companyId", companyId).  
153 - WithKV("id", parentId).  
154 - WithKV("flag", "Group"),  
155 - )  
156 - if err != nil {  
157 - return err  
158 - }  
159 - if len(items) > 0 {  
160 - for _, item := range items {  
161 - //删除分组  
162 - _, err = svcCtx.ObjectTableRepository.Delete(ctx, conn, item)  
163 - if err != nil {  
164 - return err  
165 - }  
166 - //递归删除上级分组  
167 - err = DeleteObjectGroup(ctx, svcCtx, conn, companyId, item.ParentId) 130 + err = svcCtx.ObjectTableRepository.DeleteGroup(ctx, conn, objectTable.CompanyId, objectTable.ParentId)
168 if err != nil { 131 if err != nil {
169 return err 132 return err
170 } 133 }
@@ -188,6 +188,11 @@ func (logic *ByteNoticeLogic) handleDelete(conn transaction.Conn, notice *domain @@ -188,6 +188,11 @@ func (logic *ByteNoticeLogic) handleDelete(conn transaction.Conn, notice *domain
188 if err != nil { 188 if err != nil {
189 return err 189 return err
190 } 190 }
  191 + //删除分组
  192 + err = logic.svcCtx.ObjectTableRepository.DeleteGroup(logic.ctx, conn, objectTable.CompanyId, objectTable.ParentId)
  193 + if err != nil {
  194 + return err
  195 + }
191 } 196 }
192 } 197 }
193 return nil 198 return nil
@@ -197,7 +202,7 @@ func (logic *ByteNoticeLogic) handleDelete(conn transaction.Conn, notice *domain @@ -197,7 +202,7 @@ func (logic *ByteNoticeLogic) handleDelete(conn transaction.Conn, notice *domain
197 func (logic *ByteNoticeLogic) saveTables(conn transaction.Conn, tables []*domain.ObjectTable) error { 202 func (logic *ByteNoticeLogic) saveTables(conn transaction.Conn, tables []*domain.ObjectTable) error {
198 if len(tables) > 0 { 203 if len(tables) > 0 {
199 for _, item := range tables { 204 for _, item := range tables {
200 - objectTable, err := logic.svcCtx.ObjectTableRepository.FindOne(logic.ctx, conn, item.Id) 205 + objectTable, err := logic.svcCtx.ObjectTableRepository.FindOne(logic.ctx, conn, item.Id, item.TableType)
201 if err == nil && objectTable.Id > 0 { 206 if err == nil && objectTable.Id > 0 {
202 item.Id = objectTable.Id 207 item.Id = objectTable.Id
203 item.Version = objectTable.Version + 1 208 item.Version = objectTable.Version + 1
@@ -163,7 +163,7 @@ func (l *SyncTableLogic) getFields(list []*domain.ObjectTable) []*domain.ObjectF @@ -163,7 +163,7 @@ func (l *SyncTableLogic) getFields(list []*domain.ObjectTable) []*domain.ObjectF
163 // SaveTables 保存数据 163 // SaveTables 保存数据
164 func (l *SyncTableLogic) SaveTables(conn transaction.Conn, list []*domain.ObjectTable) error { 164 func (l *SyncTableLogic) SaveTables(conn transaction.Conn, list []*domain.ObjectTable) error {
165 for _, item := range list { 165 for _, item := range list {
166 - objectTable, err := l.svcCtx.ObjectTableRepository.FindOne(l.ctx, conn, item.Id) 166 + objectTable, err := l.svcCtx.ObjectTableRepository.FindOne(l.ctx, conn, item.Id, item.TableType)
167 if err == nil && objectTable.Id > 0 { 167 if err == nil && objectTable.Id > 0 {
168 item.IsLocal = objectTable.IsLocal 168 item.IsLocal = objectTable.IsLocal
169 item.Version = objectTable.Version + 1 169 item.Version = objectTable.Version + 1
@@ -78,10 +78,10 @@ func (repository *ObjectTableRepository) UpdateWithVersion(ctx context.Context, @@ -78,10 +78,10 @@ func (repository *ObjectTableRepository) UpdateWithVersion(ctx context.Context,
78 func (repository *ObjectTableRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.ObjectTable) (*domain.ObjectTable, error) { 78 func (repository *ObjectTableRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.ObjectTable) (*domain.ObjectTable, error) {
79 var ( 79 var (
80 tx = conn.DB() 80 tx = conn.DB()
81 - m = &models.ObjectTable{Id: dm.Identify().(int)} 81 + m = &models.ObjectTable{Id: dm.Identify().(int), TableType: dm.TableType}
82 ) 82 )
83 queryFunc := func() (interface{}, error) { 83 queryFunc := func() (interface{}, error) {
84 - tx = tx.Where("id = ?", m.Id).Unscoped().Delete(m) 84 + tx = tx.Where("id = ? and table_type = ?", m.Id, dm.TableType).Unscoped().Delete(m)
85 return m, tx.Error 85 return m, tx.Error
86 } 86 }
87 if _, err := repository.Query(queryFunc, m.CacheKeyFunc()); err != nil { 87 if _, err := repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
@@ -90,7 +90,7 @@ func (repository *ObjectTableRepository) Delete(ctx context.Context, conn transa @@ -90,7 +90,7 @@ func (repository *ObjectTableRepository) Delete(ctx context.Context, conn transa
90 return repository.ModelToDomainModel(m) 90 return repository.ModelToDomainModel(m)
91 } 91 }
92 92
93 -func (repository *ObjectTableRepository) FindOne(ctx context.Context, conn transaction.Conn, id int) (*domain.ObjectTable, error) { 93 +func (repository *ObjectTableRepository) FindOne(ctx context.Context, conn transaction.Conn, id int, tableType string) (*domain.ObjectTable, error) {
94 var ( 94 var (
95 err error 95 err error
96 tx = conn.DB() 96 tx = conn.DB()
@@ -135,6 +135,15 @@ func (repository *ObjectTableRepository) Find(ctx context.Context, conn transact @@ -135,6 +135,15 @@ func (repository *ObjectTableRepository) Find(ctx context.Context, conn transact
135 if v, ok := queryOptions["companyId"]; ok { 135 if v, ok := queryOptions["companyId"]; ok {
136 tx = tx.Where("company_id = ?", v) 136 tx = tx.Where("company_id = ?", v)
137 } 137 }
  138 + if v, ok := queryOptions["id"]; ok {
  139 + tx = tx.Where("id = ?", v)
  140 + }
  141 + if v, ok := queryOptions["flag"]; ok {
  142 + tx = tx.Where("flag = ?", v)
  143 + }
  144 + if v, ok := queryOptions["parentId"]; ok {
  145 + tx = tx.Where("parent_id = ?", v)
  146 + }
138 if v, ok := queryOptions["tableTypeIn"]; ok { 147 if v, ok := queryOptions["tableTypeIn"]; ok {
139 tx = tx.Where("table_type in ?", v) 148 tx = tx.Where("table_type in ?", v)
140 } 149 }
@@ -158,6 +167,43 @@ func (repository *ObjectTableRepository) Find(ctx context.Context, conn transact @@ -158,6 +167,43 @@ func (repository *ObjectTableRepository) Find(ctx context.Context, conn transact
158 return total, dms, nil 167 return total, dms, nil
159 } 168 }
160 169
  170 +// DeleteGroup 删除分组
  171 +func (repository *ObjectTableRepository) DeleteGroup(ctx context.Context, conn transaction.Conn, companyId int64, parentId int) error {
  172 + //分组下是否有数据
  173 + _, list, err := repository.Find(ctx, conn, domain.NewQueryOptions().WithKV("companyId", companyId).WithKV("parentId", parentId))
  174 + if err != nil {
  175 + return err
  176 + }
  177 + if len(list) > 0 {
  178 + return nil
  179 + }
  180 + _, items, err := repository.Find(
  181 + ctx, conn,
  182 + domain.NewQueryOptions().
  183 + WithKV("companyId", companyId).
  184 + WithKV("id", parentId).
  185 + WithKV("flag", "Group"),
  186 + )
  187 + if err != nil {
  188 + return err
  189 + }
  190 + if len(items) > 0 {
  191 + for _, item := range items {
  192 + //删除分组
  193 + _, err = repository.Delete(ctx, conn, item)
  194 + if err != nil {
  195 + return err
  196 + }
  197 + //递归删除上级分组
  198 + err = repository.DeleteGroup(ctx, conn, companyId, item.ParentId)
  199 + if err != nil {
  200 + return err
  201 + }
  202 + }
  203 + }
  204 + return nil
  205 +}
  206 +
161 func (repository *ObjectTableRepository) ModelToDomainModel(from *models.ObjectTable) (*domain.ObjectTable, error) { 207 func (repository *ObjectTableRepository) ModelToDomainModel(from *models.ObjectTable) (*domain.ObjectTable, error) {
162 to := &domain.ObjectTable{} 208 to := &domain.ObjectTable{}
163 err := copier.Copy(to, from) 209 err := copier.Copy(to, from)
@@ -29,9 +29,10 @@ type ObjectTableRepository interface { @@ -29,9 +29,10 @@ type ObjectTableRepository interface {
29 Update(ctx context.Context, conn transaction.Conn, dm *ObjectTable) (*ObjectTable, error) 29 Update(ctx context.Context, conn transaction.Conn, dm *ObjectTable) (*ObjectTable, error)
30 UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *ObjectTable) (*ObjectTable, error) 30 UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *ObjectTable) (*ObjectTable, error)
31 Delete(ctx context.Context, conn transaction.Conn, dm *ObjectTable) (*ObjectTable, error) 31 Delete(ctx context.Context, conn transaction.Conn, dm *ObjectTable) (*ObjectTable, error)
32 - FindOne(ctx context.Context, conn transaction.Conn, id int) (*ObjectTable, error) 32 + FindOne(ctx context.Context, conn transaction.Conn, id int, tableType string) (*ObjectTable, error)
33 FindOneByTableId(ctx context.Context, conn transaction.Conn, tableId int) (*ObjectTable, error) 33 FindOneByTableId(ctx context.Context, conn transaction.Conn, tableId int) (*ObjectTable, error)
34 Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ObjectTable, error) 34 Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ObjectTable, error)
  35 + DeleteGroup(ctx context.Context, conn transaction.Conn, companyId int64, parentId int) error
35 } 36 }
36 37
37 type ObjectTableDataQuery struct { 38 type ObjectTableDataQuery struct {