diff --git a/pkg/database/dao/mock/subject_template_group.go b/pkg/database/dao/mock/subject_template_group.go index d9d8ce65..ace33a92 100644 --- a/pkg/database/dao/mock/subject_template_group.go +++ b/pkg/database/dao/mock/subject_template_group.go @@ -49,6 +49,20 @@ func (mr *MockSubjectTemplateGroupManagerMockRecorder) BulkCreateWithTx(tx, rela return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "BulkCreateWithTx", reflect.TypeOf((*MockSubjectTemplateGroupManager)(nil).BulkCreateWithTx), tx, relations) } +// BulkDeleteBySubjectPKs mocks base method. +func (m *MockSubjectTemplateGroupManager) BulkDeleteBySubjectPKs(tx *sqlx.Tx, subjectPKs []int64) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "BulkDeleteBySubjectPKs", tx, subjectPKs) + ret0, _ := ret[0].(error) + return ret0 +} + +// BulkDeleteBySubjectPKs indicates an expected call of BulkDeleteBySubjectPKs. +func (mr *MockSubjectTemplateGroupManagerMockRecorder) BulkDeleteBySubjectPKs(tx, subjectPKs interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "BulkDeleteBySubjectPKs", reflect.TypeOf((*MockSubjectTemplateGroupManager)(nil).BulkDeleteBySubjectPKs), tx, subjectPKs) +} + // BulkDeleteWithTx mocks base method. func (m *MockSubjectTemplateGroupManager) BulkDeleteWithTx(tx *sqlx.Tx, relations []dao.SubjectTemplateGroup) error { m.ctrl.T.Helper() diff --git a/pkg/database/dao/subject_template_group.go b/pkg/database/dao/subject_template_group.go index 6961ccdc..66463edd 100644 --- a/pkg/database/dao/subject_template_group.go +++ b/pkg/database/dao/subject_template_group.go @@ -34,6 +34,7 @@ type SubjectTemplateGroup struct { type SubjectTemplateGroupManager interface { GetTemplateGroupMemberCount(groupPK, templateID int64) (int64, error) + GetMaxExpiredAtBySubjectGroup(subjectPK, groupPK int64, excludeTemplateID int64) (int64, error) ListPagingTemplateGroupMember( groupPK, templateID int64, limit, offset int64, @@ -46,7 +47,7 @@ type SubjectTemplateGroupManager interface { BulkUpdateExpiredAtWithTx(tx *sqlx.Tx, relations []SubjectTemplateGroup) error BulkUpdateExpiredAtByRelationWithTx(tx *sqlx.Tx, relations []SubjectRelation) error BulkDeleteWithTx(tx *sqlx.Tx, relations []SubjectTemplateGroup) error - GetMaxExpiredAtBySubjectGroup(subjectPK, groupPK int64, excludeTemplateID int64) (int64, error) + BulkDeleteBySubjectPKs(tx *sqlx.Tx, subjectPKs []int64) error } type subjectTemplateGroupManager struct { @@ -235,3 +236,9 @@ func (m *subjectTemplateGroupManager) ListThinRelationWithMaxExpiredAtByGroupPK( return relations, err } + +func (m *subjectTemplateGroupManager) BulkDeleteBySubjectPKs(tx *sqlx.Tx, subjectPKs []int64) error { + sql := `DELETE FROM subject_template_group + WHERE subject_pk in (?)` + return database.SqlxDeleteWithTx(tx, sql, subjectPKs) +} diff --git a/pkg/service/group.go b/pkg/service/group.go index 2eb775c9..f5d648ce 100644 --- a/pkg/service/group.go +++ b/pkg/service/group.go @@ -899,6 +899,16 @@ func (l *groupService) BulkDeleteBySubjectPKsWithTx(tx *sqlx.Tx, subjectPKs []in err, "manager.BulkDeleteBySubjectPKs subject_pks=`%+v` fail", subjectPKs) } + // 批量删除subject template group + err = l.subjectTemplateGroupManager.BulkDeleteBySubjectPKs(tx, subjectPKs) + if err != nil { + return errorWrapf( + err, + "subjectTemplateGroupManager.BulkDeleteBySubjectPKsWithTx subjectPKs=`%+v` fail", + subjectPKs, + ) + } + // 批量删除用户的subject system group err = l.subjectSystemGroupManager.DeleteBySubjectPKsWithTx(tx, subjectPKs) if err != nil { diff --git a/pkg/service/group_test.go b/pkg/service/group_test.go index 77e7e47e..59f2d8af 100644 --- a/pkg/service/group_test.go +++ b/pkg/service/group_test.go @@ -94,6 +94,28 @@ var _ = Describe("GroupService", func() { assert.Contains(GinkgoT(), err.Error(), "BulkDeleteBySubjectPKs") }) + It("manager.BulkDeleteBySubjectPKs fail", func() { + mockSubjectService := mock.NewMockSubjectGroupManager(ctl) + + mockSubjectService.EXPECT().BulkDeleteBySubjectPKs(gomock.Any(), []int64{1, 2}).Return( + nil, + ).AnyTimes() + + mockSubjectTemplateGroupService := mock.NewMockSubjectTemplateGroupManager(ctl) + mockSubjectTemplateGroupService.EXPECT().BulkDeleteBySubjectPKs(gomock.Any(), []int64{1, 2}).Return( + errors.New("error"), + ).AnyTimes() + + manager := &groupService{ + manager: mockSubjectService, + subjectTemplateGroupManager: mockSubjectTemplateGroupService, + } + + err := manager.BulkDeleteBySubjectPKsWithTx(nil, []int64{1, 2}) + assert.Error(GinkgoT(), err) + assert.Contains(GinkgoT(), err.Error(), "subjectTemplateGroupManager.BulkDeleteBySubjectPKsWithTx") + }) + It("subjectSystemGroupManager.DeleteBySubjectPKsWithTx fail", func() { mockSubjectService := mock.NewMockSubjectGroupManager(ctl) @@ -101,14 +123,20 @@ var _ = Describe("GroupService", func() { nil, ).AnyTimes() + mockSubjectTemplateGroupService := mock.NewMockSubjectTemplateGroupManager(ctl) + mockSubjectTemplateGroupService.EXPECT().BulkDeleteBySubjectPKs(gomock.Any(), []int64{1, 2}).Return( + nil, + ).AnyTimes() + mockSubjectSystemGroupService := mock.NewMockSubjectSystemGroupManager(ctl) mockSubjectSystemGroupService.EXPECT().DeleteBySubjectPKsWithTx(gomock.Any(), []int64{1, 2}).Return( errors.New("error"), ).AnyTimes() manager := &groupService{ - manager: mockSubjectService, - subjectSystemGroupManager: mockSubjectSystemGroupService, + manager: mockSubjectService, + subjectSystemGroupManager: mockSubjectSystemGroupService, + subjectTemplateGroupManager: mockSubjectTemplateGroupService, } err := manager.BulkDeleteBySubjectPKsWithTx(nil, []int64{1, 2}) @@ -123,14 +151,20 @@ var _ = Describe("GroupService", func() { nil, ).AnyTimes() + mockSubjectTemplateGroupService := mock.NewMockSubjectTemplateGroupManager(ctl) + mockSubjectTemplateGroupService.EXPECT().BulkDeleteBySubjectPKs(gomock.Any(), []int64{1, 2}).Return( + nil, + ).AnyTimes() + mockSubjectSystemGroupService := mock.NewMockSubjectSystemGroupManager(ctl) mockSubjectSystemGroupService.EXPECT().DeleteBySubjectPKsWithTx(gomock.Any(), []int64{1, 2}).Return( nil, ).AnyTimes() manager := &groupService{ - manager: mockSubjectService, - subjectSystemGroupManager: mockSubjectSystemGroupService, + manager: mockSubjectService, + subjectSystemGroupManager: mockSubjectSystemGroupService, + subjectTemplateGroupManager: mockSubjectTemplateGroupService, } err := manager.BulkDeleteBySubjectPKsWithTx(nil, []int64{1, 2})