刘光辉
118 分钟以前 06f314545ac3514d7ce28939920866ae8188d31c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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();
    }
}