package jnpf.limsService.impl; import jnpf.database.util.TenantDataSourceUtil; import jnpf.lims.api.model.TestWorkItemSnapshotActorRequest; import jnpf.lims.api.model.TestWorkItemSnapshotBatch; import jnpf.limsService.LimsTestWorkItemActorSnapshotService; import jnpf.limsService.LimsTestWorkItemSnapshotService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.StringUtils; @Service public class LimsTestWorkItemActorSnapshotServiceImpl implements LimsTestWorkItemActorSnapshotService { private final LimsTestWorkItemSnapshotService snapshotService; private final TenantRoute tenantRoute; @Autowired public LimsTestWorkItemActorSnapshotServiceImpl( LimsTestWorkItemSnapshotService snapshotService) { this(snapshotService, new TenantRoute() { @Override public void switchTo(String tenantId) { TenantDataSourceUtil.switchTenant(tenantId); } @Override public void clear() { TenantDataSourceUtil.clearLocalTenantInfo(); } }); } LimsTestWorkItemActorSnapshotServiceImpl( LimsTestWorkItemSnapshotService snapshotService, TenantRoute tenantRoute) { this.snapshotService = snapshotService; this.tenantRoute = tenantRoute; } @Override public TestWorkItemSnapshotBatch resolve(TestWorkItemSnapshotActorRequest actor) { if (actor == null || !StringUtils.hasText(actor.getTenantId()) || !StringUtils.hasText(actor.getUserId())) { throw new IllegalArgumentException("内部检验任务快照操作者不能为空"); } String tenantId = actor.getTenantId().trim(); try { tenantRoute.switchTo(tenantId); return snapshotService.resolve(actor.getRequest(), tenantId, actor.getUserId().trim()); } finally { tenantRoute.clear(); } } interface TenantRoute { void switchTo(String tenantId); void clear(); } }