刘光辉
7 小时以前 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package jnpf.onlinedev.service;
 
import cn.dev33.satoken.stp.StpUtil;
import jnpf.base.UserInfo;
import jnpf.base.entity.VisualdevEntity;
import jnpf.base.entity.VisualdevReleaseEntity;
import jnpf.base.service.UploadEditAccess;
import jnpf.base.service.VisualdevReleaseService;
import jnpf.base.service.VisualdevService;
import jnpf.base.util.FlowFormDataUtil;
import jnpf.base.util.UploadEditStateMachine;
import jnpf.exception.DataException;
import jnpf.exception.WorkFlowException;
import jnpf.onlinedev.model.OnlineInfoModel;
import jnpf.onlinedev.model.PaginationModel;
import jnpf.onlinedev.model.UploadTemplateQuery;
import jnpf.onlinedev.util.OnlinePublicUtils;
import jnpf.util.JsonUtil;
import jnpf.util.TicketUtil;
import jnpf.util.UserProvider;
import jnpf.util.context.RequestContext;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
 
import java.util.*;
 
/** 上传模板入口:复用原生查询与权限,只授权文件引用,不复制文件或保存业务单据。 */
@Service
@RequiredArgsConstructor
public class UploadTemplateService {
    static final String MODEL_ID = "859351030394158725";
    static final String MENU_ID = "859363791895428741";
    private final VisualdevReleaseService releases;
    private final VisualdevService models;
    private final VisualDevListService lists;
    private final FlowFormDataUtil formData;
    private final UploadEditAccess access;
 
    public Map<String, Object> list(UploadTemplateQuery query) throws WorkFlowException {
        access.authorizeTemporary(query.getContext(), UserProvider.getLoginUserId());
        StpUtil.checkPermission(MODEL_ID);
        if (query.getCurrentPage() < 1 || query.getCurrentPage() > 100000
                || query.getKeyword() != null && query.getKeyword().length() > 200) {
            throw new DataException("模板查询参数无效");
        }
        VisualdevReleaseEntity release = releases.getById(MODEL_ID);
        VisualdevEntity model = models.getReleaseInfo(MODEL_ID);
        if (release == null || model == null) throw new DataException("文件/记录模板管理尚未发布");
        PaginationModel page = new PaginationModel();
        page.setCurrentPage(query.getCurrentPage());
        page.setPageSize(20);
        page.setMenuId(MENU_ID);
        page.setSystemCode(RequestContext.getAppCode());
        page.setQueryJson(JsonUtil.getObjectToString(Collections.singletonMap("wenben_mingcheng",
                Objects.toString(query.getKeyword(), "").trim())));
        List<Map<String, Object>> rows = lists.getDataList(OnlinePublicUtils.getVisualJsonModel(release), page);
        List<Map<String, Object>> result = new ArrayList<>();
        for (Map<String, Object> row : rows) {
            String id = UploadEditAccess.rowId(row);
            if (id.isBlank()) continue;
            // 仅对原生权限查询可见的记录读取详情;详情继续执行源表单字段权限。
            Map<String, Object> detail = formData.getFilePermissionDataInfo(model, id,
                    OnlineInfoModel.builder().menuId(MENU_ID).build());
            if (detail == null || detail.isEmpty()) continue;
            List<Map<String, Object>> files = UploadEditStateMachine.files(detail.get("biz_files")).stream()
                    .filter(UploadTemplateService::isDocx).map(UploadTemplateService::canonical).toList();
            result.add(Map.of("id", id, "name", Objects.toString(detail.get("wenben_mingcheng"), ""), "files", files));
        }
        return Map.of("list", result, "total", page.getTotal(), "currentPage", page.getCurrentPage(), "pageSize", 20);
    }
 
    @SuppressWarnings("unchecked")
    public Map<String, Object> select(UploadTemplateQuery query) throws WorkFlowException {
        // 重新校验当前页、数据权限及文件成员关系;不接受列表展示时的过期授权。
        Map<String, Object> page = list(query);
        List<Map<String, Object>> rows = (List<Map<String, Object>>) page.get("list");
        Map<String, Object> row = rows.stream().filter(r -> Objects.equals(r.get("id"), query.getTemplateId()))
                .findFirst().orElseThrow(() -> new DataException("模板已变化或无权使用,请刷新模板列表"));
        List<Map<String, Object>> files = (List<Map<String, Object>>) row.get("files");
        Map<String, Object> file = files.stream().filter(f -> Objects.equals(f.get("fileId"), query.getFileId()))
                .findFirst().orElseThrow(() -> new DataException("模板文件已变化,请重新选择"));
        UserInfo user = UserProvider.getUser();
        Map<String, Object> selected = new LinkedHashMap<>(file);
        jnpf.base.model.uploadedit.UploadEditContext target = new jnpf.base.model.uploadedit.UploadEditContext();
        target.setModelId(query.getContext().getModelId()); target.setField(query.getContext().getField());
        target.setParentField(query.getContext().getParentField());
        if (access.isDmsAuditTemporary(target) && "wenjian".equals(query.getContext().getField())
                && (query.getContext().getParentField() == null || query.getContext().getParentField().isBlank()))
            selected.put(jnpf.base.util.UploadTemplateSource.FIELD, query.getTemplateId());
        Map<String, Object> proof = new LinkedHashMap<>(selected);
        proof.put("purpose", "upload-edit-source");
        proof.put("tenantId", user.getTenantId() == null || user.getTenantId().isEmpty() ? "0" : user.getTenantId());
        proof.put("ownerId", user.getUserId());
        Map<String, Object> result = new LinkedHashMap<>(selected);
        result.put("uploadReceipt", TicketUtil.createTicket(JsonUtil.getObjectToString(proof), 86400L));
        return result;
    }
 
    static boolean isDocx(Map<String, Object> file) {
        return !Objects.toString(file.get("fileId"), "").isBlank()
                && Objects.toString(file.get("name"), "").toLowerCase(Locale.ROOT).endsWith(".docx");
    }
    static Map<String, Object> canonical(Map<String, Object> file) {
        Map<String, Object> result = new LinkedHashMap<>();
        for (String key : List.of("fileId", "name", "fileSize", "fileExtension", "url", "contentSavedAt")) result.put(key, file.get(key));
        result.put("fileExtension", "docx");
        return result;
    }
}