刘光辉
5 小时以前 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
58
59
60
61
package jnpf.base.controller;
 
import cn.dev33.satoken.same.SaSameUtil;
import cn.dev33.satoken.stp.StpUtil;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.base.model.uploadedit.UploadEditLease;
import jnpf.base.model.uploadedit.UploadEditSaveCommand;
import jnpf.base.service.UploadEditDraftService;
import jnpf.database.util.TenantDataSourceUtil;
import jnpf.exception.DataException;
import jnpf.onlinedev.UploadEditApi;
import jnpf.util.NoDataSourceBind;
import jnpf.util.UserProvider;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import java.util.function.Supplier;
 
@RestController
@RequestMapping("/Internal/visualdev/upload-edit")
@RequiredArgsConstructor
public class UploadEditInternalController implements UploadEditApi {
    private final UploadEditDraftService service;
    @Override @PostMapping("/status-access") @NoDataSourceBind
    public ActionResult<Boolean> canReadStatus(@RequestParam("tenantId") String tenantId,
            @RequestParam("ownerId") String ownerId, @RequestParam("leaseId") String leaseId) {
        return asOwner(tenantId, ownerId, () -> ActionResult.success(service.canReadStatus(tenantId, ownerId, leaseId)));
    }
 
    @Override @PostMapping("/lease") @NoDataSourceBind
    public ActionResult<UploadEditLease> lease(@RequestParam("tenantId") String tenantId,
            @RequestParam("ownerId") String ownerId, @RequestParam("leaseId") String leaseId) {
        return asOwner(tenantId, ownerId, () -> ActionResult.success(service.lease(tenantId, ownerId, leaseId)));
    }
    @Override @PostMapping("/save") @NoDataSourceBind
    public ActionResult<Boolean> save(@RequestBody UploadEditSaveCommand command) {
        return asOwner(command.getTenantId(), command.getOwnerId(), () -> ActionResult.success(service.save(command)));
    }
 
    private <T> T asOwner(String tenantId, String ownerId, Supplier<T> work) {
        SaSameUtil.checkCurrentRequestToken();
        if (tenantId == null || tenantId.isBlank() || ownerId == null || ownerId.isBlank()) throw new DataException("缺少编辑租约归属");
        UserInfo previous = UserProvider.getLocalLoginUser();
        boolean switched = false;
        try {
            TenantDataSourceUtil.switchTenant(tenantId);
            UserInfo owner = new UserInfo();
            owner.setUserId(ownerId);
            owner.setTenantId(tenantId);
            UserProvider.setLocalLoginUser(owner);
            StpUtil.switchTo(UserProvider.splicingLoginId(ownerId));
            switched = true;
            return work.get();
        } finally {
            if (switched) StpUtil.endSwitch();
            UserProvider.clearLocalUser();
            if (previous != null) UserProvider.setLocalLoginUser(previous);
            TenantDataSourceUtil.clearLocalTenantInfo();
        }
    }
}